-
Notifications
You must be signed in to change notification settings - Fork 1
/
connect_with_jpeg_4.py
148 lines (117 loc) · 4.31 KB
/
connect_with_jpeg_4.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
'''
Copyright 2022 i-PRO Co., Ltd.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
'''
'''
[Abstract]
Try connecting to an i-PRO camera with JPEG(1 shot).
JPEG(1 shot) で i-PRO カメラと接続してみる
[Details]
Save the received JPEG image as a file.
Add a 6-digit number to the end of the file name and save it as a serial number.
受信した JPEG 画像をファイル保存します。
ファイル名の末尾に6ケタの番号を付けて連番で保存します。
[Author]
kinoshita hidetoshi (木下英俊)
[Library install]
cv2: pip install opencv-python
numpy: pip install numpy
requests: pip install requests
'''
import requests
from requests.auth import HTTPDigestAuth
import numpy as np
import cv2
import os
user_id = "user-id" # Change to match your camera setting
user_pw = "password" # Change to match your camera setting
host = "192.168.0.10" # Change to match your camera setting
winname = "VIDEO" # Window title
resolution = 1920 # Resolution
pathOut = 'image' # Image file save folder name
# URL
url = f"http://{host}/cgi-bin/camera?resolution={resolution}"
# Exception 定義
BackendError = type('BackendError', (Exception,), {})
def IsWindowVisible(winname):
'''
[Abstract]
Check if the target window exists.
対象ウィンドウが存在するかを確認する。
[Param]
winname : Window title
[Return]
True : exist
存在する
False : not exist
存在しない
[Exception]
BackendError :
'''
try:
ret = cv2.getWindowProperty(winname, cv2.WND_PROP_VISIBLE)
if ret == -1:
raise BackendError('Use Qt as backend to check whether window is visible or not.')
return bool(ret)
except cv2.error:
return False
def SaveBinaryData(data, filename):
'''
[Abstract]
バイナリデータを指定ファイル名で保存する
[Param]
data : 保存するバイナリデータ
filename : ファイル名
'''
fout = open(filename, 'wb')
fout.write(data)
fout.close()
if __name__ == '__main__':
'''
[Abstract]
main function
'''
initialized = False
count = 0
if not os.path.exists(pathOut):
os.mkdir(pathOut)
while True:
try:
# Request and receive image from camera.
rs = requests.get(url, auth=HTTPDigestAuth(user_id, user_pw))
# Save jpeg file.
count += 1
filename = os.path.join(pathOut, 'image_{:06d}.jpg'.format(count))
SaveBinaryData(rs.content, filename)
# Convert from binary to ndarray.
img_buf= np.frombuffer(rs.content, dtype=np.uint8)
# Convert from ndarray to OpenCV image.
img1 = cv2.imdecode(img_buf, cv2.IMREAD_UNCHANGED)
# Please modify the value to fit your PC screen size.
img2 = cv2.resize(img1, (1280, 720))
# Display video.
cv2.imshow(winname, img2)
if initialized==False:
# Specify window position only once at startup.
cv2.moveWindow(winname, 100, 100)
initialized = True
# Press the "q" key to finish.
k = cv2.waitKey(1) & 0xff # necessary to display the video by imshow ()
if k == ord("q"):
break
# Exit the program if there is no specified window.
if not IsWindowVisible(winname):
break
except KeyboardInterrupt:
# Press '[ctrl] + [c]' on the console to exit the program.
print("KeyboardInterrupt")
break
cv2.destroyAllWindows()