{"id":79062,"date":"2024-03-21T23:03:53","date_gmt":"2024-03-22T03:03:53","guid":{"rendered":"https:\/\/www.engineersgarage.com\/?p=79062"},"modified":"2024-03-22T14:01:07","modified_gmt":"2024-03-22T18:01:07","slug":"opencv4-5-raspberry-pi-image-video-access-recording","status":"publish","type":"post","link":"https:\/\/www.engineersgarage.com\/opencv4-5-raspberry-pi-image-video-access-recording\/","title":{"rendered":"How to use the Raspberry Pi camera with OpenCV"},"content":{"rendered":"<p><span data-preserver-spaces=\"true\">OpenCV is a popular cross-platform framework for image processing and real-time computer vision. The software framework is useful for efficiently developing real-time computer vision applications that would be extremely time-consuming to create from scratch.\u00a0<\/span><\/p>\n<p><span data-preserver-spaces=\"true\">The power of OpenCV\u2019s framework is that it runs seamlessly on embedded platforms like microcomputers. This means developing and running embedded computer vision applications in real-time can be done with little hassle. We covered how to install OpenCV 4.5 and QT5 on Raspberry Pi in <a href=\"https:\/\/www.engineersgarage.com\/raspberry-pi-camera-python\/\">this article<\/a>.<\/span><\/p>\n<p><span data-preserver-spaces=\"true\">Once you\u2019ve installed OpenCV, it\u2019s time to capture any desired images and\/or videos. The system can then process them for useful insights and applications. What\u2019s nice about using Raspberry Pi is the images and videos can be captured by a Raspberry Pi camera \u2014 which easily installs on a webcam or CSI port.\u00a0<\/span><\/p>\n<p><span data-preserver-spaces=\"true\">In this project, we\u2019ll use a Raspberry Pi camera and record the captured images and videos on a Micro SD card (which hosts Raspberry Pi OS). Let&#8217;s get started.\u00a0<\/span><\/p>\n<p><strong><span data-preserver-spaces=\"true\">Components required\u00a0<\/span><\/strong><\/p>\n<p><span data-preserver-spaces=\"true\"><strong>1.<\/strong> Raspberry Pi 4B\/3B x1<br \/>\n<\/span><span data-preserver-spaces=\"true\"><strong>2.<\/strong> Raspberry Pi camera x1<br \/>\n<\/span><span data-preserver-spaces=\"true\"><strong>3.<\/strong> Power adaptor for Raspberry Pi x1<br \/>\n<\/span><span data-preserver-spaces=\"true\"><strong>4.<\/strong> HDMI-To-Micro-HDMI cable x1<br \/>\n<\/span><span data-preserver-spaces=\"true\"><strong>5.<\/strong> Display monitor x1<br \/>\n<\/span><span data-preserver-spaces=\"true\"><strong>6.<\/strong> Keyboard and mouse x1<br \/>\n<\/span><span data-preserver-spaces=\"true\"><strong>7.<\/strong> Raspberry Pi case with camera case x1 (optional)<\/span><\/p>\n<p><strong><span data-preserver-spaces=\"true\">The camera module<br \/>\n<\/span><\/strong><span data-preserver-spaces=\"true\">To begin, attach the camera module to Raspberry Pi\u2019s CSI port. Raspberry Pi must be installed with an operating system like Raspbian or Debian Linux \u2014 and OpenCV must be installed on the Raspberry Pi OS. Review the\u00a0<\/span><span data-preserver-spaces=\"true\">installation process of OpenCV 4.5 on Raspbian Bullseye <\/span><a class=\"editor-rtfLink\" href=\"https:\/\/www.engineersgarage.com\/opencv4-5-installation-raspberry-pi-raspbian-bullseye\/\" target=\"_blank\" rel=\"noopener\">here<\/a><span data-preserver-spaces=\"true\">.\u00a0<\/span><span data-preserver-spaces=\"true\">For tips on attaching the camera module to RPi\u2019s CSI port, <\/span><a class=\"editor-rtfLink\" href=\"https:\/\/www.engineersgarage.com\/raspberry-pi-camera-python\/\" target=\"_blank\" rel=\"noopener\"><span data-preserver-spaces=\"true\">click here<\/span><\/a><span data-preserver-spaces=\"true\">.\u00a0 \u00a0<\/span><\/p>\n<p><span data-preserver-spaces=\"true\">Once the camera module is set, enable the camera module in Raspberry Pi OS. Open the terminal and execute the following command.<\/span><\/p>\n<p><span style=\"color: #800000;\"><strong>$ sudo raspi-config\u00a0<\/strong><\/span><\/p>\n<p><span data-preserver-spaces=\"true\">In the configuration window, select &#8216;Interface Options,&#8217; and then &#8216;Legacy Camera.&#8217; Select &#8216;yes&#8217; to enable the camera module and reboot the system.\u00a0<\/span><\/p>\n<p><a href=\"https:\/\/www.engineersgarage.com\/wp-content\/uploads\/2023\/07\/CV02-01.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-79063 size-full\" src=\"https:\/\/www.engineersgarage.com\/wp-content\/uploads\/2023\/07\/CV02-01.png\" alt=\"\" width=\"653\" height=\"440\" srcset=\"https:\/\/www.engineersgarage.com\/wp-content\/uploads\/2023\/07\/CV02-01.png 653w, https:\/\/www.engineersgarage.com\/wp-content\/uploads\/2023\/07\/CV02-01-300x202.png 300w, https:\/\/www.engineersgarage.com\/wp-content\/uploads\/2023\/07\/CV02-01-353x238.png 353w\" sizes=\"auto, (max-width: 653px) 100vw, 653px\" \/><\/a><\/p>\n<p><a href=\"https:\/\/www.engineersgarage.com\/wp-content\/uploads\/2023\/07\/CV02-02.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-79064 size-full\" src=\"https:\/\/www.engineersgarage.com\/wp-content\/uploads\/2023\/07\/CV02-02.png\" alt=\"\" width=\"655\" height=\"444\" srcset=\"https:\/\/www.engineersgarage.com\/wp-content\/uploads\/2023\/07\/CV02-02.png 655w, https:\/\/www.engineersgarage.com\/wp-content\/uploads\/2023\/07\/CV02-02-300x203.png 300w, https:\/\/www.engineersgarage.com\/wp-content\/uploads\/2023\/07\/CV02-02-351x238.png 351w\" sizes=\"auto, (max-width: 655px) 100vw, 655px\" \/><\/a><strong>Test the camera<br \/>\n<\/strong>It\u2019s important to test the camera module to ensure it works before starting the OpenCV code. To do so, open the terminal and execute the below command.<\/p>\n<p><strong><span style=\"color: #800000;\">$ raspistill -o test.jpg<\/span><\/strong><\/p>\n<p><a href=\"https:\/\/www.engineersgarage.com\/wp-content\/uploads\/2023\/07\/CV02-03.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-79065 size-full\" src=\"https:\/\/www.engineersgarage.com\/wp-content\/uploads\/2023\/07\/CV02-03.png\" alt=\"\" width=\"654\" height=\"124\" srcset=\"https:\/\/www.engineersgarage.com\/wp-content\/uploads\/2023\/07\/CV02-03.png 654w, https:\/\/www.engineersgarage.com\/wp-content\/uploads\/2023\/07\/CV02-03-300x57.png 300w, https:\/\/www.engineersgarage.com\/wp-content\/uploads\/2023\/07\/CV02-03-368x70.png 368w\" sizes=\"auto, (max-width: 654px) 100vw, 654px\" \/><\/a><\/p>\n<p><span data-preserver-spaces=\"true\">You can tell the camera module is working if an image is clicked and saved on Raspbian. If there\u2019s no image, either the camera module is faulty or it\u2019s not installed correctly on the CSI port.\u00a0<\/span><\/p>\n<p><strong><span data-preserver-spaces=\"true\">Installing Picamera<br \/>\n<\/span><\/strong><span data-preserver-spaces=\"true\">Next, install the Picamera library for use with the RPi camera. You\u2019ll install the package in the virtual environment we created when installing OpenCV 4.5 on Raspberry Pi.\u00a0<\/span><\/p>\n<p><span data-preserver-spaces=\"true\">Open the terminal and activate the virtual environment by executing this command:\u00a0<\/span><\/p>\n<p><span style=\"color: #800000;\"><strong>$ workon cv<\/strong><\/span><\/p>\n<p><span data-preserver-spaces=\"true\">Note that &#8216;cv&#8217; is the name of the virtual environment created in the previous tutorial. You can install the Picamera package within the virtual environment with the following:<\/span><\/p>\n<p><span style=\"color: #800000;\"><strong>$ pip install &#8220;picamera[array]&#8221;\u00a0<\/strong><\/span><\/p>\n<p><a href=\"https:\/\/www.engineersgarage.com\/wp-content\/uploads\/2023\/07\/CV02-04.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-79067 size-full\" src=\"https:\/\/www.engineersgarage.com\/wp-content\/uploads\/2023\/07\/CV02-04.png\" alt=\"\" width=\"657\" height=\"314\" srcset=\"https:\/\/www.engineersgarage.com\/wp-content\/uploads\/2023\/07\/CV02-04.png 657w, https:\/\/www.engineersgarage.com\/wp-content\/uploads\/2023\/07\/CV02-04-300x143.png 300w, https:\/\/www.engineersgarage.com\/wp-content\/uploads\/2023\/07\/CV02-04-368x176.png 368w\" sizes=\"auto, (max-width: 657px) 100vw, 657px\" \/><\/a><\/p>\n<p><span data-preserver-spaces=\"true\">Picamera is necessary to work with the camera module. The OpenCV array sub-module is required for the images. This is because OpenCV represents images as NumPy arrays. The array sub-module lets us obtain the NumPy arrays from the camera module.\u00a0<\/span><\/p>\n<p><strong><span data-preserver-spaces=\"true\">The images<br \/>\n<\/span><\/strong><span data-preserver-spaces=\"true\">There are two ways to record and access images using the RPi camera via OpenCV. One way is to capture an image using the Picamera module and then use OpenCV to read and display the image. The other way is to directly write an image as a Numpy array, and then display it.\u00a0<\/span><\/p>\n<p><span data-preserver-spaces=\"true\">First, import the required modules:\u00a0<\/span><\/p>\n<p><span style=\"color: #800000;\"><strong>from picamera import PiCamera<br \/>\nimport time<br \/>\nimport cv2\u00a0<\/strong><\/span><\/p>\n<p><span data-preserver-spaces=\"true\">To write an image as an OpenCV array, the \u2018picamera.array\u2019 module must be imported in the Python script as follows.\u00a0<\/span><\/p>\n<p><span style=\"color: #800000;\"><strong>from picamera.array import PiRGBArray<\/strong><\/span><\/p>\n<p><span data-preserver-spaces=\"true\">Whether capturing an image using Picamera or the picamera.array, it\u2019s necessary to instantiate a Picamera object.\u00a0<\/span><\/p>\n<p><span style=\"color: #800000;\"><strong>camera = PiCamera()<\/strong><\/span><\/p>\n<p><span data-preserver-spaces=\"true\">To capture an image using the Picamera module, use this code:\u00a0<\/span><\/p>\n<p><span style=\"color: #800000;\"><strong>time.sleep(0.1)<br \/>\ncamera.capture(&#8216;test.jpg&#8217;)<\/strong><\/span><\/p>\n<p><span data-preserver-spaces=\"true\">A delay of 100 milliseconds is provided, allowing the camera module to warm up. The camera.capture() method takes a shot and saves it on a MicroSD card by a filename that\u2019s specified as a parameter.\u00a0<\/span><\/p>\n<p><span data-preserver-spaces=\"true\">To capture an image using the picamera.array module, use this code:\u00a0<\/span><\/p>\n<p><span style=\"color: #800000;\"><strong>rawImage = PiRGBArray(camera)<br \/>\ntime.sleep(0.1)<br \/>\ncamera.capture(rawImage, format = &#8220;rgb&#8221;)<br \/>\nimage = rawImage.array<\/strong><\/span><\/p>\n<p><span data-preserver-spaces=\"true\">An object of the PiRGBArray class is instantiated to store an image array. The time delay is provided for the camera module to warm up. The image is captured as a NumPy array using the camera.capture() method. The captured image array is stored in a variable &#8216;image.\u2019\u00a0<\/span><\/p>\n<p><span data-preserver-spaces=\"true\">OpenCV\u2019s imread() function is used to read an image. The function has the following prototype.\u00a0<\/span><\/p>\n<p><strong><span style=\"color: #800000;\" data-preserver-spaces=\"true\">imread(filename, flags)<\/span><\/strong><\/p>\n<p><span data-preserver-spaces=\"true\">The imread() function takes two arguments. The first argument is a fully qualified path to the image file. The second argument is an optional flag specifying how the image is represented. It can take any one of the following values.\u00a0<\/span><\/p>\n<ul>\n<li><span data-preserver-spaces=\"true\">cv2.IMREAD_UNCHANGED or -1: representing an image as it is<\/span><\/li>\n<li>cv2.IMREAD_GRAYSCALE or 0: representing a grayscale image<\/li>\n<li>cv2.IMREAD_COLOR or 1: representing a color image.<\/li>\n<\/ul>\n<p><span data-preserver-spaces=\"true\">This code reads the test.jpeg as it is.\u00a0<\/span><\/p>\n<p><span style=\"color: #800000;\"><strong>image = cv2.imread(&#8216;test.jpg&#8217;, -1)<\/strong><\/span><\/p>\n<p><span data-preserver-spaces=\"true\">To display an image in the OpenCV window, the imshow() function is used, which has this prototype:<\/span><\/p>\n<p><span style=\"color: #800000;\"><strong>imshow(window_name, image)<\/strong><\/span><\/p>\n<p><span data-preserver-spaces=\"true\">The function takes two arguments. The first is the name of the OpenCV window that displays the image. The window can be defined and configured before calling in the imshow() function as follows.\u00a0<\/span><\/p>\n<p><span style=\"color: #800000;\"><strong>cv2.namedWindow(&#8216;Image&#8217;, cv2.WINDOW_NORMAL)<br \/>\n<\/strong><\/span><span style=\"color: #800000;\"><strong>cv2.resizeWindow(&#8216;Image&#8217;, 640, 480)<\/strong><\/span><\/p>\n<p><span data-preserver-spaces=\"true\">The window can be directly instantiated from the imshow() function itself even if it not prior defined. However, the function does not configure the window.\u00a0\u00a0<\/span><\/p>\n<p><span data-preserver-spaces=\"true\">The second argument in the imshow() function is the image object, which is either a captured image file or an image array that\u2019s to be displayed. The code below displays the image object captured through Picamera or the picamera.array in an OpenCV window named &#8216;image.&#8217;\u00a0<\/span><\/p>\n<p><span style=\"color: #800000;\"><strong>cv2.imshow(&#8216;Image&#8217;, image)<br \/>\n<\/strong><\/span><span style=\"color: #800000;\"><strong>cv2.waitKey(0)<\/strong><\/span><\/p>\n<p><span data-preserver-spaces=\"true\">The imshow() function is used with the waitKey() and destroyAllWindows() \/ destroyWindow() functions. The waitKey() function is a keyboard-binding function that takes a single argument, indicating the time image object is displayed in an OpenCV window in milliseconds.\u00a0<\/span><\/p>\n<p><span data-preserver-spaces=\"true\">The code continues if the user presses any key before the time specified. If \u20180\u2019 is passed as an argument, the program waits indefinitely for a keystroke.\u00a0<\/span><\/p>\n<p><span data-preserver-spaces=\"true\">The waitkey() function can also detect specific keystrokes to manipulate an image or close the OpenCV window. The destroyAllWindows() function closes\u00a0<\/span><em><span data-preserver-spaces=\"true\">all<\/span><\/em><span data-preserver-spaces=\"true\">\u00a0open OpenCV windows and releases its resources. The destroyWindow() function only closes the current OpenCV window and releases its resources.\u00a0<\/span><\/p>\n<p><span data-preserver-spaces=\"true\">To write an image, use OpenCV\u2019s imwrite() function. The function must be to record an image object if it\u2019s captured through the picamera.array. It has the following prototype.<\/span><\/p>\n<p><span style=\"color: #800000;\"><strong>imwrite(filename, image)<\/strong><\/span><\/p>\n<p><span data-preserver-spaces=\"true\">The function takes two arguments. The first is the filename, including its extension (like jpg, jpeg, png, etc.), that saves the image object. The second argument is the image object to be saved. The following code saves an image object &#8216;image&#8217; as &#8216;test.jpeg&#8217;. The image object is captured through the picamera.array.\u00a0<\/span><\/p>\n<p><span style=\"color: #800000;\"><strong>cv2.imwrite(&#8220;test.jpeg&#8221;, image)<\/strong><\/span><\/p>\n<p><strong><span data-preserver-spaces=\"true\">The Python scripts<br \/>\n<\/span><\/strong><span data-preserver-spaces=\"true\">The following Python script captures an image using Picamera. The image is instantly saved as a &#8216;test.jpeg&#8217; when captured using this module. The OpenCV functions are then used to read and display the stored image in an OpenCV window.\u00a0<\/span><\/p>\n<p><script src=\"https:\/\/gist.github.com\/Ajak58a\/54cc838387277d7a9fbf7e3d91e02fc6.js\"><\/script><\/p>\n<p><span data-preserver-spaces=\"true\">The following Python script captures an image using the picamera.array. The script first captures a still image as an image array object. The captured image is displayed in an OpenCV window. Then, it\u2019s saved on MicroSD card as a &#8216;test.jpeg&#8217; using the imwrite() function in OpenCV.\u00a0<\/span><\/p>\n<p><script src=\"https:\/\/gist.github.com\/Ajak58a\/e8c28f246abb390b83e312a7fb6fc8bf.js\"><\/script><\/p>\n<div id=\"attachment_79068\" style=\"width: 750px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.engineersgarage.com\/wp-content\/uploads\/2023\/07\/CV02-05.jpeg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-79068\" class=\"wp-image-79068 size-large\" src=\"https:\/\/www.engineersgarage.com\/wp-content\/uploads\/2023\/07\/CV02-05-1024x576.jpeg\" alt=\"\" width=\"740\" height=\"416\" srcset=\"https:\/\/www.engineersgarage.com\/wp-content\/uploads\/2023\/07\/CV02-05-1024x576.jpeg 1024w, https:\/\/www.engineersgarage.com\/wp-content\/uploads\/2023\/07\/CV02-05-300x169.jpeg 300w, https:\/\/www.engineersgarage.com\/wp-content\/uploads\/2023\/07\/CV02-05-768x432.jpeg 768w, https:\/\/www.engineersgarage.com\/wp-content\/uploads\/2023\/07\/CV02-05-368x207.jpeg 368w, https:\/\/www.engineersgarage.com\/wp-content\/uploads\/2023\/07\/CV02-05.jpeg 1280w\" sizes=\"auto, (max-width: 740px) 100vw, 740px\" \/><\/a><p id=\"caption-attachment-79068\" class=\"wp-caption-text\">An image the author&#8217;s son captured using OpenCV on the Raspberry Pi camera module.<\/p><\/div>\n<p><strong>Accessing videos<br \/>\n<\/strong>Typically, the cv.VideoCapture() function is used in OpenCV to capture a video. This function creates a video capture object that can be displayed or streamed as video. When using Raspberry Pi\u2019s camera module, it\u2019s more effective to capture a continuous image sequence as image array objects to access a video.<\/p>\n<p>Each captured image serves as a frame of the video. The number of frames captured in a second depends upon the frame rate set for the Picamera object. The frames are displayed in an OpenCV window in real time. Continuously and consecutively playing the captured frames simulates an actual video. The same frames can also be stored as a video file.<\/p>\n<p>To capture image arrays as frames of a video, use the picamera.capture_continuous() function. The Python script that follows captures the images and displays them in an OpenCV window as a video. The images are taken using the picamera.array module.<\/p>\n<p><script src=\"https:\/\/gist.github.com\/Ajak58a\/2680ce236e426e4c4711d4deb93d4222.js\"><\/script><\/p>\n<p><strong>Recording videos<br \/>\n<\/strong>To record a video using the RPi camera module, use the cv.VideoWriter() function. It saves the output video to a specified directory. The function has the following prototype:<\/p>\n<p><span style=\"color: #800000;\"><strong>VideoWriter(filename, apiPreference, fourcc, fps, frameSize[, isColor])<\/strong><\/span><\/p>\n<p>This function has these arguments:<\/p>\n<ul>\n<li><strong>filename:<\/strong> pathname for the output video file<\/li>\n<li><strong style=\"font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen-Sans, Ubuntu, Cantarell, 'Helvetica Neue', sans-serif;\">apiPreference: <\/strong><span style=\"font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen-Sans, Ubuntu, Cantarell, 'Helvetica Neue', sans-serif;\">API backends identifier<\/span><\/li>\n<li><strong style=\"font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen-Sans, Ubuntu, Cantarell, 'Helvetica Neue', sans-serif;\">fourcc:<\/strong><span style=\"font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen-Sans, Ubuntu, Cantarell, 'Helvetica Neue', sans-serif;\"> a 4-character code of codec used to compress the frames (fourcc)<\/span><\/li>\n<li><strong style=\"font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen-Sans, Ubuntu, Cantarell, 'Helvetica Neue', sans-serif;\">fps:<\/strong><span style=\"font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen-Sans, Ubuntu, Cantarell, 'Helvetica Neue', sans-serif;\"> frame rate of the created video stream<\/span><\/li>\n<li><strong style=\"font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen-Sans, Ubuntu, Cantarell, 'Helvetica Neue', sans-serif;\">frame_size:<\/strong><span style=\"font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen-Sans, Ubuntu, Cantarell, 'Helvetica Neue', sans-serif;\"> Ssize of the video frames<\/span><\/li>\n<li><strong style=\"font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen-Sans, Ubuntu, Cantarell, 'Helvetica Neue', sans-serif;\">isColor:<\/strong><span style=\"font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen-Sans, Ubuntu, Cantarell, 'Helvetica Neue', sans-serif;\"> if not zero, the encoder will expect and encode the color frames; otherwise, it will work with grayscale frames. This flag is currently supported on Windows only.<\/span><\/li>\n<\/ul>\n<p>To save the output video in the AVI format, use the following code.<\/p>\n<p><strong><span style=\"color: #800000;\">cv2.VideoWriter_fourcc(&#8216;M&#8217;,&#8217;J&#8217;,&#8217;P&#8217;,&#8217;G&#8217;)<\/span><\/strong><\/p>\n<p>To save the output video in MP4 format, use the following code.<\/p>\n<p><span style=\"color: #800000;\"><strong>cv2.VideoWriter_fourcc(*&#8217;XVID&#8217;)<\/strong><\/span><\/p>\n<p>After the video is recorded, the cv.VideoWriter object must be released by calling the video_writer.release() function. The Python script that follows records a video by capturing continuous images as image array objects that are accessed in an OpenCV window.<\/p>\n<p><script src=\"https:\/\/gist.github.com\/Ajak58a\/5ee312e75347c1b516b1d9f34116b721.js\"><\/script><\/p>\n<p>This is a video of my son recorded using OpenCV with the Raspberry Pi camera module.<\/p>\n<p><span style=\"color: #3366ff;\"><strong>[Link to d<a href=\"https:\/\/www.engineersgarage.com\/wp-content\/uploads\/2023\/07\/CV02-DV.avi\">CV02-DV<\/a>emonstration video CV02-DV]<\/strong><\/span><\/p>\n<p><strong>Conclusion<br \/>\n<\/strong>OpenCV is an excellent framework for editing and processing images and videos for real-time computer vision applications. The framework supports embedded vision projects as it runs seamlessly over various microcomputers, including Raspberry Pi.<\/p>\n<p>After installing OpenCV, it\u2019s easy to access images and videos through the available peripherals. Images and videos can be captured and recorded using a Raspberry Pi camera module attached to CSI port or a USB webcam. In this project, we used the RPi camera module to access and record images and videos.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>OpenCV is a popular cross-platform framework for image processing and real-time computer vision. The software framework is useful for efficiently developing real-time computer vision applications that would be extremely time-consuming to create from scratch.\u00a0 The power of OpenCV\u2019s framework is that it runs seamlessly on embedded platforms like microcomputers. This means developing and running embedded&hellip;<\/p>\n","protected":false},"author":387,"featured_media":79069,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","footnotes":""},"categories":[3993],"tags":[236,3504,4120,1441,1724,4121,4119],"class_list":{"2":"type-post","15":"entry","16":"has-post-thumbnail"},"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v25.2 (Yoast SEO v25.2) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to use the Raspberry Pi camera with OpenCV<\/title>\n<meta name=\"description\" content=\"How to capture and record images and videos from a Raspberry Pi camera using OpenCV.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.engineersgarage.com\/opencv4-5-raspberry-pi-image-video-access-recording\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to use the Raspberry Pi camera with OpenCV\" \/>\n<meta property=\"og:description\" content=\"How to capture and record images and videos from a Raspberry Pi camera using OpenCV.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.engineersgarage.com\/opencv4-5-raspberry-pi-image-video-access-recording\/\" \/>\n<meta property=\"og:site_name\" content=\"Engineers Garage\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/engineersgarage\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.engineersgarage.com\/wp-content\/uploads\/2023\/07\/RPi-CV02-05-rotator.png\" \/>\n\t<meta property=\"og:image:width\" content=\"810\" \/>\n\t<meta property=\"og:image:height\" content=\"500\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Nikhil Agnihotri\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@EngineersGarage\" \/>\n<meta name=\"twitter:site\" content=\"@EngineersGarage\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Nikhil Agnihotri\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.engineersgarage.com\/opencv4-5-raspberry-pi-image-video-access-recording\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.engineersgarage.com\/opencv4-5-raspberry-pi-image-video-access-recording\/\"},\"author\":{\"name\":\"Nikhil Agnihotri\",\"@id\":\"https:\/\/www.engineersgarage.com\/#\/schema\/person\/3c71105e3b40a1fd12c79c15602914b6\"},\"headline\":\"How to use the Raspberry Pi camera with OpenCV\",\"datePublished\":\"2024-03-22T03:03:53+00:00\",\"dateModified\":\"2024-03-22T18:01:07+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.engineersgarage.com\/opencv4-5-raspberry-pi-image-video-access-recording\/\"},\"wordCount\":1759,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.engineersgarage.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.engineersgarage.com\/opencv4-5-raspberry-pi-image-video-access-recording\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.engineersgarage.com\/wp-content\/uploads\/2023\/07\/RPi-CV02-05-rotator.png\",\"keywords\":[\"camera\",\"images\",\"microsd\",\"opencv\",\"raspberrypi\",\"raspberrypicamera\",\"videos\"],\"articleSection\":[\"Raspberry pi\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.engineersgarage.com\/opencv4-5-raspberry-pi-image-video-access-recording\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.engineersgarage.com\/opencv4-5-raspberry-pi-image-video-access-recording\/\",\"url\":\"https:\/\/www.engineersgarage.com\/opencv4-5-raspberry-pi-image-video-access-recording\/\",\"name\":\"How to use the Raspberry Pi camera with OpenCV\",\"isPartOf\":{\"@id\":\"https:\/\/www.engineersgarage.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.engineersgarage.com\/opencv4-5-raspberry-pi-image-video-access-recording\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.engineersgarage.com\/opencv4-5-raspberry-pi-image-video-access-recording\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.engineersgarage.com\/wp-content\/uploads\/2023\/07\/RPi-CV02-05-rotator.png\",\"datePublished\":\"2024-03-22T03:03:53+00:00\",\"dateModified\":\"2024-03-22T18:01:07+00:00\",\"description\":\"How to capture and record images and videos from a Raspberry Pi camera using OpenCV.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.engineersgarage.com\/opencv4-5-raspberry-pi-image-video-access-recording\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.engineersgarage.com\/opencv4-5-raspberry-pi-image-video-access-recording\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.engineersgarage.com\/opencv4-5-raspberry-pi-image-video-access-recording\/#primaryimage\",\"url\":\"https:\/\/www.engineersgarage.com\/wp-content\/uploads\/2023\/07\/RPi-CV02-05-rotator.png\",\"contentUrl\":\"https:\/\/www.engineersgarage.com\/wp-content\/uploads\/2023\/07\/RPi-CV02-05-rotator.png\",\"width\":810,\"height\":500},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.engineersgarage.com\/opencv4-5-raspberry-pi-image-video-access-recording\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.engineersgarage.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to use the Raspberry Pi camera with OpenCV\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.engineersgarage.com\/#website\",\"url\":\"https:\/\/www.engineersgarage.com\/\",\"name\":\"Engineers Garage\",\"description\":\"Electronic Projects, Electrical Engineering Resources, Makers Articles and Product News\",\"publisher\":{\"@id\":\"https:\/\/www.engineersgarage.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.engineersgarage.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.engineersgarage.com\/#organization\",\"name\":\"Engineer's Garage - WTWH Media\",\"url\":\"https:\/\/www.engineersgarage.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.engineersgarage.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.engineersgarage.com\/wp-content\/uploads\/2019\/08\/EGlogo.png\",\"contentUrl\":\"https:\/\/www.engineersgarage.com\/wp-content\/uploads\/2019\/08\/EGlogo.png\",\"width\":372,\"height\":52,\"caption\":\"Engineer's Garage - WTWH Media\"},\"image\":{\"@id\":\"https:\/\/www.engineersgarage.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/engineersgarage\",\"https:\/\/x.com\/EngineersGarage\",\"https:\/\/www.youtube.com\/channel\/UC0VITh11JSYk-UW7toLebUw\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.engineersgarage.com\/#\/schema\/person\/3c71105e3b40a1fd12c79c15602914b6\",\"name\":\"Nikhil Agnihotri\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.engineersgarage.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/d26b31563fa36169891c8c7d312e9b33de8a0e3da1340ec14ef2de84d5c43497?s=96&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/d26b31563fa36169891c8c7d312e9b33de8a0e3da1340ec14ef2de84d5c43497?s=96&r=g\",\"caption\":\"Nikhil Agnihotri\"},\"url\":\"https:\/\/www.engineersgarage.com\/author\/nikhil-agnihotri\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"How to use the Raspberry Pi camera with OpenCV","description":"How to capture and record images and videos from a Raspberry Pi camera using OpenCV.","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:\/\/www.engineersgarage.com\/opencv4-5-raspberry-pi-image-video-access-recording\/","og_locale":"en_US","og_type":"article","og_title":"How to use the Raspberry Pi camera with OpenCV","og_description":"How to capture and record images and videos from a Raspberry Pi camera using OpenCV.","og_url":"https:\/\/www.engineersgarage.com\/opencv4-5-raspberry-pi-image-video-access-recording\/","og_site_name":"Engineers Garage","article_publisher":"https:\/\/www.facebook.com\/engineersgarage","og_image":[{"width":810,"height":500,"url":"https:\/\/www.engineersgarage.com\/wp-content\/uploads\/2023\/07\/RPi-CV02-05-rotator.png","type":"image\/png"}],"author":"Nikhil Agnihotri","twitter_card":"summary_large_image","twitter_creator":"@EngineersGarage","twitter_site":"@EngineersGarage","twitter_misc":{"Written by":"Nikhil Agnihotri","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.engineersgarage.com\/opencv4-5-raspberry-pi-image-video-access-recording\/#article","isPartOf":{"@id":"https:\/\/www.engineersgarage.com\/opencv4-5-raspberry-pi-image-video-access-recording\/"},"author":{"name":"Nikhil Agnihotri","@id":"https:\/\/www.engineersgarage.com\/#\/schema\/person\/3c71105e3b40a1fd12c79c15602914b6"},"headline":"How to use the Raspberry Pi camera with OpenCV","datePublished":"2024-03-22T03:03:53+00:00","dateModified":"2024-03-22T18:01:07+00:00","mainEntityOfPage":{"@id":"https:\/\/www.engineersgarage.com\/opencv4-5-raspberry-pi-image-video-access-recording\/"},"wordCount":1759,"commentCount":0,"publisher":{"@id":"https:\/\/www.engineersgarage.com\/#organization"},"image":{"@id":"https:\/\/www.engineersgarage.com\/opencv4-5-raspberry-pi-image-video-access-recording\/#primaryimage"},"thumbnailUrl":"https:\/\/www.engineersgarage.com\/wp-content\/uploads\/2023\/07\/RPi-CV02-05-rotator.png","keywords":["camera","images","microsd","opencv","raspberrypi","raspberrypicamera","videos"],"articleSection":["Raspberry pi"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.engineersgarage.com\/opencv4-5-raspberry-pi-image-video-access-recording\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.engineersgarage.com\/opencv4-5-raspberry-pi-image-video-access-recording\/","url":"https:\/\/www.engineersgarage.com\/opencv4-5-raspberry-pi-image-video-access-recording\/","name":"How to use the Raspberry Pi camera with OpenCV","isPartOf":{"@id":"https:\/\/www.engineersgarage.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.engineersgarage.com\/opencv4-5-raspberry-pi-image-video-access-recording\/#primaryimage"},"image":{"@id":"https:\/\/www.engineersgarage.com\/opencv4-5-raspberry-pi-image-video-access-recording\/#primaryimage"},"thumbnailUrl":"https:\/\/www.engineersgarage.com\/wp-content\/uploads\/2023\/07\/RPi-CV02-05-rotator.png","datePublished":"2024-03-22T03:03:53+00:00","dateModified":"2024-03-22T18:01:07+00:00","description":"How to capture and record images and videos from a Raspberry Pi camera using OpenCV.","breadcrumb":{"@id":"https:\/\/www.engineersgarage.com\/opencv4-5-raspberry-pi-image-video-access-recording\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.engineersgarage.com\/opencv4-5-raspberry-pi-image-video-access-recording\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.engineersgarage.com\/opencv4-5-raspberry-pi-image-video-access-recording\/#primaryimage","url":"https:\/\/www.engineersgarage.com\/wp-content\/uploads\/2023\/07\/RPi-CV02-05-rotator.png","contentUrl":"https:\/\/www.engineersgarage.com\/wp-content\/uploads\/2023\/07\/RPi-CV02-05-rotator.png","width":810,"height":500},{"@type":"BreadcrumbList","@id":"https:\/\/www.engineersgarage.com\/opencv4-5-raspberry-pi-image-video-access-recording\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.engineersgarage.com\/"},{"@type":"ListItem","position":2,"name":"How to use the Raspberry Pi camera with OpenCV"}]},{"@type":"WebSite","@id":"https:\/\/www.engineersgarage.com\/#website","url":"https:\/\/www.engineersgarage.com\/","name":"Engineers Garage","description":"Electronic Projects, Electrical Engineering Resources, Makers Articles and Product News","publisher":{"@id":"https:\/\/www.engineersgarage.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.engineersgarage.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.engineersgarage.com\/#organization","name":"Engineer's Garage - WTWH Media","url":"https:\/\/www.engineersgarage.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.engineersgarage.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.engineersgarage.com\/wp-content\/uploads\/2019\/08\/EGlogo.png","contentUrl":"https:\/\/www.engineersgarage.com\/wp-content\/uploads\/2019\/08\/EGlogo.png","width":372,"height":52,"caption":"Engineer's Garage - WTWH Media"},"image":{"@id":"https:\/\/www.engineersgarage.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/engineersgarage","https:\/\/x.com\/EngineersGarage","https:\/\/www.youtube.com\/channel\/UC0VITh11JSYk-UW7toLebUw"]},{"@type":"Person","@id":"https:\/\/www.engineersgarage.com\/#\/schema\/person\/3c71105e3b40a1fd12c79c15602914b6","name":"Nikhil Agnihotri","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.engineersgarage.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/d26b31563fa36169891c8c7d312e9b33de8a0e3da1340ec14ef2de84d5c43497?s=96&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d26b31563fa36169891c8c7d312e9b33de8a0e3da1340ec14ef2de84d5c43497?s=96&r=g","caption":"Nikhil Agnihotri"},"url":"https:\/\/www.engineersgarage.com\/author\/nikhil-agnihotri\/"}]}},"featured_image_src":"https:\/\/www.engineersgarage.com\/wp-content\/uploads\/2023\/07\/RPi-CV02-05-rotator-600x400.png","featured_image_src_square":"https:\/\/www.engineersgarage.com\/wp-content\/uploads\/2023\/07\/RPi-CV02-05-rotator-600x500.png","author_info":{"display_name":"Nikhil Agnihotri","author_link":"https:\/\/www.engineersgarage.com\/author\/nikhil-agnihotri\/"},"_links":{"self":[{"href":"https:\/\/www.engineersgarage.com\/wp-json\/wp\/v2\/posts\/79062","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.engineersgarage.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.engineersgarage.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.engineersgarage.com\/wp-json\/wp\/v2\/users\/387"}],"replies":[{"embeddable":true,"href":"https:\/\/www.engineersgarage.com\/wp-json\/wp\/v2\/comments?post=79062"}],"version-history":[{"count":0,"href":"https:\/\/www.engineersgarage.com\/wp-json\/wp\/v2\/posts\/79062\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.engineersgarage.com\/wp-json\/wp\/v2\/media\/79069"}],"wp:attachment":[{"href":"https:\/\/www.engineersgarage.com\/wp-json\/wp\/v2\/media?parent=79062"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.engineersgarage.com\/wp-json\/wp\/v2\/categories?post=79062"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.engineersgarage.com\/wp-json\/wp\/v2\/tags?post=79062"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}