Skip to content

Commit

Permalink
Add TypedDict
Browse files Browse the repository at this point in the history
  • Loading branch information
yihong1120 committed Jul 9, 2024
1 parent 4d3df80 commit 7962b48
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 9 deletions.
10 changes: 5 additions & 5 deletions src/danger_detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
from typing import TypedDict


class BoundingBox(TypedDict):
class InputData(TypedDict):
x1: float
y1: float
x2: float
y2: float


class DetectionData(TypedDict):
bbox: BoundingBox
confidence: float
class_label: int


class ResultData(TypedDict):
warnings: set[str]


class DangerDetector:
"""
A class to detect potential safety hazards based on the detection data.
Expand Down
6 changes: 5 additions & 1 deletion src/line_notifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@
from PIL import Image


class NotificationData(TypedDict):
class InputData(TypedDict):
message: str
image: np.ndarray | None


class ResultData(TypedDict):
response_code: int


class LineNotifier:
"""
A class for managing notifications sent via the LINE Notify API.
Expand Down
16 changes: 16 additions & 0 deletions src/live_stream_detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import os
import time
from pathlib import Path
from typing import TypedDict

import cv2
import numpy as np
Expand All @@ -26,6 +27,21 @@
load_dotenv()


class InputData(TypedDict):
frame: cv2.Mat
model_key: str
run_local: bool


class DetectionData(TypedDict):
x1: float
y1: float
x2: float
y2: float
confidence: float
label: int


class LiveStreamDetector:
"""
A class to perform live stream detection and tracking
Expand Down
12 changes: 11 additions & 1 deletion src/live_stream_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@
import argparse
import datetime
from collections.abc import Generator
from typing import TypedDict

import cv2
from ultralytics import YOLO


class DetectionResult(TypedDict):
ids: list[int]
data: list[list[float]]
frame: cv2.Mat
timestamp: float


class LiveStreamDetector:
"""
A class to perform live stream detection and tracking using YOLOv8.
Expand All @@ -30,7 +38,9 @@ def __init__(
self.model = YOLO(self.model_path)
self.cap = cv2.VideoCapture(self.stream_url)

def generate_detections(self) -> Generator[tuple, None, None]:
def generate_detections(
self,
) -> Generator[tuple[list[int], list[list[float]], cv2.Mat, float]]:
"""
Yields detection results, timestamp per frame from video capture.
Expand Down
11 changes: 11 additions & 0 deletions src/stream_capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,23 @@
import gc
import time
from collections.abc import Generator
from typing import TypedDict

import cv2
import speedtest
import streamlink


class InputData(TypedDict):
stream_url:str
capture_interval:int


class ResultData(TypedDict):
frame: cv2.Mat
timestamp: float


class StreamCapture:
"""
A class to capture frames from a video stream.
Expand Down
4 changes: 2 additions & 2 deletions tests/stream_caputre_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def test_execute_capture(self, mock_capture_frames):
frame, timestamp = next(frames_generator)
self.assertIsNotNone(frame)
self.assertAlmostEqual(
timestamp, expected_timestamp, delta=40,
timestamp, expected_timestamp, delta=60,
) # 允许更大的差异

@patch('src.stream_capture.StreamCapture.capture_youtube_frames')
Expand All @@ -193,7 +193,7 @@ def test_execute_capture_youtube(self, mock_capture_youtube_frames):

frame, timestamp = next(frames_generator)
self.assertIsNotNone(frame)
self.assertAlmostEqual(timestamp, 1234567890.0, delta=40)
self.assertAlmostEqual(timestamp, 1234567890.0, delta=60)


if __name__ == '__main__':
Expand Down

0 comments on commit 7962b48

Please sign in to comment.