SarowarSaurav commited on
Commit
13ef1fa
1 Parent(s): ba2ce75

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -15
app.py CHANGED
@@ -1,31 +1,56 @@
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
 
1
  import gradio as gr
2
+ from ultralytics import YOLO
3
  from PIL import Image
4
+ import numpy as np
5
+ import cv2
6
 
7
+ # Load the pre-trained YOLOv5 model
8
+ # Use 'yolov5s' or any available YOLO model from ultralytics
9
+ model = YOLO('yolov5s') # Change to a leaf disease detection model if available
10
 
11
  def identify_disease(image):
12
+ # Convert the image to RGB if it's not
13
+ if image.mode != 'RGB':
14
+ image = image.convert('RGB')
15
 
16
+ # Perform inference
17
+ results = model(image)
18
 
19
+ # Extract predictions
20
+ predictions = results[0]
21
+ boxes = predictions.boxes
22
+ labels = boxes.cls.cpu().numpy()
23
+ scores = boxes.conf.cpu().numpy()
24
+ class_names = model.names
25
+
26
+ # Annotate image with bounding boxes and labels
27
+ annotated_image = np.array(image)
28
+ for box, label, score in zip(boxes.xyxy.cpu().numpy(), labels, scores):
29
+ x1, y1, x2, y2 = map(int, box)
30
+ class_name = class_names[int(label)]
31
+ confidence = f"{score * 100:.2f}%"
32
+ annotated_image = cv2.rectangle(annotated_image, (x1, y1), (x2, y2), (0, 255, 0), 2)
33
+ annotated_image = cv2.putText(annotated_image, f"{class_name} {confidence}", (x1, y1 - 10),
34
+ cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
35
+
36
+ # Convert annotated image back to PIL format
37
+ annotated_image = Image.fromarray(annotated_image)
38
+
39
+ # Prepare results for display
40
+ results_list = [{"Disease": class_names[int(label)], "Confidence": f"{score * 100:.2f}%"} for label, score in zip(labels, scores)]
41
+
42
+ return annotated_image, results_list
43
 
44
  # Define Gradio interface
45
  interface = gr.Interface(
46
+ fn=identify_disease,
47
  inputs=gr.inputs.Image(type="pil"),
48
  outputs=[
49
+ gr.outputs.Image(type="pil", label="Annotated Image"),
50
+ gr.outputs.Dataframe(headers=["Disease", "Confidence"], label="Predictions")
51
  ],
52
+ title="Leaf Disease Identifier with YOLOv5",
53
+ description="Upload an image of a leaf, and this tool will identify the disease with confidence scores."
54
  )
55
 
56
  # Launch the app