YarramsettiNaresh commited on
Commit
20bd21b
·
1 Parent(s): a82751a
Files changed (3) hide show
  1. app.py +80 -0
  2. model_- 11 october 2024 11_07.pt +3 -0
  3. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Created by yarramsettinaresh GORAKA DIGITAL PRIVATE LIMITED at 24/10/24
2
+ import gradio as gr
3
+ import cv2
4
+ import time
5
+ from ultralytics import YOLO
6
+
7
+ # Load your YOLO model (adjust model path or type as needed)
8
+ model_path = "/Users/yarramsettinaresh/Downloads/model_- 11 october 2024 11_07.pt"
9
+ model = YOLO(model_path)
10
+
11
+
12
+ def ultralytics_predict(model, frame):
13
+ confidence_threshold = 0.2
14
+ start_time = time.time()
15
+ results = model(frame) # Perform inference on the frame
16
+ end_time = time.time()
17
+
18
+ duration = end_time - start_time
19
+ print(f"Prediction duration: {duration:.4f} seconds")
20
+ duration_str = f"{duration:.4f} S"
21
+
22
+ object_count = {} # Dictionary to store counts of detected objects
23
+
24
+ for detection in results[0].boxes: # Iterate through detections
25
+ conf = float(detection.conf[0]) # Confidence score
26
+ if conf > confidence_threshold:
27
+ conf, pos, text, color = ultralytics(detection, duration_str)
28
+ cv2.rectangle(frame, pos[0], pos[1], color, 2)
29
+ cv2.putText(frame, text, (pos[0][0], pos[0][1] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
30
+
31
+ # Update object count
32
+ class_id = int(detection.cls[0])
33
+ class_name = model.names[class_id]
34
+ object_count[class_name] = object_count.get(class_name, 0) + 1
35
+
36
+ return object_count # Return the count of detected objects
37
+
38
+
39
+ def ultralytics(detection, duration):
40
+ COLOUR_MAP = {
41
+ 0: (0, 0, 255), # Red in BGR format
42
+ 1: (0, 255, 0) # Green in BGR format
43
+ }
44
+
45
+ conf = float(detection.conf[0]) # Confidence score
46
+ class_id = int(detection.cls[0]) # Class ID
47
+ name = model.names[class_id] # Get class name
48
+ xmin, ymin, xmax, ymax = map(int, detection.xyxy[0]) # Bounding box coordinates
49
+ color = COLOUR_MAP.get(class_id, (255, 255, 255)) # Default to white if not found
50
+
51
+ # Draw bounding box and label on the frame
52
+ pos = (xmin, ymin), (xmax, ymax)
53
+ text = f"{name} {round(conf, 2)} :{duration}"
54
+
55
+ return conf, pos, text, color
56
+
57
+
58
+ def process_frame(frame):
59
+ object_count = ultralytics_predict(model, frame)
60
+ return frame, object_count # Return frame and object count
61
+
62
+
63
+ def detect_image(image):
64
+ image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) # Convert to BGR format for OpenCV
65
+ result_frame, object_count = process_frame(image)
66
+ result_frame = cv2.cvtColor(result_frame, cv2.COLOR_BGR2RGB) # Convert back to RGB for Gradio
67
+ return result_frame, object_count # Return both the frame and the count
68
+
69
+
70
+ # Create Gradio Interface
71
+ gr.Interface(
72
+ fn=detect_image,
73
+ inputs=gr.Image(type="numpy"), # Updated input format
74
+ outputs=[
75
+ gr.Image(type="numpy"), # Image output
76
+ gr.JSON(), # Object count output as JSON
77
+ ],
78
+ title="YOLO Object Detection",
79
+ description="Upload an image to detect objects using YOLO model."
80
+ ).launch()
model_- 11 october 2024 11_07.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e4f23f88664dbf1177b44c2c989c627da63d8c27ba3db4456da86f26271e7a44
3
+ size 5480979
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ gradio
2
+ opencv-python
3
+ ultralytics