def get(self): frame_no = self.frame_no self.vid_cap.set(cv2.CAP_PROP_POS_FRAMES, frame_no) ret, frame = self.vid_cap.read() if ret: frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) if self.mmap is None: frame_info = frame else: if self.mmap.mmap_shape[0] != frame.shape[ 0] or self.mmap.mmap_shape[1] != frame.shape[1]: frame = utils.imresize(frame, width=self.mmap.mmap_shape[0], height=self.mmap.mmap_shape[1]) frame_info = self.mmap.write_mmap(frame) self.frame_no += self.move_frame else: frame_info = None return frame_no, frame_info
def process_frame(self, frame_no, in_frame): if frame_no == 1714: a = 0 ret_img = in_frame ret_img = self.draw_text(ret_img, self.frame_no_text_pos, "Frame : {}".format(frame_no), font=self.font_frame) # 기존 plate 검출을 가지고 있다가 점점 박스 두께를 줄이도록 하며, final 검출 이후엔 없애도록 하자! # 그런데, 두개를 동시에 잡았을 땐 어떻게??? plt_info_list = [] if frame_no in self.pd_info_dict: plt_pos_arr, plt_list = self.pd_info_dict[frame_no] for plt_pos in plt_pos_arr: plt_pts = utils.transform_quadrilateral_to_rectangle( plt_pos, algo='max', margin=0) crop_plt_img = ret_img[plt_pts[0][1]:plt_pts[1][1], plt_pts[0][0]:plt_pts[1][0]] plt_info_list.append((plt_pts, crop_plt_img.copy())) ret_img = utils.draw_quadrilateral_on_image(ret_img, self.camera_roi, color=utils.GREEN, clockwise_=False, thickness=4) if len(plt_info_list) > 0: # ROI 선들이 번호판위를 지나갈수 있기 때문에 한번더 그려준다. for plt_info in plt_info_list: pts, crop_plt_img = plt_info ret_img[pts[0][1]:pts[1][1], pts[0][0]:pts[1][0]] = crop_plt_img # 박스를 그리자! ret_img = utils.draw_quadrilateral_on_image(ret_img, plt_pos_arr, color=utils.RED, clockwise_=True, thickness=10) if plt_list is not None: for idx in range(len(plt_pos_arr)): if idx < len(plt_list): plt_num = plt_list[idx]['plt_num'] else: plt_num = "not found" plt_pos = plt_pos_arr[idx] test_box2 = (min(plt_pos[0][0] + 600, ret_img.shape[1]), plt_pos[0][1] - 10) text_box1 = (test_box2[0] - 600, plt_pos[0][1] - (self.font_height + 20)) ret_img = cv2.rectangle(ret_img, text_box1, test_box2, utils.BLACK, -1) ret_img = self.draw_text(ret_img, (text_box1[0], text_box1[1]), plt_num) self.last_plt_list = (plt_info_list, plt_list) if self.last_plt_list: plt_info_list, plt_list = self.last_plt_list box_l = self.last_box_tl[0] box_r = self.last_box_br[0] text_pos_x = self.last_text_pos[0] for idx in range(len(plt_info_list)): if idx < len(plt_list): if self.is_paint_black_box: ret_img = cv2.rectangle(ret_img, (box_l, self.last_box_tl[1]), (box_r, self.last_box_br[1]), utils.BLACK, -1) plt_num = plt_list[idx]['plt_num'] plt_url = plt_list[idx]['plt_uri'] ret_img = self.draw_text( ret_img, (text_pos_x, self.last_text_pos[1]), plt_num) plt_img = utils.imread(plt_url) if plt_img is not None: plt_img = utils.imresize(plt_img, height=self.plt_height) plt_height = plt_img.shape[0] plt_width = plt_img.shape[1] ret_img[self.last_plt_pos_y:self.last_plt_pos_y + plt_height, text_pos_x:text_pos_x + plt_width] = plt_img box_l += 700 box_r += 700 text_pos_x += 700 return ret_img
def make(self): if not self.sim_save_dir: return video_fname = self.camera_id + '.' video_fname += utils.get_datetime().replace(":", "-") + '.mp4' video_path = os.path.join(self.sim_save_dir, video_fname) if os.path.isfile(video_path): shutil.rmtree(video_path) self.logger.info( "start making video from ImgExtract and ImgAVR. {}".format( video_path)) if self.total_frame: process1 = (ffmpeg.input(self.video_url).output( 'pipe:', vframes=self.total_frame, format='rawvideo', pix_fmt='rgb24').run_async(pipe_stdout=True)) else: process1 = (ffmpeg.input(self.video_url).output( 'pipe:', format='rawvideo', pix_fmt='rgb24').run_async(pipe_stdout=True)) process2 = (ffmpeg.input( 'pipe:', format='rawvideo', pix_fmt='rgb24', s='{}x{}'.format(self.video_width, self.video_height)).output( video_path, pix_fmt='yuv420p').overwrite_output().run_async( pipe_stdin=True)) read_size = self.video_width * self.video_height * 3 frame_no = 0 while True: in_bytes = process1.stdout.read(read_size) if not in_bytes: break in_frame = (np.frombuffer(in_bytes, np.uint8).reshape( [self.video_height, self.video_width, 3])) if self.img_width != self.video_width or self.img_height != self.video_height: in_frame = utils.imresize(in_frame, width=self.img_width, height=self.img_height) out_frame = self.process_frame(frame_no, in_frame) process2.stdin.write(out_frame.astype(np.uint8).tobytes()) if frame_no != 0 and (frame_no % (30 * 60)) == 0: self.logger.info( "making video. {} th frame writing".format(frame_no)) frame_no += 1 process2.stdin.close() process1.wait() process2.wait() self.logger.info("end making video. total:{} frame".format(frame_no))