-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCameraList.py
73 lines (52 loc) · 2.26 KB
/
CameraList.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import asyncio
import platform
import subprocess
import cv2
if platform.system() == 'Windows':
import winsdk.windows.devices.enumeration as windows_devices
VIDEO_DEVICES = 4
class Camera:
def __init__(self):
self.cameras = []
def get_camera_info(self) -> list:
self.cameras = []
camera_indexes = self.get_camera_indexes()
if len(camera_indexes) == 0:
return self.cameras
self.cameras = self.add_camera_information(camera_indexes)
return self.cameras
def get_camera_indexes(self):
index = 0
camera_indexes = []
max_numbers_of_cameras_to_check = 10
while max_numbers_of_cameras_to_check > 0:
capture = cv2.VideoCapture(index)
if capture.read()[0]:
camera_indexes.append(index)
capture.release()
index += 1
max_numbers_of_cameras_to_check -= 1
return camera_indexes
# TODO add MacOS specific implementations
def add_camera_information(self, camera_indexes: list) -> list:
platform_name = platform.system()
cameras = []
if platform_name == 'Windows':
cameras_info_windows = asyncio.run(self.get_camera_information_for_windows())
for camera_index in camera_indexes:
camera_name = cameras_info_windows.get_at(camera_index).name.replace('\n', '')
cameras.append({'camera_index': camera_index, 'camera_name': camera_name})
return cameras
if platform_name == 'Linux':
for camera_index in camera_indexes:
camera_name = subprocess.run(['cat', '/sys/class/video4linux/video{}/name'.format(camera_index)],
stdout=subprocess.PIPE).stdout.decode('utf-8')
camera_name = camera_name.replace('\n', '')
cameras.append({'camera_index': camera_index, 'camera_name': camera_name})
return cameras
async def get_camera_information_for_windows(self):
return await windows_devices.DeviceInformation.find_all_async(VIDEO_DEVICES)
camera = Camera()
connected_cameras = asyncio.run(camera.get_camera_information_for_windows())
names = [camera.name for camera in connected_cameras]
print(names)