def recognize_from_image(): # prepare input data input_data = load_image( args.input, (IMAGE_HEIGHT, IMAGE_WIDTH), gen_input_ailia=True ) # net initialize env_id = ailia.get_gpu_environment_id() print(f'env_id: {env_id}') net = ailia.Net(MODEL_PATH, WEIGHT_PATH, env_id=env_id) # inference print('Start inference...') if args.benchmark: print('BENCHMARK mode') for i in range(5): start = int(round(time.time() * 1000)) preds_ailia = net.predict(input_data) end = int(round(time.time() * 1000)) print(f'\tailia processing time {end - start} ms') else: preds_ailia = net.predict(input_data) # postprocessing if args.smooth: preds_ailia = smooth_output(preds_ailia) save_pred(preds_ailia, args.savepath, IMAGE_HEIGHT, IMAGE_WIDTH) print('Script finished successfully.')
def recognize_from_image(): # net initialize net = ailia.Net(MODEL_PATH, WEIGHT_PATH, env_id=args.env_id) # input image loop for image_path in args.input: # prepare input data logger.info(image_path) input_data = load_image( image_path, (IMAGE_HEIGHT, IMAGE_WIDTH), gen_input_ailia=True, ) # inference logger.info('Start inference...') if args.benchmark: logger.info('BENCHMARK mode') for i in range(5): start = int(round(time.time() * 1000)) preds_ailia = net.predict(input_data) end = int(round(time.time() * 1000)) logger.info(f'\tailia processing time {end - start} ms') else: preds_ailia = net.predict(input_data) # postprocessing if args.smooth: preds_ailia = smooth_output(preds_ailia) savepath = get_savepath(args.savepath, image_path) logger.info(f'saved at : {savepath}') save_pred(preds_ailia, savepath, IMAGE_HEIGHT, IMAGE_WIDTH) logger.info('Script finished successfully.')
def recognize_from_video(): # net initialize net = ailia.Net(MODEL_PATH, WEIGHT_PATH, env_id=args.env_id) capture = webcamera_utils.get_capture(args.video) # create video writer if savepath is specified as video format if args.savepath != SAVE_IMAGE_PATH: logger.warning( 'currently, video results cannot be output correctly...') 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 while (True): ret, frame = capture.read() if (cv2.waitKey(1) & 0xFF == ord('q')) or not ret: break input_image, input_data = webcamera_utils.preprocess_frame( frame, IMAGE_HEIGHT, IMAGE_WIDTH, ) # inference preds_ailia = net.predict(input_data) # postprocessing if args.smooth: preds_ailia = smooth_output(preds_ailia) gen_img = gen_preds_img(preds_ailia, IMAGE_HEIGHT, IMAGE_WIDTH) plt.imshow(gen_img) plt.pause(.01) if not plt.get_fignums(): break # # save results # if writer is not None: # writer.write(res_img) capture.release() cv2.destroyAllWindows() if writer is not None: writer.release() logger.info('Script finished successfully.')
def recognize_from_video(): # net initialize env_id = ailia.get_gpu_environment_id() print(f'env_id: {env_id}') net = ailia.Net(MODEL_PATH, WEIGHT_PATH, env_id=env_id) if args.video == '0': print('[INFO] Webcam mode is activated') capture = cv2.VideoCapture(0) if not capture.isOpened(): print("[ERROR] webcamera not found") sys.exit(1) else: if check_file_existance(args.video): capture = cv2.VideoCapture(args.video) while (True): ret, frame = capture.read() if cv2.waitKey(1) & 0xFF == ord('q'): break if not ret: continue input_image, input_data = preprocess_frame( frame, IMAGE_HEIGHT, IMAGE_WIDTH, ) # inference preds_ailia = net.predict(input_data) # postprocessing if args.smooth: preds_ailia = smooth_output(preds_ailia) gen_img = gen_preds_img(preds_ailia, IMAGE_HEIGHT, IMAGE_WIDTH) plt.imshow(gen_img) plt.pause(.01) capture.release() cv2.destroyAllWindows() print('Script finished successfully.')