SarowarSaurav commited on
Commit
c455a41
·
verified ·
1 Parent(s): 6f2a51c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -8
app.py CHANGED
@@ -4,8 +4,12 @@ from PIL import Image
4
  import numpy as np
5
  import cv2
6
 
7
- # Load the YOLOv8 model (ensure this path is correct)
8
- model = YOLO('yolov8n.pt')
 
 
 
 
9
 
10
  def identify_disease(image):
11
  # Convert the image to RGB if it's not
@@ -13,12 +17,17 @@ def identify_disease(image):
13
  image = image.convert('RGB')
14
 
15
  # Perform inference
16
- results = model(image)
17
- predictions = results[0]
 
 
 
 
 
18
 
19
  # Check if there are any detections
20
  if len(predictions.boxes) == 0:
21
- # No detections, return the image with a message
22
  annotated_image = np.array(image)
23
  cv2.putText(annotated_image, "No disease detected", (10, 30),
24
  cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2)
@@ -27,10 +36,10 @@ def identify_disease(image):
27
 
28
  # Extract predictions
29
  boxes = predictions.boxes
30
- labels = boxes.cls.cpu().numpy()
31
- scores = boxes.conf.cpu().numpy()
32
  class_names = model.names
33
-
34
  # Annotate image with bounding boxes and labels
35
  annotated_image = np.array(image)
36
  for box, label, score in zip(boxes.xyxy.cpu().numpy(), labels, scores):
 
4
  import numpy as np
5
  import cv2
6
 
7
+ # Load the YOLOv8 model (make sure the path is correct and model is downloaded)
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
 
17
  image = image.convert('RGB')
18
 
19
  # Perform inference
20
+ try:
21
+ results = model(image)
22
+ predictions = results[0]
23
+ print("Inference completed.")
24
+ except Exception as e:
25
+ print(f"Error during inference: {e}")
26
+ return image, [{"Disease": "Error", "Confidence": "N/A"}]
27
 
28
  # Check if there are any detections
29
  if len(predictions.boxes) == 0:
30
+ print("No detections found.")
31
  annotated_image = np.array(image)
32
  cv2.putText(annotated_image, "No disease detected", (10, 30),
33
  cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2)
 
36
 
37
  # Extract predictions
38
  boxes = predictions.boxes
39
+ labels = boxes.cls.cpu().numpy() if boxes.cls is not None else []
40
+ scores = boxes.conf.cpu().numpy() if boxes.conf is not None else []
41
  class_names = model.names
42
+
43
  # Annotate image with bounding boxes and labels
44
  annotated_image = np.array(image)
45
  for box, label, score in zip(boxes.xyxy.cpu().numpy(), labels, scores):