Skip to content

Commit

Permalink
rev 21.01 eliminate double image read and change ave_speed to standar…
Browse files Browse the repository at this point in the history
…d deviation
  • Loading branch information
pageauc committed Aug 5, 2023
1 parent 2c6cac4 commit 8114ab0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
20 changes: 11 additions & 9 deletions speed-cam.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import subprocess
import numpy as np

PROG_VER = "12.00" # current version of this python script
PROG_VER = "12.01" # current version of this python script

'''
speed-cam.py written by Claude Pageau
Expand Down Expand Up @@ -1163,16 +1163,19 @@ def db_open(db_file):


# ------------------------------------------------------------------------------
def speed_get_contours(image, grayimage1):
def speed_get_contours(grayimage1):
"""
Read Camera image and crop and process
with opencv to detect motion contours.
Added timeout in case camera has a problem.
"""
image_ok = False
start_time = time.time()
timeout = 60 # seconds to wait if camera communications is lost.
# Note to self. Look at adding setting to config.py
timeout = 60 # seconds to wait if camera communications is lost eg network stream.
# Note to self. Look at adding setting to config.py
global image_crop
global differenceimage

while not image_ok:
image = vs.read() # Read image data from video steam thread instance
# crop image to motion tracking area only
Expand All @@ -1195,7 +1198,6 @@ def speed_get_contours(image, grayimage1):
# Convert to gray scale, which is easier
grayimage2 = cv2.cvtColor(image_crop, cv2.COLOR_BGR2GRAY)
# Get differences between the two greyed images
global differenceimage
differenceimage = cv2.absdiff(grayimage1, grayimage2)
# Blur difference image to enhance motion vectors
differenceimage = cv2.blur(differenceimage, (BLUR_SIZE, BLUR_SIZE))
Expand All @@ -1216,7 +1218,7 @@ def speed_get_contours(image, grayimage1):
)
# Update grayimage1 to grayimage2 ready for next image2
grayimage1 = grayimage2
return grayimage1, contours
return image, grayimage1, contours


# ------------------------------------------------------------------------------
Expand Down Expand Up @@ -1324,8 +1326,7 @@ def speed_camera():
logging.info("Begin Motion Tracking .....")
print(HORIZ_LINE)
while still_scanning: # process camera thread images and calculate speed
image2 = vs.read() # Read image data from video steam thread instance
grayimage1, contours = speed_get_contours(image2, grayimage1)
image2, grayimage1, contours = speed_get_contours(grayimage1)
# if contours found, find the one with biggest area
if contours:
total_contours = len(contours)
Expand Down Expand Up @@ -1432,7 +1433,8 @@ def speed_camera():
continue
track_count += 1 # increment track counter
speed_list.append(cur_ave_speed)
ave_speed = np.mean(speed_list)
ave_speed = np.std(speed_list) # chsnged from mesn to standard deviation

prev_start_time = cur_track_time
event_timer = time.time()
if track_count >= track_counter:
Expand Down
2 changes: 1 addition & 1 deletion webserver.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# This script will run the webserver.py as a background task
# You will then be able close the terminal session. To auto start
# Add the following command to the /etc/rc.local
# /home/pi/pi-timolo/webserver.sh start
# /home/pi/speed-camera/webserver.sh start
# To implement webserver3.py copy it to webserver.py. Use Buster or later

progpath="/home/pi/speed-camera"
Expand Down

0 comments on commit 8114ab0

Please sign in to comment.