From 44a03cfcebee6d5ba731f2bb72489eb6d9b67bc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Br=C3=BCns?= Date: Mon, 24 Jun 2024 23:01:38 +0200 Subject: [PATCH] Use in-memory buffer for camera image instead of temporary file --- bCNC/Camera.py | 4 ++++ bCNC/Pendant.py | 11 +++++------ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/bCNC/Camera.py b/bCNC/Camera.py index f6007eea7..5bfb30eea 100644 --- a/bCNC/Camera.py +++ b/bCNC/Camera.py @@ -196,6 +196,10 @@ def read(self): def save(self, filename): cv.imwrite(filename, self.original) + def jpg(self): + s, jpg = cv.imencode(".jpg", self.image) + return jpg if s else None + # ----------------------------------------------------------------------- # Rotate image in steps of 90deg # ----------------------------------------------------------------------- diff --git a/bCNC/Pendant.py b/bCNC/Pendant.py index 19ab0ce29..f6b353bbd 100644 --- a/bCNC/Pendant.py +++ b/bCNC/Pendant.py @@ -168,15 +168,14 @@ def do_GET(self): return if Pendant.camera is None: Pendant.camera = Camera.Camera("webcam") - Pendant.camera.start() + if not Pendant.camera.start(): + Pendant.camera = None if Pendant.camera.read(): - Pendant.camera.save("camera.jpg") try: - f = open("camera.jpg","rb") - self.do_HEAD(200, content="image/jpeg", cl=self.get_file_size(f)) - self.wfile.write(f.read()) - f.close() + img = Pendant.camera.jpg() + self.do_HEAD(200, content="image/jpeg", cl=len(img)) + self.wfile.write(img) except Exception: pass else: