Skip to content

Commit

Permalink
Send screenshot and webcam feed asynchronous
Browse files Browse the repository at this point in the history
  • Loading branch information
bkbilly committed May 26, 2024
1 parent 520c3f0 commit ea40686
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 15 deletions.
4 changes: 2 additions & 2 deletions lnxlink/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def run_module(self, name, method):

try:
start_time = time.time()
if isinstance(method, (dict, list, bool)):
if isinstance(method, (dict, list, bool, bytes)):
pub_data = method
else:
pub_data = method()
Expand Down Expand Up @@ -337,7 +337,7 @@ def setup_discovery_entities(self, addon, service, exp_name, options):
}
control_name_topic = exp_name.lower().replace(" ", "_")
subtopic = addon.name.lower().replace(" ", "_")
if "method" in options:
if "method" in options or options.get("subtopic", False):
subcontrol = exp_name.lower().replace(" ", "_")
subtopic = f"{subtopic}/{subcontrol}"
state_topic = f"{self.pref_topic}/monitor_controls/{subtopic}"
Expand Down
23 changes: 16 additions & 7 deletions lnxlink/modules/screenshot.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Shows an image of the desktop as a camera entity"""
import base64
from threading import Thread
from .scripts.helpers import import_install_package


Expand All @@ -8,9 +9,11 @@ class Addon:

def __init__(self, lnxlink):
"""Setup addon"""
self.lnxlink = lnxlink
self.name = "Screenshot"
self.run = False
self._requirements()
self.read_thr = None

def _requirements(self):
self.lib = {
Expand All @@ -23,12 +26,12 @@ def get_camera_frame(self):
"""Convert screen image to Base64 text"""
if self.run:
with self.lib["mss"].mss() as sct:
sct_img = sct.grab(sct.monitors[1])
frame = self.lib["np"].array(sct_img)
_, buffer = self.lib["cv2"].imencode(".jpg", frame)
frame = base64.b64encode(buffer)
return frame
return None
while True:
sct_img = sct.grab(sct.monitors[1])
frame = self.lib["np"].array(sct_img)
_, buffer = self.lib["cv2"].imencode(".jpg", frame)
frame = base64.b64encode(buffer)
self.lnxlink.run_module(f"{self.name}/Screenshot feed", frame)

def get_info(self):
"""Gather information from the system"""
Expand All @@ -44,14 +47,20 @@ def exposed_controls(self):
},
"Screenshot feed": {
"type": "camera",
"method": self.get_camera_frame,
"encoding": "b64",
"subtopic": True,
},
}

def start_control(self, topic, data):
"""Control system"""
if data.lower() == "off":
self.run = False
if self.read_thr is not None:
self.read_thr.join()
self.read_thr = None
elif data.lower() == "on":
self.run = True
if self.read_thr is None:
self.read_thr = Thread(target=self.get_camera_frame, daemon=True)
self.read_thr.start()
21 changes: 15 additions & 6 deletions lnxlink/modules/webcam.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Shows an image from the webcamera"""
import base64
from threading import Thread
from .scripts.helpers import import_install_package


Expand All @@ -8,9 +9,11 @@ class Addon:

def __init__(self, lnxlink):
"""Setup addon"""
self.lnxlink = lnxlink
self.name = "Webcam"
self.vid = None
self._requirements()
self.read_thr = None

def _requirements(self):
self.lib = {
Expand All @@ -20,11 +23,11 @@ def _requirements(self):
def get_camera_frame(self):
"""Convert camera feed to Base64 text"""
if self.vid is not None:
_, frame = self.vid.read()
_, buffer = self.lib["cv2"].imencode(".jpg", frame)
frame = base64.b64encode(buffer)
return frame
return None
while True:
_, frame = self.vid.read()
_, buffer = self.lib["cv2"].imencode(".jpg", frame)
frame = base64.b64encode(buffer)
self.lnxlink.run_module(f"{self.name}/Webcam feed", frame)

def get_info(self):
"""Gather information from the system"""
Expand All @@ -42,8 +45,8 @@ def exposed_controls(self):
},
"Webcam feed": {
"type": "camera",
"method": self.get_camera_frame,
"encoding": "b64",
"subtopic": True,
},
}

Expand All @@ -52,5 +55,11 @@ def start_control(self, topic, data):
if data.lower() == "off":
self.vid.release()
self.vid = None
if self.read_thr is not None:
self.read_thr.join()
self.read_thr = None
elif data.lower() == "on":
self.vid = self.lib["cv2"].VideoCapture(0)
if self.read_thr is None:
self.read_thr = Thread(target=self.get_camera_frame, daemon=True)
self.read_thr.start()

0 comments on commit ea40686

Please sign in to comment.