From 3c09734f6adc48d7a90f9e62621c5069af2e07cd Mon Sep 17 00:00:00 2001 From: alban Date: Mon, 16 Dec 2024 21:39:20 +0100 Subject: [PATCH] feat: add public interface --- README.md | 6 ++++++ process.py | 4 ++++ public.py | 52 ++++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 3 ++- 4 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 public.py diff --git a/README.md b/README.md index 7718f5e..71a4970 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,13 @@ cd yiking python3 -m venv .venv source .venv/bin/activate pip3 install -r requirements.txt + +# Affiche l'interface pour le public +python3 public.py + +# Affiche l'interface opérateur python3 main.py + ``` ... devrait afficher une interface utilisateur. diff --git a/process.py b/process.py index f26cbcb..6dea670 100644 --- a/process.py +++ b/process.py @@ -151,12 +151,16 @@ def process_frame(params, cam_id): # 7. Sortie des 6 couleurs en --- ou - - return_text = "" + img_public = raw_image.copy() img_final = raw_image.copy() for i in boules_proches: boule = boules[i] return_text += f"{boules_couleurs[i]}\n" cv2.circle(img_final, (boule.x, boule.y), boule.rayon, (0, 255, 0), 2) + cv2.circle(img_public, (boule.x, boule.y), boule.rayon, (0, 255, 0), 2) cv2.putText(img_final, boules_bgr[i], (boule.x, boule.y), cv2.FONT_HERSHEY_SIMPLEX, fontScale=0.75, color=(255, 255, 255), thickness=1, lineType=cv2.LINE_AA) + cv2.imwrite('/tmp/yiking.png', img_public) + return img_final, return_text diff --git a/public.py b/public.py new file mode 100644 index 0000000..17c83ea --- /dev/null +++ b/public.py @@ -0,0 +1,52 @@ +import tkinter as tk +from PIL import Image, ImageTk +import os +import time +from threading import Thread +import inotify.adapters + +class YikingInterface: + def __init__(self, root): + self.root = root + self.root.title("Yiking") + self.root.configure(bg="black") + + # Canvas for displaying images + self.canvas = tk.Canvas(self.root, bg="black", width=1024, height=768, highlightthickness=0, bd=0) + self.canvas.pack(fill="both", expand=True) + + self.image_path = "/tmp/yiking.png" + + # Load initial image if it exists + self.load_and_display_image() + + # Start file watcher in a separate thread + self.start_file_watcher() + + def load_and_display_image(self): + """Load and display the image on the canvas, rescaling if necessary.""" + if os.path.exists(self.image_path): + img = Image.open(self.image_path) + img = img.resize((1024, 768), Image.Resampling.LANCZOS) + self.tk_image = ImageTk.PhotoImage(img) + self.canvas.create_image(0, 0, anchor="nw", image=self.tk_image) + + def start_file_watcher(self): + """Start a file watcher to monitor changes on the image file.""" + def watch_file(): + notifier = inotify.adapters.Inotify() + notifier.add_watch("/tmp") + + for event in notifier.event_gen(yield_nones=False): + (_, type_names, path, filename) = event + if filename == "yiking.png" and "IN_CLOSE_WRITE" in type_names: + print(f"File updated: {os.path.join(path, filename)}") + self.load_and_display_image() + + watcher_thread = Thread(target=watch_file, daemon=True) + watcher_thread.start() + +if __name__ == "__main__": + root = tk.Tk() + app = YikingInterface(root) + root.mainloop() diff --git a/requirements.txt b/requirements.txt index a58a8d2..9cf382f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ numpy~=2.1.3 opencv-python -pillow \ No newline at end of file +pillow +inotify \ No newline at end of file