kevin56789
Posts: 4
Joined: Mon Jun 08, 2015 4:37 pm

How to save webcam video into Pi???

Sat Jun 13, 2015 4:15 pm

I am working with a Genius brand webcam to film people during daytime. The goal of my project is to capture people moving around and use OpenCV to convert the video of the people into contour data of the people (turn them into polygons). I am currently having trouble with my code that is supposed to save the video onto the Raspi, which opens up the camera and projects what it sees, but it either doesn't save the video or doesn't record anything at all.

I really appreciate the help. Thank you.

Here is my current code:
import numpy as np
import cv2
path = ('/home/pi/output.avi')

cap = cv2.VideoCapture(0)
if not cap:
print "!!! Failed VideoCapture: invalid parameter!"
sys.exit(1)

# Define the codec and create VideoWriter object
fourcc = cv2.cv.CV_FOURCC(*'MJPG')

video_writer = cv2.VideoWriter(path, fourcc, 40.0, (640, 480))
if not video_writer :
print "!!! Failed VideoWriter: invalid parameters"
sys.exit(1)

while (cap.isOpened()):
ret, frame = cap.read()
if ret == False:
break

cv2.imshow('frame',frame)
video_writer.write(frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break

cap.release()
video_writer.release()

Return to “Python”