-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdetect.py
136 lines (121 loc) · 5.97 KB
/
detect.py
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 as cv
riding = ['motorcycling','riding a bike','riding scooter','riding mountain bike']
smoking = ['smoking','smoking hookah']
driving = ['driving car','driving tractor']
alcohol = ['tasting beer','drinking beer']
#Determine the IOU of two bounding boxes
def bb_intersection_over_union(boxA, boxB):
# determine the (x, y)-coordinates of the intersection rectangle
xA = max(boxA[0], boxB[0])
yA = max(boxA[1], boxB[1])
xB = min(boxA[2], boxB[2])
yB = min(boxA[3], boxB[3])
intersectionArea = (xB - xA) * (yB - yA)
unionArea = (boxA[2]*boxA[3])+(boxB[2]*boxB[3])-intersectionArea;
overlapArea = intersectionArea/unionArea;
return overlapArea
def detectChecking(img, boxes, confidences, classids, idxs, colors, labels,height,labelh):
# If there are any detections
detect = 0
if len(idxs) > 0:
print("idxs :",idxs.flatten())
motorcycle = list(filter(lambda x: classids[x] == 0,idxs.flatten()))
if (labelh in riding) and len(motorcycle) > 0:
whelmet = list(filter(lambda x: classids[x] == 1,idxs.flatten()))
helmet = list(filter(lambda x: classids[x] == 2,idxs.flatten()))
print("Motorcycle :",motorcycle)
print("helmet :",helmet)
print("whelmet :",whelmet)
if len(motorcycle) != 0:
for i in motorcycle:
x, y = boxes[i][0], boxes[i][1]
w, h = boxes[i][2], boxes[i][3]
motorBox = [x,y,w,h]
if len(helmet) != 0:
for j in helmet:
x, y = boxes[j][0], boxes[j][1]
w, h = boxes[j][2], boxes[j][3]
helmetBox = [x,y,w,h]
iou = bb_intersection_over_union(motorBox,helmetBox)
print("iou:",iou)
if iou < 0.25:
detect = True #person weared helmet
break
if detect: #if person not weared helmet return 1 for adding
detect = 0
else:
detect = 1
#if any without helmet class detects
if len(whelmet) != 0:
for j in whelmet:
x, y = boxes[i][0], boxes[i][1]
w, h = boxes[i][2], boxes[i][3]
whelmetBox = [x,y,w,h]
iou = bb_intersection_over_union(motorBox,whelmetBox)
print("iou :",iou)
if iou < 0.35:
detect = 1
return detect #person not weared helmet
break
return detect
#Smoking detection
elif labelh in smoking or alcohol:
smoke = list(filter(lambda x: classids[x] == 0,idxs.flatten()))
print(smoke)
# for i in idxs.flatten():
# x, y = boxes[i][0], boxes[i][1]
# w, h = boxes[i][2], boxes[i][3]
# color = [int(c) for c in colors[classids[i]]]
# #Draw the bounding box rectangle and label on the image
# cv.rectangle(img, (x, y), (x+w, y+h), color, 2)
# text = "{}: {:4f}".format(labels[classids[i]], confidences[i])
# cv.putText(img, text, (x, y-5), cv.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
# cv.imwrite("frame.jpg",img)
#key = cv.waitKey(0) & 0xFF
if len(smoke) > 0:
detect = 2
return detect
else:
detect = 0
return detect
#Seat belt detection
elif labelh in driving:
inside_car = list(filter(lambda x: classids[x] == 0,idxs.flatten()))
woutseatbelt = list(filter(lambda x: classids[x] == 1,idxs.flatten()))
wseatbelt = list(filter(lambda x: classids[x] == 2,idxs.flatten()))
if len(inside_car) != 0:
for i in inside_car:
x, y = boxes[i][0], boxes[i][1]
w, h = boxes[i][2], boxes[i][3]
carBox = [x,y,w,h]
if len(wseatbelt) != 0:
for j in wseatbelt:
x, y = boxes[j][0], boxes[j][1]
w, h = boxes[j][2], boxes[j][3]
seatbeltBox = [x,y,w,h]
iou = bb_intersection_over_union(carBox,seatbeltBox)
print("iou:",iou)
if iou < 0.25:
detect = True #person weared helmet
break
if detect: #if person not weared helmet return 1 for adding
detect = 0
else:
detect = 3
#if any without helmet class detects
if len(woutseatbelt) != 0:
for j in woutseatbelt:
x, y = boxes[j][0], boxes[j][1]
w, h = boxes[j][2], boxes[j][3]
wseatbeltBox = [x,y,w,h]
iou = bb_intersection_over_union(carBox,wseatbeltBox)
print("iou :",iou)
if iou < 0.25:
detect = 3
return detect #person not weared helmet
break
return detect
cv.imshow("frame",img)
key = cv.waitKey(1) & 0xFF
else:
return detect