-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSourceCode.py
More file actions
136 lines (117 loc) · 5.79 KB
/
SourceCode.py
File metadata and controls
136 lines (117 loc) · 5.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import cv2
import numpy as np
from ultralytics import YOLO
#working effectively
"""
understanding the prob:
build a collision warnin system that
detectcs vehicles such as car, buses, trucks and pedestrain/bikes
warns the driver if any objet gets close
avoivds false postives
object detection: identifying what is in the frame
proximity estimation: determing how close objects are
false postives : ensuiring warnings only trigger for genuine threats
a car occupying 30 % of the frame height is likely within 5 meters
a pedestrain at 15% might be 3 meters away.
"""
# Initialize
model = YOLO("yolov8n.pt") #yolov8 is pre-trained model to detect objects, openCV handle video input/output and drawing warningas
#cap = cv2.VideoCapture("speed4.mp4")
#cap = cv2.VideoCapture("camVideo.mp4")
cap = cv2.VideoCapture("speed2.mp4")
# Constants
TARGET_WIDTH, TARGET_HEIGHT = 1280, 720 #to make the frame size constant, it gives fixed resolution
CROP_RATIO = 0.85 # Crop out bonnet (lower 15%) #we will ignore bottom 15% car hood
# Proximity thresholds
MIN_DANGER_HEIGHT_LARGE = 0.25 * TARGET_HEIGHT # For cars/buses/trucks
#if the vehicle's height is > 25% of the frame height-> its very close
MIN_DANGER_HEIGHT_SMALL = 0.15 * TARGET_HEIGHT # For bikes/pedestrians
#pedestrain/bikes are smaller, so the height threshold is lower that is 0.15
Y_LOW_THRESHOLD = 0.65 * TARGET_HEIGHT # Objects in lower 35% of frame
#the object must be lower in the 35% of the frame to be considered a risk
CONFIDENCE_THRESHOLD = 0.7 # Reduce false positives
"""the above is object height threshold
closer object appear larger in the frame
closer objects also appear lower in the frame
(when looked from windhield dashcam)
therefore we will consider if a car is taller than 25% of the frame-> car/buses/trucks close
if a pedestrain is taller tahn 15% of the frame-> pedestrain/bicycle
y_low_threshold : this will check vertical postion, object below this lines are prioritized and are consider closer to car
"""
# Class-specific warnings (generalized for pedestrians/bikes)
CLASS_WARNINGS = {
0: "WARNING: PEDESTRIAN/BICYCLE!", # Person
2: "WARNING: CAR CLOSE!", # Car
3: "WARNING: BIKE CLOSE!", # Motorcycle (optional: replace with general warning if needed)
5: "WARNING: BUS CLOSE!", # Bus
7: "WARNING: TRUCK CLOSE!" # Truck
}
"""
its difficult to generalize for pedestrain and bikes
YOLOv8 confuse may confuse bikes and pesdestrain
we will make sepearte logic for it """
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
# Resize and crop bonnet
frame = cv2.resize(frame, (TARGET_WIDTH, TARGET_HEIGHT)) #it is help in keeping the frame size consistent
cropped_frame = frame[:int(TARGET_HEIGHT * CROP_RATIO), :] #cropped the frame implemented, we are just cropping the height so as to ignore the hood if it comes
#this willl help reduce the false postives
# Detect objects (classes: 0=person, 2=car, 3=motorcycle, 5=bus, 7=truck)
results = model(cropped_frame, classes=[0, 2, 3, 5, 7], conf=CONFIDENCE_THRESHOLD)
"""
classes=[0,2,3,5,7]: detect only
0:pedestrain
2:cars
3: motorcyles
5: buses
7: trucks
conf=confidence=threshold =0.7 : this would ignire low-confidence detections and reduce noise
"""
warning_text = None
for result in results:
boxes = result.boxes.xyxy.cpu().numpy()
class_ids = result.boxes.cls.cpu().numpy().astype(int)
for box, cls_id in zip(boxes, class_ids):
x1, y1, x2, y2 = box #these are bounding boxes coordinates
bbox_height = y2 - y1 #height of the bounding box in pixel
#closer objects appears larger in the frame and will have taller bounding box
y_bottom = y2 #vertical position
#as object closer to car appear lower in the frame, while a faraway one appears higher.
# bottom edge of the bounding
# Class-specific logic
if cls_id in [2, 5, 7]: # Cars, buses, trucks
if bbox_height > MIN_DANGER_HEIGHT_LARGE and y_bottom > Y_LOW_THRESHOLD: #trigger warning (e.g. "Car close")
#if the vehicle is taller than 25% of the frame and its bottom edge is in the lower 35%-> then trigger warning
warning_text = CLASS_WARNINGS[cls_id]
break
elif cls_id in [0, 3]: # Pedestrians or bikes
if bbox_height > MIN_DANGER_HEIGHT_SMALL and y_bottom > Y_LOW_THRESHOLD: #this condition is for pedestrain/bikes
#if the pedestrain is taller than 15% of the frame and low in the frame-> triggers a genral warning
#trigger warning
warning_text = CLASS_WARNINGS[0] # General warning for pedestrians/bikes
break
"""example scenarios
distant car:
bbox_height=10%, y_bottom=40% -> no warning (too small +too high)
close pedestrain:
bbox_height=20%, y_bottom=70%-> pedestrain meets both thresholds
approaching truck:
bbox_height=35%, y_bottom=80%->" truck close
y_bottom represents the bottom edge of the bounding box in pixels, it helps us avoid false alrams
y_low_threshold=0.65*frame_height 65% of frame height
if y_Bottom>y_low_threshold: objecct is in the lower 35% of the frame-> close
"""
# Display warning
if warning_text:
font_scale = TARGET_WIDTH / 1000
cv2.putText(frame, warning_text,
(int(TARGET_WIDTH*0.05), int(TARGET_HEIGHT*0.1)),
cv2.FONT_HERSHEY_SIMPLEX, font_scale*1.5,
(0, 0, 255), 3, cv2.LINE_AA) # this lines draws the warning text on the frame
cv2.imshow("Collision Warning", frame)
if cv2.waitKey(1) == 27:
break
cap.release()
cv2.destroyAllWindows()