0

I'm working on my academic project. I've to display outputs from two webcams onto the screen simultaneously without lag. For that, I'm using pygame surface(because it is SDL) and multiprocessing in python. Using multiprocessing we can only able to pipe one object between two processes only. Here is my expected code to be run:

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

I know this code won't work but something similar like piping pygame-surface object to cam1_core and cam2_core processes (but pipes have only one start point and one end point also it is not a good idea to pipe/queue objects between processes) or piping/queue camera images to display. I'm using multiprocessing to get images simultaneously. Any relevant information for this kind of problem is highly appreciated.

Jid
  • 1
  • can you be more concrete on what's the problem? I understand you want to pipe 2 webcams to the output, but why are you not being able to do it? You don't know how to get the video stream? to pipe do display? or the problem is having 2 webcameras/streams? – pedrorijo91 Jan 05 '17 at 14:11
  • @pedrorijo91 I'm able to get camera stream but not onto the pygame surface. – Jid Jan 05 '17 at 16:21
  • can't you do this in one thread using `if camera == 1: get_from_camera_one; camera = 2 else: get_from_camera_two ; camera = 1` – furas Jan 06 '17 at 04:02
  • @furas I can do that but Processes are better than threads and I want to display two camera output simultaneously. Unfortunately Processes don't have shared memory. – Jid Jan 06 '17 at 04:30
  • I think you could use only main thread/process/program to do this - without using `threading`/`multiprocessing` module. It gives you better control. – furas Jan 06 '17 at 04:36
  • @furus but we cannot access two cameras at a single time instance in serial processing. There is lag in accessing cameras. – Jid Jan 06 '17 at 04:42

0 Answers0