|
|
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) |
|
|
image = cv2.resize(image, (128,128)) |
|
|
image = np.array(image, dtype=np.float32) |
|
|
image = image.astype('float32') / 255.0 |
|
|
return np.expand_dims(image, axis=0) |
|
|
|
|
|
def predict_animal(image): |
|
|
processed_image = preprocess_image(image) |
|
|
|
|
|
|
|
|
predictions = model.predict(processed_image) |
|
|
|
|
|
|
|
|
top_3_idx = np.argsort(predictions[0])[-3:][::-1] |
|
|
|
|
|
|
|
|
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() |