-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmotrack2.py
261 lines (239 loc) · 9.7 KB
/
motrack2.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#!/usr/bin/python3
from __future__ import print_function
PROG_VERSION = "1.1"
import logging
# Setup Logging
logging.basicConfig(level=logging.DEBUG,
format="%(asctime)s %(levelname)-8s %(message)s",
datefmt="%Y-%m-%d %H:%M:%S")
logging.info("Loading Python Libraries ...")
import os
import sys
import time
import datetime
import math
import cv2
import subprocess
# import the main strmcam launch module
try:
from strmcam import strmcam
except Exception as err_msg:
print("ERROR: %s" % err_msg)
sys.exit(1)
# list of valid camera sources
CONFIG_FILENAME = "config.py" # Settings variables file to import
if os.path.exists(CONFIG_FILENAME):
# Read Configuration variables from config.py file
try:
logging.info("Import Settings from %s", CONFIG_FILENAME)
from config import *
except ImportError:
logging.error("Problem Importing configuration variables from %s" %
CONFIG_FILENAME)
sys.exit(1)
else:
logging.error("Configuration File Not Found %s" % CONFIG_FILENAME)
sys.exit(1)
PROG_NAME = os.path.basename(__file__)
# ------------------------------------------------------------------------------
def show_settings(filename):
'''
Display program configuration variable settings
read config file and print each decoded line
'''
with open(filename, 'rb') as f:
for line in f:
print(line.decode().strip())
print("")
# ------------------------------------------------------------------------------
def get_image_name(path, prefix):
'''
build image file names by number sequence or
date/time Added tenth of second
'''
rightNow = datetime.datetime.now()
filename = "%s/%s%04d%02d%02d-%02d%02d%02d%d.jpg" % (
path,
prefix,
rightNow.year,
rightNow.month,
rightNow.day,
rightNow.hour,
rightNow.minute,
rightNow.second,
rightNow.microsecond / 100000,
)
return filename
# ------------------------------------------------------------------------------
def timer_end(timer_start, timer_sec):
'''
Check if timelapse timer has expired
Return updated start time status of expired timer True or False
'''
rightNow = datetime.datetime.now()
timeDiff = (rightNow - timer_start).total_seconds()
if timeDiff >= timer_sec:
return True
else:
return False
# ------------------------------------------------------------------------------
def get_motion_track_point(grayimage1, grayimage2):
'''
Process two grayscale images.
check for motion and return center point
of motion for largest contour.
'''
movement_center = ()
# Get differences between the two greyed images
differenceimage = cv2.absdiff(grayimage1, grayimage2)
# Blur difference image to enhance motion vectors
differenceimage = cv2.blur(differenceimage, (BLUR_SIZE, BLUR_SIZE))
# Get threshold of blurred difference image
# based on THRESHOLD_SENSITIVITY variable
retval, thresholdimage = cv2.threshold(
differenceimage, THRESHOLD_SENSITIVITY, 255, cv2.THRESH_BINARY
)
try:
# opencv2 syntax default
contours, hierarchy = cv2.findContours(
thresholdimage, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE
)
except ValueError:
# opencv 3 syntax
thresholdimage, contours, hierarchy = cv2.findContours(
thresholdimage, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE
)
if contours:
c = max(contours, key = cv2.contourArea)
(x, y, w, h) = cv2.boundingRect(c)
if w * h <= TRACK_MIN_AREA:
return None
movement_center = (int(x + w / 2), int(y + h / 2))
return movement_center
# ------------------------------------------------------------------------------
def track_motion_distance(xy1, xy2):
'''
Return the triangulated distance between two tracking locations
'''
x1, y1 = xy1
x2, y2 = xy2
trackLen = int(abs(math.hypot(x2 - x1, y2 - y1)))
return trackLen
# ------------------------------------------------------------------------------
if __name__ == "__main__":
if SHOW_SETTINGS_ON:
show_settings(CONFIG_FILENAME)
if not os.path.exists(IM_DIR): # Check if image directory exists
os.makedirs(IM_DIR) # Create directory if Not Found
logging.info("%s ver %s written by Claude Pageau", PROG_NAME, PROG_VERSION)
vs = strmcam()
logging.info("Wait ...")
time.sleep(3) # Allow Camera to warm up
# initialize first gray image
start_track = True
track_hist = []
image1 = vs.read()
try:
grayimage1 = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)
except cv2.error:
logging.error('Problem Connecting to %s. Review Log Messages and Correct', CAMERA)
sys.exit(1)
im_height, im_width, _ = image1.shape
if TRACK_TRIG_AUTO:
# Auto calculate variables below
TRACK_TRIG_LEN = int(im_width / 8) # auto calc track len.
TRACK_INTERVAL_LEN = int(TRACK_TRIG_LEN / 2.0) # Max allowed px distance from previous track point
logging.info("Auto Calculated TRACK_TRIG_LEN=%i and TRACK_INTERVAL_LEN=%i",
TRACK_TRIG_LEN, TRACK_INTERVAL_LEN)
logging.info("Start Stream %s" % CAMERA)
logging.info("Start Motion Tracking Loop. Ctrl-c Quits ...")
logging.info("--------------------------------------------")
tracking = True
try:
while tracking:
image2 = vs.read()
try:
grayimage2 = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY)
except:
continue
motion_xy = get_motion_track_point(grayimage1, grayimage2)
grayimage1 = grayimage2 # update for next track
if motion_xy and start_track:
track_timer_start = datetime.datetime.now()
mpoint_start = motion_xy
prev_mpoint = motion_xy
max_radius = TRACK_INTERVAL_LEN
if LOGGING_ON:
logging.info("New: (%i, %i) Track Start",
mpoint_start[0], mpoint_start[1])
if TRACK_HIST_ON: # Reset Tracking History
track_hist = [mpoint_start]
start_track = False
elif motion_xy:
mpoint2 = motion_xy
track_length = track_motion_distance(mpoint_start, mpoint2)
if TRACK_HIST_ON:
track_hist.append(mpoint2) # Append current mpoint to track history
if GUI_ON and CIRCLE_ON:
cv2.circle(image2,
mpoint2,
CIRCLE_SIZE,
LINE_COLOR,
LINE_THICKNESS)
if track_length <= max_radius:
max_radius = track_length + TRACK_INTERVAL_LEN
else:
# ignore out of range points and reset start point
mpoint_start = mpoint2
if LOGGING_ON:
logging.info(" Reset Track: (%i, %i) Radius %i Exceeds %i",
mpoint2[0], mpoint2[1], track_length, max_radius)
start_track = True
continue
if track_length > TRACK_TRIG_LEN:
# This was a valid track
if LOGGING_ON:
logging.info(" End Track: (%i, %i) Length %i GT %i TRACK_TRIG_LEN px",
mpoint2[0], mpoint2[1], track_length, TRACK_TRIG_LEN)
filename = get_image_name(IM_DIR, IM_PREFIX)
logging.info(" Saving %s", filename)
# Resize image before saving ..
if CIRCLE_ON: # Put Circle on last motion xy before saving image
if TRACK_HIST_ON:
for point in track_hist:
cv2.circle(image2,
point,
CIRCLE_SIZE,
LINE_COLOR,
LINE_THICKNESS)
else:
cv2.circle(image2,
mpoint2,
CIRCLE_SIZE,
LINE_COLOR,
LINE_THICKNESS)
im_resized = cv2.resize(image2, (int(im_width * IM_BIGGER),
int(im_height * IM_BIGGER)))
cv2.imwrite(filename, im_resized)
time.sleep(TRACK_DELAY_SEC) # Delay before starting another track
start_track = True
else:
if LOGGING_ON:
logging.info(" Track: (%i, %i) Track Len is %i px", mpoint2[0], mpoint2[1], track_length)
if not start_track and timer_end(track_timer_start, TRACK_TIMEOUT_SEC):
if LOGGING_ON:
logging.info(" Track Timeout: GT %i TRACK_TIMEOUT_SEC",
TRACK_TIMEOUT_SEC)
start_track = True
if GUI_ON:
cv2.imshow("MoTrack (q in Window Quits)", image2)
if cv2.waitKey(1) & 0xFF == ord("q"):
cv2.destroyAllWindows()
except KeyboardInterrupt:
print("")
logging.info("User Pressed Keyboard ctrl-c")
logging.info("Exiting %s ver %s", PROG_NAME, PROG_VERSION)
logging.info("Stop Stream Thread: %s", CAMERA)
logging.info("Wait ...")
vs.stop()
sys.exit(0)