Skip to content
This repository has been archived by the owner on Dec 8, 2024. It is now read-only.

rasp camera #48

Merged
merged 2 commits into from
Sep 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions client/drivers/camera_overlord.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# This script periodically takes photos and saves them to
# a temporary file on ram. This allows multiple programs to
# access the camera feed at once.

from picamera2 import Picamera2
import os
import time

picam2 = Picamera2()
picam2.start()
picam2.options['quality'] = 80

try:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can I be pedantic and ask that you wrap this in a if __name__ == "__main__" guard

f = open('/tmp/snapshot.jpg','x')
f.close()
except:
print('Snapshot already exists')

try:
while True:
picam2.capture_file("/tmp/snapshot2.jpg")
os.replace("/tmp/snapshot2.jpg", "/tmp/snapshot.jpg")
time.sleep(0.5)

except KeyboardInterrupt:
picam2.close()
print('Closed nicely')
quit()
19 changes: 19 additions & 0 deletions client/models/pose_detection/frame_capturer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from abc import ABC, abstractmethod

import numpy as np
import time
import cv2
import os


class FrameCapturer(ABC):
Expand Down Expand Up @@ -30,3 +32,20 @@ def get_frame(self) -> tuple[np.ndarray, int]:
def release(self) -> None:
"""Release the camera."""
self._cam.release()


class RaspCapturer(FrameCapturer):
"""FrameCapturer using a temp file to read from the camera.
File is created using client/drivers/camera_overlord.py"""
def get_frame(self) -> tuple[np.ndarray, int]:
tries = 0
while True:
array = cv2.imread('/tmp/snapshot.jpg')
if array is None:
tries += 1
if tries > 5: raise FileNotFoundError('No snapshot found')
time.sleep(0.05)
else:
tries = 0
finfo = os.stat('/tmp/snapshot.jpg')
return (array, finfo.st_mtime)