Difference between revisions of "Video Input from a Raspberry Pi B+ Camera Module"

From ESE205 Wiki
Jump to: navigation, search
 
(8 intermediate revisions by 3 users not shown)
Line 1: Line 1:
Although the steps I've outlined below might seem trivial, there are a number of conflicting sources on how to access the video feed from the Raspberry Pi B+ using OpenCV, many of which ''do not work''.  The following guide is designed to help you quickly and efficiently work recording video into your project.  Please note that my instructions were tested using Python 2 with OpenCV.
+
In developing my project, FingerSpark, I found a number of conflicting sources on how to access the video feed from the Raspberry Pi B+ using OpenCV, many of which ''do not work''.  The following guide is designed to help you quickly and efficiently work recording video into your project.  Please note that my methodology was tested using Python 2 with OpenCV.
  
 
==Step 1: Import OpenCV into your Python File==
 
==Step 1: Import OpenCV into your Python File==
Assuming that you've successfully installed OpenCV on your Raspberry Pi B+, you need to include <code>import cv2</code><br> at the top of your .py project file.  This will allow you to use the OpenCV methods.
+
Assuming that you've successfully installed OpenCV on your Raspberry Pi B+, you need to include <code>import cv2</code> at the top of your .py project file.  This will allow you to use the OpenCV methods necessary to access the camera module's video feed.
  
 
==Step 2: Specify Location, Length, and FPS==
 
==Step 2: Specify Location, Length, and FPS==
Line 15: Line 15:
 
For example, under the previously described import statement: <code>[vid_loc,vid_len,vid_fps]=['/home/pi/Desktop/v2a.h264','5000','90']</code>
 
For example, under the previously described import statement: <code>[vid_loc,vid_len,vid_fps]=['/home/pi/Desktop/v2a.h264','5000','90']</code>
  
 +
==Step 3: Pass Video Preferences to Raspberry Pi's OS==
 +
Next, you need to include code that sends your video preferences to the Pi's operating system, so it can correctly format your video.  For example:
 +
<blockquote>
 +
<code>stem='raspivid -w 640 -h 480 -fps '</code><br>
 +
<code>os.system(stem+vid_fps+' -t '+vid_len+' -o '+vid_loc)</code><br>
 +
</blockquote>
  
 +
==Step 4: Capture the Video==
 +
Use the VideoCapture method in OpenCV with the video location as the only parameter to capture the video from the camera module.  The line of code you write should look like this: <code>cap = cv2.VideoCapture(vid_loc)</code>
  
 +
==Step 5: Process the Video==
 +
Call the <code>read()</code> method on the <code>cap</code> object you created.  To iterate through individual frames, use a <code>while</code> loop, with a break condition for when the frame position is greater than the total number of frames that you want in your video.  <code>cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES)</code> will return the number of the current frame in the video.
  
 
+
==Step 6: End the Video==
 +
When you have finished iterating through the frames of your video (after your <code>while</code> loop), call <code>cap.release()</code> to end the recording.
  
 
[[Category:HowTos]]
 
[[Category:HowTos]]
 +
[[Category:Raspberry_Pi]]

Latest revision as of 20:09, 16 August 2018

In developing my project, FingerSpark, I found a number of conflicting sources on how to access the video feed from the Raspberry Pi B+ using OpenCV, many of which do not work. The following guide is designed to help you quickly and efficiently work recording video into your project. Please note that my methodology was tested using Python 2 with OpenCV.

Step 1: Import OpenCV into your Python File

Assuming that you've successfully installed OpenCV on your Raspberry Pi B+, you need to include import cv2 at the top of your .py project file. This will allow you to use the OpenCV methods necessary to access the camera module's video feed.

Step 2: Specify Location, Length, and FPS

Next, I would suggest creating global variables for:

1. The location on the Raspberry Pi where you would like to save your video clip, which will include the clip's name (it's usually best to use an absolute path in case you end up moving your project file)

2. The intended duration (in milliseconds) of your video clip

3. The number of frames per second in your video (I successfully tested values up to 90 FPS)

For example, under the previously described import statement: [vid_loc,vid_len,vid_fps]=['/home/pi/Desktop/v2a.h264','5000','90']

Step 3: Pass Video Preferences to Raspberry Pi's OS

Next, you need to include code that sends your video preferences to the Pi's operating system, so it can correctly format your video. For example:

stem='raspivid -w 640 -h 480 -fps '
os.system(stem+vid_fps+' -t '+vid_len+' -o '+vid_loc)

Step 4: Capture the Video

Use the VideoCapture method in OpenCV with the video location as the only parameter to capture the video from the camera module. The line of code you write should look like this: cap = cv2.VideoCapture(vid_loc)

Step 5: Process the Video

Call the read() method on the cap object you created. To iterate through individual frames, use a while loop, with a break condition for when the frame position is greater than the total number of frames that you want in your video. cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES) will return the number of the current frame in the video.

Step 6: End the Video

When you have finished iterating through the frames of your video (after your while loop), call cap.release() to end the recording.