0

enter image description here

I will use two webcams to process image on Raspberrypi3. I want to fix device video number to each camera. Because I need to know which camera is leftside or rightside. How to fix video numbers to each camera?

from collections import deque
import numpy as np
import argparse
import imutils
import cv2
import time
from multiprocessing import Process, Queue

class opencv:
    def __init__(self):
        self.FrameWidth = 1024          
        self.FrameHeight = 576          
    def rescale_frame(self, frame, percent=75):
        width = int(frame.shape[1] * percent/ 100)
        height = int(frame.shape[0] * percent/ 100)
        dim = (width, height)
        return cv2.resize(frame, dim, interpolation =cv2.INTER_AREA),width,height

    def cam(self, video):
        camera = cv2.VideoCapture(video)
        camera.set(3, self.FrameWidth)
        camera.set(4, self.FrameHeight)
        while True:
            (grab, frame) = camera0.read()
            frame,width,height = self.rescale_frame(frame, percent=50)              # pixcel 50% 
            cv2.imshow("Frame", frame)
            key = cv2.waitKey(60) & 0xFF
            if (key == 27):
                break
            if (key == ord('i')):
                while True:
                    cv2.imshow("Frame", frame)
                    cv2.waitKey(0)
                    if key == ord('i'):
                        break

        camera.release()
        cv2.destroyAllWindows()

if __name__ == "__main__":
    cam0 = opencv()
    cam1 = opencv()
    p1 = Process(target = cam0.cam, args = (0,))
    p2 = Process(target = cam1.cam, args = (1,))
    p1.start()
    p2.start()
Suyoung Park
  • 31
  • 1
  • 1
  • 3
  • Looks like a duplicate of this one: https://stackoverflow.com/questions/35821763/create-opencv-videocapture-from-interface-name-instead-of-camera-numbers Hope it helps:) – user48538 Jul 23 '18 at 15:28
  • Not really, that duplicate was how to identify different webcams by brand name, it doesn't help with two identical models – Martin Beckett Jul 24 '18 at 15:41

1 Answers1

0

In general you cannot with openCV and usb webcams. The OS will find them in an unknown order - typically the order they power up. You could try connecting them with a switching usb hub and turn them on in the same order with a few second delay between each.

Unless there is some per-camera serial number you can read directly.

Martin Beckett
  • 94,801
  • 28
  • 188
  • 263
  • I'm using usb hub now. – Suyoung Park Jul 23 '18 at 16:01
  • There are some hubs that have an on/off switch for each port. If you turn on one camera wait 1second and turn on other camera they will be found in that order. It is more difficult to do for a production system, but you can't really run 2x usb webcams on a pi for real time – Martin Beckett Jul 23 '18 at 16:07
  • I tried to run my code on raspberrypi3 B+ but I got a error 'select timeout' ......Can you give me some advice? – Suyoung Park Jul 24 '18 at 15:10