diff --git a/deploy/pipeline/pipeline.py b/deploy/pipeline/pipeline.py index 54bbc78e5a85858073e0adadd4976cbf6a93eaa2..da25566558010c57a0b418f69ec0ba60e796ff85 100644 --- a/deploy/pipeline/pipeline.py +++ b/deploy/pipeline/pipeline.py @@ -21,6 +21,9 @@ import math import paddle import sys import copy +import threading +import queue +import time from collections import Sequence, defaultdict from datacollector import DataCollector, Result @@ -139,7 +142,6 @@ class Pipeline(object): return input def run_multithreads(self): - import threading if self.multi_camera: multi_res = [] threads = [] @@ -572,6 +574,18 @@ class PipePredictor(object): if self.cfg['visual']: self.visualize_image(batch_file, batch_input, self.pipeline_res) + def capturevideo(self, capture, queue): + frame_id = 0 + while(1): + if queue.full(): + time.sleep(0.1) + else: + ret, frame = capture.read() + if not ret: + return + frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) + queue.put(frame_rgb) + def predict_video(self, video_file, thread_idx=0): # mot # mot -> attr @@ -647,14 +661,19 @@ class PipePredictor(object): } # store info for vehicle parking in region illegal_parking_dict = None - while (1): + framequeue = queue.Queue(10) + + thread = threading.Thread( + target=self.capturevideo, + args=(capture, framequeue)) + thread.start() + time.sleep(1) + + while(not framequeue.empty()): if frame_id % 10 == 0: print('Thread: {}; frame id: {}'.format(thread_idx, frame_id)) - ret, frame = capture.read() - if not ret: - break - frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) + frame_rgb = framequeue.get() if frame_id > self.warmup_frame: self.pipe_timer.total_time.start() @@ -720,7 +739,7 @@ class PipePredictor(object): self.pipe_timer.total_time.end() if self.cfg['visual']: _, _, fps = self.pipe_timer.get_total_time() - im = self.visualize_video(frame, mot_res, frame_id, fps, + im = self.visualize_video(frame_rgb, mot_res, frame_id, fps, entrance, records, center_traj) # visualize if len(self.pushurl)>0: @@ -894,7 +913,7 @@ class PipePredictor(object): if self.cfg['visual']: _, _, fps = self.pipe_timer.get_total_time() - im = self.visualize_video(frame, self.pipeline_res, + im = self.visualize_video(frame_rgb, self.pipeline_res, self.collector, frame_id, fps, entrance, records, center_traj, self.illegal_parking_time != -1, @@ -912,7 +931,7 @@ class PipePredictor(object): print('save result to {}'.format(out_path)) def visualize_video(self, - image, + image_rgb, result, collector, frame_id, @@ -922,6 +941,7 @@ class PipePredictor(object): center_traj=None, do_illegal_parking_recognition=False, illegal_parking_dict=None): + image = cv2.cvtColor(image_rgb, cv2.COLOR_RGB2BGR) mot_res = copy.deepcopy(result.get('mot')) if mot_res is not None: ids = mot_res['boxes'][:, 0]