Python 2.7 使用python中的多处理访问两个网络摄像头,以同时显示在pygame界面上
我正在做我的学术项目。我必须同时在屏幕上显示两个网络摄像头的输出,没有延迟。为此,我使用pygame surface(因为它是SDL)和python中的多处理。使用多进程,我们只能在两个进程之间通过管道传输一个对象。 以下是我希望运行的代码:Python 2.7 使用python中的多处理访问两个网络摄像头,以同时显示在pygame界面上,python-2.7,pygame,multiprocessing,ubuntu-14.04,pygame-surface,Python 2.7,Pygame,Multiprocessing,Ubuntu 14.04,Pygame Surface,我正在做我的学术项目。我必须同时在屏幕上显示两个网络摄像头的输出,没有延迟。为此,我使用pygame surface(因为它是SDL)和python中的多处理。使用多进程,我们只能在两个进程之间通过管道传输一个对象。 以下是我希望运行的代码: #!/usr/bin/python import os, sys from multiprocessing import Process import time import pygame import Tkinter as tk import pygam
#!/usr/bin/python
import os, sys
from multiprocessing import Process
import time
import pygame
import Tkinter as tk
import pygame.camera
from pygame.locals import *
# Initializations
pygame.init()
pygame.camera.init()
pygame.display.init()
w= 320
h = 240
fps = 45
clist = pygame.camera.list_cameras()
screen = pygame.display.set_mode((w*2,h))
def cam1_core():
print 'left started'
writer1 = imageio.get_writer('left_eye.avi', fps=fps)
cam1 = pygame.camera.Camera(clist[0], (w, h))
cam1.start()
time.sleep(1)
i=0
while i < 500:
imgb = cam1.get_image()
img1 = pygame.surfarray.array3d(imgb)
screen.blit(imgb, (0, 0))
pygame.display.update()
i = i + 1
#sys.stdout.flush()
cam1.stop()
sys.stdout.flush()
def cam2_core():
print 'right started'
cam2 = pygame.camera.Camera(clist[1], (w, h))
cam2.start()
time.sleep(1)
j=0
while j < 500:
imga = cam2.get_image()
img2 = pygame.surfarray.array3d(imga)
screen.blit(imga, (w, 0))
pygame.display.update()
j = j + 1
#sys.stdout.flush()
cam2.stop()
print 'right closed'
sys.stdout.flush()
if __name__ == '__main__':
p1 = Process(target=cam1_core)
p2 = Process(target=cam2_core)
p1.start()
p2.start()
#/usr/bin/python
导入操作系统,系统
从多处理导入进程
导入时间
导入pygame
将Tkinter作为tk导入
导入pygame.camera
从pygame.locals导入*
#初始化
pygame.init()
pygame.camera.init()
pygame.display.init()
w=320
h=240
fps=45
clist=pygame.camera.list\u cameras()
screen=pygame.display.set_模式((w*2,h))
def cam1_芯()
打印“左开始”
writer1=imageio.get\u writer('left\u eye.avi',fps=fps)
cam1=pygame.camera.camera(clist[0],(w,h))
cam1.start()
时间。睡眠(1)
i=0
当我<500时:
imgb=cam1.get_image()
img1=pygame.surfarray.array3d(imgb)
屏幕blit(imgb,(0,0))
pygame.display.update()
i=i+1
#sys.stdout.flush()
cam1.停止()
sys.stdout.flush()
def凸轮2_芯():
打印“右开始”
cam2=pygame.camera.camera(clist[1],(w,h))
cam2.start()
时间。睡眠(1)
j=0
当j<500时:
imga=cam2.get_image()
img2=pygame.surfarray.array3d(imga)
屏幕blit(imga,(w,0))
pygame.display.update()
j=j+1
#sys.stdout.flush()
cam2.停止()
打印“右关闭”
sys.stdout.flush()
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
p1=过程(目标=cam1_核心)
p2=过程(目标=cam2_核心)
p1.开始()
p2.start()
我知道这段代码不起作用,但类似于将pygame surface对象管道化到cam1_core和cam2_core进程(但管道只有一个起点和一个终点,而且在进程之间对对象进行管道/排队不是一个好主意)或管道/排队摄影机图像来显示。我使用多重处理来同时获取图像。非常感谢您提供有关此类问题的任何相关信息。您能更具体地说明问题所在吗?我知道您想通过管道将2个网络摄像头传输到输出,但为什么您不能这样做?你不知道如何获取视频流?要做什么显示?或者问题是有2个网络摄像机/流?@pedrorijo91我可以获取摄像机流,但不能在pygame界面上。如果摄像机==1:get_from_camera_one,你不能在一个线程中使用
来完成吗;摄像机=2其他:从摄像机2获取摄像机;camera=1
@furas我可以这样做,但进程比线程好,我想同时显示两个摄像头输出。不幸的是,进程没有共享内存。我认为您可以只使用主线程/进程/程序来实现这一点,而无需使用线程化
/多处理
模块。它给你更好的控制。