-
Notifications
You must be signed in to change notification settings - Fork 0
/
camerawidget.py
233 lines (179 loc) · 7.73 KB
/
camerawidget.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import logging
from functools import partial
from kivy import platform
from kivy.clock import Clock
from kivy.core.window import Window
from kivy.graphics import Color, Rectangle
from kivy.graphics.context_instructions import PushMatrix, PopMatrix, Rotate
from kivy.uix.stencilview import StencilView
from kivy.properties import (
ObjectProperty, StringProperty, ListProperty, BooleanProperty, NumericProperty, OptionProperty)
from camera2.camera2 import PyCameraInterface
if platform == "android":
from android.permissions import request_permission, check_permission, Permission
logger = logging.getLogger(__file__)
logger.setLevel(logging.DEBUG)
handler = logging.StreamHandler()
handler.setLevel(logging.DEBUG)
logger.addHandler(handler)
class CameraDisplayWidget(StencilView):
texture = ObjectProperty(None, allownone=True)
resolution = ListProperty([1, 1])
tex_coords = ListProperty([0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0])
correct_camera = BooleanProperty(False)
_rect_pos = ListProperty([0, 0])
_rect_size = ListProperty([1, 1])
camera_resolution = ListProperty([1920, 1080])
current_camera = ObjectProperty(None, allownone=True)
camera_display_widget = ObjectProperty(None, allownone=True)
cameras_to_use = ListProperty()
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.camera_interface = PyCameraInterface()
self.debug_print_camera_info()
self.inspect_cameras()
# Rotaciona a imagem no Android
with self.canvas.before:
PushMatrix()
Rotate(angle=-90, origin=self.center)
with self.canvas.after:
PopMatrix()
self.bind(
pos=self._update_rect,
size=self._update_rect,
resolution=self._update_rect,
texture=self._update_rect,
)
logger.info("*** CameraDisplayWidget.__init__()...")
self.restart_stream()
def inspect_cameras(self):
cameras = self.camera_interface.cameras
for camera in cameras:
if camera.facing == "BACK":
self.cameras_to_use.append(camera)
for camera in cameras:
if camera.facing == "FRONT":
self.cameras_to_use.append(camera)
def rotate_cameras(self):
logger.info("rotate_cameras acionado...")
self.ensure_camera_closed()
self.cameras_to_use = self.cameras_to_use[1:] + [self.cameras_to_use[0]]
self.attempt_stream_camera(self.cameras_to_use[0])
def restart_stream(self):
self.ensure_camera_closed()
Clock.schedule_once(self._restart_stream, 0)
def _restart_stream(self, dt):
# logger.info("On restart, state is {}".format(self.camera_permission_state))
self.attempt_stream_camera(self.cameras_to_use[0])
def debug_print_camera_info(self):
cameras = self.camera_interface.cameras
camera_infos = ["Camera ID {}, facing {}".format(c.camera_id, c.facing) for c in cameras]
for camera in cameras:
logger.info("Camera ID {}, facing {}, resolutions {}".format(
camera.camera_id, camera.facing, camera.supported_resolutions))
def stream_camera_index(self, index):
self.attempt_stream_camera(self.camera_interface.cameras[index])
def attempt_stream_camera(self, camera):
"""Start streaming from the given camera, if we have the CAMERA
permission, otherwise request the permission first.
"""
self.stream_camera(camera)
def stream_camera(self, camera):
resolution = self.select_resolution(
Window.size,
camera.supported_resolutions,
# best=(1920, 1080)
best=(720, 720)
# best=(320, 240),
)
if resolution is None:
logger.error(f"Found no good resolution in {camera.supported_resolutions} for Window.size {Window.size}")
return
else:
logger.info(f"Chose resolution {resolution} from choices {camera.supported_resolutions}")
self.camera_resolution = resolution
camera.open(callback=self._stream_camera_open_callback)
def _stream_camera_open_callback(self, camera, action):
if action == "OPENED":
logger.info("Camera opened, preparing to start preview")
Clock.schedule_once(partial(self._stream_camera_start_preview, camera), 0)
else:
logger.info("Ignoring camera event {action}")
def _stream_camera_start_preview(self, camera, *args):
logger.info("Starting preview of camera {camera}")
if camera.facing == "FRONT":
self.correct_camera = True
else:
self.correct_camera = False
self.texture = camera.start_preview(tuple(self.camera_resolution))
with self.canvas:
Color(1, 1, 1, 1)
Rectangle(
pos=self._rect_pos,
size=self._rect_size,
texture=self.texture,
tex_coords=self.tex_coords
)
self.current_camera = camera
def select_resolution(self, window_size, resolutions, best=None):
if best in resolutions:
return best
if not resolutions:
return None
win_x, win_y = window_size
larger_resolutions = [(x, y) for (x, y) in resolutions if (x > win_x and y > win_y)]
if larger_resolutions:
return min(larger_resolutions, key=lambda r: r[0] * r[1])
smaller_resolutions = resolutions # if we didn't find one yet, all are smaller than the requested Window size
return max(smaller_resolutions, key=lambda r: r[0] * r[1])
def ensure_camera_closed(self):
if self.current_camera is not None:
self.current_camera.close()
self.current_camera = None
def on_pause(self):
logger.info("Closing camera due to pause")
self.ensure_camera_closed()
return super().on_pause()
def on_resume(self):
logger.info("Opening camera due to resume")
self.restart_stream()
def on_correct_camera(self, instance, correct):
logger.info("Correct became", correct)
if correct:
self.tex_coords = [1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0]
logger.info("Set 0!")
else:
self.tex_coords = [0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0]
logger.info("Set 1!")
def on_tex_coords(self, instance, value):
logger.info("tex_coords became", self.tex_coords)
def _update_rect(self, *args):
self._update_rect_to_fill()
def _update_rect_to_fit(self, *args):
logger.info('*** _update_rect_to_fit')
w, h = self.resolution
aspect_ratio = h / w
aspect_width = self.width
aspect_height = self.width * h / w
if aspect_height > self.height:
aspect_height = self.height
aspect_width = aspect_height * w / h
aspect_height = int(aspect_height)
aspect_width = int(aspect_width)
self._rect_pos = [self.center_x - aspect_width / 2,
self.center_y - aspect_height / 2]
self._rect_size = [aspect_width, aspect_height]
def _update_rect_to_fill(self, *args):
logger.info('*** _update_rect_to_fit')
w, h = self.resolution
aspect_ratio = h / w
aspect_width = self.width
aspect_height = self.width * h / w
if aspect_height < self.height:
aspect_height = self.height
aspect_width = aspect_height * w / h
aspect_height = int(aspect_height)
aspect_width = int(aspect_width)
self._rect_pos = [self.center_x - aspect_width / 2,
self.center_y - aspect_height / 2]
self._rect_size = [aspect_width, aspect_height]