-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
executable file
·85 lines (69 loc) · 2.66 KB
/
run.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
#!/usr/bin/env python3
import json
import cv2
import sys
import time
import threading
import numpy as np
from datetime import datetime
from threading import Timer
from picamera import PiCamera
from securepi import app
from securepi import tools
from securepi.DetectorAPI import DetectorAPI
from securepi.camera import VideoCamera
from flask import Response
from securepi.models import User, Records
from securepi import db
import imageio
UPDATE_INTERVAL = 5 # only once in this time interval
VIDEO_CAMERA = VideoCamera(flip=False) # creates a camera object, flip vertically
API = DetectorAPI()
THRESHOLD = 0.7
def check_for_objects():
while True:
# try:
frame_in_bytes = VIDEO_CAMERA.get_frame()
#Convert the frame from bytes to nparray so it can be processed by the API
decoded = cv2.imdecode(np.frombuffer(frame_in_bytes, np.uint8), -1)
boxes, scores, classes, num = API.processFrame(decoded)
for i in range(len(boxes)):
# Class 1 represents human
if classes[i] == 1 and scores[i] > THRESHOLD:
box = boxes[i]
cv2.rectangle(decoded,(box[1],box[0]),(box[3],box[2]),(0,0,255),2)
color_conversion = cv2.cvtColor(decoded, cv2.COLOR_BGR2RGB)
now = datetime.now()
imageio.imwrite('securepi/static/records/{}.jpg'.format(now.strftime("%d-%m-%Y_%H:%M:%S")), color_conversion)
#update database
new_record = Records(created_at = now.strftime("%Y-%m-%d"), file_type = "picture", path_filename = "{}.jpg".format(now.strftime("%d-%m-%Y_%H:%M:%S")))
db.session.add(new_record)
db.session.commit()
# except:
# print("Error: ", sys.exc_info()[0])
# print(sys.exc_info()[1])
# print(sys.exc_info()[2].tb_lineno)
def gen(camera):
while True:
frame = camera.get_frame()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')
@app.route('/video_feed')
def video_feed():
return Response(gen(VIDEO_CAMERA),
mimetype='multipart/x-mixed-replace; boundary=frame')
def autoupdate():
print("AUTO UPDATE THREAD STARTING")
while True:
print('autoupdate check')
tools.update_config()
time.sleep(60*60*2)
if __name__ == '__main__':
t = threading.Thread(target=check_for_objects, args=())
t.daemon = True
t.start()
autoupdateThread = threading.Thread(target=autoupdate)
autoupdateThread.daemon = True
autoupdateThread.start()
#TODO CHECK EVERY ONCE AN HOUR IF THE IP ADDRESS IS THE SAME OR SEND MAIL WITH NEW ONE
app.run(host='0.0.0.0', debug=False)