list91 commited on
Commit
b788209
·
verified ·
1 Parent(s): eb0bee0

Upload /home/user/study/tomato_disease/tomato-disease-training/hf_requirements.txt with huggingface_hub

Browse files
home/user/study/tomato_disease/tomato-disease-training/hf_requirements.txt ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import cv2
3
+ import numpy as np
4
+ import torch
5
+ import gradio as gr
6
+
7
+ # Список классов болезней
8
+ TOMATO_CLASSES = [
9
+ 'Tomato___Bacterial_spot',
10
+ 'Tomato___Early_blight',
11
+ 'Tomato___Late_blight',
12
+ 'Tomato___Leaf_Mold',
13
+ 'Tomato___Septoria_leaf_spot',
14
+ 'Tomato___Spider_mites Two-spotted_spider_mite',
15
+ 'Tomato___Target_Spot',
16
+ 'Tomato___Tomato_Yellow_Leaf_Curl_Virus',
17
+ 'Tomato___Tomato_mosaic_virus',
18
+ 'Tomato___healthy'
19
+ ]
20
+
21
+ def load_model():
22
+ """Загрузка обученной модели"""
23
+ try:
24
+ model_path = 'tomato_disease_classifier.pth'
25
+
26
+ if not os.path.exists(model_path):
27
+ return None, None
28
+
29
+ model_data = torch.load(model_path)
30
+
31
+ return model_data['classifier'], model_data['scaler']
32
+ except Exception as e:
33
+ print(f"Ошибка загрузки модели: {e}")
34
+ return None, None
35
+
36
+ def preprocess_image(image):
37
+ """Подготовка изображения для предсказания"""
38
+ if image is None:
39
+ return None
40
+
41
+ # Resize и flatten
42
+ img_resized = cv2.resize(image, (64, 64))
43
+ img_flattened = img_resized.flatten()
44
+
45
+ return img_flattened
46
+
47
+ def predict_disease(image):
48
+ """Предсказание болезни томата"""
49
+ if image is None:
50
+ return "Пожалуйста, загрузите изображение"
51
+
52
+ # Загрузка модели
53
+ classifier, scaler = load_model()
54
+ if classifier is None or scaler is None:
55
+ return "Ошибка загрузки модели"
56
+
57
+ # Предобработка изображения
58
+ processed_image = preprocess_image(image)
59
+ if processed_image is None:
60
+ return "Не удалось обработать изображение"
61
+
62
+ # Масштабирование
63
+ processed_image = scaler.transform([processed_image])
64
+
65
+ # Предсказание
66
+ prediction = classifier.predict(processed_image)
67
+ probabilities = classifier.predict_proba(processed_image)[0]
68
+
69
+ # Формирование результата
70
+ result = f"Обнаружено: {prediction[0]}\n\n"
71
+ result += "Вероятности:\n"
72
+ for disease, prob in zip(TOMATO_CLASSES, probabilities):
73
+ result += f"{disease}: {prob*100:.2f}%\n"
74
+
75
+ return result
76
+
77
+ # Создание Gradio интерфейса
78
+ iface = gr.Interface(
79
+ fn=predict_disease,
80
+ inputs=gr.Image(type="numpy", label="Загрузите изображение листа томата"),
81
+ outputs=gr.Textbox(label="Результат диагностики"),
82
+ title="Диагностика болезней томатов",
83
+ description="Загрузите изображение листа томата для определения заболевания"
84
+ )
85
+
86
+ # Запуск интерфейса
87
+ if __name__ == "__main__":
88
+ iface.launch()