-
-
Save danielballan/912d50f3ae6d3f449d5feb66d9cfe43c to your computer and use it in GitHub Desktop.
Trigger laptop camera
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
import matplotlib.pyplot as plt | |
from ophyd import DeviceStatus | |
import queue | |
import weakref | |
class LaptopCamera: | |
def __init__(self, cam_id=0): | |
self.cam_id = cam_id | |
# There must be some "lazy" way to initialize it | |
self.cap = cv2.VideoCapture(cam_id) | |
self.cap.release() | |
self.image = None | |
# This loop will run on a thread for the lifetime of the | |
# instance, waiting for the device to be triggered and processing | |
# each trigger in the background. | |
self._status_queue = queue.Queue() | |
def capture_loop(): | |
while True: | |
status = self.status_queue.get() | |
if status == 'POISON_PILL': | |
break | |
self.cap.open(self.cam_id) | |
self.image = self.cap.read()[1] | |
self.cap.release() | |
status._finished() | |
threading.Thread(target=capture_loop).start() | |
def trigger(self): | |
status = DeviceStatus(self) | |
self._status_queue.put(status) | |
return status | |
def __del__(self): | |
# Kill the capture_loop thread. | |
self._status_queue.put('POISON_PILL') | |
if __name__ == '__main__': | |
lc = LaptopCamera() | |
lc.trigger() | |
lc.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There is an issue: