def recognize_from_image(): # prepare input data org_img = load_image( args.input, (IMAGE_HEIGHT, IMAGE_WIDTH), ) input_data = load_image(args.input, (IMAGE_HEIGHT, IMAGE_WIDTH), normalize_type='127.5', gen_input_ailia=True) # net initialize net = ailia.Net(MODEL_PATH, WEIGHT_PATH, env_id=args.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 detections = but.postprocess(preds_ailia) # generate detections for detection in detections: but.plot_detections(org_img, detection, save_image_path=args.savepath) print('Script finished successfully.')
def recognize_from_video(net): if args.back == True: IMAGE_HEIGHT = IMAGE_HEIGHT_BACK IMAGE_WIDTH = IMAGE_WIDTH_BACK ANCHOR_PATH = ANCHOR_PATH_BACK else: IMAGE_HEIGHT = IMAGE_HEIGHT_FRONT IMAGE_WIDTH = IMAGE_WIDTH_FRONT ANCHOR_PATH = ANCHOR_PATH_FRONT 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 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, normalize_type='127.5') # inference input_blobs = net.get_input_blob_list() net.set_input_blob_data(input_data, input_blobs[0]) net.update() preds_ailia = net.get_results() # postprocessing detections = but.postprocess(preds_ailia, anchor_path=ANCHOR_PATH, back=args.back) but.show_result(input_image, detections) # remove padding dh = input_image.shape[0] dw = input_image.shape[1] sh = frame.shape[0] sw = frame.shape[1] input_image = input_image[(dh - sh) // 2:(dh - sh) // 2 + sh, (dw - sw) // 2:(dw - sw) // 2 + sw, :] cv2.imshow('frame', input_image) # save results if writer is not None: writer.write(input_image) capture.release() cv2.destroyAllWindows() if writer is not None: writer.release() logger.info('Script finished successfully.')
def face_detect(img, face_net): IMAGE_BLAZE_SIZE = 128 img_0 = img img = normalize_image(img, normalize_type='127.5') img = cv2.resize(img, (IMAGE_BLAZE_SIZE, IMAGE_BLAZE_SIZE)) img = img.transpose((2, 0, 1)) img = np.expand_dims(img, axis=0) output = face_net.predict([img]) detections = but.postprocess(output) detections = detections[0] # sort by confidence detections = sorted(detections, key=lambda x: x[16], reverse=True) if len(detections) == 0: return None, (0, 0) detection = detections[0] h, w = img_0.shape[:2] ymin = int(detection[0] * h) xmin = int(detection[1] * w) ymax = int(detection[2] * h) xmax = int(detection[3] * w) h = ymax - ymin w = xmax - xmin if h > w: p = (h - w) // 2 w = h xmin -= p else: p = (w - h) // 2 h = w ymin -= p img = img_0[ymin:ymin + h, xmin:xmin + w] h2, w2 = img.shape[:2] if h != h2 or w != w2: return None, (0, 0) return img, (ymin, xmin)
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: f_h = int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT)) f_w = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH)) save_h, save_w = webcamera_utils.calc_adjust_fsize( f_h, f_w, IMAGE_HEIGHT, IMAGE_WIDTH) writer = webcamera_utils.get_writer(args.savepath, save_h, save_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, normalize_type='127.5') # inference input_blobs = net.get_input_blob_list() net.set_input_blob_data(input_data, input_blobs[0]) net.update() preds_ailia = net.get_results() # postprocessing detections = but.postprocess(preds_ailia) but.show_result(input_image, detections) cv2.imshow('frame', input_image) # save results if writer is not None: writer.write(input_image) capture.release() cv2.destroyAllWindows() if writer is not None: writer.release() 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) org_img = load_image(image_path, (IMAGE_HEIGHT, IMAGE_WIDTH)) input_data = load_image(image_path, (IMAGE_HEIGHT, IMAGE_WIDTH), normalize_type='127.5', gen_input_ailia=True) # 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)) preds_ailia = net.predict([input_data]) end = int(round(time.time() * 1000)) logger.info(f'\tailia processing time {end - start} ms') if i != 0: total_time = total_time + (end - start) logger.info( f'\taverage time {total_time / (args.benchmark_count-1)} ms') else: preds_ailia = net.predict([input_data]) # post-processing detections = but.postprocess(preds_ailia) # generate detections savepath = get_savepath(args.savepath, image_path) logger.info(f'saved at : {savepath}') for detection in detections: but.plot_detections(org_img, detection, save_image_path=savepath) logger.info('Script finished successfully.')
def recognize_from_image(net): if args.back == True: IMAGE_HEIGHT = IMAGE_HEIGHT_BACK IMAGE_WIDTH = IMAGE_WIDTH_BACK ANCHOR_PATH = ANCHOR_PATH_BACK else: IMAGE_HEIGHT = IMAGE_HEIGHT_FRONT IMAGE_WIDTH = IMAGE_WIDTH_FRONT ANCHOR_PATH = ANCHOR_PATH_FRONT # input image loop for image_path in args.input: # prepare input data logger.info(image_path) org_img = cv2.imread(image_path, cv2.IMREAD_COLOR) input_data = load_image(image_path) input_data = cv2.cvtColor(input_data, cv2.COLOR_BGRA2RGB) input_data, pad_hw, resized_hw = preprocess( input_data, (IMAGE_HEIGHT, IMAGE_WIDTH)) # 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)) preds_ailia = net.predict([input_data]) end = int(round(time.time() * 1000)) logger.info(f'\tailia processing time {end - start} ms') if i != 0: total_time = total_time + (end - start) logger.info( f'\taverage time {total_time / (args.benchmark_count - 1)} ms') else: preds_ailia = net.predict([input_data]) # post-processing detections = but.postprocess(preds_ailia, anchor_path=ANCHOR_PATH, back=args.back) # remove padding pad_x = pad_hw[1] / IMAGE_WIDTH pad_y = pad_hw[0] / IMAGE_HEIGHT resized_x = resized_hw[1] / IMAGE_WIDTH resized_y = resized_hw[0] / IMAGE_HEIGHT for d in detections: d[:, [1, 3, 4, 6, 8, 10, 12, 14]] = ( d[:, [1, 3, 4, 6, 8, 10, 12, 14]] - pad_x) / resized_x d[:, [0, 2, 5, 7, 9, 11, 13, 15]] = ( d[:, [0, 2, 5, 7, 9, 11, 13, 15]] - pad_y) / resized_y # generate detections savepath = get_savepath(args.savepath, image_path) logger.info(f'saved at : {savepath}') for detection in detections: but.plot_detections(org_img, detection, save_image_path=savepath) logger.info('Script finished successfully.')