KhadijaAsehnoune12
commited on
Commit
•
8bd5f74
1
Parent(s):
a665ee5
Update app.py
Browse files
app.py
CHANGED
@@ -1,118 +1,11 @@
|
|
1 |
-
import os
|
2 |
-
import sys
|
3 |
-
|
4 |
-
current = os.path.dirname(os.path.realpath(__file__))
|
5 |
-
parent = os.path.dirname(current)
|
6 |
-
sys.path.append(parent)
|
7 |
-
|
8 |
-
import albumentations as A
|
9 |
import gradio as gr
|
10 |
-
import matplotlib.pyplot as plt
|
11 |
-
import numpy as np
|
12 |
-
import torch
|
13 |
-
from albumentations.pytorch import ToTensorV2
|
14 |
-
from model import Classifier
|
15 |
-
from PIL import Image
|
16 |
-
|
17 |
-
# Load the model
|
18 |
-
model = Classifier.load_from_checkpoint("KhadijaAsehnoune12/orange-disease-detector")
|
19 |
-
model.eval()
|
20 |
-
|
21 |
-
# Define labels
|
22 |
-
labels = [
|
23 |
-
"Chancre citrique",
|
24 |
-
"Feuillage endommagé",
|
25 |
-
"Jaunissement des feuilles d'agrumes",
|
26 |
-
"Maladie de l'oïdium",
|
27 |
-
"Mouche blanche - Aleurode",
|
28 |
-
"Trou de balle",
|
29 |
-
"Verdissement des agrumes",
|
30 |
-
"cochenille blanche de l'oranger",
|
31 |
-
"déclin des agrumes",
|
32 |
-
"feuille saine",
|
33 |
-
]
|
34 |
-
|
35 |
-
# Preprocess function
|
36 |
-
def preprocess(image):
|
37 |
-
image = np.array(image)
|
38 |
-
resize = A.Resize(224, 224)
|
39 |
-
normalize = A.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))
|
40 |
-
to_tensor = ToTensorV2()
|
41 |
-
transform = A.Compose([resize, normalize, to_tensor])
|
42 |
-
image = transform(image=image)["image"]
|
43 |
-
return image
|
44 |
-
|
45 |
-
|
46 |
-
# Define the function to make predictions on an image
|
47 |
-
def predict(image):
|
48 |
-
try:
|
49 |
-
image = preprocess(image).unsqueeze(0)
|
50 |
-
|
51 |
-
# Prediction
|
52 |
-
# Make a prediction on the image
|
53 |
-
with torch.no_grad():
|
54 |
-
output = model(image)
|
55 |
-
# convert to probabilities
|
56 |
-
probabilities = torch.nn.functional.softmax(output[0])
|
57 |
-
|
58 |
-
# get top probabilities
|
59 |
-
topk_prob, topk_label = torch.topk(probabilities, 3)
|
60 |
-
|
61 |
-
# Return the top 3 predictions
|
62 |
-
return {
|
63 |
-
labels[label]: float(prob) for label, prob in zip(topk_label, topk_prob)
|
64 |
-
}
|
65 |
-
except Exception as e:
|
66 |
-
print(f"Error predicting image: {e}")
|
67 |
-
return []
|
68 |
-
|
69 |
-
|
70 |
-
# Define the interface
|
71 |
-
def app():
|
72 |
-
title = "Leaf Disease Image Classification"
|
73 |
-
|
74 |
-
gr.Interface(
|
75 |
-
title=title,
|
76 |
-
fn=predict,
|
77 |
-
inputs=gr.Image(type="pil"),
|
78 |
-
outputs=gr.Label(
|
79 |
-
num_top_classes=3,
|
80 |
-
),
|
81 |
-
examples=[
|
82 |
-
"MoucheB.jpg",
|
83 |
-
"verdissement.jpg",
|
84 |
-
],
|
85 |
-
).launch()
|
86 |
-
|
87 |
-
|
88 |
-
# Run the app
|
89 |
-
if __name__ == "__main__":
|
90 |
-
app()
|
91 |
-
|
92 |
-
|
93 |
-
# import gradio as gr
|
94 |
-
# import requests
|
95 |
-
# from PIL import Image
|
96 |
-
# from torchvision import transforms
|
97 |
-
# import torch
|
98 |
-
|
99 |
-
# model = torch.hub.load('KhadijaAsehnoune12/orange-disease-detector', 'orange-disease-detector', pretrained=True ,trust_repo=True).eval()
|
100 |
-
|
101 |
-
# # Download human-readable labels for ImageNet.
|
102 |
-
# response = requests.get("https://git.io/JJkYN")
|
103 |
-
# labels = response.text.split("\n")
|
104 |
-
|
105 |
-
# def predict(inp):
|
106 |
-
# inp = transforms.ToTensor()(inp).unsqueeze(0)
|
107 |
-
# with torch.no_grad():
|
108 |
-
# prediction = torch.nn.functional.softmax(model(inp)[0], dim=0)
|
109 |
-
# confidences = {labels[i]: float(prediction[i]) for i in range(100)}
|
110 |
-
# return confidences
|
111 |
-
|
112 |
-
# gr.Interface(fn=predict,
|
113 |
-
# inputs=gr.Image(type="pil"),
|
114 |
-
# outputs=gr.Label(num_top_classes=3),
|
115 |
-
# examples=["MoucheB.jpg" , "verdissement.jpg"],
|
116 |
-
# css=".footer{display:none !important}",
|
117 |
-
# title=None).launch()
|
118 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
+
gr.Interface.load(
|
4 |
+
"KhadijaAsehnoune12/orange-disease-detector",
|
5 |
+
theme="default",
|
6 |
+
examples=[["alligator.jpg"], ["laptop.jpg"]],
|
7 |
+
inputs=gr.Image(type="pil"),
|
8 |
+
outputs=gr.Label(num_top_classes=3),
|
9 |
+
examples=["MoucheB.jpg" , "verdissement.jpg"],
|
10 |
+
css=".footer{display:none !important}",
|
11 |
+
title=None).launch()
|