forked from turner11/Traffic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorchastrator.py
101 lines (82 loc) · 3.44 KB
/
orchastrator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
from collections import namedtuple
from functools import partial
import cv2
import rx
from rx import operators as op
import logging
from imutils.video import FPS
from commands.show_command import ShowCommand
from trackers.opencv_tracker import OpenCvTracker
from yolo_detectors.yolo_detector import YoloDetector
logger = logging.getLogger(__name__)
KeyAndFrame = namedtuple('KeyAndFrame', ['key', 'frame'])
KeyFrameDetections = namedtuple('KeyAndFrame', ['key', 'frame', 'detections'])
class Orchestrator(object):
""""""
def __init__(self, url, yolo):
""""""
super().__init__()
self.url = url
self.yolo = yolo
def __repr__(self):
return super().__repr__()
@staticmethod
def get_stream(url, observer, scheduler):
cap = cv2.VideoCapture(url)
if not cap.isOpened():
observer.on_error("Error opening video stream or file")
fps = FPS().start()
try:
while cap.isOpened():
is_read_success, raw_frame = cap.read()
# Using the FPS for getting smooth video while waiting
fps.update()
fps.stop()
wait_time = max(fps.fps(), 1)
key = chr(cv2.waitKey(round(wait_time)) & 0xFF)
is_q_pressed = key == 'q'
if is_q_pressed:
break
if is_read_success:
observer.on_next(KeyAndFrame(key, raw_frame))
else:
observer.on_error('Failed to read video capture')
observer.on_completed()
break
finally:
cap.release()
cv2.destroyAllWindows()
observer.on_completed()
def start(self):
url = self.url
source_func = partial(self.get_stream, url)
# noinspection PyTypeChecker
source = rx.create(source_func)
detector = YoloDetector.factory(yolo=self.yolo)
tracker = OpenCvTracker()
from commands.detect_command import DetectCommand
from commands.draw_bonding_box_command import DrawBoundingBoxCommand
from commands.track_command import TrackCommand
from commands.draw_stats_command import DrawStatsCommand
from commands.save_frame_command import SaveFrameCommand
cmd_detect = DetectCommand(detector=detector)
cmd_draw = DrawBoundingBoxCommand()
cmd_track = TrackCommand(tracker)
cmd_stats = DrawStatsCommand(additional_info={"Tracker": tracker.tracker})
cmd_show = ShowCommand(title=f'Traffic: {self.yolo}')
# fn = r'out_video.avi'
# fourcc = cv2.VideoWriter_fourcc(*"MJPG")
# video_writer = cv2.VideoWriter(fn, fourcc, 14, (720, 576), True)
# cmd_save = SaveFrameCommand(video_writer=video_writer)
composed = source.pipe(
op.map(lambda kf: KeyFrameDetections(kf.key, kf.frame, cmd_detect(kf))),
op.map(lambda kfd: KeyFrameDetections(kfd.key, cmd_draw(kfd), kfd.detections)),
op.map(lambda kfd: KeyAndFrame(kfd.key, cmd_track(kfd))),
op.map(lambda kfd: KeyAndFrame(kfd.key, cmd_stats(kfd))),
op.map(cmd_show),
# op.map(cmd_save),
)
composed.subscribe(on_next=lambda kf: kf,
on_completed=lambda: logger.debug("Stream ended"),
on_error=lambda e: logger.exception('Got on error'))
# video_writer.release()