Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,21 +1,46 @@
|
|
|
|
|
|
1 |
import tensorflow as tf
|
2 |
import numpy as np
|
3 |
-
from urllib.request import urlretrieve
|
4 |
-
import gradio as gr
|
5 |
|
6 |
-
|
7 |
-
model = tf.keras.models.load_model("
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
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 |
+
# Görüntüyü gri tonlamaya dönüştür
|
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 |
+
# Görüntüyü yeniden boyutlandır ve normalize et
|
22 |
+
img = cv2.resize(img, (28, 28))
|
23 |
+
img = img.astype('float32') / 255.0
|
24 |
+
img = img.reshape(1, 28, 28, 1)
|
25 |
+
|
26 |
+
# Modelden tahmin al
|
27 |
+
preds = model.predict(img)[0]
|
28 |
+
|
29 |
+
# Tahmin sonuçlarını formatla
|
30 |
+
sorted_preds = sorted(zip(labels, preds), key=lambda x: x[1], reverse=True)[:3]
|
31 |
+
formatted_preds = "\n".join([f"{label}: {prob:.2f}" for label, prob in sorted_preds])
|
32 |
+
|
33 |
+
return formatted_preds
|
34 |
+
except Exception as e:
|
35 |
+
return f"Error: {e}"
|
36 |
+
|
37 |
+
# Gradio arayüzü
|
38 |
+
interface = gr.Interface(
|
39 |
+
fn=predict,
|
40 |
+
inputs="sketchpad",
|
41 |
+
outputs="textbox",
|
42 |
+
title="Sketch Recognition App",
|
43 |
+
description="Draw a number (0-9) and see the model's top predictions."
|
44 |
+
)
|
45 |
+
|
46 |
+
interface.launch(debug=True)
|