File size: 2,015 Bytes
8be046b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import gradio as gr
import tensorflow as tf
import numpy as np
from PIL import Image

# Sınıf isimleri - sizin veri setinize göre
CLASS_NAMES = [
    "Africanized Honey Bees (Killer Bees)",
    "Aphids",
    "Armyworms",
    "Brown Marmorated Stink Bugs",
    "Cabbage Loopers",
    "Citrus Canker",
    "Colorado Potato Beetles",
    "Corn Borers",
    "Corn Earworms",
    "Fall Armyworms",
    "Fruit Flies",
    "Spider Mites",
    "Thrips",
    "Tomato Hornworms",
    "Western Corn Rootworms"
]

# Model yükleme
model = tf.keras.models.load_model("XceptionFarmInsectClassifier.h5")

def predict_insect(image):
    """
    Yüklenen görüntüyü işler ve böcek sınıfını tahmin eder.
    
    Args:
        image: PIL Image veya numpy array
        
    Returns:
        dict: Sınıf olasılıkları
    """
    # Görüntüyü işle
    if isinstance(image, np.ndarray):
        img = Image.fromarray(image.astype('uint8'))
    else:
        img = image
    
    # Boyutlandır
    img = img.resize((224, 224))
    img_array = np.array(img)
    
    # Normalize et
    img_array = img_array / 255.0
    
    # Batch dimension ekle
    img_array = np.expand_dims(img_array, axis=0)
    
    # Tahmin yap
    predictions = model.predict(img_array, verbose=0)
    
    # Sonuçları dictionary'e çevir
    results = {CLASS_NAMES[i]: float(predictions[0][i]) for i in range(len(CLASS_NAMES))}
    
    return results

# Gradio arayüzü oluştur
iface = gr.Interface(
    fn=predict_insect,
    inputs=gr.Image(type="pil", label="Böcek Fotoğrafı Yükleyin"),
    outputs=gr.Label(num_top_classes=5, label="Tahmin Sonuçları"),
    title="🐛 Farm Insect Classifier API",
    description="Zararlı böcekleri tespit eden AI modeli. Bir böcek fotoğrafı yükleyin.",
    examples=[
        # Örnek görüntü yolları ekleyebilirsiniz
    ],
    allow_flagging="never"
)

# API olarak çalıştır
if __name__ == "__main__":
    iface.launch(share=False, server_name="0.0.0.0", server_port=7860)