Python basic tutorial: Python reads multiple USB cameras through cv2

@This article comes from the public number: csdn2299, if you like, you can follow the public number programmer academy.
This example shares the specific code of Python reading multiple USB cameras through cv2 for your reference. The specific content is as follows

The camera data can be easily obtained through cv2.
For example, the following steps can open the camera display, and save the picture through the q key

import cv2
 
capture = cv2.VideoCapture(0)
# 打开自带的摄像头
if capture.isOpened():
 # 以下两步设置显示屏的宽高
 capture .set(cv2.CAP_PROP_FRAME_WIDTH, 640)
 capture .set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
  
 # 持续读取摄像头数据
 while True:
  read_code, frame = capture.read()
  if not read_code:
   break
  cv2.imshow("screen_title", frame)
  # 输入 q 键,保存当前画面为图片
  if cv2.waitKey(1) == ord('q'):
   # 设置图片分辨率
   frame = cv2.resize(frame, (1920, 1080))
   cv2.imwrite('pic.jpg', frame)
   break
 # 释放资源  
 capture.release()
 cv2.destroyWindow("screen_title")

Now that I have connected a USB camera and want to operate them simultaneously through code, how can I achieve this?
Observe that the open function of capture has passed in the camera number, and the increased camera number is incremented.
So amended as follows

import cv2
 
capture = cv2.VideoCapture(0)
capture_usb = cv2.VideoCapture(1)
# 打开自带的摄像头
if capture.isOpened():
 if capture_usb.isOpened():
 # 以下设置显示屏的宽高
 capture.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
 capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
 capture_usb.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
 capture_usb.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
  
 # 持续读取摄像头数据
 while True:
  read_code, frame = capture.read()
  read_code2, frame2 = capture_usb.read()
  if not read_code or not read_code2:
   break
  cv2.imshow("screen_title", frame)
  cv2.imshow("screen_title_usb", frame2)
  # 输入 q 键,保存当前画面为图片
  if cv2.waitKey(1) == ord('q'):
   # 设置图片分辨率
   frame = cv2.resize(frame, (1920, 1080))
   cv2.imwrite('pic.jpg', frame)
   capture_usb.release()
   break
 # 释放资源  
 capture.release()
 cv2.destroyWindow("screen_title")

Although it is already possible to operate multiple cameras at the same time, we can't distinguish which number value represents which USB device if another USB camera is connected.

Checked the source code of the implementation (see: cap_8cpp_source.html) The Insert picture description here
open function really only receives the index parameter, and there is no other implementation with the same name. If you want to uniquely determine the parameters by similar hardware id, you need to implement such a method separately.

However, by analyzing the source code, it is found that there is such a method called in the middle of getAvailableBackends_CaptureByIndex (), so boldly guess that the camera information should be saved in a data structure in the order of USB access, so it can be assumed that the camera is used in the order of device insertion

Thank you very much for reading
. When I chose to study python at university, I found that I ate a bad computer foundation. I did n’t have an academic qualification. This is
nothing to do. I can only make up for it, so I started my own counterattack outside of coding. The road, continue to learn the core knowledge of python, in-depth study of computer basics, sorted out, if you are not willing to be mediocre, then join me in coding, and continue to grow!
In fact, there are not only technology here, but also things beyond those technologies. For example, how to be an exquisite programmer, rather than "cock silk", the programmer itself is a noble existence, isn't it? [Click to join] Want to be yourself, want to be a noble person, come on!

Published 50 original articles · 21 praises · 20,000+ views

Guess you like

Origin blog.csdn.net/chengxun03/article/details/105545007