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

Commit

Permalink
Merge pull request #48 from LimaoC/raspcamera
Browse files Browse the repository at this point in the history
cleaning up
  • Loading branch information
avg-lebesgue-enjoyer authored Sep 15, 2024
2 parents 4088bc8 + 0c91f1d commit 9261e26
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
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:
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)

0 comments on commit 9261e26

Please sign in to comment.