[英]how to run webcam through python on rpi3
我正在嘗試使用我的覆盆子-pi-3的MS lifecam。 當我輸入以下命令時,它在命令行上工作:
$ fswebcam img.jpg
Trying source module v4l2...
/dev/video0 opened.
...
Writing JPEG image to 'img.jpg' # this works fine
現在我想通過python代碼運行相機:
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()
報告的錯誤是:
SystemError: ioctl(VIDIOC_S_FMT) failure: no supported formats
我在這里很困惑。 相機由rasp-pi支持,所以看起來我的python代碼必須在某處更新。 你能幫我嗎?
試試這個:
camera = pygame.camera.Camera(pygame.camera.list_cameras()[0])
camera.start()
img = camera.get_image()
pygame.image.save(img, FILENAME)
有問題,一旦我停止使用視頻流的過程,錯誤就解決了。
細節
我有同樣的問題。 而且
/dev/video0
列出了,camera.start()導致了同樣的錯誤。
我跑了
sudo motion
早。 所以我驗證了服務正在運行,停止它,然后嘗試了pygame。 它起作用了。
sudo service --status-all
sudo service motion stop
你也可以用這個:
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")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.