File size: 1,437 Bytes
88d4067
 
 
 
 
 
 
 
 
 
 
 
a28f7bf
 
7838f0b
a28f7bf
88d4067
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
614e4d5
 
7eb4c6f
88d4067
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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()