Spaces:
Sleeping
Sleeping
SarowarSaurav
commited on
Commit
•
d28d64b
1
Parent(s):
c455a41
Update app.py
Browse files
app.py
CHANGED
@@ -4,7 +4,7 @@ from PIL import Image
|
|
4 |
import numpy as np
|
5 |
import cv2
|
6 |
|
7 |
-
# Load the YOLOv8 model (
|
8 |
try:
|
9 |
model = YOLO('yolov8n.pt')
|
10 |
print("Model loaded successfully.")
|
@@ -12,20 +12,25 @@ 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
|
16 |
-
|
17 |
-
image
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
# Perform inference
|
20 |
try:
|
21 |
results = model(image)
|
22 |
predictions = results[0]
|
23 |
-
print("
|
24 |
except Exception as e:
|
25 |
-
print(f"Error during inference: {e}")
|
26 |
-
return image, [{"Disease": "Error", "Confidence": "N/A"}]
|
27 |
|
28 |
-
# Check
|
29 |
if len(predictions.boxes) == 0:
|
30 |
print("No detections found.")
|
31 |
annotated_image = np.array(image)
|
@@ -34,29 +39,31 @@ def identify_disease(image):
|
|
34 |
annotated_image = Image.fromarray(annotated_image)
|
35 |
return annotated_image, [{"Disease": "None", "Confidence": "N/A"}]
|
36 |
|
37 |
-
# Extract predictions
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
|
59 |
-
|
|
|
|
|
60 |
|
61 |
# Define Gradio interface with updated syntax
|
62 |
interface = gr.Interface(
|
|
|
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.")
|
|
|
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)
|
|
|
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(
|