def main(): parser = ArgumentParser() parser.add_argument('config', help='Config file') parser.add_argument('--input', help='input video file') parser.add_argument('--output', help='output video file (mp4 format)') parser.add_argument('--checkpoint', help='Checkpoint file') parser.add_argument( '--device', default='cuda:0', help='Device used for inference') parser.add_argument( '--show', action='store_true', default=False, help='whether to show visualizations.') parser.add_argument( '--score-thr', type=float, default=0.8, help='bbox score threshold') args = parser.parse_args() # build the model from a config file and a checkpoint file model = init_model(args.config, args.checkpoint, device=args.device) cap = cv2.VideoCapture(args.input) if args.output is not None: save_out_video = True fps = cap.get(cv2.CAP_PROP_FPS) size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))) fourcc = cv2.VideoWriter_fourcc(*'mp4v') videoWriter = cv2.VideoWriter(args.output, fourcc, fps, size) frame_id = 0 while (cap.isOpened()): flag, frame = cap.read() if not flag: break # test a single image result = inference_vid(model, frame, frame_id) vis_frame = model.show_result( frame, result, score_thr=args.score_thr, show=False) if save_out_video: videoWriter.write(vis_frame) if args.show: cv2.imshow(args.input, vis_frame) if cv2.waitKey(1) & 0xFF == ord('q'): break frame_id += 1 cap.release() if save_out_video: videoWriter.release() cv2.destroyAllWindows()
def main(args): url = 'http://' + args.inference_addr + '/predictions/' + args.model_name with open(args.video, 'rb') as video: tmp_res = requests.post(url, video) content = tmp_res.content if args.result_video: with open(args.result_video, 'wb') as file: file.write(content) video_path = args.result_video else: video_path = tempfile.NamedTemporaryFile().name with open(video_path, 'wb') as file: file.write(content) cap = cv2.VideoCapture(video_path) fps = cap.get(cv2.CAP_PROP_FPS) while cap.isOpened(): flag, frame = cap.read() if not flag: break cv2.imshow('torchserve_result', frame) if cv2.waitKey(int(1000 / fps)) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows() model = init_model(args.config, args.checkpoint, device=args.device) cap = cv2.VideoCapture(args.video) fps = cap.get(cv2.CAP_PROP_FPS) frame_id = 0 while cap.isOpened(): flag, frame = cap.read() if not flag: break # test a single image result = inference_vid(model, frame, frame_id) vis_frame = model.show_result(frame, result, score_thr=args.score_thr, show=False) cv2.imshow('pytorch_result', vis_frame) if cv2.waitKey(int(1000 / fps)) & 0xFF == ord('q'): break frame_id += 1 cap.release() cv2.destroyAllWindows()
def initialize(self, context): properties = context.system_properties self.map_location = 'cuda' if torch.cuda.is_available() else 'cpu' self.device = torch.device(self.map_location + ':' + str(properties.get('gpu_id')) if torch.cuda. is_available() else self.map_location) self.manifest = context.manifest model_dir = properties.get('model_dir') serialized_file = self.manifest['model']['serializedFile'] checkpoint = os.path.join(model_dir, serialized_file) self.config_file = os.path.join(model_dir, 'config.py') self.model = init_model(self.config_file, checkpoint, self.device) self.score_thr = 0.5 self.initialized = True
def main(): parser = ArgumentParser() parser.add_argument('config', help='Config file') parser.add_argument('--input', help='input video file') parser.add_argument('--output', help='output video file (mp4 format)') parser.add_argument('--checkpoint', help='Checkpoint file') parser.add_argument('--device', default='cuda:0', help='Device used for inference') parser.add_argument('--show', action='store_true', default=False, help='whether to show visualizations.') parser.add_argument('--color', default=(0, 255, 0), help='Color of tracked bbox lines.') parser.add_argument('--thickness', default=3, type=int, help='Thickness of bbox lines.') args = parser.parse_args() # build the model from a config file and a checkpoint file model = init_model(args.config, args.checkpoint, device=args.device) cap = cv2.VideoCapture(args.input) save_out_video = False if args.output is not None: save_out_video = True fps = cap.get(cv2.CAP_PROP_FPS) size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))) fourcc = cv2.VideoWriter_fourcc(*'mp4v') videoWriter = cv2.VideoWriter(args.output, fourcc, fps, size) frame_id = 0 while (cap.isOpened()): flag, frame = cap.read() if not flag: break if frame_id == 0: init_bbox = list(cv2.selectROI(args.input, frame, False, False)) # convert (x1, y1, w, h) to (x1, y1, x2, y2) init_bbox[2] += init_bbox[0] init_bbox[3] += init_bbox[1] # test a single image result = inference_sot(model, frame, init_bbox, frame_id) vis_frame = model.show_result(frame, result, color=args.color, thickness=args.thickness, show=False) if save_out_video: videoWriter.write(vis_frame) if args.show: cv2.imshow(args.input, vis_frame) if cv2.waitKey(1) & 0xFF == ord('q'): break frame_id += 1 cap.release() if save_out_video: videoWriter.release() cv2.destroyAllWindows()
def main(): parser = ArgumentParser() parser.add_argument('config', help='config file') parser.add_argument('--input', help='input video file or folder') parser.add_argument('--output', help='output video file (mp4 format) or folder') parser.add_argument('--checkpoint', help='checkpoint file') parser.add_argument('--device', default='cuda:0', help='device used for inference') parser.add_argument('--show', action='store_true', help='whether show the results on the fly') parser.add_argument('--backend', choices=['cv2', 'plt'], default='cv2', help='the backend to visualize the results') parser.add_argument('--fps', help='FPS of the output video') args = parser.parse_args() assert args.output or args.show # load images if osp.isdir(args.input): imgs = sorted(os.listdir(args.input)) IN_VIDEO = False else: imgs = mmcv.VideoReader(args.input) IN_VIDEO = True # define output if args.output is not None: if args.output.endswith('.mp4'): OUT_VIDEO = True out_dir = tempfile.TemporaryDirectory() out_path = out_dir.name _out = args.output.rsplit('/', 1) if len(_out) > 1: os.makedirs(_out[0], exist_ok=True) else: OUT_VIDEO = False out_path = args.output os.makedirs(out_path, exist_ok=True) fps = args.fps if args.show or OUT_VIDEO: if fps is None and IN_VIDEO: fps = imgs.fps if not fps: raise ValueError('Please set the FPS for the output video.') fps = int(fps) # build the model from a config file and a checkpoint file model = init_model(args.config, args.checkpoint, device=args.device) # test and show/save the images raw_video_path = "/data/taofuyu/tao_dataset/car_reid/high_video/f73dc1dc-a5b3040f/" all_video = [] for dir, _, files in os.walk(raw_video_path): for ff in files: if ff.split(".")[-1] == "mp4": all_video.append(os.path.join(dir, ff)) for video in all_video: appeared_id = [] imgs = mmcv.VideoReader(video) print(video) print( os.path.join("/data/taofuyu/tao_dataset/car_reid/high/", video.split("/")[-1].split(".")[0])) prog_bar = mmcv.ProgressBar(len(imgs)) for i, img in enumerate(imgs): if isinstance(img, str): img = osp.join(video, img) result = inference_mot(model, img, frame_id=i) result = result['track_results'] car_result = result[6] #6: roof for car in car_result: video_path = video sample_save_path = os.path.join( "/data/taofuyu/tao_dataset/car_reid/roof/", video_path.split("/")[-1].split(".")[0]) if not os.path.exists(sample_save_path): os.makedirs(sample_save_path) car_id = str(int(car[0])) car_box = car[1:-1] x_min, y_min, x_max, y_max = map(int, list(car_box)) car_patch = img[y_min:y_max, x_min:x_max] h, w, _ = car_patch.shape if h <= 0 or w <= 0: continue if not car_id in appeared_id: cv2.imwrite( os.path.join(sample_save_path, "id_" + car_id + ".jpg"), car_patch) appeared_id.append(car_id) track_save_path = os.path.join(sample_save_path, "id_" + car_id) if not os.path.exists(track_save_path): os.makedirs(track_save_path) cv2.imwrite( track_save_path + "/" + str(i) + "_" + car_id + ".jpg", car_patch) # if args.output is not None: # if IN_VIDEO or OUT_VIDEO: # out_file = osp.join(out_path, f'{i:06d}.jpg') # else: # out_file = osp.join(out_path, img.rsplit('/', 1)[-1]) # else: # out_file = None # model.show_result( # img, # result, # show=args.show, # wait_time=int(1000. / fps) if fps else 0, # out_file=out_file, # backend=args.backend) prog_bar.update()
def main(): parser = ArgumentParser() parser.add_argument('config', help='Config file') parser.add_argument('--input', help='input video file') parser.add_argument('--output', help='output video file (mp4 format)') parser.add_argument('--checkpoint', help='Checkpoint file') parser.add_argument('--device', default='cuda:0', help='Device used for inference') parser.add_argument('--show', action='store_true', default=False, help='whether to show visualizations.') parser.add_argument('--color', default=(0, 255, 0), help='Color of tracked bbox lines.') parser.add_argument('--thickness', default=3, type=int, help='Thickness of bbox lines.') parser.add_argument('--fps', help='FPS of the output video') parser.add_argument('--gt_bbox_file', help='The path of gt_bbox file') args = parser.parse_args() # load images if osp.isdir(args.input): imgs = sorted(filter(lambda x: x.endswith(('.jpg', '.png', '.jpeg')), os.listdir(args.input)), key=lambda x: int(x.split('.')[0])) IN_VIDEO = False else: imgs = mmcv.VideoReader(args.input) IN_VIDEO = True # define output if args.output is not None: if args.output.endswith('.mp4'): OUT_VIDEO = True out_dir = tempfile.TemporaryDirectory() out_path = out_dir.name _out = args.output.rsplit(os.sep, 1) if len(_out) > 1: os.makedirs(_out[0], exist_ok=True) else: OUT_VIDEO = False out_path = args.output os.makedirs(out_path, exist_ok=True) fps = args.fps if args.show or OUT_VIDEO: if fps is None and IN_VIDEO: fps = imgs.fps if not fps: raise ValueError('Please set the FPS for the output video.') fps = int(fps) # build the model from a config file and a checkpoint file model = init_model(args.config, args.checkpoint, device=args.device) prog_bar = mmcv.ProgressBar(len(imgs)) # test and show/save the images for i, img in enumerate(imgs): if isinstance(img, str): img = osp.join(args.input, img) img = mmcv.imread(img) if i == 0: if args.gt_bbox_file is not None: bboxes = mmcv.list_from_file(args.gt_bbox_file) init_bbox = list(map(float, bboxes[0].split(','))) else: init_bbox = list(cv2.selectROI(args.input, img, False, False)) # convert (x1, y1, w, h) to (x1, y1, x2, y2) init_bbox[2] += init_bbox[0] init_bbox[3] += init_bbox[1] result = inference_sot(model, img, init_bbox, frame_id=i) if args.output is not None: if IN_VIDEO or OUT_VIDEO: out_file = osp.join(out_path, f'{i:06d}.jpg') else: out_file = osp.join(out_path, img.rsplit('os.sep', 1)[-1]) else: out_file = None model.show_result(img, result, show=args.show, wait_time=int(1000. / fps) if fps else 0, out_file=out_file, thickness=args.thickness) prog_bar.update() if args.output and OUT_VIDEO: print( f'\nmaking the output video at {args.output} with a FPS of {fps}') mmcv.frames2video(out_path, args.output, fps=fps, fourcc='mp4v') out_dir.cleanup()
def main(): parser = ArgumentParser() parser.add_argument('config', help='config file') parser.add_argument('--input', help='input video file or folder') parser.add_argument('--output', help='output video file (mp4 format) or folder') parser.add_argument('--checkpoint', help='checkpoint file') parser.add_argument('--device', default='cuda:0', help='device used for inference') parser.add_argument('--show', action='store_true', help='whether show the results on the fly') parser.add_argument('--backend', choices=['cv2', 'plt'], default='cv2', help='the backend to visualize the results') parser.add_argument('--fps', help='FPS of the output video') args = parser.parse_args() assert args.output or args.show # load images if osp.isdir(args.input): imgs = sorted(os.listdir(args.input)) IN_VIDEO = False else: imgs = mmcv.VideoReader(args.input) IN_VIDEO = True # define output if args.output is not None: if args.output.endswith('.mp4'): OUT_VIDEO = True out_dir = tempfile.TemporaryDirectory() out_path = out_dir.name _out = args.output.rsplit('/', 1) if len(_out) > 1: os.makedirs(_out[0], exist_ok=True) else: OUT_VIDEO = False out_path = args.output os.makedirs(out_path, exist_ok=True) fps = args.fps if args.show or OUT_VIDEO: if fps is None and IN_VIDEO: fps = imgs.fps if not fps: raise ValueError('Please set the FPS for the output video.') fps = int(fps) # build the model from a config file and a checkpoint file model = init_model(args.config, args.checkpoint, device=args.device) prog_bar = mmcv.ProgressBar(len(imgs)) # test and show/save the images for i, img in enumerate(imgs): if isinstance(img, str): img = osp.join(args.input, img) result = inference_mot(model, img, frame_id=i) if args.output is not None: if IN_VIDEO or OUT_VIDEO: out_file = osp.join(out_path, f'{i:06d}.jpg') else: out_file = osp.join(out_path, img.rsplit('/', 1)[-1]) else: out_file = None model.show_result(img, result, show=args.show, wait_time=int(1000. / fps) if fps else 0, out_file=out_file, backend=args.backend) prog_bar.update() if OUT_VIDEO: print(f'making the output video at {args.output} with a FPS of {fps}') mmcv.frames2video(out_path, args.output, fps=fps) out_dir.cleanup()
def main(): parser = ArgumentParser() parser.add_argument('config', help='Config file') parser.add_argument('--input', help='input video file') parser.add_argument('--output', help='output video file (mp4 format)') parser.add_argument('--checkpoint', help='Checkpoint file') parser.add_argument( '--device', default='cuda:0', help='Device used for inference') parser.add_argument( '--show', action='store_true', default=False, help='whether to show visualizations.') parser.add_argument( '--score-thr', type=float, default=0.8, help='bbox score threshold') parser.add_argument( '--thickness', default=3, type=int, help='Thickness of bbox lines.') parser.add_argument('--fps', help='FPS of the output video') args = parser.parse_args() # load images if osp.isdir(args.input): imgs = sorted(os.listdir(args.input)) IN_VIDEO = False else: imgs = mmcv.VideoReader(args.input) IN_VIDEO = True # define output if args.output is not None: if args.output.endswith('.mp4'): OUT_VIDEO = True out_dir = tempfile.TemporaryDirectory() out_path = out_dir.name _out = args.output.rsplit('/', 1) if len(_out) > 1: os.makedirs(_out[0], exist_ok=True) else: OUT_VIDEO = False out_path = args.output os.makedirs(out_path, exist_ok=True) fps = args.fps if args.show or OUT_VIDEO: if fps is None and IN_VIDEO: fps = imgs.fps if not fps: raise ValueError('Please set the FPS for the output video.') fps = int(fps) # build the model from a config file and a checkpoint file model = init_model(args.config, args.checkpoint, device=args.device) prog_bar = mmcv.ProgressBar(len(imgs)) # test and show/save the images for i, img in enumerate(imgs): if isinstance(img, str): img = osp.join(args.input, img) img = mmcv.imread(img) result = inference_vid(model, img, frame_id=i) if args.output is not None: if IN_VIDEO or OUT_VIDEO: out_file = osp.join(out_path, f'{i:06d}.jpg') else: out_file = osp.join(out_path, img.rsplit('/', 1)[-1]) else: out_file = None model.show_result( img, result, score_thr=args.score_thr, show=args.show, wait_time=int(1000. / fps) if fps else 0, out_file=out_file, thickness=args.thickness) prog_bar.update() if OUT_VIDEO: print( f'\nmaking the output video at {args.output} with a FPS of {fps}') mmcv.frames2video(out_path, args.output, fps=fps, fourcc='mp4v') out_dir.cleanup()