-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera.py
More file actions
322 lines (268 loc) · 11.9 KB
/
camera.py
File metadata and controls
322 lines (268 loc) · 11.9 KB
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import logging
import threading
from os import path, makedirs
from datetime import datetime
from time import sleep, time
from ctypes import sizeof, c_float
import numpy as np
from numpy.typing import NDArray
import cv2
import lmfit
from lmfit.lineshapes import gaussian2d
from scipy.stats import skew, norm
#import debugpy
from PyQt6.QtCore import pyqtSignal, pyqtSlot, QObject, QThread, QTimer, QMutex
from PyQt6.QtWidgets import QApplication
USE_FAKE_DATA = False
class Camera_Search(QObject):
result = pyqtSignal(list)
finished = pyqtSignal()
def __init__(self, skip_idxs=[]):
QObject.__init__(self)
self.skip_idxs = skip_idxs
self.q_thread : QThread = QThread()
self.q_thread.setObjectName(f"Cam_Search")
self.moveToThread(self.q_thread)
self.q_thread.finished.connect(self.q_thread.deleteLater)
self.finished.connect(self.deleteLater)
self.finished.connect(self.q_thread.quit)
self.q_thread.started.connect(self.findCameras)
def findCameras(self):
#https://stackoverflow.com/a/61768256
# checks the first 10 indexes.
# debugpy.debug_this_thread()
index = 0
arr = []
i = 10
while i > 0:
if index in self.skip_idxs:
arr.append(index)
else:
try:
cap = cv2.VideoCapture(index)
if cap.read()[0]:
arr.append(index)
cap.release()
except cv2.error as e:
if "Camera index out of range" in str(e):
pass
except Exception as e:
logging.warning(f"Error opening camera {index}: {type(e)} {e}")
index += 1
i -= 1
self.result.emit(arr)
self.finished.emit()
class USB_Camera(QObject):
ready_sig = pyqtSignal(int, bool)
status_sig = pyqtSignal(int, str)
stats_sig = pyqtSignal(int, dict)
finished_sig = pyqtSignal(int)
update_image_sig = pyqtSignal(int)
def __init__(self, camera_index:int, save_images=True, save_path="."):
QObject.__init__(self)
self.camera_index = camera_index
self.camera_type = ""
self.active = False
self.acquiring = False
self.last_frame_time = time()
self.serial = "N/A"
self.save_images = save_images
self.save_path = save_path
self.save_png = False
#For testing only
if USE_FAKE_DATA:
self.fake_update = 0
self.fake_center_x = 0
self.fake_center_y = 0
self.fake_sig_x = 20
self.fake_sig_y = 0
self.q_thread : QThread = QThread()
self.q_thread.setObjectName(f"Cam_{camera_index}")
self.moveToThread(self.q_thread)
self.q_thread.finished.connect(self.threadFinished)
@pyqtSlot()
def init(self):
threading.current_thread().name = QThread.currentThread().objectName() #fix names
# debugpy.debug_this_thread()
self.status_sig.emit(self.camera_index, "Starting")
self.cam = cv2.VideoCapture()
self.cam.setExceptionMode(True)
try:
self.cam.open(self.camera_index)
self.width = int(self.cam.get(cv2.CAP_PROP_FRAME_WIDTH))
self.height = int(self.cam.get(cv2.CAP_PROP_FRAME_HEIGHT))
self.img = np.empty((self.width, self.height), dtype=np.uint8)
logging.info(f"Started camera {self.camera_index}.")
self.active = True
self.ready_sig.emit(self.camera_index, True)
self.status_sig.emit(self.camera_index, "Running")
if not self.acquiring:
self.acquiring = True
self.stats = Camera_Stats(self.camera_index, self.img.T) #Note the transpose!
self.stats.stats_sig.connect(self.stats_sig)
while self.active:
ret, img = self.cam.read()
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# For testing only
if USE_FAKE_DATA:
# img = np.zeros_like(img)
if self.fake_update % 180 == 0:
self.fake_center_x += img.shape[1] / 10
self.fake_center_y += img.shape[0] / 10
self.fake_sig_x += 10
self.fake_sig_y += 15
x_sub = np.arange(0, img.shape[1])
y_sub = np.arange(0, img.shape[0])
x, y = np.meshgrid(x_sub, y_sub)
self.fake_gauss = gaussian2d(x, y, amplitude=1, centerx=self.fake_center_x, centery=self.fake_center_y, sigmax=abs(self.fake_sig_x), sigmay=abs(self.fake_sig_y))
self.fake_gauss = (self.fake_gauss / np.max(self.fake_gauss)) * 255
self.fake_gauss += np.random.normal(0, 25, self.fake_gauss.shape)
img = np.clip(self.fake_gauss + (img * 0.5), 0, 255).astype(np.uint8)
self.fake_update += 1
np.copyto(self.img, img.T)
self.update_image_sig.emit(self.camera_index)
#stats - update when processing thread is ready
if self.stats.mutex.tryLock():
self.stats.history["Minimum"] += [np.min(img)]
self.stats.history["Maximum"] += [np.max(img)]
self.stats.history["Mean"] += [np.mean(img)]
self.stats.history["frame_count"] = self.stats.history["frame_count"] + 1
self.stats.history["sums"] += img
self.stats.mutex.unlock()
QApplication.processEvents()
self.stats.stop()
self.acquiring = False
except Exception as e:
logging.error(f"Error starting camera {self.camera_index}: {type(e)} {e}")
try:
self.cam.release()
except Exception:
pass
self.status_sig.emit(self.camera_index, "Error")
self.ready_sig.emit(self.camera_index, False)
@pyqtSlot()
def stop(self):
self.status_sig.emit(self.camera_index, "Stopping")
self.active = False
#self.acq_timer.stop()
self.cam.release()
logging.info(f"Stopped camera {self.camera_index}.")
self.width = 0
self.height = 0
self.serial = ""
self.status_sig.emit(self.camera_index, "Stopped")
@pyqtSlot()
def shutdown(self):
if self.active:
self.stop()
self.status_sig.emit(self.camera_index, "Shutting Down")
try:
del self.cam
except AttributeError:
pass
self.status_sig.emit(self.camera_index, "Standby")
self.finished_sig.emit(self.camera_index)
self.q_thread.quit()
def threadFinished(self):
del self.q_thread
@pyqtSlot(str)
def setSaveOpts(self, save_path):
self.save_path = save_path
def getTypeString(self):
return str(self.camera_index)
class Camera_Stats(QObject):
stats_sig = pyqtSignal(int, dict, dict)
def __init__(self, camera_index: int, img: NDArray):
QObject.__init__(self)
self.camera_index = camera_index
self.frame_rate = 15
self.img_shape = img.shape
self.mutex = QMutex()
self.stats = {"Gaussian":{}}
self.plots = {}
self.history = {}
self.fake_center_x = 0
self.fake_center_y = 0
self.fake_sig_x = -50
self.fake_sig_y = -50
self.q_thread : QThread = QThread()
self.q_thread.setObjectName(f"Stats_Cam_{camera_index}")
self.moveToThread(self.q_thread)
self.q_thread.started.connect(self.init)
self.q_thread.finished.connect(self.threadFinished)
self.q_thread.start()
@pyqtSlot()
def init(self):
threading.current_thread().name = QThread.currentThread().objectName() #fix names
# debugpy.debug_this_thread()
self.resetStats()
self.stats_timer = QTimer()
self.stats_timer.setInterval(500)
self.stats_timer.timeout.connect(self.updateStats)
self.stats_timer.start()
logging.info(f"Started stats for camera {self.camera_index}.")
@pyqtSlot()
def updateStats(self):
# debugpy.debug_this_thread()
self.mutex.lock()
try:
self.stats["Minimum"] = np.min(self.history["Minimum"])
self.stats["Maximum"] = np.max(self.history["Maximum"])
self.stats["Mean"] = np.mean(self.history["Mean"]) #NON-GENERALIZABLE STATS WARNING: ONLY ALLOWED BECAUSE ALL SAMPLES ARE IDENTICAL IN SIZE!
self.stats['Frame Rate'] = self.history["frame_count"] / (self.stats_timer.interval() / 1000)
img_means : NDArray = np.copy(self.history["sums"] / self.history["frame_count"])
except ValueError:
# this can occur during intialization if threads are out of sync
img_means = np.zeros(self.img_shape, dtype=float)
pass
self.resetStats()
self.mutex.unlock()
subsampling = 2
model = lmfit.models.Gaussian2dModel()
x_sub = np.arange(0, img_means.shape[1], subsampling)
y_sub = np.arange(0, img_means.shape[0], subsampling)
z_sub = img_means[::subsampling,::subsampling]
# x_sub = np.arange(0, img_means.shape[1])
# y_sub = np.arange(0, img_means.shape[0])
# z_sub = img_means
x, y = np.meshgrid(x_sub, y_sub)
# For testing only
# self.fake_center_x += img_means.shape[0] / 10
# self.fake_center_y += img_means.shape[1] / 10
# self.fake_sig_x += 10
# self.fake_sig_y += 10
# z_sub = gaussian2d(x, y, amplitude=30, centerx=self.fake_center_x, centery=self.fake_center_y, sigmax=abs(self.fake_sig_x), sigmay=abs(self.fake_sig_y))
params = model.guess(z_sub.flatten(), x.flatten(), y.flatten())
result = model.fit(z_sub, x=x, y=y, calc_covar=False, params=params, max_nfev=5000)
x_in_range = (result.params["centerx"].value > (img_means.shape[1] * -0.2)) and \
(result.params["centerx"].value < (img_means.shape[1] * 1.2))
y_in_range = (result.params["centery"].value > (img_means.shape[0] * -0.2)) and \
(result.params["centery"].value < (img_means.shape[0] * 1.2))
if result.rsquared > 0.5 and x_in_range and y_in_range and result.nfev < 5000:
self.stats["Gaussian"]["Center X"] = result.params["centerx"].value
self.stats["Gaussian"]["Center Y"] = result.params["centery"].value
self.stats["Gaussian"]["Sigma X"] = result.params["sigmax"].value
self.stats["Gaussian"]["Sigma Y"] = result.params["sigmay"].value
else:
self.stats["Gaussian"]["Center X"] = ""
self.stats["Gaussian"]["Center Y"] = ""
self.stats["Gaussian"]["Sigma X"] = ""
self.stats["Gaussian"]["Sigma Y"] = ""
self.stats["Gaussian"]["R^2"] = result.rsquared
self.stats["Gaussian"]["Iterations"] = result.nfev
self.stats_sig.emit(self.camera_index, self.stats, self.plots)
def resetStats(self):
self.history["Minimum"] = []
self.history["Maximum"] = []
self.history["Mean"] = []
self.history["sums"] = np.zeros(self.img_shape, dtype=float)
self.history["x_sums"] = []
self.history["y_sums"] = []
self.history["frame_count"] = 0
def stop(self):
self.stats_timer.stop()
logging.info(f"Stopped stats for camera {self.camera_index}.")
self.q_thread.quit()
self.deleteLater()
def threadFinished(self):
del self.q_thread