mybYusuf's picture
Update app.py
a28f7bf verified
import gradio as gr
import tensorflow as tf
import cv2
import numpy as np
import json
model = tf.keras.models.load_model('animal_classifier_model.h5')
with open('class_labels.json', 'r') as f:
class_labels = json.load(f)
def preprocess_image(image):
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) # Veri okuma uyumsuzluğunu kaldırma
image = cv2.resize(image, (128,128)) # Model için kullandığımız boyuta getir
image = np.array(image, dtype=np.float32) # Numpy dizisine çevir
image = image.astype('float32') / 255.0 # Normalize et
return np.expand_dims(image, axis=0) # Batch boyutu ekle
def predict_animal(image):
processed_image = preprocess_image(image)
# Tahmin yap
predictions = model.predict(processed_image)
# En yüksek 3 tahmini al
top_3_idx = np.argsort(predictions[0])[-3:][::-1]
# Sonuçları hazırla
results = {class_labels[str(idx)]: float(predictions[0][idx]) for idx in top_3_idx}
return results
iface = gr.Interface(
fn=predict_animal,
inputs=gr.Image(),
outputs=gr.Label(num_top_classes=3),
title="Hayvan Türü Sınıflandırıcı",
description="Bu model 10 farklı hayvan türünü tanıyabilir: Collie, Dolphin, Elephant, Fox, Moose, Rabbit, Sheep, Squirrel, Giant Panda, ve Polar Bear",
examples=[
["collie.jpg"],
["elephant.jpg"],
["rabbit.jpg"]
]
)
iface.launch()