def recognize_from_video(detector): capture = webcamera_utils.get_capture(args.video) # create video writer if savepath is specified as video format if args.savepath != SAVE_IMAGE_PATH: f_h = int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT)) f_w = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH)) save_h, save_w = f_h, f_w writer = webcamera_utils.get_writer(args.savepath, save_h, save_w) else: writer = None if args.write_prediction: frame_count = 0 frame_digit = int( math.log10(capture.get(cv2.CAP_PROP_FRAME_COUNT)) + 1) video_name = os.path.splitext(os.path.basename(args.video))[0] while (True): ret, frame = capture.read() if (cv2.waitKey(1) & 0xFF == ord('q')) or not ret: break raw_img = frame if args.detector: detector.compute(raw_img, args.threshold, args.iou) res_img = plot_results(detector, raw_img, COCO_CATEGORY) detect_object = detector else: img, ratio = preprocess(raw_img, (HEIGHT, WIDTH)) output = detector.run(img[None, :, :, :]) predictions = postprocess(output[0], (HEIGHT, WIDTH))[0] detect_object = predictions_to_object(predictions, raw_img, ratio, args.iou, args.threshold) detect_object = reverse_letterbox( detect_object, raw_img, (raw_img.shape[0], raw_img.shape[1])) res_img = plot_results(detect_object, raw_img, COCO_CATEGORY) cv2.imshow('frame', res_img) # save results if writer is not None: writer.write(res_img) # write prediction if args.write_prediction: savepath = get_savepath( args.savepath, video_name, post_fix='_%s' % (str(frame_count).zfill(frame_digit) + '_res'), ext='.png') pred_file = '%s.txt' % savepath.rsplit('.', 1)[0] write_predictions(pred_file, detect_object, frame, COCO_CATEGORY) frame_count += 1 capture.release() cv2.destroyAllWindows() if writer is not None: writer.release() logger.info('Script finished successfully.')
def recognize_from_video(): # net initialize detector = ailia.Detector( MODEL_PATH, WEIGHT_PATH, len(COCO_CATEGORY), format=ailia.NETWORK_IMAGE_FORMAT_RGB, channel=ailia.NETWORK_IMAGE_CHANNEL_FIRST, range=ailia.NETWORK_IMAGE_RANGE_U_FP32, algorithm=ailia.DETECTOR_ALGORITHM_YOLOV3, env_id=args.env_id, ) if args.detection_width != DETECTION_SIZE or args.detection_height != DETECTION_SIZE: detector.set_input_shape( args.detection_width, args.detection_height ) capture = webcamera_utils.get_capture(args.video) # create video writer if savepath is specified as video format if args.savepath != SAVE_IMAGE_PATH: f_h = int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT)) f_w = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH)) writer = webcamera_utils.get_writer(args.savepath, f_h, f_w) else: writer = None if args.write_prediction: frame_count = 0 frame_digit = int(math.log10(capture.get(cv2.CAP_PROP_FRAME_COUNT)) + 1) video_name = os.path.splitext(os.path.basename(args.video))[0] while(True): ret, frame = capture.read() if (cv2.waitKey(1) & 0xFF == ord('q')) or not ret: break img = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA) detector.compute(img, args.threshold, args.iou) res_img = plot_results(detector, frame, COCO_CATEGORY, False) cv2.imshow('frame', res_img) # save results if writer is not None: writer.write(res_img) # write prediction if args.write_prediction: savepath = get_savepath(args.savepath, video_name, post_fix = '_%s' % (str(frame_count).zfill(frame_digit) + '_res'), ext='.png') pred_file = '%s.txt' % savepath.rsplit('.', 1)[0] write_predictions(pred_file, detector, frame, COCO_CATEGORY) frame_count += 1 capture.release() cv2.destroyAllWindows() if writer is not None: writer.release() logger.info('Script finished successfully.')
def recognize_from_video(video, detector): capture = webcamera_utils.get_capture(args.video) # create video writer if savepath is specified as video format if args.savepath != SAVE_IMAGE_PATH: f_h = int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT)) f_w = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH)) writer = webcamera_utils.get_writer(args.savepath, f_h, f_w) else: writer = None if args.write_prediction: frame_count = 0 frame_digit = int( math.log10(capture.get(cv2.CAP_PROP_FRAME_COUNT)) + 1) video_name = os.path.splitext(os.path.basename(args.video))[0] while (True): ret, img = capture.read() # press q to end video capture if (cv2.waitKey(1) & 0xFF == ord('q')) or not ret: break boxes, scores, cls_inds = detect_objects(img, detector) Detection = namedtuple('Detection', ['category', 'prob', 'x', 'y', 'w', 'h']) ary = [] h, w = (img.shape[0], img.shape[1]) for i, box in enumerate(boxes): d = Detection(int(cls_inds[i]), scores[i], box[0] / w, box[1] / h, (box[2] - box[0]) / w, (box[3] - box[1]) / h) ary.append(d) img = plot_results(ary, img, COCO_CATEGORY) cv2.imshow('frame', img) # save results if writer is not None: writer.write(img) # write prediction if args.write_prediction: savepath = get_savepath( args.savepath, video_name, post_fix='_%s' % (str(frame_count).zfill(frame_digit) + '_res'), ext='.png') pred_file = '%s.txt' % savepath.rsplit('.', 1)[0] write_predictions(pred_file, ary, img, COCO_CATEGORY) frame_count += 1 capture.release() cv2.destroyAllWindows() if writer is not None: writer.release()
def recognize_from_image(): # net initialize detector = ailia.Detector( MODEL_PATH, WEIGHT_PATH, len(COCO_CATEGORY), format=ailia.NETWORK_IMAGE_FORMAT_RGB, channel=ailia.NETWORK_IMAGE_CHANNEL_FIRST, range=ailia.NETWORK_IMAGE_RANGE_U_FP32, algorithm=ailia.DETECTOR_ALGORITHM_YOLOV3, env_id=args.env_id, ) if args.detection_width != DETECTION_SIZE or args.detection_height != DETECTION_SIZE: detector.set_input_shape( args.detection_width, args.detection_height ) if args.profile: detector.set_profile_mode(True) # input image loop for image_path in args.input: # prepare input data logger.info(image_path) img = load_image(image_path) logger.debug(f'input image shape: {img.shape}') # inference logger.info('Start inference...') if args.benchmark: logger.info('BENCHMARK mode') total_time = 0 for i in range(args.benchmark_count): start = int(round(time.time() * 1000)) detector.compute(img, args.threshold, args.iou) end = int(round(time.time() * 1000)) if i != 0: total_time = total_time + (end - start) logger.info(f'\tailia processing time {end - start} ms') logger.info(f'\taverage time {total_time / (args.benchmark_count-1)} ms') else: detector.compute(img, args.threshold, args.iou) # plot result res_img = plot_results(detector, img, COCO_CATEGORY) savepath = get_savepath(args.savepath, image_path) logger.info(f'saved at : {savepath}') cv2.imwrite(savepath, res_img) # write prediction if args.write_prediction: pred_file = '%s.txt' % savepath.rsplit('.', 1)[0] write_predictions(pred_file, detector, img, COCO_CATEGORY) if args.profile: print(detector.get_summary()) logger.info('Script finished successfully.')
def recognize_from_video(detector): capture = webcamera_utils.get_capture(args.video) # create video writer if savepath is specified as video format if args.savepath != SAVE_IMAGE_PATH: f_h = int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT)) f_w = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH)) writer = webcamera_utils.get_writer(args.savepath, f_h, f_w) else: writer = None if args.write_prediction: frame_count = 0 frame_digit = int(math.log10(capture.get(cv2.CAP_PROP_FRAME_COUNT)) + 1) video_name = os.path.splitext(os.path.basename(args.video))[0] while (True): ret, frame = capture.read() if (cv2.waitKey(1) & 0xFF == ord('q')) or not ret: break if args.detector: img = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA) detector.compute(img, args.threshold, args.iou) res_img = plot_results(detector, frame, COCO_CATEGORY) else: img = letterbox_convert(frame, (IMAGE_HEIGHT, IMAGE_WIDTH)) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = np.transpose(img, [2, 0, 1]) img = img.astype(np.float32) / 255 img = np.expand_dims(img, 0) output = detector.predict([img]) detect_object = post_processing( img, args.threshold, args.iou, output ) detect_object = reverse_letterbox(detect_object[0], frame, (IMAGE_HEIGHT,IMAGE_WIDTH)) res_img = plot_results(detect_object, frame, COCO_CATEGORY) cv2.imshow('frame', res_img) # save results if writer is not None: writer.write(res_img) # write prediction if args.write_prediction: savepath = get_savepath(args.savepath, video_name, post_fix = '_%s' % (str(frame_count).zfill(frame_digit) + '_res'), ext='.png') pred_file = '%s.txt' % savepath.rsplit('.', 1)[0] write_predictions(pred_file, detect_object, frame, COCO_CATEGORY) frame_count += 1 capture.release() cv2.destroyAllWindows() if writer is not None: writer.release() logger.info('Script finished successfully.')
def recognize_from_image(detector): # input image loop for image_path in args.input: # prepare input data logger.debug(f'input image: {image_path}') raw_img = cv2.imread(image_path, cv2.IMREAD_COLOR) if not args.detector: img, ratio = preprocess(raw_img, (HEIGHT, WIDTH)) logger.debug(f'input image shape: {raw_img.shape}') def compute(): if args.detector: detector.compute(raw_img, args.threshold, args.iou) return None else: return detector.run(img[None, :, :, :]) # inference logger.info('Start inference...') if args.benchmark: logger.info('BENCHMARK mode') total_time = 0 for i in range(args.benchmark_count): start = int(round(time.time() * 1000)) output = compute() end = int(round(time.time() * 1000)) if i != 0: total_time = total_time + (end - start) logger.info(f'\tailia processing time {end - start} ms') logger.info( f'\taverage time {total_time / (args.benchmark_count-1)} ms') else: output = compute() if args.detector: res_img = plot_results(detector, raw_img, COCO_CATEGORY) detect_object = detector else: predictions = postprocess(output[0], (HEIGHT, WIDTH))[0] detect_object = predictions_to_object(predictions, raw_img, ratio, args.iou, args.threshold) detect_object = reverse_letterbox( detect_object, raw_img, (raw_img.shape[0], raw_img.shape[1])) res_img = plot_results(detect_object, raw_img, COCO_CATEGORY) # plot result savepath = get_savepath(args.savepath, image_path) logger.info(f'saved at : {savepath}') cv2.imwrite(savepath, res_img) # write prediction if args.write_prediction: pred_file = '%s.txt' % savepath.rsplit('.', 1)[0] write_predictions(pred_file, detect_object, raw_img, COCO_CATEGORY) logger.info('Script finished successfully.')
def recognize_from_image(): # net initialize detector = ailia.Net(MODEL_PATH, WEIGHT_PATH, env_id=args.env_id) if args.profile: detector.set_profile_mode(True) # input image loop for image_path in args.input: # prepare input data logger.info(image_path) # prepare input data org_img = load_image(image_path) org_img = cv2.cvtColor(org_img, cv2.COLOR_BGRA2BGR) logger.info(f'input image shape: {org_img.shape}') img = letterbox_convert(org_img, (IMAGE_HEIGHT, IMAGE_WIDTH)) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = np.transpose(img, [2, 0, 1]) img = img.astype(np.float32) / 255 img = np.expand_dims(img, 0) # inference logger.info('Start inference...') if args.benchmark: logger.info('BENCHMARK mode') total_time = 0 for i in range(args.benchmark_count): start = int(round(time.time() * 1000)) output = detector.predict([img]) end = int(round(time.time() * 1000)) if i != 0: total_time = total_time + (end - start) logger.info(f'\tailia processing time {end - start} ms') logger.info( f'\taverage time {total_time / (args.benchmark_count-1)} ms') else: output = detector.predict([img]) detect_object = yolov5_utils.post_processing(img, args.threshold, args.iou, output) detect_object = reverse_letterbox(detect_object[0], org_img, (IMAGE_HEIGHT, IMAGE_WIDTH)) # plot result res_img = plot_results(detect_object, org_img, COCO_CATEGORY) # plot result savepath = get_savepath(args.savepath, image_path) logger.info(f'saved at : {savepath}') cv2.imwrite(savepath, res_img) # write prediction if args.write_prediction: pred_file = '%s.txt' % savepath.rsplit('.', 1)[0] write_predictions(pred_file, detect_object, org_img, COCO_CATEGORY) if args.profile: print(detector.get_summary()) logger.info('Script finished successfully.')
def recognize_from_image(filename, detector): if args.profile: detector.set_profile_mode(True) # load input image img = load_image(filename) img = cv2.cvtColor(img, cv2.COLOR_BGRA2BGR) logger.info('Start inference...') if args.benchmark: for mode in range(2): if mode == 0: logger.info('BENCHMARK mode (without post process)') else: logger.info('BENCHMARK mode (with post process)') zeros = np.zeros((1, 3, 512, 512)) total_time = 0 for i in range(args.benchmark_count): start = int(round(time.time() * 1000)) if mode == 0: detector.predict(zeros) else: boxes, scores, cls_inds = detect_objects(img, detector) end = int(round(time.time() * 1000)) if i != 0: total_time = total_time + (end - start) logger.info(f'\tailia processing time {end - start} ms') logger.info( f'\taverage time {total_time / (args.benchmark_count-1)} ms') else: boxes, scores, cls_inds = detect_objects(img, detector) try: logger.info('\n'.join([ 'pos:{}, ids:{}, score:{:.3f}'.format( '(%.1f,%.1f,%.1f,%.1f)' % (box[0], box[1], box[2], box[3]), COCO_CATEGORY[int(obj_cls)], score) for box, obj_cls, score in zip(boxes, cls_inds, scores) ])) except: # FIXME: do not use base 'except' pass Detection = namedtuple('Detection', ['category', 'prob', 'x', 'y', 'w', 'h']) ary = [] h, w = (img.shape[0], img.shape[1]) for i, box in enumerate(boxes): d = Detection(int(cls_inds[i]), scores[i], box[0] / w, box[1] / h, (box[2] - box[0]) / w, (box[3] - box[1]) / h) ary.append(d) im2show = plot_results(ary, img, COCO_CATEGORY) savepath = get_savepath(args.savepath, filename) logger.info(f'saved at : {savepath}') cv2.imwrite(savepath, im2show) # write prediction if args.write_prediction: pred_file = '%s.txt' % savepath.rsplit('.', 1)[0] write_predictions(pred_file, ary, img, category=COCO_CATEGORY) if args.profile: print(detector.get_summary()) logger.info('Script finished successfully.')