Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import tensorflow as tf
|
| 4 |
+
import cv2
|
| 5 |
+
import numpy as np
|
| 6 |
+
import json
|
| 7 |
+
|
| 8 |
+
# Model ve etiketleri yükle
|
| 9 |
+
model = tf.keras.models.load_model('animal_classifier_model.h5')
|
| 10 |
+
|
| 11 |
+
with open('class_labels.json', 'r') as f:
|
| 12 |
+
class_labels = json.load(f)
|
| 13 |
+
|
| 14 |
+
def preprocess_image(image):
|
| 15 |
+
# Gradio'dan gelen görüntüyü işle
|
| 16 |
+
image = cv2.resize(image, (96, 96)) # Model için kullandığımız boyuta getir
|
| 17 |
+
image = image.astype('float32') / 255.0 # Normalize et
|
| 18 |
+
return np.expand_dims(image, axis=0) # Batch boyutu ekle
|
| 19 |
+
|
| 20 |
+
def predict_animal(image):
|
| 21 |
+
# Görüntüyü preprocessten geçir
|
| 22 |
+
processed_image = preprocess_image(image)
|
| 23 |
+
|
| 24 |
+
# Tahmin yap
|
| 25 |
+
predictions = model.predict(processed_image)
|
| 26 |
+
|
| 27 |
+
# En yüksek 3 tahmini al
|
| 28 |
+
top_3_idx = np.argsort(predictions[0])[-3:][::-1]
|
| 29 |
+
|
| 30 |
+
# Sonuçları hazırla
|
| 31 |
+
results = {class_labels[str(idx)]: float(predictions[0][idx]) for idx in top_3_idx}
|
| 32 |
+
|
| 33 |
+
return results
|
| 34 |
+
|
| 35 |
+
# Gradio arayüzünü oluştur
|
| 36 |
+
iface = gr.Interface(
|
| 37 |
+
fn=predict_animal,
|
| 38 |
+
inputs=gr.Image(),
|
| 39 |
+
outputs=gr.Label(num_top_classes=3),
|
| 40 |
+
title="Hayvan Türü Sınıflandırıcı",
|
| 41 |
+
description="Bu model 10 farklı hayvan türünü tanıyabilir: Collie, Dolphin, Elephant, Fox, Moose, Rabbit, Sheep, Squirrel, Giant Panda, ve Polar Bear",
|
| 42 |
+
examples=[
|
| 43 |
+
["example_images/collie.jpg"],
|
| 44 |
+
["example_images/elephant.jpg"],
|
| 45 |
+
["example_images/fox.jpg"]
|
| 46 |
+
]
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
# Uygulamayı başlat
|
| 50 |
+
iface.launch()
|