forked from JaideepCherukuri/smart-surveillance-using-pi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
79 lines (58 loc) · 2.67 KB
/
main.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
import cv2
import numpy as np
import adv
import telepot
from telepot.loop import MessageLoop
import time
bot = telepot.Bot("987039509:AAHX_HjTmoaqG_9ZUiGMBKzuBrq7-d9_Xcg")
MessageLoop(bot, adv.handle).run_as_thread()
# Pretrained classes in the model
classNames = {0: 'background',
1: 'person'}
def id_class_name(class_id, classes):
for key, value in classes.items():
if class_id == key:
return value
def adjust_gamma(image, gamma=1.0):
# build a lookup table mapping the pixel values [0, 255] to
# their adjusted gamma values
invGamma = 1.0 / gamma
table = np.array([((i / 255.0) ** invGamma) * 255
for i in np.arange(0, 256)]).astype("uint8")
# apply gamma correction using the lookup table
return cv2.LUT(image, table)
cap= cv2.VideoCapture(0)
# Loading model
model = cv2.dnn.readNetFromTensorflow('models/frozen_inference_graph.pb',
'models/ssd_mobilenet_v2_coco_2018_03_29.pbtxt')
send_status = 1
i = 0
while True:
ret, image = cap.read()
if cv2.waitKey(50) & 0xFF == ord('q'):
break
if ret == False:
break
image = adjust_gamma(image, gamma=1.5)
image_height, image_width, _ = image.shape
model.setInput(cv2.dnn.blobFromImage(image, size=(300, 300), swapRB=True))
output = model.forward()
for detection in output[0, 0, :, :]:
confidence = detection[2]
if confidence > 0.5:
class_id = detection[1]
if class_id == 1.0:
class_name=id_class_name(class_id,classNames)
box_x = detection[3] * image_width
box_y = detection[4] * image_height
box_width = detection[5] * image_width
box_height = detection[6] * image_height
cv2.rectangle(image, (int(box_x), int(box_y)), (int(box_width), int(box_height)), (23, 230, 210), thickness=1)
i += 1
if(send_status == 1):
cv2.imwrite("img/cap_"+str(int(i/50))+".jpg", image)
bot.sendPhoto(824389035, photo=open("img/cap_"+str(int(i/50))+".jpg", 'rb'), caption="<b>[Motion Detected]</b> \n<pre>I think there is someone in your house</pre>. \n\nDo you want to take any action? \nClick /yes to call police \nClick /no to ignore", parse_mode='HTML')
send_status = 0
cv2.imshow('image', image)
cv2.waitKey(0)
cv2.destroyAllWindows()