Motion detection is one of the common intelligent video surveillance methods. It can effectively reduce the computational processing capacity of the processor by judging and recognizing moving objects and then taking photo records. It only needs to monitor and record when there is a mobile event trigger. To avoid monitoring recording and shooting a lot of useless photo information. Today, this blog will take you to use the Python language to simply capture images through the USB camera for motion detection, and detect photos with moving objects and save them immediately. The first thing is to set up the environment, which has been explained in the USB image acquisition blog. Just connect our USB camera, mouse and keyboard to the dragonbaord 410c development board. If you are using a wired USB mouse and keyboard, The USB mouse and camera can share one. After setting up the environment, we can write the motion detection program. Similarly, here we directly dry the goods, the specific code is as follows: #!/usr/bin/env python #This code is improved on the basis of the previous code, so we saved the code in front of us. MOTION_THRESHOLD = 0.3 #initialize web camera Vc = cv2.VideoCapture(DEVICE_NUMBER) #If the camera is successfully initialized, the camera data can be successfully read and the loop mode is entered, and the camera image frame is processed cyclically. While retval: #Set stop exit button if cv2.waitKey(1) == 27: The above is the Python code that implements the entire motion detection. The very simple process can realize the motion detection of the image data collected by the camera. If you are interested, you can also modify it slightly. The identification and tracking of moving objects will be further introduced to you in the follow-up blog. After the code is written, save it as monition.py, then run python monition.py to see the following effects. When your camera moves, the system will display motion on the video! Indicates movement. Bluetooth Speakers,Portable Wireless Speaker,Portable Bluetooth Speaker,Waterproof Bluetooth Speaker TOPWAY EM ENTERPRISE LIMITED , https://www.topwayemltd.com
Import cv2, sys
Import numpy as np
# Constants
DEVICE_NUMBER = 0
FONT_FACES = [
cv2.FONT_HERSHEY_SIMPLEX,
cv2.FONT_HERSHEY_PLAIN,
cv2.FONT_HERSHEY_DUPLEX,
cv2.FONT_HERSHEY_COMPLEX,
cv2.FONT_HERSHEY_TRIPLEX,
cv2.FONT_HERSHEY_COMPLEX_SMALL,
cv2.FONT_HERSHEY_SCRIPT_SIMPLEX,
cv2.FONT_HERSHEY_SCRIPT_COMPLEX
]
# Check if the webcam init was successful
If vc.isOpened(): # try to get the first frame
Retval, frame = vc.read()
Previous_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
Else:
Sys.exit(1)
# Define the frame which the webcam will show
Frame_show = frame
# Convert frame to grayscale to make phase comparison
Frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Convert frames into floaTIng point
F_frame = np.float32(frame)
F_previous_frame = np.float32(previous_frame)
#Build background model (dx, dy) = cv2.phaseCorrelate(f_frame,f_previous_frame)
# Determine moTIon from the phase correlaTIon
If abs(dx) > MOTION_THRESHOLD and abs(dy) > MOTION_THRESHOLD:
#Write text information font_typeface = FONT_FACES[5]
Font_scale = 2
Font_color = (0,0,255)
Font_weight = 5
x = 0
y = 50
cv2.putText(frame_show, "Motion!", (x,y), font_typeface, font_scale, font_color, font_weight)
#展示视频 Cv2.imshow("DB410c Workshop #5: Motion Detection", frame_show)
# Retain previous frame for comparison
Previous_frame = frame
# Refresh into the next frame processing retval, frame = vc.read()
# Exit program after waiting for a pressed key
Break