在python中使用多处理访问两个网络摄像头以同时在pygame表面上显示

时间:2017-01-05 14:05:36

标签: python-2.7 pygame multiprocessing ubuntu-14.04 pygame-surface

我正在研究我的学术项目。我将两个网络摄像头的输出同时显示在屏幕上而没有延迟。为此,我使用pygame表面(因为它是SDL)和python中的多处理。使用多处理,我们只能在两个进程之间管道一个对象。 这是我要运行的预期代码:

#!/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()

我知道这段代码不起作用,但类似管道pygame-surface对象到cam1_core和cam2_core进程(但是管道只有一个起点和一个终点,管道/队列对象也不是一个好主意处理)或管道/队列相机图像显示。我正在使用多处理来同时获取图像。我们非常感谢您对此类问题的任何相关信息。

0 个答案:

没有答案