-
Notifications
You must be signed in to change notification settings - Fork 4
working and resolving issue #70 - add MJPEG video stream endpoint #72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MoveFastAndBreakThings-dot
wants to merge
2
commits into
uaarg:main
Choose a base branch
from
MoveFastAndBreakThings-dot:sam/aruco-video-stream
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import time | ||
|
|
||
| from src.modules.emu import Emu | ||
| from src.modules.imaging.detector import ArucoDetector | ||
| from src.modules.imaging.camera import DebugCamera | ||
| from src.modules.imaging.location import DebugLocationProvider | ||
| from src.modules.imaging.analysis import ImageAnalysisDelegate | ||
| from src.modules.imaging.aruco_stream import ArucoEmuStreamer | ||
| from src.modules.imaging.video_emu_stream import SharedFrameCamera, VideoEmuStreamer | ||
|
|
||
|
|
||
| def main(): | ||
| emu = Emu("tmp") | ||
| emu.start_comms() | ||
| time.sleep(1) | ||
|
|
||
| # Base camera — swap DebugCamera for RPiCamera/OakdCamera on real hardware | ||
| base_camera = DebugCamera("res/test-image.jpeg") | ||
|
|
||
| # SharedFrameCamera captures at 15fps; both video stream and analysis read from it | ||
| shared_cam = SharedFrameCamera(base_camera, fps=15) | ||
| shared_cam.start() | ||
|
|
||
| # Video stream → EMU /video endpoint (browser: <img src="http://HOST:8080/video">) | ||
| video_streamer = VideoEmuStreamer(emu, shared_cam, fps=15, quality=70) | ||
| video_streamer.start() | ||
|
|
||
| # ArUco detection pipeline reads latest frame from shared camera | ||
| detector = ArucoDetector() | ||
| location_provider = DebugLocationProvider() | ||
|
|
||
| analysis = ImageAnalysisDelegate(detector, shared_cam, location_provider) | ||
| aruco_streamer = ArucoEmuStreamer(emu, "tmp") | ||
| analysis.subscribe(aruco_streamer.on_detection) | ||
| analysis.start() | ||
|
|
||
| try: | ||
| while True: | ||
| time.sleep(1) | ||
| except KeyboardInterrupt: | ||
| pass | ||
| finally: | ||
| analysis.stop() | ||
| video_streamer.stop() | ||
| shared_cam.stop() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
MoveFastAndBreakThings-dot marked this conversation as resolved.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| from PIL import Image, ImageDraw | ||
| import os | ||
| from typing import Optional, Tuple | ||
|
|
||
| from src.modules.emu import Emu | ||
| from src.modules.imaging.detector import BoundingBox | ||
|
|
||
|
|
||
| class ArucoEmuStreamer: | ||
| def __init__(self, emu: Emu, output_dir: str = "tmp"): | ||
| self.emu = emu | ||
| self.output_dir = output_dir | ||
| self.counter = 0 | ||
|
|
||
| if not os.path.exists(self.output_dir): | ||
| os.makedirs(self.output_dir) | ||
|
|
||
| def on_detection(self, image: Image.Image, bounding_box: Optional[BoundingBox], position: Optional[Tuple[float, float]]): | ||
| im = image.copy() | ||
|
|
||
| if bounding_box is not None: | ||
| draw = ImageDraw.Draw(im) | ||
| bb = (bounding_box.position.x, bounding_box.position.y, | ||
| bounding_box.position.x + bounding_box.size.x, | ||
| bounding_box.position.y + bounding_box.size.y) | ||
| draw.rectangle(bb, outline="red") | ||
|
|
||
| filename = f"{self.counter}.jpeg" | ||
| filepath = os.path.join(self.output_dir, filename) | ||
| im.save(filepath) | ||
|
MoveFastAndBreakThings-dot marked this conversation as resolved.
|
||
|
|
||
| self.emu.send_image(filename) | ||
| self.counter += 1 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| import io | ||
| import threading | ||
| import time | ||
|
|
||
| from PIL import Image | ||
|
|
||
| from src.modules.emu import Emu | ||
| from src.modules.imaging.camera import CameraProvider | ||
|
|
||
|
|
||
| class SharedFrameCamera(CameraProvider): | ||
| """ | ||
| Wraps a CameraProvider and shares the latest captured frame across | ||
| multiple consumers (e.g. video streamer + analysis pipeline) without | ||
| both threads calling camera.capture() simultaneously. | ||
| """ | ||
|
|
||
| def __init__(self, camera: CameraProvider, fps: int = 15): | ||
| self._camera = camera | ||
| self._fps = fps | ||
| self._latest: Image.Image | None = None | ||
| self._lock = threading.Lock() | ||
| self._running = False | ||
| self._thread: threading.Thread | None = None | ||
|
|
||
| def start(self): | ||
| self._running = True | ||
| self._thread = threading.Thread(target=self._capture_loop, daemon=True) | ||
| self._thread.start() | ||
|
|
||
| def stop(self): | ||
| self._running = False | ||
| if self._thread: | ||
| self._thread.join() | ||
|
|
||
| def get_latest(self) -> Image.Image | None: | ||
| with self._lock: | ||
| return self._latest | ||
|
|
||
| def capture(self) -> Image.Image: | ||
| while True: | ||
| with self._lock: | ||
| if self._latest is not None: | ||
| return self._latest | ||
| time.sleep(0.01) | ||
|
|
||
| def _capture_loop(self): | ||
| interval = 1 / self._fps | ||
| while self._running: | ||
| frame = self._camera.capture() | ||
| with self._lock: | ||
| self._latest = frame | ||
| time.sleep(interval) | ||
|
MoveFastAndBreakThings-dot marked this conversation as resolved.
|
||
|
|
||
|
|
||
| class VideoEmuStreamer: | ||
| """ | ||
| Continuously grabs frames from a SharedFrameCamera and pushes them | ||
| to EMU's MJPEG /video endpoint at the given fps. | ||
| """ | ||
|
|
||
| def __init__(self, emu: Emu, shared_cam: SharedFrameCamera, fps: int = 15, quality: int = 70): | ||
| self.emu = emu | ||
| self.shared_cam = shared_cam | ||
| self.fps = fps | ||
| self.quality = quality | ||
| self._running = False | ||
| self._thread: threading.Thread | None = None | ||
|
|
||
| def start(self): | ||
| self._running = True | ||
| self._thread = threading.Thread(target=self._stream_loop, daemon=True) | ||
| self._thread.start() | ||
|
|
||
| def stop(self): | ||
| self._running = False | ||
| if self._thread: | ||
| self._thread.join() | ||
|
|
||
| def _stream_loop(self): | ||
| interval = 1 / self.fps | ||
| while self._running: | ||
| frame = self.shared_cam.get_latest() | ||
| if frame is not None: | ||
| buf = io.BytesIO() | ||
| frame.save(buf, format="JPEG", quality=self.quality) | ||
| self.emu.send_video_frame(buf.getvalue()) | ||
| time.sleep(interval) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sample file will drastically change after requested changes