python pygame.camera.init() 没有 vidcapture
- 2025-02-28 08:23:00
- admin 原创
- 78
问题描述:
我正在尝试初始化 pygame 中的相机模块并显示来自 USB 网络摄像头的视频。这是我的代码:
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:Python27libsite-packagespygamecamera.py", line 67, in init
_camera_vidcapture.init()
File "C:Python27libsite-packagespygame_camera_vidcapture.py", line 21, in init
import vidcap as vc
ImportError: No module named vidcap
请帮忙!!!我在 Windows 上
解决方案 1:
我也遇到了同样的问题,错误信息“ImportError: No module named vidcap”表示python解释器在你的机器上没有找到vidcap模块。
所以你最好按照以下步骤操作。
从http://videocapture.sourceforge.net/下载 vidcap
2.然后将相应版本的dll(在VideoCapture-0.9-5\VideoCapture-0.9-5\Python27\DLLs中名为“vidcap.pyd”)复制到“你的python路径”\DLLs\ 。
3.重新启动脚本。
完毕!。
解决方案 2:
相机模块只能在Linux上使用
解决方案 3:
我遇到了同样的问题,但我发现它不包含在 Windows 中,仅限 Linux
解决方案 4:
尝试一下:
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()
解决方案 5:
仅pygame.camera
支持 Linux:
Pygame 目前仅支持 Linux 和 v4l2 相机。
另一种解决方案是使用OpenCVVideoCapture
。安装 Python 版 OpenCV ( 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()
相关推荐
热门文章
项目管理软件有哪些?
热门标签
曾咪二维码
扫码咨询,免费领取项目管理大礼包!
云禅道AD