gpt4 book ai didi

python - pygame.camera 可以使用多少个摄像头?

转载 作者:行者123 更新时间:2023-11-30 23:29:00 27 4
gpt4 key购买 nike

pygame.camera 可以同时使用多少个摄像头?

我尝试使用 3 个网络摄像头,但程序无法运行!当我使用一两个网络摄像头时 - 没关系。 =\

这是我的代码:

# import the relevant libraries
import time
import sys
import pygame
import pygame.camera
from pygame.locals import *
from pygame.tests.base_test import pygame_quit
pygame.init()

s_width = 645
s_height = 600
width = 640
height = 480

class detectCameras(object):

def __init__(self):
pygame.camera.init()

class webCamera(object):

def __init__(self,cam_number,cam_resolution=[640,480],color_mode="RGB"):
self.cam_number = cam_number
self.cam_resolution = cam_resolution
self.color_mode = color_mode
self.cam = pygame.camera.Camera(self.cam_number,self.cam_resolution,self.color_mode)

def start(self):
self.cam.start()

def stop(self):
self.cam.stop()

def get_b(self):
return self.cam.get_buffer()

def get_image(self, new_width = 320, new_height = 240):
self.img = self.cam.get_image()
self.new_width = new_width
self.new_height = new_height
self.img = pygame.transform.scale(self.img, (self.new_width,self.new_height))
return self.img

screen = pygame.display.set_mode([s_width,s_height])
# set window title
pygame.display.set_caption("OMS CCTV")
# set icon
pygame.display.set_icon(pygame.image.load('icon.png').convert_alpha())


#detectCameras()
pygame.camera.init()
# set up a camera object

cam_on = webCamera(1) #pygame.camera.Camera(1,[width,height],"RGB")
cam_tw = webCamera(2)
cam_tr = webCamera(3)
# start the camera

cam_on.start()
cam_tw.start()
cam_tr.start()
#
mainloop = 1
while mainloop:
for event in pygame.event.get():
if event.type == pygame.QUIT:
mainloop = 0
pygame.quit()
sys.exit("Exit")
elif event.type == pygame.K_ESCAPE:
mainloop = 0
pygame.quit()
sys.exit("Exit")
# sleep between every frame
time.sleep( 0.05 )


screen.fill([0,0,0])

image_on = cam_on.get_image()
screen.blit( image_on, ( 0, 0 ) )


image_tw = cam_tw.get_image()
screen.blit( image_tw, ( 325, 0 ) )


image_tr = cam_tr.get_image()
screen.blit( image_tr, ( 0, 245 ) )

pygame.display.update()

我不知道为什么它不起作用!

最佳答案

来自the documentation对于pygame.camera:

EXPERIMENTAL!: This api may change or disappear in later pygame releases. If you use this, your code will very likely break with the next pygame release.

我认为您不能依赖它支持任何特定数量的相机;也许两个是它能做到的最好的。

关于python - pygame.camera 可以使用多少个摄像头?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21365800/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com