pedrororo commited on
Commit
7aa9f42
·
verified ·
1 Parent(s): 63fb868

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +100 -0
  2. best.pt +3 -0
  3. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import cv2
3
+ import random
4
+ import matplotlib.pyplot as plt
5
+ from ultralytics import YOLO
6
+ import gradio as gr
7
+
8
+ # Load the trained YOLO model
9
+ MODEL_PATH = "best.pt" # Replace with your model's path
10
+ model = YOLO(MODEL_PATH)
11
+
12
+ def random_color():
13
+ """Generate a random color for bounding boxes."""
14
+ return (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
15
+
16
+ def detect_and_visualize(image_path):
17
+ """
18
+ Perform object detection and visualize the results.
19
+
20
+ Args:
21
+ image_path (str): Path to the input image.
22
+
23
+ Returns:
24
+ Annotated image as an array.
25
+ Detection details as a dictionary.
26
+ """
27
+ # Perform object detection
28
+ results = model(image_path)
29
+
30
+ # Read the input image
31
+ image = cv2.imread(image_path)
32
+ image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # Convert to RGB
33
+
34
+ detections = []
35
+ for result in results:
36
+ boxes = result.boxes.xyxy.cpu().numpy() # Bounding box coordinates
37
+ confidences = result.boxes.conf.cpu().numpy() # Confidence scores
38
+ class_ids = result.boxes.cls.cpu().numpy().astype(int) # Class IDs
39
+
40
+ for box, confidence, class_id in zip(boxes, confidences, class_ids):
41
+ x_min, y_min, x_max, y_max = map(int, box)
42
+ class_name = model.names[class_id]
43
+
44
+ # Draw bounding box and label
45
+ color = random_color()
46
+ cv2.rectangle(image, (x_min, y_min), (x_max, y_max), color, 2)
47
+ label = f"{class_name} {confidence:.2f}"
48
+ cv2.putText(image, label, (x_min, y_min - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
49
+
50
+ # Append detection details
51
+ detections.append({
52
+ "label": class_name,
53
+ "confidence": float(confidence),
54
+ "bounding_box": {
55
+ "x1": x_min,
56
+ "y1": y_min,
57
+ "x2": x_max,
58
+ "y2": y_max
59
+ }
60
+ })
61
+
62
+ # Optionally save the annotated image
63
+ output_path = "output/annotated_image.jpg"
64
+ os.makedirs(os.path.dirname(output_path), exist_ok=True)
65
+ cv2.imwrite(output_path, cv2.cvtColor(image, cv2.COLOR_RGB2BGR))
66
+ print(f"Annotated image saved to {output_path}")
67
+
68
+ return cv2.cvtColor(image, cv2.COLOR_RGB2BGR), detections
69
+
70
+ # Gradio interface
71
+ def gradio_interface(image):
72
+ """
73
+ Gradio-compatible wrapper for object detection.
74
+
75
+ Args:
76
+ image (numpy.array): Input image.
77
+
78
+ Returns:
79
+ Annotated image and detection details.
80
+ """
81
+ temp_input_path = "temp_input.jpg"
82
+ cv2.imwrite(temp_input_path, cv2.cvtColor(image, cv2.COLOR_RGB2BGR)) # Save temp file for YOLO
83
+ annotated_image, detections = detect_and_visualize(temp_input_path)
84
+ os.remove(temp_input_path) # Clean up temp file
85
+ return annotated_image, detections
86
+
87
+ # Define Gradio interface
88
+ interface = gr.Interface(
89
+ fn=gradio_interface,
90
+ inputs=gr.Image(type="numpy", label="Upload Image"),
91
+ outputs=[
92
+ gr.Image(type="numpy", label="Annotated Image"),
93
+ gr.JSON(label="Detection Details")
94
+ ],
95
+ title="YOLO Object Detection",
96
+ description="Upload an image to detect objects and view annotated results along with detailed detection data."
97
+ )
98
+
99
+ if __name__ == "__main__":
100
+ interface.launch()
best.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:832aa82977bb37e2cef48b59118b64a705aefb476bcaa7fd94c9f9fe3ef84b91
3
+ size 116598226
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ gradio
2
+ ultralytics
3
+ opencv-python
4
+ matplotlib