revert threads to thread, results to color_image in OpenCVCamera
This commit is contained in:
parent
d83d34d9b3
commit
72b2e342ae
|
@ -98,7 +98,7 @@ class OpenCVCamera:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
camera = OpenCVCamera(2)
|
camera = OpenCVCamera(2)
|
||||||
color_image = camera.capture_image()
|
color_image = camera.read()
|
||||||
```
|
```
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
@ -124,8 +124,8 @@ class OpenCVCamera:
|
||||||
|
|
||||||
self.camera = None
|
self.camera = None
|
||||||
self.is_connected = False
|
self.is_connected = False
|
||||||
self.threads = {}
|
self.thread = None
|
||||||
self.results = {}
|
self.color_image = None
|
||||||
self.logs = {}
|
self.logs = {}
|
||||||
|
|
||||||
def connect(self):
|
def connect(self):
|
||||||
|
@ -212,31 +212,31 @@ class OpenCVCamera:
|
||||||
|
|
||||||
def read_loop(self):
|
def read_loop(self):
|
||||||
while True:
|
while True:
|
||||||
self.results["color_image"] = self.read()
|
self.color_image = self.read()
|
||||||
|
|
||||||
def async_read(self):
|
def async_read(self):
|
||||||
if "read" not in self.threads:
|
if self.thread is None:
|
||||||
self.threads["read"] = Thread(target=self.read_loop, args=())
|
self.thread = Thread(target=self.read_loop, args=())
|
||||||
self.threads["read"].daemon = True
|
self.thread.daemon = True
|
||||||
self.threads["read"].start()
|
self.thread.start()
|
||||||
|
|
||||||
num_tries = 0
|
num_tries = 0
|
||||||
while "color_image" not in self.results:
|
while self.color_image is None:
|
||||||
num_tries += 1
|
num_tries += 1
|
||||||
time.sleep(1/self.fps)
|
time.sleep(1/self.fps)
|
||||||
if num_tries > self.fps:
|
if num_tries > self.fps:
|
||||||
if self.threads["read"].ident is None and not self.threads["read"].is_alive():
|
if self.thread.ident is None and not self.thread.is_alive():
|
||||||
raise Exception("The thread responsible for `self.async_read()` took too much time to start. There might be an issue. Verify that `self.threads[\"read\"].start()` has been called.")
|
raise Exception("The thread responsible for `self.async_read()` took too much time to start. There might be an issue. Verify that `self.thread.start()` has been called.")
|
||||||
|
|
||||||
return self.results["color_image"] #, self.logs["timestamp_utc"]
|
return self.color_image
|
||||||
|
|
||||||
def disconnect(self):
|
def disconnect(self):
|
||||||
if getattr(self, "camera", None):
|
if getattr(self, "camera", None):
|
||||||
self.camera.release()
|
self.camera.release()
|
||||||
for name in self.threads:
|
if self.thread is not None:
|
||||||
if self.threads[name].is_alive():
|
if self.thread.is_alive():
|
||||||
# wait for the thread to finish
|
# wait for the thread to finish
|
||||||
self.threads[name].join()
|
self.thread.join()
|
||||||
|
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
self.disconnect()
|
self.disconnect()
|
||||||
|
|
Loading…
Reference in New Issue