Single_Digit_Detector / num_detect.py
abhi0987's picture
initial commit
cde47a4
# -*- coding: utf-8 -*-
"""num_detect.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1GcHZ0KGkpSs8vsjRbjMHBRVZ6M86nqYj
"""
from keras.models import load_model
model=load_model(r"C:\Users\Abhijeet Tripathi\Downloads\num_detect (1).keras")
import numpy as np
import cv2
from keras.preprocessing import image
import matplotlib.pyplot as plt
def mnist_compatible(image_path, target_size=(28, 28)):
img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
plt.imshow(img)
plt.show()
img_resized = cv2.resize(img, target_size)
img_inverted = 255 - img_resized
img_normalized = img_inverted.astype('float32') / 255.0
img_array = image.img_to_array(img_normalized)
img_reshaped = img_array.reshape((*target_size, 1))
return img_reshaped
def predict(dict):
print(dict)
path = dict['composite']
arr = mnist_compatible(path)
arr = np.expand_dims(arr, axis=0)
return str(np.argmax(model.predict(arr)))
import gradio as gr
# Import the Brush class
from gradio import Brush
iface = gr.Interface(
fn=predict,
inputs=gr.Paint(label="Input Image Component",type="filepath",brush=Brush(colors=["#32cc70"]),canvas_size=(301,601)),
outputs="text"
)
iface.launch(share='True')