-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
46 lines (40 loc) · 1.54 KB
/
util.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
import numpy as np
import cv2
import socket
def clamp(value, min_val, max_val):
return min(max(value, min_val), max_val)
def clamp_np(value, min_val, max_val):
return np.minimum(np.maximum(value, min_val), max_val)
def list_camera_ports():
"""
Test the ports and returns a tuple with the available ports and the ones that are working.
Credits to this stackoverflow answer: https://stackoverflow.com/questions/57577445/list-available-cameras-opencv-python
"""
non_working_ports = []
dev_port = 0
working_ports = []
available_ports = []
while len(non_working_ports) < 6: # if there are more than 5 non working ports stop the testing.
camera = cv2.VideoCapture(dev_port)
if not camera.isOpened():
non_working_ports.append(dev_port)
print("Port %s is not working." %dev_port)
else:
is_reading, img = camera.read()
w = camera.get(3)
h = camera.get(4)
if is_reading:
print("Port %s is working and reads images (%s x %s)" %(dev_port,h,w))
working_ports.append(dev_port)
else:
print("Port %s for camera ( %s x %s) is present but does not reads." %(dev_port,h,w))
available_ports.append(dev_port)
dev_port +=1
camera.release()
return available_ports,working_ports,non_working_ports
def get_ip():
hostname = socket.gethostname()
ip_addr = socket.gethostbyname(hostname)
return ip_addr
if __name__ == '__main__':
list_camera_ports()