Search code examples
pythonpython-3.xopencvwindows-10video-capture

Python Display OpenCV Video Windows 10 With Correct Device Name


How can I supply the correct device name to OpenCV to show video using Python on Windows 10? And how can the camera devices on Windows 10 be enumerated in Python?

The following Python code works fine to show video on Ubuntu, but fails on Windows 10 as described below:

#!/usr/bin/env python
# show_video.py # Open and play a video using OpenCV 4 and Python 3.
# see https://docs.opencv.org/3.4.1/dd/d43/tutorial_py_video_display.html

import sys
import cv2 as cv

dev_name = sys.argv[1] if len(sys.argv) > 1 else '/dev/video0'
window_name = dev_name + ' OpenCV ' + cv.__version__ + ' python ' + sys.version

print('opening '  + window_name)
cap = cv.VideoCapture(dev_name)

if not cap.isOpened():
    print('error: unable to open:', dev_name)
    sys.exit(0)

k = 0
ret, frame = cap.read() # Capture frame-by-frame
while cap.isOpened() and ret and k!=27 and k!=ord('q'):
    # Display the resulting frame
    cv.imshow(window_name, frame)

    k = cv.waitKey(30) & 0xff # press <esc> or 'q' to quit
    ret, frame = cap.read()

# When everything done, release the capture
cap.release()
cv.destroyAllWindows()

I'd like to run the app from the cmd line, however

python show_video.py /dev/video0

does not work because /dev/video0 does not exist in Windows.

I tried https://stackoverflow.com/a/4286790 to enumerate devices. It works in C++ and I don't know how it can be translated into Python. Output on my system was:

Integrated Webcam
Device path: \\?\usb#vid_0bda&pid_58c2&mi_00#6&f4034f5&0&0000#{65e8773d-8f56-11d0-a3b9-00a0c9223196}\global
Microphone (Realtek Audio)
WaveIn ID: 0

I tried the following to show video from Windows 10 cmd:

python show_video.py "\\?\usb#vid_0bda&pid_58c2&mi_00#6&f4034f5&0&0000#{65e8773d-8f56-11d0-a3b9-00a0c9223196}\global"

which outputs:

opening \\?\usb#vid_0bda&pid_58c2&mi_00#6&f4034f5&0&0000#{65e8773d-8f56-11d0-a3b9-00a0c9223196}\global OpenCV 3.4.1 python 3.6.6 |Anaconda custom (64-bit)| (default, Jun 28 2018, 11:27:44) [MSC v.1900 64 bit (AMD64)]
warning: Error opening file (/build/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp:834)
warning: \\?\usb#vid_0bda&pid_58c2&mi_00#6&f4034f5&0&0000#{65e8773d-8f56-11d0-a3b9-00a0c9223196}\global (/build/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp:835)
error: unable to open: \\?\usb#vid_0bda&pid_58c2&mi_00#6&f4034f5&0&0000#{65e8773d-8f56-11d0-a3b9-00a0c9223196}\global

I also tried python show_video.py "\\\\?\\usb#vid_0bda&pid_58c2&mi_00#6&f4034f5&0&0000#{65e8773d-8f56-11d0-a3b9-00a0c9223196}\\global" and python show_video.py "Integrated Webcam" and got similar errors.

I am using OpenCV 3.4.1 and Python 3.6.6.


Solution

  • From my knowledge, OpenCV does not provide such functionality. To get the names of the attached devices, you'll need to go lower and use the DirectShow or WMF API to get the device enumeration list. So that cannot be done with pure python.

    I see there is a already made cpython module to achieve what you want. You can try that out.

    And here is a good post which will help you to do what you want, but not with OpenCV.