Databricks View Image - databricks

I am running this in Databricks but the decision tree image will not display.
%pip install pydot
%pip install pydotplus
# Load libraries
from sklearn.tree import DecisionTreeClassifier
from sklearn import datasets
from IPython.display import Image
from sklearn import tree
import pydotplus
# Load data
iris = datasets.load_iris()
X = iris.data
y = iris.target
# Create DOT data
dot_data = tree.export_graphviz(clf, out_file=None,
feature_names=iris.feature_names,
class_names=iris.target_names)
# Draw graph
graph = pydotplus.graph_from_dot_data(dot_data)
# Show graph
Image(graph.create_png())
I only get this message (no visual):
Out[4]: <IPython.core.display.Image object>
I'm stumped. Thoughts?

Databricks has the worst documentation, and their examples do not work at this time, so I had to come up with my own solution using PIL and Matplotlib.
Here is how I display images in Databricks in Python:
from PIL import Image
import matplotlib.pyplot as plt
def display_image(path, dpi=100):
"""
Description:
Displayes an image
Inputs:
path (str): File path
dpi (int): Your monitor's pixel density
"""
img = Image.open(path)
width, height = img.size
plt.figure(figsize = (width/dpi,height/dpi))
plt.imshow(img, interpolation='nearest', aspect='auto')

Related

Clipping Raster through each features of shapefile using Python

I clipped a raster file through a shapefile. Now i want to clip that raster file through each features of shapefile. If there are 5 feature in that shp i wanted to clip the raster through those 5 features and i should get 5seperate rasters as per features. I'm using rasterio and fiona for that. Is there a way to do that?
Maybe you can start from here:
import rasterio
import Fiona
# Create a Raster object
raster = rasterio.Raster()
# Get the shapefile data
shapefile = open('shapefile.shp', 'r')
# Get the raster data
rasters = shapefile.shape.getData()
# Loop through the rasters and clip them to the features in the shapefile
for raster in rasters:
# Get the coordinates of the feature in the raster
featureX = raster.GetX()
featureY = raster.GetY()
# Clip the raster to the feature
raster.Clip(featureX, featureY)
This works for me
#import libs
import rasterio
from rasterio import plot
from rasterio.mask import mask
from rasterio.plot import show
from matplotlib import pyplot
import fiona
import shapely
import geopandas as gpd
#raster file path
ras_fp = (r'Classification.tif')
#read shapefile
gdf = gpd.read_file('vill.shp')
# gdf.crs
names = [x for x in gdf['layer']] #get column value from shapefile
#read raster data
ras_data = rasterio.open(ras_fp)
#output path
out_raster_fp = '/output'
#save output files as per shapefile features
for i in range(len(gdf)):
geom = []
coord = shapely.geometry.mapping(gdf)["features"][i]["geometry"]
geom.append(coord)
with rasterio.open(ras_fp)as src:
out_image, out_transform = rasterio.mask.mask(src,geom,crop=True)
out_meta = src.meta
out_meta.update({'driver':'GTiff',
'height':out_image.shape[1],
'width':out_image.shape[2],
'transform':out_transform})
#file_path = ''
with rasterio.open(f'{out_raster_fp}/{names[i]}.tif','w',**out_meta)as
dest:
dest.write(out_image)

Detection of walls in a plan

I work on a subject which is detection of walls in an image.
So the method I use is:
featur.Canny of skimage I got this image
FindContours of OpenCV-python
But it detects no segments at all
Import part:
import matplotlib.pyplot as plt
import skimage.segmentation as seg
import skimage.filters as filters
from skimage import feature
from skimage.segmentation import (morphological_chan_vese,
morphological_geodesic_active_contour,
inverse_gaussian_gradient,
checkerboard_level_set, mark_boundaries)
from skimage import data, img_as_float
import cv2 as cv2
The code:
img = cv2.imread(image_png, 0)
img_base = img
img = feature.canny(img, sigma=3)
dpi = 72
plt.figure(figsize=(9000/dpi, 9000/dpi), dpi=dpi)
plt.imshow(img, cmap="Greys", aspect="equal")
plt.axis("off")
plt.savefig("test_new_canny.png")
img = cv2.imread("test_new_canny.png", 0)
image_base = cv2.imread("./image_test/rdc.png", 0)
contours, _ = cv2.findContours(img, cv2.RETR_CCOMP , cv2.CHAIN_APPROX_SIMPLE )
contours = cv2.drawContours(image_base, contours, -1, (0,255,0), 3)
Image.fromarray(contours).save("test_contours.png")
Do you know why the detection doesn't work?
So I use a second method, Computes Felsenszwalb’s efficient graph based image segmentation, with Skimage
I obtain something with a small image but with a larger one the algo never finish the treatment.
Any ideas?

