0

I ran into a very strange problem that appeared out of nowhere, the same code did not cause this problem before.

The following code, the first time I ran it, it read the frame from my connected external camera; the second time I ran it, it read the frame from my MacBook's built-in camera; the third time it read the frame from the external camera.

It just keeps switching and I don't know how to fix it, I want cameraCapture = cv2.VideoCapture(0) to always get the frame from the external camera.

import cv2

cameraCapture = cv2.VideoCapture(1)

# read
success, frame = cameraCapture.read()
while success and cv2.waitKey(1) == -1:
    img = frame

    cv2.imshow("Mine", img)
    success, frame = cameraCapture.read()

Is there any way I can get the data of a certain numbered camera, such as name, resolution, etc.?

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
fjybiocs
  • 59
  • 5
  • IIUC, OpenCV uses `ffmpeg` under the covers to record video. Have a look at the `ffmpeg` part of my answer here https://stackoverflow.com/a/74325989/2836621 You may find the `AVFoundation` command allows you to predict the parameter for `cv2.VideoCapture()` or that you can use `ffmpeg` directly to read and pipe frames into your OpenCV code instead of that if it isn't working for you. – Mark Setchell Mar 05 '23 at 09:01
  • no, for video devices it **does not** use ffmpeg. it uses native apis (or gstreamer). – Christoph Rackwitz Mar 05 '23 at 12:28
  • 1
    yes, this is a problem with OpenCV. the mapping from devices to plain dumb indices may not be deterministic. just file a bug about this, and please state that you're using MacOS (i.e. API is probably AVFoundation). eventually they'll have to improve OpenCV's API such that one can actually pass a device identifier (also enumerate devices). this indices stuff is just useless and prevents OpenCV from being used for serious applications (with multiple cameras). – Christoph Rackwitz Mar 05 '23 at 12:30

1 Answers1

-2

You can use cameraCapture.get(<property id>) to get different properties of the capture device. There's a full list of them here, but the ones you're looking for are:

  • cv.CAP_PROP_FRAME_WIDTH for the width
  • cv.CAP_PROP_FRAME_HEIGHT for the height.
Adid
  • 1,504
  • 3
  • 13