Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,55 +1,31 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
3 |
from PIL import Image
|
4 |
-
import numpy as np
|
5 |
-
import torch
|
6 |
|
7 |
-
# Load the
|
8 |
-
model
|
|
|
9 |
|
10 |
def identify_disease(image):
|
11 |
-
#
|
12 |
-
|
13 |
-
image = image.convert('RGB')
|
14 |
|
15 |
-
#
|
16 |
-
results =
|
17 |
|
18 |
-
#
|
19 |
-
|
20 |
-
boxes = predictions.boxes
|
21 |
-
labels = boxes.cls.cpu().numpy()
|
22 |
-
scores = boxes.conf.cpu().numpy()
|
23 |
-
class_names = model.names
|
24 |
-
|
25 |
-
# Annotate image with bounding boxes and labels
|
26 |
-
annotated_image = np.array(image)
|
27 |
-
for box, label, score in zip(boxes.xyxy.cpu().numpy(), labels, scores):
|
28 |
-
x1, y1, x2, y2 = map(int, box)
|
29 |
-
class_name = class_names[int(label)]
|
30 |
-
confidence = f"{score * 100:.2f}%"
|
31 |
-
annotated_image = cv2.rectangle(annotated_image, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
32 |
-
annotated_image = cv2.putText(annotated_image, f"{class_name} {confidence}", (x1, y1 - 10),
|
33 |
-
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
|
34 |
-
|
35 |
-
# Convert annotated image back to PIL format
|
36 |
-
annotated_image = Image.fromarray(annotated_image)
|
37 |
-
|
38 |
-
# Prepare results for display
|
39 |
-
results_list = [{"Disease": class_names[int(label)], "Confidence": f"{score * 100:.2f}%"} for label, score in zip(labels, scores)]
|
40 |
-
|
41 |
-
return annotated_image, results_list
|
42 |
|
43 |
# Define Gradio interface
|
44 |
interface = gr.Interface(
|
45 |
-
fn=identify_disease,
|
46 |
inputs=gr.inputs.Image(type="pil"),
|
47 |
outputs=[
|
48 |
-
gr.outputs.Image(type="pil", label="
|
49 |
-
gr.outputs.Dataframe(
|
50 |
],
|
51 |
-
title="
|
52 |
-
description="Upload an image of a
|
53 |
)
|
54 |
|
55 |
# Launch the app
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
from PIL import Image
|
|
|
|
|
4 |
|
5 |
+
# Load the Hugging Face image classification pipeline with EfficientNetB0
|
6 |
+
# This model is a general-purpose model for plant disease classification
|
7 |
+
classifier = pipeline("image-classification", model="nateraw/efficientnet-b0")
|
8 |
|
9 |
def identify_disease(image):
|
10 |
+
# Use the classifier to predict the disease
|
11 |
+
predictions = classifier(image)
|
|
|
12 |
|
13 |
+
# Format the output to include disease name and confidence score
|
14 |
+
results = [{"Disease": pred["label"], "Confidence": f"{pred['score'] * 100:.2f}%"} for pred in predictions]
|
15 |
|
16 |
+
# Return the uploaded image along with the results
|
17 |
+
return image, results
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
# Define Gradio interface
|
20 |
interface = gr.Interface(
|
21 |
+
fn=identify_disease,
|
22 |
inputs=gr.inputs.Image(type="pil"),
|
23 |
outputs=[
|
24 |
+
gr.outputs.Image(type="pil", label="Uploaded Image"),
|
25 |
+
gr.outputs.Dataframe(type="pandas", label="Predictions")
|
26 |
],
|
27 |
+
title="Plant Disease Identifier",
|
28 |
+
description="Upload an image of a plant leaf, and this tool will identify the disease along with the confidence score."
|
29 |
)
|
30 |
|
31 |
# Launch the app
|