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3 |
+
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
import seaborn as sns
|
| 6 |
+
from numpy import load
|
| 7 |
+
import cv2
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
import numpy.random as nr
|
| 11 |
+
|
| 12 |
+
import warnings
|
| 13 |
+
warnings.simplefilter(action='ignore')
|
| 14 |
+
from PIL import Image, ImageFilter
|
| 15 |
+
|
| 16 |
+
%matplotlib inline
|
| 17 |
+
from google.colab import drive
|
| 18 |
+
drive.mount('/content/drive')
|
| 19 |
+
from google.colab.patches import cv2_imshow
|
| 20 |
+
from PIL import Image
|
| 21 |
+
from skimage.io import imread
|
| 22 |
+
from skimage.morphology import convex_hull_image
|
| 23 |
+
from skimage.color import rgb2gray
|
| 24 |
+
import cv2
|
| 25 |
+
nn = load_model('my_model-2.h5')
|
| 26 |
+
def imageprepare(argv,Single):
|
| 27 |
+
"""
|
| 28 |
+
This function returns the pixel values.
|
| 29 |
+
The input is a png file location.
|
| 30 |
+
"""
|
| 31 |
+
img_gray=cv2.imread(argv,cv2.IMREAD_GRAYSCALE) # read image, image size is 180x180
|
| 32 |
+
(thresh, img_bin) = cv2.threshold(img_gray, 128, 255, cv2.THRESH_BINARY)
|
| 33 |
+
im=Image.fromarray(img_bin)
|
| 34 |
+
if Single==True:
|
| 35 |
+
rgb_im = im.convert("RGB")
|
| 36 |
+
rgb_im.save("ok.jpg")
|
| 37 |
+
im=crop_to_content('/content/ok.jpg')
|
| 38 |
+
width = float(im.size[0])
|
| 39 |
+
height = float(im.size[1])
|
| 40 |
+
newImage = Image.new('L', (28, 28), (255)) # creates white canvas of 28x28 pixels
|
| 41 |
+
|
| 42 |
+
if width > height: # check which dimension is bigger
|
| 43 |
+
# Width is bigger. Width becomes 20 pixels.
|
| 44 |
+
nheight = int(round((20.0 / width * height), 0)) # resize height according to ratio width
|
| 45 |
+
if (nheight == 0): # rare case but minimum is 1 pixel
|
| 46 |
+
nheight = 1
|
| 47 |
+
# resize and sharpen
|
| 48 |
+
img = im.resize((20, nheight), Image.ANTIALIAS).filter(ImageFilter.SHARPEN)
|
| 49 |
+
wtop = int(round(((28 - nheight) / 2), 0)) # calculate horizontal position
|
| 50 |
+
newImage.paste(img, (4, wtop)) # paste resized image on white canvas
|
| 51 |
+
else:
|
| 52 |
+
# Height is bigger. Heigth becomes 20 pixels.
|
| 53 |
+
nwidth = int(round((20.0 / height * width), 0)) # resize width according to ratio height
|
| 54 |
+
if (nwidth == 0): # rare case but minimum is 1 pixel
|
| 55 |
+
nwidth = 1
|
| 56 |
+
# resize and sharpen
|
| 57 |
+
img = im.resize((nwidth, 20), Image.ANTIALIAS).filter(ImageFilter.SHARPEN)
|
| 58 |
+
wleft = int(round(((28 - nwidth) / 2), 0)) # caculate vertical pozition
|
| 59 |
+
newImage.paste(img, (wleft, 4)) # paste resized image on white canvas
|
| 60 |
+
|
| 61 |
+
# newImage.save("sample.png)
|
| 62 |
+
|
| 63 |
+
tv = list(newImage.getdata()) # get pixel values
|
| 64 |
+
|
| 65 |
+
# normalize pixels to 0 and 1. 0 is pure white, 1 is pure black.
|
| 66 |
+
tva = [(255 - x) * 1.0 / 255.0 for x in tv]
|
| 67 |
+
return tva
|
| 68 |
+
|
| 69 |
+
def show(path):
|
| 70 |
+
img_gray=cv2.imread(path,cv2.IMREAD_GRAYSCALE) # read image, image size is 180x180
|
| 71 |
+
(thresh, img_bin) = cv2.threshold(img_gray, 140, 255, cv2.THRESH_BINARY)
|
| 72 |
+
im=Image.fromarray(img_bin)
|
| 73 |
+
rgb_im = im.convert("RGB")
|
| 74 |
+
rgb_im.save("ok.jpg")
|
| 75 |
+
# plt.imshow(rgb_im)
|
| 76 |
+
# plt.show()
|
| 77 |
+
# im=crop_to_content('/content/ok.jpg')
|
| 78 |
+
image = cv2.imread('/content/ok.jpg')
|
| 79 |
+
grey = cv2.cvtColor(image.copy(), cv2.COLOR_BGR2GRAY)
|
| 80 |
+
ret, thresh = cv2.threshold(grey.copy(), 130, 255, cv2.THRESH_BINARY_INV)
|
| 81 |
+
contours, t = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 82 |
+
preprocessed_digits=[]
|
| 83 |
+
for _, c in enumerate(contours):
|
| 84 |
+
|
| 85 |
+
# Get the bounding rectangle of the current contour:
|
| 86 |
+
boundRect = cv2.boundingRect(c)
|
| 87 |
+
|
| 88 |
+
# Get the bounding rectangle data:
|
| 89 |
+
rectX = boundRect[0]
|
| 90 |
+
rectY = boundRect[1]
|
| 91 |
+
rectWidth = boundRect[2]
|
| 92 |
+
rectHeight = boundRect[3]
|
| 93 |
+
|
| 94 |
+
# Estimate the bounding rect area:
|
| 95 |
+
rectArea = rectWidth * rectHeight
|
| 96 |
+
|
| 97 |
+
# Set a min area threshold
|
| 98 |
+
minArea = 1000
|
| 99 |
+
# Filter blobs by area:
|
| 100 |
+
if rectArea > minArea:
|
| 101 |
+
|
| 102 |
+
# Draw bounding box:
|
| 103 |
+
color = (0, 255, 0)
|
| 104 |
+
cv2.rectangle(image, (int(rectX), int(rectY)),
|
| 105 |
+
(int(rectX + rectWidth), int(rectY + rectHeight)), color, 2)
|
| 106 |
+
# Crop bounding box:
|
| 107 |
+
currentCrop = image[rectY:rectY+rectHeight,rectX:rectX+rectWidth]
|
| 108 |
+
# cv2_imshow(currentCrop)
|
| 109 |
+
# cv2.waitKey(0)
|
| 110 |
+
cv2.imwrite("image.jpg", currentCrop)
|
| 111 |
+
x=imageprepare("image.jpg",False)
|
| 112 |
+
digit=np.array(x)
|
| 113 |
+
prediction = nn.predict(digit.reshape(1, 28, 28, 1))
|
| 114 |
+
print(np.argmax(prediction))
|
| 115 |
+
cv2.putText(image,str(np.argmax(prediction)),(rectX,rectHeight+rectY+50),cv2.FONT_HERSHEY_COMPLEX,2,(50,50,225),2)
|
| 116 |
+
status = cv2.imwrite('/content/kok.jpg',image)
|
| 117 |
+
return "/content/kok.jpg"
|
| 118 |
+
import numpy as np
|
| 119 |
+
import gradio as gr
|
| 120 |
+
|
| 121 |
+
def predict_sketch(img):
|
| 122 |
+
img_3d=img.reshape(-1,28,28)
|
| 123 |
+
im_resize=img_3d/255.0
|
| 124 |
+
prediction=nn.predict(im_resize).tolist()[0]
|
| 125 |
+
return {str(i):prediction[i] for i in range(10)}
|
| 126 |
+
|
| 127 |
+
def predict_upload(image):
|
| 128 |
+
im1 = image.save("/content/geeks.jpg")
|
| 129 |
+
k=show("/content/geeks.jpg")
|
| 130 |
+
return k
|
| 131 |
+
|
| 132 |
+
with gr.Blocks() as demo:
|
| 133 |
+
gr.Markdown("Flip text or image files using this demo.")
|
| 134 |
+
with gr.Tabs():
|
| 135 |
+
with gr.TabItem("Sketch"):
|
| 136 |
+
with gr.Row():
|
| 137 |
+
text_input = gr.Sketchpad()
|
| 138 |
+
text_output = gr.Label(num_top_classes=3)
|
| 139 |
+
text_button = gr.Button("Submit")
|
| 140 |
+
with gr.TabItem("Upload Image"):
|
| 141 |
+
with gr.Row():
|
| 142 |
+
image_input = gr.Image(type="pil",)
|
| 143 |
+
image_output = gr.Image(type="pil",)
|
| 144 |
+
image_button = gr.Button("Submit")
|
| 145 |
+
image_button.click(predict_upload,image_input, image_output)
|
| 146 |
+
text_button.click(predict_sketch,text_input,outputs=text_output)
|
| 147 |
+
|
| 148 |
+
demo.launch(debug=True)
|