-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdcm_rect
executable file
·78 lines (61 loc) · 3.27 KB
/
dcm_rect
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
#!/usr/bin/env python3
import cv2
import argparse
import subprocess
parser = argparse.ArgumentParser(description='dahua camera motion detection')
parser.add_argument('-i', '--ip', metavar='<ip>', type=str, help="ip to connect to")
parser.add_argument('-u', '--username', metavar='<user>', type=str, help="account username")
parser.add_argument('-p', '--password', metavar='<pass>', type=str, help="account password")
parser.add_argument('-a', '--area', default=10, metavar='<area>', type=int, help="contour area for detection (drawing rectangle)")
parser.add_argument('-pad', '--padding', default=5, metavar='<px>', type=int, help="rectangle padding")
parser.add_argument('-ww', '--width', default=1920, metavar='<px>', type=int, help="window pixels width")
parser.add_argument('-wh', '--height', default=1080, metavar='<px>', type=int, help="window pixels height")
parser.add_argument('-fs', '--fullscreen', action="store_true", help="start window in fullscreen mode")
parser.add_argument('-d', '--detect', action="store_true", help="detect screen size with xrandr and set width & height")
parser.add_argument('-r', '--resolution', default=0, metavar='<index>', type=int, help="index of resolution to use (default: 0)")
args = parser.parse_args()
if args.fullscreen:
cv2.namedWindow('Motion', cv2.WINDOW_NORMAL)
cv2.setWindowProperty('Motion', cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
if args.detect:
output = subprocess.run("xrandr | grep '*' | awk '{print $1}'", shell=True, capture_output=True, text=True)
resolutions = output.stdout.strip().split("\n")
print(f"detected resolutions: {resolutions}")
resolution = resolutions[args.resolution].split("x")
print(f"using resolution: {resolution}")
args.width, args.height = int(resolution[0]), int(resolution[1])
def motion_detection():
"""Detect motion in the RTSP stream using OpenCV."""
rtsp_url = f"rtsp://{args.username}:{args.password}@{args.ip}:554/cam/realmonitor?channel=0&subtype=0"
cap = cv2.VideoCapture(rtsp_url)
if not cap.isOpened():
print("Error: Could not open RTSP stream.")
return
fgbg = cv2.createBackgroundSubtractorMOG2(history=500, varThreshold=16, detectShadows=True)
while True:
ret, frame = cap.read()
# Crop the frame
crop_width = 704
crop_height = 384
crop_x = 0
crop_y = 0
frame = frame[crop_y:crop_y + crop_height, crop_x:crop_x + crop_width]
if not ret:
print("Error: Failed to grab frame.")
break
fgmask = fgbg.apply(frame)
_, thresh = cv2.threshold(fgmask, 128, 255, cv2.THRESH_BINARY)
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for contour in contours:
if cv2.contourArea(contour) > args.area:
x, y, w, h = cv2.boundingRect(contour)
x, y, w, h = x - args.padding, y - args.padding, w + 2 * args.padding, h + 2 * args.padding
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
frame = cv2.resize(frame, (args.width, args.height)) # resize
cv2.imshow('Motion', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
motion_detection()