Get device list with Windows DirectShow
Project description
List Capture Devices for Python OpenCV on Windows
OpenCV does not have an API for listing capture devices. The sample shows how to create a Python extension to invoke DirectShow C++ APIs for enumerating capture devices.
Environment
- Microsoft Windows SDK
- Python 2.7.9 / Python 3.6.5
- OpenCV 3.3.0
How to Build the CPython Extension
-
Create a source distribution:
python setup.py sdist
-
distutils:
python .\setup_distutils.py build
-
scikit-build:
pip wheel . --verbose
Test
import device
import cv2
def select_camera(last_index):
number = 0
hint = "Select a camera (0 to " + str(last_index) + "): "
try:
number = int(input(hint))
# select = int(select)
except Exception:
print("It's not a number!")
return select_camera(last_index)
if number > last_index:
print("Invalid number! Retry!")
return select_camera(last_index)
return number
def open_camera(index):
cap = cv2.VideoCapture(index)
return cap
def main():
# print OpenCV version
print("OpenCV version: " + cv2.__version__)
# Get camera list
device_list = device.getDeviceList()
index = 0
for name in device_list:
print(str(index) + ': ' + name)
index += 1
last_index = index - 1
if last_index < 0:
print("No device is connected")
return
# Select a camera
camera_number = select_camera(last_index)
# Open camera
cap = open_camera(camera_number)
if cap.isOpened():
width = cap.get(3) # Frame Width
height = cap.get(4) # Frame Height
print('Default width: ' + str(width) + ', height: ' + str(height))
while True:
ret, frame = cap.read()
cv2.imshow("frame", frame)
# key: 'ESC'
key = cv2.waitKey(20)
if key == 27:
break
cap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
main()
python test.py
Blog
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Close
Hashes for windows-capture-device-list-1.0.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 315080b7e53dbc33820aa32d52d10e8c965a3d9da7abb88da571f41bf249f302 |
|
MD5 | ad5d877bde565b9dcf5826a894cd922e |
|
BLAKE2b-256 | 13a62f3778ec769f4baf48df6b27b37e037f7481b5b57537f0f8997eec65202c |
Close
Hashes for windows_capture_device_list-1.0.1-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ef8f66f127afa213e665350035861e31a9811b5cd14e047195a11053868bbb60 |
|
MD5 | 09d2620c68f45d77bd660bea2a51cf9d |
|
BLAKE2b-256 | 83de3e0295594ab68f9458d0940cb9323b87656d6cfd5473cadd01dc039ea895 |
Close
Hashes for windows_capture_device_list-1.0.1-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | aae34eb793f6bc2e5b7ee961d884fb37a08606b60140e12e085a60c80d1dc598 |
|
MD5 | 21e212085b0cc1db8b3cd8bfe77dde1d |
|
BLAKE2b-256 | 539f2e171b6734a4343d4b60979ccecb8813d40e9282891d902c90fa46452738 |
Close
Hashes for windows_capture_device_list-1.0.1-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | abc67c34a02daa9cb6c0671ad58a1f33762e4281b5fb83de53158759394a432b |
|
MD5 | 3f712de9b40602ced3cd537d0731c03f |
|
BLAKE2b-256 | ed3fd12799cc9ea50b9436cd1072f8b075f822cafaa5fc65abafff81e46234dd |
Close
Hashes for windows_capture_device_list-1.0.1-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | edf119cad048f176b70490c9c41f05b54e99e54454ff45b8758cecbbe063579a |
|
MD5 | a2015ae273b13b30c1e8fb4b33643214 |
|
BLAKE2b-256 | 4d482810da631db5f523141d5459a0b9cddb1af2698965cbef1ebbb87f398d18 |
Close
Hashes for windows_capture_device_list-1.0.1-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ae38b287c10bd38e34d80d4d49cd6e731b29e1c303f2cce6cc9ac1b161c4b1c2 |
|
MD5 | 7257a9e84e0bc702b16366ec6218e8f2 |
|
BLAKE2b-256 | 6538cc1d0a98103180de0d649ffa4b789e942d0dd4408639cb9f7817c1606aac |