Spaces:
Sleeping
Sleeping
| from keras.models import load_model | |
| import numpy as np # linear algebra | |
| import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv) | |
| from numpy import load | |
| import cv2 | |
| import numpy.random as nr | |
| import warnings | |
| warnings.simplefilter(action='ignore') | |
| from PIL import Image, ImageFilter | |
| from PIL import Image | |
| from skimage.io import imread | |
| from skimage.morphology import convex_hull_image | |
| from skimage.color import rgb2gray | |
| import cv2 | |
| nn = load_model('my_model-2.h5') | |
| def imageprepare(argv,Single): | |
| """ | |
| This function returns the pixel values. | |
| The input is a png file location. | |
| """ | |
| img_gray=cv2.imread(argv,cv2.IMREAD_GRAYSCALE) # read image, image size is 180x180 | |
| (thresh, img_bin) = cv2.threshold(img_gray, 128, 255, cv2.THRESH_BINARY) | |
| im=Image.fromarray(img_bin) | |
| if Single==True: | |
| rgb_im = im.convert("RGB") | |
| rgb_im.save("ok.jpg") | |
| im=crop_to_content('/content/ok.jpg') | |
| width = float(im.size[0]) | |
| height = float(im.size[1]) | |
| newImage = Image.new('L', (28, 28), (255)) # creates white canvas of 28x28 pixels | |
| if width > height: # check which dimension is bigger | |
| # Width is bigger. Width becomes 20 pixels. | |
| nheight = int(round((20.0 / width * height), 0)) # resize height according to ratio width | |
| if (nheight == 0): # rare case but minimum is 1 pixel | |
| nheight = 1 | |
| # resize and sharpen | |
| img = im.resize((20, nheight), Image.Resampling.LANCZOS).filter(ImageFilter.SHARPEN) | |
| wtop = int(round(((28 - nheight) / 2), 0)) # calculate horizontal position | |
| newImage.paste(img, (4, wtop)) # paste resized image on white canvas | |
| else: | |
| # Height is bigger. Heigth becomes 20 pixels. | |
| nwidth = int(round((20.0 / height * width), 0)) # resize width according to ratio height | |
| if (nwidth == 0): # rare case but minimum is 1 pixel | |
| nwidth = 1 | |
| # resize and sharpen | |
| img = im.resize((nwidth, 20), Image.Resampling.LANCZOS).filter(ImageFilter.SHARPEN) | |
| wleft = int(round(((28 - nwidth) / 2), 0)) # caculate vertical pozition | |
| newImage.paste(img, (wleft, 4)) # paste resized image on white canvas | |
| # newImage.save("sample.png) | |
| tv = list(newImage.getdata()) # get pixel values | |
| # normalize pixels to 0 and 1. 0 is pure white, 1 is pure black. | |
| tva = [(255 - x) * 1.0 / 255.0 for x in tv] | |
| return tva | |
| def show(path): | |
| img_gray=cv2.imread(path,cv2.IMREAD_GRAYSCALE) # read image, image size is 180x180 | |
| (thresh, img_bin) = cv2.threshold(img_gray, 140, 255, cv2.THRESH_BINARY) | |
| im=Image.fromarray(img_bin) | |
| rgb_im = im.convert("RGB") | |
| rgb_im.save("ok.jpg") | |
| # plt.imshow(rgb_im) | |
| # plt.show() | |
| # im=crop_to_content('/content/ok.jpg') | |
| image = cv2.imread('ok.jpg') | |
| grey = cv2.cvtColor(image.copy(), cv2.COLOR_BGR2GRAY) | |
| ret, thresh = cv2.threshold(grey.copy(), 130, 255, cv2.THRESH_BINARY_INV) | |
| contours, t = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) | |
| preprocessed_digits=[] | |
| for _, c in enumerate(contours): | |
| # Get the bounding rectangle of the current contour: | |
| boundRect = cv2.boundingRect(c) | |
| # Get the bounding rectangle data: | |
| rectX = boundRect[0] | |
| rectY = boundRect[1] | |
| rectWidth = boundRect[2] | |
| rectHeight = boundRect[3] | |
| # Estimate the bounding rect area: | |
| rectArea = rectWidth * rectHeight | |
| # Set a min area threshold | |
| minArea = 1000 | |
| # Filter blobs by area: | |
| if rectArea > minArea: | |
| # Draw bounding box: | |
| color = (0, 255, 0) | |
| cv2.rectangle(image, (int(rectX), int(rectY)), | |
| (int(rectX + rectWidth), int(rectY + rectHeight)), color, 2) | |
| # Crop bounding box: | |
| currentCrop = image[rectY:rectY+rectHeight,rectX:rectX+rectWidth] | |
| # cv2_imshow(currentCrop) | |
| # cv2.waitKey(0) | |
| cv2.imwrite("image.jpg", currentCrop) | |
| x=imageprepare("image.jpg",False) | |
| digit=np.array(x) | |
| prediction = nn.predict(digit.reshape(1, 28, 28, 1)) | |
| print(np.argmax(prediction)) | |
| cv2.putText(image,str(np.argmax(prediction)),(rectX,rectHeight+rectY+50),cv2.FONT_HERSHEY_COMPLEX,2,(50,50,225),2) | |
| status = cv2.imwrite('kok.jpg',image) | |
| return "kok.jpg" | |
| import numpy as np | |
| import gradio as gr | |
| def predict_sketch(img): | |
| img_3d=img.reshape(-1,28,28,1) | |
| im_resize=img_3d/255.0 | |
| prediction=nn.predict(im_resize).tolist()[0] | |
| return {str(i):prediction[i] for i in range(10)} | |
| def predict_upload(image): | |
| im1 = image.save("geeks.jpg") | |
| k=show("geeks.jpg") | |
| return k | |
| import numpy as np | |
| import gradio as gr | |
| def predict_sketch(img): | |
| img_3d=img.reshape(-1,28,28,1) | |
| im_resize=img_3d/255.0 | |
| prediction=nn.predict(im_resize).tolist()[0] | |
| return {str(i):prediction[i] for i in range(10)} | |
| def predict_upload(image): | |
| im1 = image.save("geeks.jpg") | |
| k=show("geeks.jpg") | |
| return k | |
| description=''' | |
| <h1>Digit Recognizer</h4> | |
| <h2>You have Two options either you can draw the digit in the sketchpad or you can upload the image and see the results</h2> | |
| ''' | |
| des2="Note:Draw Digits from 0-9 and Try to Draw the Digit in the center for better accuracy" | |
| with gr.Blocks() as demo: | |
| gr.Markdown(description) | |
| with gr.Tabs(): | |
| with gr.TabItem("Sketch"): | |
| gr.Markdown(des2) | |
| with gr.Row(): | |
| text_input = gr.Sketchpad() | |
| text_output = gr.Label(num_top_classes=3) | |
| clr = gr.Button("Submit") | |
| with gr.TabItem("Upload Image"): | |
| with gr.Row(): | |
| image_input = gr.Image(type="pil",) | |
| image_output = gr.Image(type="pil",) | |
| image_button = gr.Button("Submit") | |
| examples=["tt.jpg","multitest.jpg","zz.jpg"] | |
| gr.Examples(examples=examples, | |
| inputs=image_input, | |
| fn=predict_upload, | |
| outputs=image_output) | |
| image_input.change(predict_upload,image_input, image_output,examples) | |
| text_input.change(predict_sketch,text_input,outputs=text_output) | |
| demo.launch(debug=True,share = True) | |
| ''' | |
| with gr.Blocks(title="Digit Recognizer") as demo: | |
| gr.Markdown("Flip text or image files using this demo.") | |
| with gr.Tabs(): | |
| with gr.TabItem("Sketch"): | |
| with gr.Row(): | |
| text_input = gr.Sketchpad() | |
| text_output = gr.Label(num_top_classes=3) | |
| text_button = gr.Button("Submit") | |
| with gr.TabItem("Upload Image"): | |
| with gr.Row(): | |
| image_input = gr.Image(type="pil",) | |
| image_output = gr.Image(type="pil",) | |
| image_button = gr.Button("Submit") | |
| image_button.click(predict_upload,image_input, image_output) | |
| text_button.click(predict_sketch,text_input,outputs=text_output) | |
| demo.launch(debug=True) | |
| ''' |