-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera_utils.py
202 lines (172 loc) · 5.64 KB
/
camera_utils.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
"""TBW."""
from datetime import datetime
from io import BytesIO
import logging
import os
from time import sleep
import numpy as np
from PIL import Image
import cv2
logger = logging.getLogger(__name__)
def convert_raw_image(raw):
"""TBW."""
data = cv2.cvtColor(raw, cv2.COLOR_BGR2RGB)
im = Image.fromarray(data)
mem_file = BytesIO()
im.save(mem_file, 'JPEG')
return mem_file.getvalue()
class SimCam:
"""TBW."""
counter = 0
colors = [0.5, 0.75, 1.0]
@staticmethod
def simulate_image(w, h, dtype='uint16', shift=0, rgb=False):
"""Generate a 2d array of concentric circles.
:param int w: the width in pixels
:param int h: the height in pixels.
:param str dtype: the pixel value data type
:param int shift: the number of pixels to shift the final image.
:param bool rgb: return a RGB (color) array.
:return: a 2d numpy array of type uint8
:rtype: ndarray
"""
dx = 20.0 / w
dy = 20.0 / h
x = np.arange(-10, 10, dx)
y = np.arange(-10, 10, dy)
xx, yy = np.meshgrid(x, y, sparse=True)
z = np.sin(xx ** 2 + yy ** 2) / (xx ** 2 + yy ** 2)
z = np.nan_to_num(z)
if z.min() < 0:
z -= z.min()
if dtype == 'uint16':
z = (z / z.max()) * ((1 << 12) - 1)
elif dtype == 'uint8':
z = (z / z.max()) * ((1 << 8) - 1)
z = z.astype(dtype)
if shift > 0:
z = np.roll(z, shift, axis=1)
if rgb:
SimCam.colors = np.array(SimCam.colors)
SimCam.colors += 0.025
SimCam.colors %= 1.0
r = (z.astype(float) * SimCam.colors[0]).astype(dtype)
g = (z.astype(float) * SimCam.colors[1]).astype(dtype)
b = (z.astype(float) * SimCam.colors[2]).astype(dtype)
return np.dstack((r, g, b))
else:
return z
def read(self):
"""TBW."""
SimCam.counter += 1
data = SimCam.simulate_image(640, 480,
dtype='uint8',
shift=SimCam.counter,
rgb=True)
return True, data
def read_image(self):
"""TBW."""
ok, raw = self.read()
return convert_raw_image(raw)
class WebCam(cv2.VideoCapture):
"""TBW."""
def __init__(self, prop_id=0):
"""TBW."""
logger.info('camera init')
self._id = prop_id
super().__init__()
def open(self):
"""ST."""
logger.info('camera open')
super().open(self._id)
def release(self):
"""ST."""
logger.info('camera release')
super().release()
def read(self):
"""TBW."""
return super().read()
def read_image(self):
"""TBW."""
ok, raw = self.read()
return convert_raw_image(raw)
def save_image(self, path):
ret, raw = self.read()
if not ret:
print("Can't receive frame (stream end?). Exiting ...")
pass
data = cv2.cvtColor(raw, cv2.COLOR_BGR2RGB)
im = Image.fromarray(data)
date = datetime.now()
filename = f'image_{date}.jpg'
pathname = f'{path}/{filename}'
im.save(pathname, 'JPEG')
#image = super.resize(image, (self.w, self.h))
# date = datetime.now()
# filename = f'{path}/image_{date}.jpg'
# cv2.imwrite('../photo/img_date.jpg', image)
return filename
class UsbCamera(object):
""" Init camera """
def __init__(self):
# select first video device in system
self.cam = cv2.VideoCapture('/dev/video2')
# set camera resolution
self.w = 800
self.h = 600
# set crop factor
self.cam.set(cv2.CAP_PROP_FRAME_HEIGHT, self.h)
self.cam.set(cv2.CAP_PROP_FRAME_WIDTH, self.w)
def set_resolution(self, new_w, new_h):
"""
functionality: Change camera resolution
inputs: new_w, new_h - with and height of picture, must be int
returns: None ore raise exception
"""
if isinstance(new_h, int) and isinstance(new_w, int):
# check if args are int and correct
if (new_w <= 800) and (new_h <= 600) and \
(new_w > 0) and (new_h > 0):
self.h = new_h
self.w = new_w
else:
# bad params
raise Exception('Bad resolution')
else:
# bad params
raise Exception('Not int value')
def get_image(self):
"""
functionality: Gets frame from camera.
:return: frame
"""
success, image = self.cam.read()
if success:
# scale image
image = cv2.resize(image, (self.w, self.h))
else:
image = np.zeros((self.h, self.w, 3), np.uint8)
cv2.putText(image, 'No camera', (40, 60), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 1)
return image
def get_frame(self):
image = self.get_image()
# encoding picture to jpeg
ret, jpeg = cv2.imencode('.jpg', image)
return jpeg.tobytes()
def save_image(self):
image = self.get_image()
date = datetime.now()
filename = './image_{}.jpg'
cv2.imwrite(filename.format(date), image)
return filename.format(date)
if __name__ == "__main__":
cam = WebCam()
cam.open()
path2 = os.path.dirname(os.path.realpath(__file__)) + '/../photos'
pathname=cam.save_image(path2)
print(pathname)
for i in range(100):
cam.read_image()
cam.release()
print('sleep')
sleep(2)