Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

option to perspective-warp the input image before motion-detection #153

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@
MO_CROP_X_RIGHT = 250 # Default=250
MO_CROP_Y_UPPER = 90 # Default=90
MO_CROP_Y_LOWER = 150 # Default=150
MO_WARP_ON = False # Default False; cv2.warpPerspective using 4+ control points defined below.
# on my 4gb Rpi 4, this costs ~0.17 fps
MO_WARP_INPUT_PTS = [[188,345], [179,411], [489,436], [489,363]]
MO_WARP_OUTPUT_PTS = [[179,363],[179,436], [489,436], [489,363]]

# Plugins override the specified config.py variable settings
# ----------------------------------------------------------
Expand Down
9 changes: 9 additions & 0 deletions speed-cam.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@
"MO_CROP_X_RIGHT": 250,
"MO_CROP_Y_UPPER": 90,
"MO_CROP_Y_LOWER": 150,
"MO_WARP_ON": False,
"MO_WARP_INPUT_PTS": [1,2,3,4],
"MO_WARP_OUTPUT_PTS": [1,2,3,4],
"CAMERA": "pilibcam",
"CAM_LOCATION": "Front Window",
"USBCAM_SRC": 0,
Expand Down Expand Up @@ -1151,6 +1154,9 @@ def get_motion_contours(grayimage1):
image = vs.read() # Read image data from video steam thread instance
# crop image to motion tracking area only
try:
if MO_WARP_ON:
M = cv2.getPerspectiveTransform(np.float32(MO_WARP_INPUT_PTS),np.float32(MO_WARP_OUTPUT_PTS))
image = cv2.warpPerspective(image,M,(image.shape[1], image.shape[0]),flags=cv2.INTER_NEAREST)
image_crop = image[MO_CROP_Y_UPPER:MO_CROP_Y_LOWER, MO_CROP_X_LEFT:MO_CROP_X_RIGHT]
image_ok = True
except (ValueError, TypeError):
Expand Down Expand Up @@ -1302,6 +1308,9 @@ def speed_camera():
# initialize a cropped grayimage1 image
image2 = vs.read() # Get image from VideoSteam thread instance
try:
if MO_WARP_ON:
M = cv2.getPerspectiveTransform(np.float32(MO_WARP_INPUT_PTS), np.float32(MO_WARP_OUTPUT_PTS))
image2 = cv2.warpPerspective(image2,M,(image2.shape[1], image2.shape[0]),flags=cv2.INTER_NEAREST)
# crop image to motion tracking area only
image_crop = image2[MO_CROP_Y_UPPER:MO_CROP_Y_LOWER, MO_CROP_X_LEFT:MO_CROP_X_RIGHT]
except:
Expand Down
23 changes: 23 additions & 0 deletions webserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,29 @@ def list_directory(self, path):
tpath, cur_folder = os.path.split(self.path)
f.write(b"<html><title>%s %s</title>" % (WEB_PAGE_TITLE.encode('utf-8'), self.path.encode('utf-8')))
f.write(b"<body>")
f.write(b"""
<script>

document.onkeydown = checkKey;

function checkKey(e) {

e = e || window.event;

var indexOfCurrentImg = Math.max(0, Array.from(document.querySelectorAll('a[target="imgbox"')).map((a) => a.href).indexOf(document.getElementsByTagName("iframe")[0].contentDocument.URL ))
var nextA = Array.from(document.querySelectorAll('a[target="imgbox"'))[indexOfCurrentImg-1]
var prevA = Array.from(document.querySelectorAll('a[target="imgbox"'))[indexOfCurrentImg+1]

if (e.keyCode == '37') { // left arrow
prevA.click()
}
else if (e.keyCode == '39') { // right arrow
nextA.click()
}

}
</script>
""")
# Start Left iframe Image Panel
f.write(b'<iframe width="%s" height="%s" align="left"'
% (WEB_IFRAME_WIDTH_PERCENT.encode('utf-8'), WEB_IMAGE_HEIGHT.encode('utf-8')))
Expand Down