Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,37 +2,75 @@ import cv2
|
|
2 |
import gradio as gr
|
3 |
import tensorflow as tf
|
4 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
# Model yükleniyor
|
7 |
model = tf.keras.models.load_model("number_recognition_model_colab.keras")
|
8 |
|
|
|
|
|
9 |
# Etiketler (0'dan 9'a kadar sayılar)
|
10 |
labels = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
|
11 |
|
12 |
# Tahmin fonksiyonu
|
13 |
def predict(img):
|
14 |
try:
|
15 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
if img.ndim == 3 and img.shape[-1] == 3:
|
17 |
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
18 |
elif img.ndim == 2:
|
19 |
img = np.expand_dims(img, axis=-1)
|
20 |
|
21 |
-
#
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
23 |
img = img.astype('float32') / 255.0
|
24 |
-
img = img.reshape(1,
|
|
|
|
|
|
|
25 |
|
26 |
-
#
|
27 |
preds = model.predict(img)[0]
|
28 |
|
29 |
-
#
|
30 |
-
|
31 |
-
formatted_preds = "\n".join([f"{label}: {prob:.2f}" for label, prob in sorted_preds])
|
32 |
|
33 |
-
|
|
|
34 |
except Exception as e:
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
# Gradio arayüzü
|
38 |
interface = gr.Interface(
|
|
|
2 |
import gradio as gr
|
3 |
import tensorflow as tf
|
4 |
import numpy as np
|
5 |
+
import PIL.Image
|
6 |
+
|
7 |
+
title = "Welcome on your first sketch recognition app!"
|
8 |
+
|
9 |
+
head = (
|
10 |
+
"<center>"
|
11 |
+
"The robot was trained to classify numbers (from 0 to 9). To test it, write your number in the space provided."
|
12 |
+
"</center>"
|
13 |
+
)
|
14 |
|
15 |
# Model yükleniyor
|
16 |
model = tf.keras.models.load_model("number_recognition_model_colab.keras")
|
17 |
|
18 |
+
|
19 |
+
img_size = 28
|
20 |
# Etiketler (0'dan 9'a kadar sayılar)
|
21 |
labels = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
|
22 |
|
23 |
# Tahmin fonksiyonu
|
24 |
def predict(img):
|
25 |
try:
|
26 |
+
# If the input is a dictionary (Gradio sketchpad), extract the image
|
27 |
+
if isinstance(img, dict):
|
28 |
+
img = img.get('image', None) # Get the image from the dictionary
|
29 |
+
|
30 |
+
if img is None:
|
31 |
+
raise ValueError("No image data found")
|
32 |
+
|
33 |
+
# Convert the input image to a NumPy array if needed
|
34 |
+
if not isinstance(img, np.ndarray):
|
35 |
+
img = np.array(img)
|
36 |
+
|
37 |
+
# Print shape and type of the input image
|
38 |
+
print(f"Initial image type: {type(img)}, shape: {img.shape}")
|
39 |
+
|
40 |
+
# Ensure the image is in grayscale and has a single channel
|
41 |
if img.ndim == 3 and img.shape[-1] == 3:
|
42 |
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
43 |
elif img.ndim == 2:
|
44 |
img = np.expand_dims(img, axis=-1)
|
45 |
|
46 |
+
# Print the shape of the grayscale image
|
47 |
+
print(f"Grayscale image shape: {img.shape}")
|
48 |
+
|
49 |
+
# Resize the image
|
50 |
+
img = cv2.resize(img, (img_size, img_size))
|
51 |
+
|
52 |
+
# Normalize the image
|
53 |
img = img.astype('float32') / 255.0
|
54 |
+
img = img.reshape(1, img_size, img_size, 1)
|
55 |
+
|
56 |
+
# Print the shape after resizing and normalizing
|
57 |
+
print(f"Processed image shape: {img.shape}")
|
58 |
|
59 |
+
# Get the predictions from the model
|
60 |
preds = model.predict(img)[0]
|
61 |
|
62 |
+
# Print the predictions
|
63 |
+
print("Predictions:", preds)
|
|
|
64 |
|
65 |
+
# Return the predictions for each label
|
66 |
+
return {label: float(pred) for label, pred in zip(labels, preds)}
|
67 |
except Exception as e:
|
68 |
+
# Print the exception to the console
|
69 |
+
print(f"Error during prediction: {e}")
|
70 |
+
return {"Error": str(e)}
|
71 |
+
|
72 |
+
# Set up the Gradio interface with the input as a sketchpad and output as labels
|
73 |
+
label = gr.Label(num_top_classes=3)
|
74 |
|
75 |
# Gradio arayüzü
|
76 |
interface = gr.Interface(
|