6

pygame でカメラ モジュールを初期化し、USB Web カメラからビデオを表示しようとしています。これは私のコードです:

import pygame
import pygame.camera
from pygame.camera import *
from pygame.locals import *

pygame.init()
pygame.camera.init()

cam = pygame.camera.Camera("/dev/video0",(640,480))
cam.start()
image = cam.get_image()

しかし、私はこのエラーが発生します:

Traceback (most recent call last):
  File "C:/Users/Freddie/Desktop/CAMERA/Test1.py", line 7, in <module>
    pygame.camera.init()
  File "C:\Python27\lib\site-packages\pygame\camera.py", line 67, in init
    _camera_vidcapture.init()
  File "C:\Python27\lib\site-packages\pygame\_camera_vidcapture.py", line 21, in init
    import vidcap as vc
ImportError: No module named vidcap

PLSヘルプ!!! 私はWindowsにいます

4

5 に答える 5

0

これを試して:

import pygame

import pygame.camera

import time, string


from VideoCapture import Device

from pygame.locals import *

pygame.camera.init()

cam = pygame.camera.Camera(0,(640,480),"RGB")

cam.start()

img = pygame.Surface((640,480))

cam.get_image(img)

pygame.image.save(img, "img2.jpg")

cam.stop()
于 2015-12-16T17:16:21.270 に答える
0

pygame.cameraLinux でのみサポートされています。

Pygame は現在、Linux と v4l2 カメラのみをサポートしています。

別の解決策は、 OpenCVVideoCaptureを使用することです。OpenCV for Python ( cv2 )をインストールします ( opencv -pythonを参照)。

ビデオ キャプチャ用のカメラを開きます。

capture = cv2.VideoCapture(0)

カメラ フレームを取得します。

success, camera_image = capture.read()

pygame.Surfaceを使用してカメラ フレームをオブジェクトに変換しpygame.image.frombufferます。

camera_surf = pygame.image.frombuffer(
              camera_image.tobytes(), camera_image.shape[1::-1], "BGR")

カメラとビデオもご覧ください


最小限の例:

import pygame
import cv2

capture = cv2.VideoCapture(0)
success, camera_image = capture.read()

window = pygame.display.set_mode(camera_image.shape[1::-1])
clock = pygame.time.Clock()

run = success
while run:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
    
    success, camera_image = capture.read()
    if success:
        camera_surf = pygame.image.frombuffer(
            camera_image.tobytes(), camera_image.shape[1::-1], "BGR")
    else:
        run = False
    window.blit(camera_surf, (0, 0))
    pygame.display.flip()

pygame.quit()
exit()
于 2021-09-04T09:10:45.453 に答える