Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import gradio as gr
|
3 |
+
import numpy as np
|
4 |
+
import pandas as pd
|
5 |
+
from ultralytics import YOLO
|
6 |
+
import cv2
|
7 |
+
|
8 |
+
# Load the trained YOLO model
|
9 |
+
model = YOLO('best.pt')
|
10 |
+
|
11 |
+
def predict_image(image):
|
12 |
+
"""
|
13 |
+
Perform object detection on the input image
|
14 |
+
|
15 |
+
Args:
|
16 |
+
image (numpy.ndarray): Input image
|
17 |
+
|
18 |
+
Returns:
|
19 |
+
tuple: Annotated image, results dataframe, precision-recall table
|
20 |
+
"""
|
21 |
+
# Perform prediction
|
22 |
+
results = model(image)[0]
|
23 |
+
|
24 |
+
# Create a copy of the image for annotation
|
25 |
+
annotated_image = image.copy()
|
26 |
+
|
27 |
+
# Prepare results for display
|
28 |
+
detection_results = []
|
29 |
+
|
30 |
+
# Draw bounding boxes and collect detection info
|
31 |
+
for box in results.boxes:
|
32 |
+
# Extract box coordinates
|
33 |
+
x1, y1, x2, y2 = box.xyxy[0].int().tolist()
|
34 |
+
|
35 |
+
# Get class and confidence
|
36 |
+
cls = int(box.cls[0])
|
37 |
+
conf = float(box.conf[0])
|
38 |
+
class_name = model.names[cls]
|
39 |
+
|
40 |
+
# Draw bounding box
|
41 |
+
cv2.rectangle(annotated_image, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
42 |
+
|
43 |
+
# Put text on the image
|
44 |
+
label = f'{class_name} {conf:.2f}'
|
45 |
+
cv2.putText(annotated_image, label, (x1, y1-10),
|
46 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
|
47 |
+
|
48 |
+
# Collect detection details
|
49 |
+
detection_results.append({
|
50 |
+
'Class': class_name,
|
51 |
+
'Confidence': f'{conf:.4f}',
|
52 |
+
'Bounding Box': f'[{x1},{y1},{x2},{y2}]'
|
53 |
+
})
|
54 |
+
|
55 |
+
# Create DataFrame for detections
|
56 |
+
detections_df = pd.DataFrame(detection_results)
|
57 |
+
|
58 |
+
# Generate precision-recall metrics (placeholder - you'll need to replace with actual metrics)
|
59 |
+
pr_metrics = generate_pr_metrics()
|
60 |
+
|
61 |
+
return annotated_image, detections_df, pr_metrics
|
62 |
+
|
63 |
+
def generate_pr_metrics():
|
64 |
+
"""
|
65 |
+
Generate placeholder precision and recall metrics
|
66 |
+
|
67 |
+
Note: Replace this with actual calculations from your training results
|
68 |
+
"""
|
69 |
+
# This is a placeholder - you should replace with actual metrics from your training
|
70 |
+
metrics_data = {
|
71 |
+
'Class': model.names.values(),
|
72 |
+
'Precision': [0.85, 0.90, 0.88], # Example values
|
73 |
+
'Recall': [0.80, 0.85, 0.82] # Example values
|
74 |
+
}
|
75 |
+
|
76 |
+
metrics_df = pd.DataFrame(metrics_data)
|
77 |
+
return metrics_df
|
78 |
+
|
79 |
+
# Create Gradio interface
|
80 |
+
def launch_interface():
|
81 |
+
# Input image upload
|
82 |
+
image_input = gr.Image(type="numpy", label="Upload Image")
|
83 |
+
|
84 |
+
# Outputs
|
85 |
+
annotated_image_output = gr.Image(label="Annotated Image")
|
86 |
+
detections_output = gr.Dataframe(label="Detection Results")
|
87 |
+
metrics_output = gr.Dataframe(label="Precision-Recall Metrics")
|
88 |
+
|
89 |
+
# Gradio interface
|
90 |
+
demo = gr.Interface(
|
91 |
+
fn=predict_image,
|
92 |
+
inputs=image_input,
|
93 |
+
outputs=[
|
94 |
+
annotated_image_output,
|
95 |
+
detections_output,
|
96 |
+
metrics_output
|
97 |
+
],
|
98 |
+
title="YOLO Object Detection",
|
99 |
+
description="Upload an image for object detection"
|
100 |
+
)
|
101 |
+
|
102 |
+
return demo
|
103 |
+
|
104 |
+
# Launch the app
|
105 |
+
if __name__ == "__main__":
|
106 |
+
demo = launch_interface()
|
107 |
+
demo.launch()
|