Sudhanshi
Uploaded by
6 SLIDES
83 VIEWS
60LIKES

Python OpenCV Motion Detection – Detect, Track, Excel

DESCRIPTION

https://techvidvan.com/tutorials/python-opencv-motion-detection-detect-track-excel/

Download Presentation

Python OpenCV Motion Detection – Detect, Track, Excel

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Python OpenCV Motion Detection – Detect, Track, Excel Motion detection is a process of identifying changes in a video stream over time, which is used in various applications such as surveillance and traffic monitoring. OpenCV is a free library for image and video processing that offers different tools for motion detection, such as background subtraction and contour detection. In simple terms, motion detection using OpenCV involves analyzing video frames for any movement and using the results for specific tasks, like tracking objects or detecting unusual events. Background Background subtraction is a technique used in computer vision to identify moving objects in a video stream. It works by comparing each frame in the stream to a reference image, and then subtracting the background to create a mask that highlights the moving objects. The mask is then cleaned up and analyzed to identify the objects and track their movements. OpenCV provides several models for background subtraction, with the MOG2 model being a popular choice due to its ability to handle changes in lighting and scene dynamics. How to detect and track the motion ?

  2. To detect motion in a video using OpenCV, we first create a model of the background without any motion. Then, we find the moving objects in the video using a function called cv2.findContours. Once we have detected the objects, we can track their movement over time using a variety of algorithms provided by OpenCV. Finally, we can visualize the objects by drawing a rectangle around them. This process can be useful for applications such as surveillance, activity recognition, and gesture recognition. Prerequisites for Motion Detection Using Python OpenCV It is important to have a solid understanding of the Python programming language and the OpenCV library. Apart from this you should have the following system requirements. 1. Python 3.7 and above 2. Any python editor (VS code, Pycharm,etc.) Download Python OpenCV Motion Detection Project Please download the source code of Python OpenCV Motion Detection Project: Python OpenCV Motion Detection Project Code Installation Open windows cmd as administrator

  3. 1. To install the opencv library run the command from the cmd. pip install opencv-python Let’s Implement It To implement it follow the below step. 1. First of all we are importing all the necessary libraries that are required during implementation. import cv2 2. This code creates a VideoCapture object named “cap” that can be used to capture video from the default camera (0) connected to the computer. cap = cv2.VideoCapture(0') 3. This code creates a program that can recognize things that move in a video. This code subtracts the background to detect motion. The program compares each frame of the video to a picture of what the background should look like. Anything that’s not part of the background is marked as “moving” BG_subtract = cv2.createBackgroundSubtractorMOG2()

  4. 4. Start While loop. while True: 5. This reads a frame from the video capture object ‘cap’. It returns two values: ‘ret’ which is a boolean indicating if the frame was successfully read or not, and ‘frame’, which is the actual image frame that was read from the video. ret, frame = cap.read() 6. This code applies the BackgroundSubtractor algorithm, stored in the “BG_subtract” object, to the current frame of the video stream stored in the “frame” variable. mask = BG_subtract.apply(frame) 7. This code helps detect the boundaries of the white areas (contours) in the binary mask that represents the moving objects in the video. contours, hierarchy = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

  5. 8. This code checks for contours that exceed a certain size in a binary image generated by a motion detection algorithm. It then draws a rectangle around the detected object, and adds the text “Motion Detected” in the frame. for cont in contours: if cv2.contourArea(cont) < 10000: continue (x, y, w, h) = cv2.boundingRect(cont) cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 223, 226), 2) cv2.putText(frame, "Motion Detected", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, cv2.LINE_AA) 9. This code displays the video frame with motion detected and waits for a key press. If the key pressed is ‘q’, the program exits and all windows are closed. cv2.imshow(TechVidvan, frame) if cv2.waitKey(1) & 0xFF == ord('q'): break Note :- you have to include steps 5 to 9 under the while loop. 10. release the video capture and close all windows that were opened using OpenCV in the previous code. cap.release() cv2.destroyAllWindows() Conclusion In summary, motion detection is an important technique in computer vision that helps identify moving objects in a video stream. OpenCV provides tools and algorithms to develop a simple motion detection system that can track

  6. moving objects in real-time. This technology has numerous applications and will continue to play a critical role in computer vision and image processing.

More Related
SlideServe
Audio
Live Player
Audio Wave
Play slide audio to activate visualizer