|
| 1 | +from time import sleep, time |
| 2 | +from multiprocessing import Process, Queue |
| 3 | +from queue import Full as queue_is_full |
| 4 | + |
| 5 | +import hydra |
| 6 | +from tqdm import tqdm |
| 7 | + |
| 8 | +from nodes.VideoReader import VideoReader |
| 9 | +from nodes.ShowNode import ShowNode |
| 10 | +from nodes.VideoSaverNode import VideoSaverNode |
| 11 | +from nodes.DetectionTrackingNodes import DetectionTrackingNodes |
| 12 | +from nodes.TrackerInfoUpdateNode import TrackerInfoUpdateNode |
| 13 | +from nodes.CalcStatisticsNode import CalcStatisticsNode |
| 14 | +from nodes.FlaskServerVideoNode import VideoServer |
| 15 | +from nodes.KafkaProducerNode import KafkaProducerNode |
| 16 | +from elements.VideoEndBreakElement import VideoEndBreakElement |
| 17 | + |
| 18 | +PRINT_PROFILE_INFO = False |
| 19 | + |
| 20 | + |
| 21 | +def proc_frame_reader(queue_out: Queue, config: dict, time_sleep_start: int): |
| 22 | + sleep_message = f"Система разогревается.. sleep({time_sleep_start})" |
| 23 | + for _ in tqdm(range(time_sleep_start), desc=sleep_message): |
| 24 | + sleep(1) |
| 25 | + video_reader = VideoReader(config["video_reader"]) |
| 26 | + for frame_element in video_reader.process(): |
| 27 | + ts0 = time() |
| 28 | + try: |
| 29 | + queue_out.put_nowait(frame_element) |
| 30 | + |
| 31 | + if PRINT_PROFILE_INFO: |
| 32 | + print(f"PROC_FRAME_READER: {(time()-ts0) * 1000:.0f} ms: ") |
| 33 | + |
| 34 | + except queue_is_full: |
| 35 | + if PRINT_PROFILE_INFO: |
| 36 | + print("queue_is_full => pass frame") |
| 37 | + |
| 38 | + if isinstance(frame_element, VideoEndBreakElement): |
| 39 | + break |
| 40 | + |
| 41 | +def proc_proceessor(queue_in: Queue, config: dict, frame_process: Process): |
| 42 | + detection_node = DetectionTrackingNodes(config) |
| 43 | + tracker_info_update_node = TrackerInfoUpdateNode(config) |
| 44 | + calc_statistics_node = CalcStatisticsNode(config) |
| 45 | + send_info_kafka = config["pipeline"]["send_info_kafka"] |
| 46 | + if send_info_kafka: |
| 47 | + kafka_producer_node = KafkaProducerNode(config) |
| 48 | + show_node = ShowNode(config) |
| 49 | + save_video = config["pipeline"]["save_video"] |
| 50 | + show_in_web = config["pipeline"]["show_in_web"] |
| 51 | + if save_video: |
| 52 | + video_saver_node = VideoSaverNode(config["video_saver_node"]) |
| 53 | + if show_in_web: |
| 54 | + video_server_node = VideoServer(config) |
| 55 | + |
| 56 | + while True: |
| 57 | + |
| 58 | + if not frame_process.is_alive(): |
| 59 | + break |
| 60 | + |
| 61 | + ts0 = time() |
| 62 | + frame_element = queue_in.get() |
| 63 | + ts1 = time() |
| 64 | + frame_element = detection_node.process(frame_element) |
| 65 | + frame_element = tracker_info_update_node.process(frame_element) |
| 66 | + frame_element = calc_statistics_node.process(frame_element) |
| 67 | + if send_info_kafka: |
| 68 | + frame_element = kafka_producer_node.process(frame_element) |
| 69 | + frame_element = show_node.process(frame_element) |
| 70 | + if save_video: |
| 71 | + video_saver_node.process(frame_element) |
| 72 | + |
| 73 | + if isinstance(frame_element, VideoEndBreakElement): |
| 74 | + break |
| 75 | + |
| 76 | + if show_in_web: |
| 77 | + video_server_node.update_image(frame_element.frame_result) |
| 78 | + |
| 79 | + if PRINT_PROFILE_INFO: |
| 80 | + print( |
| 81 | + f"PROC_PROCESSOR: {(time()-ts0) * 1000:.0f} ms: " |
| 82 | + + f"get {(ts1-ts0) * 1000:.0f} | " |
| 83 | + + f"nodes_inference {(time()-ts1) * 1000:.0f} | " |
| 84 | + ) |
| 85 | + |
| 86 | + |
| 87 | +@hydra.main(version_base=None, config_path="configs", config_name="app_config") |
| 88 | +def main(config) -> None: |
| 89 | + time_sleep_start = 5 |
| 90 | + |
| 91 | + frame_queue_max_size = 2 |
| 92 | + frame_queue = Queue(frame_queue_max_size) |
| 93 | + |
| 94 | + frame_process = Process(target=proc_frame_reader, args=(frame_queue, config, time_sleep_start)) |
| 95 | + frame_process.daemon = True |
| 96 | + frame_process.start() |
| 97 | + |
| 98 | + proc_proceessor(frame_queue, config, frame_process) |
| 99 | + |
| 100 | + |
| 101 | +if __name__ == "__main__": |
| 102 | + ts = time() |
| 103 | + main() |
| 104 | + print(f"\n total time: {(time()-ts) / 60:.2} minute") |
0 commit comments