SarowarSaurav commited on
Commit
9e69fb2
1 Parent(s): d28d64b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -68
app.py CHANGED
@@ -1,81 +1,38 @@
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 YOLOv8 model (ensure the model file is correct and accessible)
8
- try:
9
- model = YOLO('yolov8n.pt')
10
- print("Model loaded successfully.")
11
- except Exception as e:
12
- print(f"Error loading model: {e}")
13
 
14
- def identify_disease(image):
15
- # Convert the image to RGB if it's not already in RGB
16
- try:
17
- if image.mode != 'RGB':
18
- image = image.convert('RGB')
19
- print("Image converted to RGB successfully.")
20
- except Exception as e:
21
- print(f"Error converting image to RGB: {e}")
22
- return image, [{"Disease": "Error", "Confidence": "N/A"}]
23
 
24
- # Perform inference
25
- try:
26
- results = model(image)
27
- predictions = results[0]
28
- print("Model inference completed successfully.")
29
- except Exception as e:
30
- print(f"Error during model inference: {e}")
31
- return image, [{"Disease": "Error during inference", "Confidence": "N/A"}]
32
 
33
- # Check for detections
34
- if len(predictions.boxes) == 0:
35
- print("No detections found.")
36
- annotated_image = np.array(image)
37
- cv2.putText(annotated_image, "No disease detected", (10, 30),
38
- cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2)
39
- annotated_image = Image.fromarray(annotated_image)
40
- return annotated_image, [{"Disease": "None", "Confidence": "N/A"}]
41
-
42
- # Extract predictions and annotate image
43
- try:
44
- boxes = predictions.boxes
45
- labels = boxes.cls.cpu().numpy() if boxes.cls is not None else []
46
- scores = boxes.conf.cpu().numpy() if boxes.conf is not None else []
47
- class_names = model.names
48
-
49
- annotated_image = np.array(image)
50
- for box, label, score in zip(boxes.xyxy.cpu().numpy(), labels, scores):
51
- x1, y1, x2, y2 = map(int, box)
52
- class_name = class_names[int(label)]
53
- confidence = f"{score * 100:.2f}%"
54
- annotated_image = cv2.rectangle(annotated_image, (x1, y1), (x2, y2), (0, 255, 0), 2)
55
- annotated_image = cv2.putText(annotated_image, f"{class_name} {confidence}", (x1, y1 - 10),
56
- cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
57
- annotated_image = Image.fromarray(annotated_image)
58
- print("Image annotation completed.")
59
-
60
- # Prepare results list for output
61
- results_list = [{"Disease": class_names[int(label)], "Confidence": f"{score * 100:.2f}%"} for label, score in zip(labels, scores)]
62
- return annotated_image, results_list
63
-
64
- except Exception as e:
65
- print(f"Error during annotation: {e}")
66
- return image, [{"Disease": "Error during annotation", "Confidence": "N/A"}]
67
 
68
- # Define Gradio interface with updated syntax
69
  interface = gr.Interface(
70
- fn=identify_disease,
71
- inputs=gr.Image(type="pil"),
72
  outputs=[
73
- gr.Image(type="pil", label="Annotated Image"),
74
- gr.Dataframe(headers=["Disease", "Confidence"], label="Predictions")
 
75
  ],
76
- title="Leaf Disease Identifier with YOLOv8",
77
- description="Upload an image of a leaf, and this tool will identify the disease with confidence scores."
78
  )
79
 
80
  # Launch the app
81
- interface.launch()
 
 
1
  import gradio as gr
2
+ from transformers import pipeline
3
  from PIL import Image
 
 
4
 
5
+ # Load a pre-trained model for plant disease classification
6
+ model = pipeline("image-classification", model="microsoft/resnet-50") # Substitute with a specific plant disease model if available on Hugging Face
 
 
 
 
7
 
8
+ def classify_disease(image):
9
+ # Run the model on the image
10
+ results = model(image)
 
 
 
 
 
 
11
 
12
+ # Format the top result (assuming the top-1 result is most accurate)
13
+ disease_name = results[0]['label']
14
+ confidence_score = results[0]['score']
 
 
 
 
 
15
 
16
+ # Format the output
17
+ return {
18
+ "Disease Name": disease_name,
19
+ "Confidence Score": f"{confidence_score:.2f}",
20
+ "Uploaded Image": image
21
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
+ # Create Gradio Interface
24
  interface = gr.Interface(
25
+ fn=classify_disease,
26
+ inputs=gr.inputs.Image(type="pil"),
27
  outputs=[
28
+ gr.outputs.Textbox(label="Disease Name"),
29
+ gr.outputs.Textbox(label="Confidence Score"),
30
+ gr.outputs.Image(type="pil", label="Uploaded Image")
31
  ],
32
+ title="Tobacco Plant Disease Identification",
33
+ description="Upload an image of a tobacco plant leaf, and this model will identify the disease and show the confidence score."
34
  )
35
 
36
  # Launch the app
37
+ if __name__ == "__main__":
38
+ interface.launch()