forked from pvys/CV-camera-finder
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOpenSelectedCamera.py
76 lines (62 loc) · 2.07 KB
/
OpenSelectedCamera.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
74
75
76
# Setting CameraIp
import imp
from pymf import get_MF_devices
import cv2
import json
import threading
class CaptureThread(threading.Thread):
def __init__(self,id,name):
threading.Thread.__init__(self)
self.id = id
self.name = name
def run(self):
video = cv2.VideoCapture(self.id, cv2.CAP_DSHOW)
while (video.isOpened):
ret, frame = video.read()
if ret:
cv2.imshow(self.name, frame)
# Wait until a key is pressed.
# Retreive the ASCII code of the key pressed
k = cv2.waitKey(1) & 0xFF
# Check if 'ESC' is pressed.
if(k == 27):
# Break the loop.
break
if (g_thread_exit_flag):
break
# Release the VideoCapture object.
video.release()
def SelectIpList(cameraName,ipList):
index = 0
print('Please choose the ip:',cameraName)
for ip in ipList:
print('{}:{}'.format(index,ip))
index=index+1
try:
selectIndex = int((input('choose the number(default is 0):'.format(len(ipList) - 1))))
except:
selectIndex = 0
return selectIndex
if __name__ == '__main__':
fileUrl = 'cameraSetting.json'
jsonData = {}
ipList = []
device_list = get_MF_devices()
g_thread_exit_flag = False
for i in range(len(device_list)):
ipList.append(str(201+i))
for cv_index, device_name in enumerate(device_list):
g_thread_exit_flag = False
cap = CaptureThread(cv_index, device_name)
cap.start()
selectIndex = SelectIpList(device_name, ipList)
while (selectIndex < 0 or selectIndex >= len(ipList)):
selectIndex = SelectIpList(device_name,ipList)
jsonData[ipList[selectIndex]] = cv_index
ipList.remove(ipList[selectIndex])
g_thread_exit_flag = True
cap.join()
with open(fileUrl, 'w') as f:
print('write to {}-->'.format(fileUrl), jsonData)
json.dump(jsonData, f)
cv2.destroyAllWindows()