{"id":87777,"date":"2023-07-12T10:36:36","date_gmt":"2023-07-12T05:06:36","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=87777"},"modified":"2026-06-03T15:22:21","modified_gmt":"2026-06-03T09:52:21","slug":"python-opencv-motion-detection-detect-track-excel","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/python-opencv-motion-detection-detect-track-excel\/","title":{"rendered":"Python OpenCV Motion Detection &#8211; Detect, Track, Excel"},"content":{"rendered":"<p>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.<\/p>\n<p>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.<\/p>\n<h3>Background<\/h3>\n<p>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.<\/p>\n<h3>How to detect and track the motion ?<\/h3>\n<p>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.<\/p>\n<h3>Prerequisites for Motion Detection Using Python OpenCV<\/h3>\n<p>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.<\/p>\n<p>1. Python 3.7 and above<br \/>\n2. Any python editor (VS code, Pycharm,etc.)<\/p>\n<h3>Download Python OpenCV Motion Detection Project<\/h3>\n<p>Please download the source code of Python OpenCV Motion Detection Project: <a href=\"https:\/\/drive.google.com\/file\/d\/1gq9jqLkEzPVL9hIQErB0ZS_Uix26QKqY\/view?usp=drive_link\"><strong>Python<\/strong> <strong>OpenCV Motion Detection Project Code<\/strong><\/a><\/p>\n<h3>Installation<\/h3>\n<p>Open windows cmd as administrator<\/p>\n<p>1. To install the opencv library run the command from the cmd.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">pip install opencv-python\r\n<\/pre>\n<h3>Let\u2019s Implement It<\/h3>\n<p>To implement it follow the below step.<\/p>\n<p>1. First of all we are importing all the necessary libraries that are required during implementation.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import cv2 \r\n<\/pre>\n<p>2. This code creates a VideoCapture object named &#8220;cap&#8221; that can be used to capture video from the default camera (0) connected to the computer.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">cap = cv2.VideoCapture(0')\r\n<\/pre>\n<p>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&#8217;s not part of the background is marked as &#8220;moving&#8221;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">BG_subtract = cv2.createBackgroundSubtractorMOG2()\r\n<\/pre>\n<p>4. Start While loop.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">while True:\r\n<\/pre>\n<p>5. This reads a frame from the video capture object &#8216;cap&#8217;. It returns two values: &#8216;ret&#8217; which is a boolean indicating if the frame was successfully read or not, and &#8216;frame&#8217;, which is the actual image frame that was read from the video.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">ret, frame = cap.read()\r\n<\/pre>\n<p>6. This code applies the BackgroundSubtractor algorithm, stored in the &#8220;BG_subtract&#8221; object, to the current frame of the video stream stored in the &#8220;frame&#8221; variable.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">mask = BG_subtract.apply(frame)\r\n<\/pre>\n<p>7. This code helps detect the boundaries of the white areas (contours) in the binary mask that represents the moving objects in the video.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">contours, hierarchy = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)\r\n<\/pre>\n<p>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 &#8220;Motion Detected&#8221; in the frame.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">for cont in contours:\r\n        if cv2.contourArea(cont) &lt; 10000:\r\n            continue\r\n        (x, y, w, h) = cv2.boundingRect(cont)\r\n        cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 223, 226), 2)\r\n        cv2.putText(frame, \"Motion Detected\", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, cv2.LINE_AA)\r\n<\/pre>\n<p>9. This code displays the video frame with motion detected and waits for a key press. If the key pressed is &#8216;q&#8217;, the program exits and all windows are closed.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">cv2.imshow(TechVidvan, frame)\r\n    if cv2.waitKey(1) &amp; 0xFF == ord('q'):\r\n        break\r\n<\/pre>\n<p><strong>Note :-<\/strong> you have to include steps 5 to 9 under the while loop.<\/p>\n<p>10. release the video capture and close all windows that were opened using OpenCV in the previous code.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">cap.release()\r\ncv2.destroyAllWindows()\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/07\/motion-detection-output-1.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-88084 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/07\/motion-detection-output-1.webp\" alt=\"motion detection output\" width=\"962\" height=\"710\" \/><\/a><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/07\/opencv-motion-detection-output.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-88085 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/07\/opencv-motion-detection-output.webp\" alt=\"opencv motion detection output\" width=\"1920\" height=\"1080\" \/><\/a><\/p>\n<h3>How to Detect Motion from Video ?<\/h3>\n<p>Make the changes here,<\/p>\n<p>Give the path of video from which you want to detect the motion.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">cap = cv2.VideoCapture(\"Video\/motion.mp4\")\r\n<\/pre>\n<h3>Python OpenCV Motion Detection Output<\/h3>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/07\/opencv-motion-detection-project-output.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-88086 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/07\/opencv-motion-detection-project-output.webp\" alt=\"opencv motion detection project output\" width=\"1920\" height=\"1080\" \/><\/a><\/p>\n<h3>Video Output<\/h3>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/07\/python-opencv-motion-detection-output.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-88082 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/07\/python-opencv-motion-detection-output.webp\" alt=\"python opencv motion detection output\" width=\"800\" height=\"449\" \/><\/a><\/p>\n<h3>Conclusion<\/h3>\n<p>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 moving objects in real-time. This technology has numerous applications and will continue to play a critical role in computer vision and image processing.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":88081,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[193],"tags":[5032,5033,5034,391,191,392,5035,481],"class_list":["post-87777","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-opencv-tutorials","tag-motion-detection-project","tag-opencv-motion-detection","tag-opencv-motion-detection-project","tag-opencv-project","tag-opencv-project-for-practice","tag-opencv-project-ideas","tag-python-opencv-motion-detection-project","tag-python-opencv-project-ideas"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python OpenCV Motion Detection - Detect, Track, Excel - TechVidvan<\/title>\n<meta name=\"description\" content=\"Discover the power of Python OpenCV motion detection. Enhance your projects with precise tracking and analysis. Start exploring now!\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/python-opencv-motion-detection-detect-track-excel\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python OpenCV Motion Detection - Detect, Track, Excel - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Discover the power of Python OpenCV motion detection. Enhance your projects with precise tracking and analysis. Start exploring now!\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/python-opencv-motion-detection-detect-track-excel\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-12T05:06:36+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-06-03T09:52:21+00:00\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python OpenCV Motion Detection - Detect, Track, Excel - TechVidvan","description":"Discover the power of Python OpenCV motion detection. Enhance your projects with precise tracking and analysis. Start exploring now!","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/python-opencv-motion-detection-detect-track-excel\/","og_locale":"en_US","og_type":"article","og_title":"Python OpenCV Motion Detection - Detect, Track, Excel - TechVidvan","og_description":"Discover the power of Python OpenCV motion detection. Enhance your projects with precise tracking and analysis. Start exploring now!","og_url":"https:\/\/techvidvan.com\/tutorials\/python-opencv-motion-detection-detect-track-excel\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2023-07-12T05:06:36+00:00","article_modified_time":"2026-06-03T09:52:21+00:00","author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/python-opencv-motion-detection-detect-track-excel\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-opencv-motion-detection-detect-track-excel\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Python OpenCV Motion Detection &#8211; Detect, Track, Excel","datePublished":"2023-07-12T05:06:36+00:00","dateModified":"2026-06-03T09:52:21+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-opencv-motion-detection-detect-track-excel\/"},"wordCount":727,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-opencv-motion-detection-detect-track-excel\/#primaryimage"},"thumbnailUrl":"","keywords":["motion detection project","opencv motion detection","opencv motion detection project","opencv project","opencv project for practice","opencv project ideas","python opencv motion detection project","python opencv project ideas"],"articleSection":["OpenCV Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/python-opencv-motion-detection-detect-track-excel\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/python-opencv-motion-detection-detect-track-excel\/","url":"https:\/\/techvidvan.com\/tutorials\/python-opencv-motion-detection-detect-track-excel\/","name":"Python OpenCV Motion Detection - Detect, Track, Excel - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-opencv-motion-detection-detect-track-excel\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-opencv-motion-detection-detect-track-excel\/#primaryimage"},"thumbnailUrl":"","datePublished":"2023-07-12T05:06:36+00:00","dateModified":"2026-06-03T09:52:21+00:00","description":"Discover the power of Python OpenCV motion detection. Enhance your projects with precise tracking and analysis. Start exploring now!","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-opencv-motion-detection-detect-track-excel\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/python-opencv-motion-detection-detect-track-excel\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/python-opencv-motion-detection-detect-track-excel\/#primaryimage","url":"","contentUrl":""},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/python-opencv-motion-detection-detect-track-excel\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Python OpenCV Motion Detection &#8211; Detect, Track, Excel"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/87777","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=87777"}],"version-history":[{"count":2,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/87777\/revisions"}],"predecessor-version":[{"id":448066,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/87777\/revisions\/448066"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=87777"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=87777"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=87777"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}