user3442005
user3442005

Reputation: 103

how to run webcam through python on rpi3

I am trying to use a MS lifecam with my raspberry-pi-3. It works on the command line, when I type the following command:

$ fswebcam img.jpg
Trying source module v4l2...
/dev/video0 opened.
...
Writing JPEG image to 'img.jpg'  # this works fine

Now I want to run the camera through a python code:

import pygame
import pygame.camera
from pygame.locals import *
DEVICE = '/dev/video0'
SIZE = (640, 480)  # I also tried with img size (384,288), same error
FILENAME = 'capture.jpg'
pygame.init()
pygame.camera.init()
camera = pygame.camera.Camera(DEVICE, SIZE)
camera.start()   # error on executing this line
pygame.image.save(screen, FILENAME)
camera.stop()

The reported error is:

SystemError: ioctl(VIDIOC_S_FMT) failure: no supported formats

I am puzzled here. The camera is supported by rasp-pi, so it looks like my python code has to be updated somewhere. Can you help?

Upvotes: 5

Views: 2595

Answers (3)

bugfreerammohan
bugfreerammohan

Reputation: 1489

you can also use this :

 import cv2
cv2.namedWindow("preview")
vc = cv2.VideoCapture(0)
if vc.isOpened(): 
rval, frame = vc.read()
else:
rval = False

while rval:
cv2.imshow("preview", frame)
rval, frame = vc.read()
key = cv2.waitKey(20)
if key == 27:  # exit on ESC
    break
cv2.destroyWindow("preview")

Upvotes: 0

nunya
nunya

Reputation: 1

Had problem and once I stopped a process using the video stream the error was resolved.

details

I had the same problem. And while

/dev/video0

was listed, camera.start() resulted in the same error.

I had ran

   sudo motion

earlier. so I verified the service was running, stopped it, then tried pygame. and it worked.

   sudo service --status-all
   sudo service motion stop

Upvotes: 0

Andark
Andark

Reputation: 410

Try use this:

camera = pygame.camera.Camera(pygame.camera.list_cameras()[0])
camera.start()
img = camera.get_image()
pygame.image.save(img, FILENAME)

Upvotes: 1

Related Questions