Making Predictions (classifying chess pieces)

I am trying to identify all the pieces present on the Chessboard via machine learning.Currently I am predicting for a single piece.I want to load the trained model from the disk,loop through the board, get the playing square crop, and the model will predict the piece that is on that square.
I want to do like this- https://www.youtube.com/watch?v=jcFvrCsoY_w
This is my current code for prediction of single piece.Help me to loop through the board and get playing square crop like above video.
import cv2
import time
import os
import glob
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from tensorflow.keras.models import load_model
model = load_model('/home/tejas/Videos/chess/model_50x50.hd5')
label_map = list('KQRBNP_kqrbnp')
def predict(img, model, img_size=(50,50), plot=False):
img = cv2.resize(img, img_size)
img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY )
if plot:
plt.imshow(img, cmap='gray')
img = img.reshape(1, *img_size, 1) / 255
pred = model.predict(img)
return label_map[np.argmax(pred)]
path = '/media/tejas/creator/chess/train_data/black/r/r_90_1579252980.226565.jpg'
name_map = {
'K':'White King',
'Q':'White Queen',
'R':'White Rook',
'B':'White Bishop',
'N':'White Knight',1y0
'P':'White Pawn',
'_':'Empty Square',
'k':'Black King',
'q':'Black Queen',
'r':'Black Rook',
'b':'Black Bishop',
'n':'Black Knight',
'p':'Black Pawn',
}
img = cv2.imread(path)
pred = predict(img, model, plot=True)
print('The image is a', name_map[pred])
Thanks !!!

how to show binary image data in python?

i can show the image using image.open, but how do i display from the binary data?
trying to use plot gets: ValueError: x and y can be no greater than 2-D, but have shapes (64,) and (64, 64, 3). this makes sense as that is what the result is supposed to be, but how do i display it?
import pathlib
import glob
from os.path import join
import matplotlib.pyplot as plt
from PIL import Image
import tensorflow as tf
def parse(image): # my like ings, but with .png instead of .jpeg.
image_string = tf.io.read_file(image)
image = tf.image.decode_png(image_string, channels=3)
image = tf.image.convert_image_dtype(image, tf.float32)
image = tf.image.resize(image, [64, 64])
return image
root = "in/flower_photos/tulips"
path = join(root,"*.jpg")
files = sorted(glob.glob(path))
file=files[0]
image = Image.open(file)
image.show()
binary=parse(file)
print(type(binary))
# how do i see this?
#plt.plot(binary) # does not seem to work
#plt.show() # does not seem to work
found a nice pillow tutorial.
from matplotlib import image
from matplotlib import pyplot
from PIL import Image
# load the image
filename='Sydney-Opera-House.jpg'
im = Image.open(filename)
# summarize some details about the image
print(im.format)
print(im.mode)
print(im.size)
# show the image
#image.show()
# load image as pixel array
data = image.imread(filename)
# summarize shape of the pixel array
print(data.dtype)
print(data.shape)
# display the array of pixels as an image
pyplot.imshow(data)
pyplot.show()

how to map netcdf data ob base map

file contains values of echos w.r.t to lat/long, I have to plot complete range of echos over base map.
from netCDF4 import Dataset
import numpy as np
import pandas as pd
from google.colab import files
upload = files.upload()
my_example_nc_file = 'a.nc'
fh = Dataset(my_example_nc_file, mode='r')
lons = fh.variables['longitude'][:]
lats = fh.variables['latitude'][:]
ech= fh.variables['echos'][:]
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
%matplotlib inline
m = Basemap(width=5000000,height=3500000,
resolution='l',projection='stere',\
lat_ts=40,lat_0=lat_0,lon_0=lon_0)
xi, yi = m(lons, lats)
#simple plot
#m.plot(xi, yi, 'co')
m.scatter(rge,yi, marker = 'o', color='r', zorder=5)
Current code execute below results.
enter image description here
I want to plot total echos with variation represented by colors as presented in below screen short
enter image description here

Resources