-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode
More file actions
167 lines (122 loc) · 5.15 KB
/
Copy pathcode
File metadata and controls
167 lines (122 loc) · 5.15 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import cv2
import datetime
import imutils
import numpy as np
import time
from centroidtracker import CentroidTracker
import clx.xms
import requests
protopath = "MobileNetSSD_deploy.prototxt"
modelpath = "MobileNetSSD_deploy.caffemodel"
detector = cv2.dnn.readNetFromCaffe(prototxt=protopath, caffeModel=modelpath)
#detector.setPreferableBackend(cv2.dnn.DNN_BACKEND_INFERENCE_ENGINE)
#detector.setPreferableTarget(cv2.dnn.DNN_TARGET_CPU)
counter = 0
client = clx.xms.Client(service_plan_id='12496f19ca8740ea82d8b0497b3f5ee4' , token= 'b5225fc227814a08a4419b28c3d287a2')
create = clx.xms.api.MtBatchTextSmsCreate()
create.sender = '+447537454924'
create.recipients = {'+919591440466'}
create.body = 'ALERT : Intruder has been detected at your work place!'
CLASSES = ["background", "aeroplane", "bicycle", "bird", "boat",
"bottle", "bus", "car", "cat", "chair", "cow", "diningtable",
"dog", "horse", "motorbike", "person", "pottedplant", "sheep",
"sofa", "train", "tvmonitor"]
tracker = CentroidTracker(maxDisappeared=20, maxDistance=90)
def non_max_suppression_fast(boxes, overlapThresh):
try:
if len(boxes) == 0:
return []
if boxes.dtype.kind == "i":
boxes = boxes.astype("float")
pick = []
x1 = boxes[:, 0]
y1 = boxes[:, 1]
x2 = boxes[:, 2]
y2 = boxes[:, 3]
area = (x2 - x1 + 1) * (y2 - y1 + 1)
idxs = np.argsort(y2)
while len(idxs) > 0:
last = len(idxs) - 1
i = idxs[last]
pick.append(i)
xx1 = np.maximum(x1[i], x1[idxs[:last]])
yy1 = np.maximum(y1[i], y1[idxs[:last]])
xx2 = np.minimum(x2[i], x2[idxs[:last]])
yy2 = np.minimum(y2[i], y2[idxs[:last]])
w = np.maximum(0, xx2 - xx1 + 1)
h = np.maximum(0, yy2 - yy1 + 1)
overlap = (w * h) / area[idxs[:last]]
idxs = np.delete(idxs, np.concatenate(([last],np.where(overlap > overlapThresh)[0])))
return boxes[pick].astype("int")
except Exception as e:
print("Exception occurred in non_max_suppression : {}".format(e))
def main():
global counter
global text
cap = cv2.VideoCapture('test_video.mp4')
global objectId
lpc_count = 0
opc_count = 0
object_id_list = []
while True:
ret, frame = cap.read()
frame = imutils.resize(frame, width=600)
(H, W) = frame.shape[:2]
blob = cv2.dnn.blobFromImage(frame, 0.007843, (W, H), 127.5)
detector.setInput(blob)
person_detections = detector.forward()
rects = []
for i in np.arange(0, person_detections.shape[2]):
confidence = person_detections[0, 0, i, 2]
if confidence > 0.5:
idx = int(person_detections[0, 0, i, 1])
if CLASSES[idx] != "person":
continue
person_box = person_detections[0, 0, i, 3:7] * np.array([W, H, W, H])
(startX, startY, endX, endY) = person_box.astype("int")
rects.append(person_box)
boundingboxes = np.array(rects)
boundingboxes = boundingboxes.astype(int)
rects = non_max_suppression_fast(boundingboxes, 0.3)
objects = tracker.update(rects)
for (objectId, bbox) in objects.items():
x1, y1, x2, y2 = bbox
x1 = int(x1)
y1 = int(y1)
x2 = int(x2)
y2 = int(y2)
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 0, 255), 2)
text = "ID: {}".format(objectId)
cv2.putText(frame, text, (x1, y1-5), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 0, 255), 1)
if objectId not in object_id_list:
object_id_list.append(objectId)
cv2.putText(frame, 'PERSON DETECTION', (5, 30), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 0, 255), 1)
try:
lpc_count = len(objects)
opc_count = len(object_id_list)
except:
lpc_count = 0
opc_count = 0
lpc_txt = "LPC: {}".format(lpc_count)
opc_txt = "OPC: {}".format(opc_count)
cv2.putText(frame, lpc_txt, (5, 60), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 0, 255), 1)
cv2.putText(frame, opc_txt, (5, 90), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 0, 255), 1)
cv2.imshow("Application", frame)
if(person_detections.any() and counter == 0):
try:
if(lpc_count>0):
counter = 1
print('ALERT : Intruder has been detected at your work place!')
#batch = client.create_batch(create)
except (requests.exceptions.RequestException, clx.xms.exceptions.ApiException) as ex:
print('Failed to communicate with XMS: %s' % str(ex))
if(person_detections.any() and counter == 1):
if(lpc_count==0):
counter = 0
print()
#sms ends here
key = cv2.waitKey(1)
if key == ord('q'):
break
cv2.destroyAllWindows()
main()