Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import gradio as gr
|
|
| 2 |
from keras.models import load_model
|
| 3 |
import numpy as np
|
| 4 |
import cv2
|
|
|
|
| 5 |
|
| 6 |
# Modeli yükle
|
| 7 |
model = load_model("keras_model.h5", compile=False)
|
|
@@ -11,11 +12,29 @@ class_names = open("labels.txt", "r").readlines()
|
|
| 11 |
|
| 12 |
# Sınıflandırma fonksiyonu
|
| 13 |
def classify_image(image):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
# Görüntünün geçerli olup olmadığını kontrol et
|
| 15 |
if image is None:
|
| 16 |
return {class_names[i].strip(): 0.0 for i in range(len(class_names))}, "Hata: Görüntü yüklenemedi."
|
| 17 |
|
| 18 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
# Görüntünün boş olup olmadığını kontrol et
|
| 20 |
if isinstance(image, np.ndarray) and (image.size == 0 or image.shape[0] == 0 or image.shape[1] == 0):
|
| 21 |
return {class_names[i].strip(): 0.0 for i in range(len(class_names))}, "Hata: Geçersiz görüntü boyutu."
|
|
@@ -23,6 +42,10 @@ def classify_image(image):
|
|
| 23 |
# Görüntüyü yeniden boyutlandır
|
| 24 |
img_resized = cv2.resize(image, (224, 224)) # Modelin beklediği boyut
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
# Görüntüyü işle
|
| 27 |
img_array = np.expand_dims(img_resized, axis=0) # Batch boyutunu ekle
|
| 28 |
img_array = img_array / 255.0 # Normalizasyon
|
|
@@ -36,12 +59,14 @@ def classify_image(image):
|
|
| 36 |
|
| 37 |
except Exception as e:
|
| 38 |
# Hata durumunda
|
|
|
|
|
|
|
| 39 |
return {class_names[i].strip(): 0.0 for i in range(len(class_names))}, f"Hata oluştu: {str(e)}"
|
| 40 |
|
| 41 |
# Gradio arayüzü
|
| 42 |
interface = gr.Interface(
|
| 43 |
fn=classify_image,
|
| 44 |
-
inputs=gr.Image(type="
|
| 45 |
outputs=[
|
| 46 |
gr.Label(num_top_classes=3, label="Tahminler"),
|
| 47 |
gr.Text(label="Sonuç")
|
|
|
|
| 2 |
from keras.models import load_model
|
| 3 |
import numpy as np
|
| 4 |
import cv2
|
| 5 |
+
import PIL.Image as Image
|
| 6 |
|
| 7 |
# Modeli yükle
|
| 8 |
model = load_model("keras_model.h5", compile=False)
|
|
|
|
| 12 |
|
| 13 |
# Sınıflandırma fonksiyonu
|
| 14 |
def classify_image(image):
|
| 15 |
+
# Debug bilgisi
|
| 16 |
+
print(f"Gelen görüntü tipi: {type(image)}")
|
| 17 |
+
if image is not None:
|
| 18 |
+
if isinstance(image, np.ndarray):
|
| 19 |
+
print(f"Görüntü şekli: {image.shape}, dtype: {image.dtype}")
|
| 20 |
+
else:
|
| 21 |
+
print(f"Görüntü numpy array değil")
|
| 22 |
+
else:
|
| 23 |
+
print("Görüntü None")
|
| 24 |
+
|
| 25 |
# Görüntünün geçerli olup olmadığını kontrol et
|
| 26 |
if image is None:
|
| 27 |
return {class_names[i].strip(): 0.0 for i in range(len(class_names))}, "Hata: Görüntü yüklenemedi."
|
| 28 |
|
| 29 |
try:
|
| 30 |
+
# PIL Image formatında gelirse numpy array'e dönüştür
|
| 31 |
+
if isinstance(image, Image.Image):
|
| 32 |
+
image = np.array(image)
|
| 33 |
+
|
| 34 |
+
# RGB'den BGR'ye dönüştür (OpenCV için)
|
| 35 |
+
if len(image.shape) == 3 and image.shape[2] == 3:
|
| 36 |
+
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
| 37 |
+
|
| 38 |
# Görüntünün boş olup olmadığını kontrol et
|
| 39 |
if isinstance(image, np.ndarray) and (image.size == 0 or image.shape[0] == 0 or image.shape[1] == 0):
|
| 40 |
return {class_names[i].strip(): 0.0 for i in range(len(class_names))}, "Hata: Geçersiz görüntü boyutu."
|
|
|
|
| 42 |
# Görüntüyü yeniden boyutlandır
|
| 43 |
img_resized = cv2.resize(image, (224, 224)) # Modelin beklediği boyut
|
| 44 |
|
| 45 |
+
# BGR'den RGB'ye dönüştür (model için)
|
| 46 |
+
if len(img_resized.shape) == 3 and img_resized.shape[2] == 3:
|
| 47 |
+
img_resized = cv2.cvtColor(img_resized, cv2.COLOR_BGR2RGB)
|
| 48 |
+
|
| 49 |
# Görüntüyü işle
|
| 50 |
img_array = np.expand_dims(img_resized, axis=0) # Batch boyutunu ekle
|
| 51 |
img_array = img_array / 255.0 # Normalizasyon
|
|
|
|
| 59 |
|
| 60 |
except Exception as e:
|
| 61 |
# Hata durumunda
|
| 62 |
+
import traceback
|
| 63 |
+
traceback.print_exc() # Detaylı hata bilgisini yazdır
|
| 64 |
return {class_names[i].strip(): 0.0 for i in range(len(class_names))}, f"Hata oluştu: {str(e)}"
|
| 65 |
|
| 66 |
# Gradio arayüzü
|
| 67 |
interface = gr.Interface(
|
| 68 |
fn=classify_image,
|
| 69 |
+
inputs=gr.Image(type="pil", label="Bir görüntü yükleyin"), # PIL formatına değiştirdik
|
| 70 |
outputs=[
|
| 71 |
gr.Label(num_top_classes=3, label="Tahminler"),
|
| 72 |
gr.Text(label="Sonuç")
|