File size: 1,349 Bytes
773f76d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from typing import List

import cv2

from facefusion.types import CameraPoolSet

CAMERA_POOL_SET : CameraPoolSet =\
{
	'capture': {}
}


def get_local_camera_capture(camera_id : int) -> cv2.VideoCapture:
	camera_key = str(camera_id)

	if camera_key not in CAMERA_POOL_SET.get('capture'):
		camera_capture = cv2.VideoCapture(camera_id)

		if camera_capture.isOpened():
			CAMERA_POOL_SET['capture'][camera_key] = camera_capture

	return CAMERA_POOL_SET.get('capture').get(camera_key)


def get_remote_camera_capture(camera_url : str) -> cv2.VideoCapture:
	if camera_url not in CAMERA_POOL_SET.get('capture'):
		camera_capture = cv2.VideoCapture(camera_url)

		if camera_capture.isOpened():
			CAMERA_POOL_SET['capture'][camera_url] = camera_capture

	return CAMERA_POOL_SET.get('capture').get(camera_url)


def clear_camera_pool() -> None:
	for camera_capture in CAMERA_POOL_SET.get('capture').values():
		camera_capture.release()

	CAMERA_POOL_SET['capture'].clear()


def detect_local_camera_ids(id_start : int, id_end : int) -> List[int]:
	local_camera_ids = []

	for camera_id in range(id_start, id_end):
		cv2.utils.logging.setLogLevel(0)
		camera_capture = get_local_camera_capture(camera_id)
		cv2.utils.logging.setLogLevel(3)

		if camera_capture and camera_capture.isOpened():
			local_camera_ids.append(camera_id)

	return local_camera_ids