Skip to content

Commit

Permalink
feat - add photographer mode
Browse files Browse the repository at this point in the history
Camera is now a singleton
  • Loading branch information
EtienneSchmitz committed Sep 20, 2023
1 parent 358a2da commit 34ed740
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 7 deletions.
9 changes: 4 additions & 5 deletions rpi/manager/thymio/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from tasks.thymio.follow_line import FollowLine
from tasks.thymio.object_collector import ObjectCollector
from tasks.api import API
from tasks.thymio.photographer import Photographer

from manager.base import BaseManager
from controller.thymio.controller import ThymioController
Expand All @@ -18,15 +18,14 @@ def __init__(self):
self.logger = logging.getLogger(__name__)

self.tasks = [
ObjectCollector(self.controller),
FollowLine(self.controller),
API(self.controller),
# ObjectCollector(self.controller),
# FollowLine(self.controller),
Photographer(self.controller),
]

self.num_modes = len(self.tasks)

def change_mode(self):
print("test")
current_time = time.time()

if current_time - self.last_mode_change_time < self.mode_change_delay:
Expand Down
45 changes: 45 additions & 0 deletions rpi/tasks/thymio/photographer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import logging
import cv2 as cv

from controller.thymio.controller import ThymioController
from vision.camera import Camera
from tasks.base import Task

from vision import detect_objects


class Photographer(Task):
def __init__(self, controller: ThymioController):
self.controller = controller
self.logger = logging.getLogger(__name__)
self.cam = Camera()
self.controller.set_led("top", [0,32,0])

def run(self):
self.logger.info("PHOTOGRAPHER")
img = self.cam.grab_frame_loop()

if img is None:
return

found_obj = detect_objects(img, render=True)

if found_obj:
obj = found_obj[0]
label = obj.label

if label == "cube":
self.controller.set_led("bottom.left", [32,0,0])
self.controller.set_led("bottom.right", [32,0,0])
elif label == "star":
self.controller.set_led("bottom.left", [0,0,32])
self.controller.set_led("bottom.right", [0,0,32])
elif label == "ball":
self.controller.set_led("bottom.left", [32,0,32])
self.controller.set_led("bottom.right", [32,0,32])
else:
pass


def close(self):
pass
14 changes: 12 additions & 2 deletions rpi/vision/camera.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
import cv2 as cv

class Camera:
def __init__(self):
_instance = None

def __new__(cls):
if cls._instance is None:
cls._instance = super().__new__(cls)
cls._instance.init_camera()
return cls._instance

def init_camera(self):
self.cap = cv.VideoCapture(0)

def grab_frame_loop(self):
_, img = self.cap.read()

return img

def __del__(self):
self.cap.release()

0 comments on commit 34ed740

Please sign in to comment.