forked from log0/video_streaming_with_flask_example
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.py
228 lines (183 loc) · 7.49 KB
/
utils.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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import base64
import os
import time
from datetime import datetime
import logging
import json
from importlib import import_module
import requests
import cv2
from apscheduler.schedulers.background import BackgroundScheduler
Camera = import_module(
'camera.camera_' + os.environ.get('CAMERA', 'opencv')).Camera
logger = logging.getLogger()
def _dump_message(message):
logger.info(message)
print(message)
DETECT_API_CREDENTIALS = {
'user': os.environ['DETECT_API_USERNAME'],
'pass': os.environ['DETECT_API_PASSWORD']
}
REMOTE_DETECT_SERVER = os.environ.get(
'REMOTE_DETECT_SERVER', 'http://localhost:5001/detect')
UPSTREAM_REPORT_SERVER = os.environ.get(
'UPSTREAM_REPORT_SERVER', 'https://doorman.printdebug.com/report')
REPORT_UP = os.environ.get('REPORT_UP') == 'True'
RESET_MOTION_TRACKER = int(os.environ.get('RESET_MOTION_TRACKER', 10))
JOB_ID = 'detect_job'
CAPTURE_DIRECTORY = os.path.dirname(os.path.realpath(__file__))
if not os.path.exists(os.path.join(CAPTURE_DIRECTORY, 'capture')):
os.makedirs(os.path.join(CAPTURE_DIRECTORY, 'capture'))
CAPTURE_DIRECTORY = os.path.join(CAPTURE_DIRECTORY, 'capture')
logger.info('REMOTE_DETECT_SERVER (where yolo detection requests go to) is set to %s',
REMOTE_DETECT_SERVER)
SCHEDULER = BackgroundScheduler()
def check_detect(jpg):
detections = requests.post(REMOTE_DETECT_SERVER,
auth=(DETECT_API_CREDENTIALS['user'],
DETECT_API_CREDENTIALS['pass']),
data={'b64image': base64.b64encode(jpg)})
if detections.status_code == 200:
return detections.json()
else:
logger.error("Failed to detect %s because of error %i",
REMOTE_DETECT_SERVER, detections.status_code)
detections.raise_for_status()
def draw_boxes(image, boxes):
image = cv2.rectangle(image,
(boxes['topleft']['x'],
boxes['topleft']['y'],),
(boxes['bottomright']['x'],
boxes['bottomright']['y'],),
(0, 255, 0),
3)
image = cv2.putText(image, boxes['label'],
(boxes['topleft']['x'],
boxes['topleft']['y'],),
cv2.FONT_HERSHEY_SIMPLEX,
1,
(255, 255, 255),
2,
cv2.LINE_AA)
return image
def kill_job():
_dump_message("======= KILLING JOBS =======")
SCHEDULER.remove_all_jobs()
def send_upstream_message(message, status):
post_up = requests.post(UPSTREAM_REPORT_SERVER, json={
'message': message,
'status': status,
'now': datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
'key': os.environ['UPSTREAM_SECRET_KEY']
})
logger.info('Sent message %s', str(message))
if post_up.status_code != 200:
kill_job()
post_up.raise_for_status()
def report_upstream(frame):
try:
jpg = cv2.imencode('.jpg', frame)[1]
detections = check_detect(jpg)
if detections['results']:
send_upstream_message(
message=detections['results'], status='success')
except IOError as e:
logger.exception(e)
send_upstream_message(message=str(e), status='error')
kill_job()
def _start_tracking():
# source https://codereview.stackexchange.com/questions/178121/opencv-motion-detection-and-tracking
# When program is started
# Are we finding motion or tracking
status = 'motion'
# How long have we been tracking
idle_time = 0
# Background for motion detection
background = None
# An MIL tracker for when we find motion
tracker = cv2.TrackerMIL_create()
last_recorded = datetime.now()
# Webcam footage (or video)
video = Camera()
# LOOP
while True:
# Check first frame
frame, _ = video.get_frame()
# Grayscale footage
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Blur footage to prevent artifacts
gray = cv2.GaussianBlur(gray, (21, 21), 0)
# Check for background
if background is None:
# Set background to current frame
background = gray
if status == 'motion':
# Difference between current frame and background
frame_delta = cv2.absdiff(background, gray)
# Create a threshold to exclude minute movements
thresh = cv2.threshold(frame_delta, 25, 255, cv2.THRESH_BINARY)[1]
# Dialate threshold to further reduce error
thresh = cv2.dilate(thresh, None, iterations=2)
# Check for contours in our threshold
_, cnts, hierarchy = cv2.findContours(
thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Check each contour
if len(cnts) != 0:
# If the contour is big enough
# Set largest contour to first contour
largest = 0
# For each contour
for i in range(len(cnts)):
# If this contour is larger than the largest
if i != 0 & int(cv2.contourArea(cnts[i])) > int(cv2.contourArea(cnts[largest])):
# This contour is the largest
largest = i
if cv2.contourArea(cnts[largest]) > 1100:
# Create a bounding box for our contour
(x, y, w, h) = cv2.boundingRect(cnts[0])
# Convert from float to int, and scale up our boudning box
(x, y, w, h) = (int(x), int(y), int(w), int(h))
# Initialize tracker
bbox = (x, y, w, h)
try:
ok = tracker.init(frame, bbox)
# Switch from finding motion to tracking
status = 'tracking'
except cv2.error as e:
logger.exception(e)
# If we are tracking
if status == 'tracking':
# Update our tracker
ok, bbox = tracker.update(frame)
# Create a visible rectangle for our viewing pleasure
if ok:
now = datetime.now()
p1 = (int(bbox[0]), int(bbox[1]))
p2 = (int(bbox[0] + bbox[2]), int(bbox[1] + bbox[3]))
# cv2.rectangle(frame, p1, p2, (0, 0, 255), 1)
if (now - last_recorded).total_seconds() > RESET_MOTION_TRACKER:
logger.info('Motion detected at %s', str(now))
cv2.imwrite(os.path.join(
CAPTURE_DIRECTORY, now.strftime('%Y-%m-%d_%H_%M_%S') + '.jpg'), frame)
last_recorded = now
# If we have been tracking for more than a few seconds
if idle_time >= 2:
# Reset to motion
status = 'motion'
# Reset timer
idle_time = 0
# Reset background, frame, and tracker
background = None
tracker = None
ok = None
# Recreate tracker
tracker = cv2.TrackerMIL_create()
# Incriment timer
idle_time += 1
def start_motion_tracker():
if REPORT_UP and 'SECRET_KEY' in os.environ:
logger.info('Starting motiong tracker')
_start_tracking()
else:
logger.info(
'Not starting motiong tracker, REPORT_UP and SECRET_KEY must be defined')