def recognize_from_video(): etl_word = codecs.open(ETL_PATH, 'r', 'utf-8').readlines() # net initialize classifier = ailia.Classifier( MODEL_PATH, WEIGHT_PATH, env_id=args.env_id, format=ailia.NETWORK_IMAGE_FORMAT_GRAY, range=ailia.NETWORK_IMAGE_RANGE_U_FP32, ) capture = webcamera_utils.get_capture(args.video) # create video writer if savepath is specified as video format if args.savepath is not None: 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 in_frame, frame = webcamera_utils.adjust_frame_size( frame, IMAGE_HEIGHT, IMAGE_WIDTH) frame = preprocess_image(frame) # inference # compute execution time classifier.compute(frame, MAX_CLASS_COUNT) # get result count = classifier.get_class_count() logger.info('=' * 80) logger.info(f'class_count: {count}') for idx in range(count): logger.info(f"+ idx={idx}") info = classifier.get_class(idx) logger.info( f" category={info.category} [ {etl_word[info.category].rstrip()} ]" ) logger.info(f" prob={info.prob}") cv2.imshow('frame', in_frame) # save results if writer is not None: writer.write(in_frame) time.sleep(SLEEP_TIME) capture.release() cv2.destroyAllWindows() if writer is not None: writer.release() logger.info('Script finished successfully.')
def recognize_from_video(): # net initialize pose = ailia.PoseEstimator(MODEL_PATH, WEIGHT_PATH, env_id=args.env_id, algorithm=ALGORITHM) shape = pose.get_input_shape() print(shape) IMAGE_WIDTH = shape[3] IMAGE_HEIGHT = shape[2] capture = get_capture(args.video) while (True): ret, frame = capture.read() if (cv2.waitKey(1) & 0xFF == ord('q')) or not ret: break input_image, input_data = adjust_frame_size( frame, IMAGE_HEIGHT, IMAGE_WIDTH, ) input_data = cv2.cvtColor(input_data, cv2.COLOR_BGR2BGRA) # inference _ = pose.compute(input_data) # postprocessing display_result(input_image, pose) cv2.imshow('frame', input_image) capture.release() cv2.destroyAllWindows() print('Script finished successfully.')
def recognize_from_video(): # net initialize classifier = ailia.Classifier( MODEL_PATH, WEIGHT_PATH, env_id=args.env_id, format=ailia.NETWORK_IMAGE_FORMAT_RGB, range=ailia.NETWORK_IMAGE_RANGE_U_FP32, ) capture = webcamera_utils.get_capture(args.video) # create video writer if savepath is specified as video format if args.savepath is not None: 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 # prepare input data input_image, input_data = webcamera_utils.adjust_frame_size( frame, IMAGE_HEIGHT, IMAGE_WIDTH) input_data = cv2.cvtColor(input_data, cv2.COLOR_BGR2BGRA) # inference classifier.compute(input_data, MAX_CLASS_COUNT) # get result count = classifier.get_class_count() logger.info('=' * 80) for idx in range(count): # logger.info result logger.info(f'+ idx={idx}') info = classifier.get_class(idx) logger.info( f' category={info.category}' f'[ {inceptionv3_labels.imagenet_category[info.category]} ]') logger.info(f' prob={info.prob}') cv2.imshow('frame', input_image) time.sleep(SLEEP_TIME) # 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 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) net.set_input_shape((1, IMAGE_HEIGHT, IMAGE_WIDTH, 3)) 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) fig = create_figure() tight_layout = True while(True): ret, frame = capture.read() if cv2.waitKey(1) & 0xFF == ord('q'): break if not ret: continue input_image, resized_img = adjust_frame_size( frame, IMAGE_HEIGHT, IMAGE_WIDTH ) resized_img = cv2.cvtColor(resized_img, cv2.COLOR_BGR2RGB) if args.apply_rotate: rotation_angle = np.random.randint(360) rotated_img = generate_rotated_image( resized_img, rotation_angle, size=(IMAGE_HEIGHT, IMAGE_WIDTH), crop_center=True, crop_largest_rect=True ) input_data = rotated_img.reshape((1, IMAGE_HEIGHT, IMAGE_WIDTH, 3)) else: rotation_angle = 0 rotated_img = resized_img input_data = rotated_img.reshape((1, IMAGE_HEIGHT, IMAGE_WIDTH, 3)) # inference preds_ailia = net.predict(input_data) # visualize predicted_angle = np.argmax(preds_ailia, axis=1)[0] plt = visualize(fig, rotated_img, rotation_angle, predicted_angle, tight_layout) plt.pause(.01) tight_layout = False capture.release() cv2.destroyAllWindows() print('Script finished successfully.')
def compare_videoframe_image(): # net initialize net = ailia.Net(MODEL_PATH, WEIGHT_PATH, env_id=args.env_id) # img part fname = args.video[1] input_data = load_and_preprocess(fname) _ = net.predict(input_data) i_feature = net.get_blob_data(net.find_blob_index_by_name('conv5_3')) # video part capture = webcamera_utils.get_capture(args.video[0]) # create video writer if savepath is specified as video format if args.savepath is not None: writer = webcamera_utils.get_writer( args.savepath, IMAGE_HEIGHT, IMAGE_WIDTH ) else: writer = None while(True): ret, frame = capture.read() if (cv2.waitKey(1) & 0xFF == ord('q')) or not ret: break _, resized_frame = webcamera_utils.adjust_frame_size( frame, IMAGE_HEIGHT, IMAGE_WIDTH ) input_data = preprocess(resized_frame, input_is_bgr=True) # inference _ = net.predict(input_data) v_feature = net.get_blob_data(net.find_blob_index_by_name('conv5_3')) # show result dist = distance(i_feature, v_feature) print('=============================================================') print(f'{os.path.basename(fname)} vs video frame = {dist}') if dist < THRESHOLD: print('Same person') else: print('Not same person') cv2.imshow('frame', resized_frame) time.sleep(SLEEP_TIME) # save results if writer is not None: writer.write(resized_frame) capture.release() cv2.destroyAllWindows() if writer is not None: writer.release() print('Script finished successfully.')
def recognize_from_video(): # net initialize detector = ailia.Detector(MODEL_PATH, WEIGHT_PATH, len(FACE_CATEGORY), format=ailia.NETWORK_IMAGE_FORMAT_RGB, channel=ailia.NETWORK_IMAGE_CHANNEL_FIRST, range=RANGE, algorithm=ALGORITHM, env_id=args.env_id) capture = webcamera_utils.get_capture(args.video) if args.savepath != SAVE_IMAGE_PATH: writer = webcamera_utils.get_writer( args.savepath, IMAGE_HEIGHT, IMAGE_WIDTH, fps=capture.get(cv2.CAP_PROP_FPS), ) else: writer = None while (True): ret, frame = capture.read() if (cv2.waitKey(1) & 0xFF == ord('q')) or not ret: break _, resized_img = webcamera_utils.adjust_frame_size( frame, IMAGE_HEIGHT, IMAGE_WIDTH) img = cv2.cvtColor(resized_img, cv2.COLOR_BGR2BGRA) detector.compute(img, THRESHOLD, IOU) detections = [] for idx in range(detector.get_object_count()): obj = detector.get_object(idx) detections.append(obj) detections = nms_between_categories(detections, frame.shape[1], frame.shape[0], categories=[0, 1], iou_threshold=IOU) res_img = plot_results(detections, resized_img, FACE_CATEGORY, False) cv2.imshow('frame', res_img) # 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 preprocessing(img): original_img, img = webcamera_utils.adjust_frame_size( img, INPUT_HEIGHT, INPUT_WIDTH) input_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) input_img = input_img / 255.0 input_img = input_img - mean / std input_img = input_img.transpose(2, 0, 1) return img, np.expand_dims(input_img, 0).astype(np.float32)
def recognize_from_video(): # net initialize env_id = ailia.get_gpu_environment_id() print(f'env_id: {env_id}') classifier = ailia.Classifier( MODEL_PATH, WEIGHT_PATH, env_id=env_id, format=ailia.NETWORK_IMAGE_FORMAT_RGB, range=ailia.NETWORK_IMAGE_RANGE_U_FP32 ) 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 _, resized_frame = adjust_frame_size(frame, IMAGE_HEIGHT, IMAGE_WIDTH) input_data = cv2.cvtColor( resized_frame.astype(np.float32), cv2.COLOR_RGB2BGRA ).astype(np.uint8) classifier.compute(input_data, MAX_CLASS_COUNT) count = classifier.get_class_count() # show results print('==============================================================') print(f'class_count: {count}') for idx in range(count): print(f'+ idx={idx}') info = classifier.get_class(idx) print(f' category={info.category} [ ' +\ f'{googlenet_labels.imagenet_category[info.category]} ]') print(f' prob={info.prob}') cv2.imshow('frame', frame) time.sleep(SLEEP_TIME) capture.release() cv2.destroyAllWindows() print('Script finished successfully.')
def recognize_from_video(): etl_word = codecs.open(ETL_PATH, 'r', 'utf-8').readlines() # net initialize env_id = ailia.get_gpu_environment_id() print(f'env_id: {env_id}') classifier = ailia.Classifier( MODEL_PATH, WEIGHT_PATH, env_id=env_id, format=ailia.NETWORK_IMAGE_FORMAT_GRAY, range=ailia.NETWORK_IMAGE_RANGE_U_FP32 ) 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 in_frame, frame = adjust_frame_size(frame, IMAGE_HEIGHT, IMAGE_WIDTH) frame = preprocess_image(frame) # inference # compute execution time classifier.compute(frame, MAX_CLASS_COUNT) # get result count = classifier.get_class_count() print('==============================================================') print(f'class_count: {count}') for idx in range(count) : print(f"+ idx={idx}") info = classifier.get_class(idx) print(f" category={info.category} [ {etl_word[info.category]} ]" ) print(f" prob={info.prob}") cv2.imshow('frame', in_frame) time.sleep(SLEEP_TIME) capture.release() cv2.destroyAllWindows() print('Script finished successfully.')
def get_faces(detector, frame, w, h): # detect face org_detections = [] if args.face == "blazeface": org_detections = compute_blazeface(detector, frame) else: img = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA) detector.compute(img, args.face_threshold, FACE_IOU) count = detector.get_object_count() for idx in range(count): obj = detector.get_object(idx) org_detections.append(obj) # remove overwrapped detection org_detections = nms_between_categories(org_detections, w, h, categories=[0, 1], iou_threshold=FACE_IOU) detections = [] for idx in range(len(org_detections)): # get detected face obj = org_detections[idx] margin = FACE_MARGIN cx = (obj.x + obj.w / 2) * w cy = (obj.y + obj.h / 2) * h cw = max(obj.w * w * margin, obj.h * h * margin) fx = max(cx - cw / 2, 0) fy = max(cy - cw / 2, 0) fw = min(cw, w - fx) fh = min(cw, h - fy) top_left = (int(fx), int(fy)) bottom_right = (int((fx + fw)), int(fy + fh)) # get detected face crop_img = frame[top_left[1]:bottom_right[1], top_left[0]:bottom_right[0], 0:3] if crop_img.shape[0] <= 0 or crop_img.shape[1] <= 0: continue crop_img, resized_frame = adjust_frame_size(crop_img, IMAGE_HEIGHT, IMAGE_WIDTH) detections.append({ "resized_frame": resized_frame, "top_left": top_left, "bottom_right": bottom_right, "id_sim": 0, "score_sim": 0, "fe": None }) return detections
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: print( '[WARNING] currently, video results cannot be output correctly...') # TODO: DEBUG: save image shape 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 _, resized_image = webcamera_utils.adjust_frame_size( frame, IMAGE_HEIGHT, IMAGE_WIDTH) # add noise resized_image = add_noise(resized_image) resized_image = resized_image / 255.0 input_data = resized_image.transpose(2, 0, 1) input_data.shape = (1, ) + input_data.shape # inference preds_ailia = net.predict(input_data) # side by side preds_ailia[:, :, :, 0:input_data.shape[3] // 2] = input_data[:, :, :, 0:input_data.shape[3] // 2] # postprocessing output_img = preds_ailia[0].transpose(1, 2, 0) cv2.imshow('frame', output_img) # # save results # if writer is not None: # writer.write(output_img) capture.release() cv2.destroyAllWindows() if writer is not None: writer.release() print('Script finished successfully.')
def recognize_from_video(): # net initialize pose = ailia.PoseEstimator( MODEL_PATH, WEIGHT_PATH, env_id=args.env_id, algorithm=ALGORITHM ) baseline = ailia.Net( BASELINE_MODEL_PATH, BASELINE_WEIGHT_PATH, env_id=args.env_id ) baseline.set_input_shape((1, 32)) 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.adjust_frame_size( frame, IMAGE_HEIGHT, IMAGE_WIDTH, ) input_data = cv2.cvtColor(input_data, cv2.COLOR_BGR2BGRA) # inference _ = pose.compute(input_data) # postprocessing display_result(input_image, pose, baseline) cv2.imshow('frame', input_image) # display 3d pose plt.pause(0.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 compare_videoframe_image(): # 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) # img part fname = args.video[1] input_data = load_and_preprocess(fname) _ = net.predict(input_data) i_feature = net.get_blob_data(net.find_blob_index_by_name('conv5_3')) # video part if args.video[0] == '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 _, resized_frame = adjust_frame_size(frame, IMAGE_HEIGHT, IMAGE_WIDTH) input_data = preprocess(resized_frame, input_is_bgr=True) # inference _ = net.predict(input_data) v_feature = net.get_blob_data(net.find_blob_index_by_name('conv5_3')) # show result dist = distance(i_feature, v_feature) print('=============================================================') print(f'{os.path.basename(fname)} vs video frame = {dist}') if dist < THRESHOLD: print('Same person') else: print('Not same person') cv2.imshow('frame', resized_frame) time.sleep(SLEEP_TIME) capture.release() cv2.destroyAllWindows() print('Script finished successfully.')
def recognize_from_video(): # net initialize classifier = ailia.Classifier( MODEL_PATH, WEIGHT_PATH, env_id=args.env_id, format=ailia.NETWORK_IMAGE_FORMAT_RGB, range=IMAGE_RANGE, ) capture = webcamera_utils.get_capture(args.video) # create video writer if savepath is specified as video format if args.savepath is not None: 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 in_frame, frame = webcamera_utils.adjust_frame_size( frame, IMAGE_HEIGHT, IMAGE_WIDTH) frame = preprocess_image(frame) # inference classifier.compute(frame, MAX_CLASS_COUNT) # get result # count = classifier.get_class_count() plot_results(in_frame, classifier, resnet50_labels.imagenet_category) cv2.imshow('frame', in_frame) time.sleep(SLEEP_TIME) # save results if writer is not None: writer.write(in_frame) capture.release() cv2.destroyAllWindows() if writer is not None: writer.release() logger.info('Script finished successfully.')
def compare_image_and_video(): # prepare base image base_imgs = prepare_input_data(args.video[1]) # net itinialize env_id = ailia.get_gpu_environment_id() print(f'env_id: {env_id}') net = ailia.Net(MODEL_PATH, WEIGHT_PATH, env_id=env_id) # web camera if args.video[0] == '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[0]): capture = cv2.VideoCapture(args.video[0]) # inference loop while (True): ret, frame = capture.read() if cv2.waitKey(1) & 0xFF == ord('q'): break if not ret: continue frame, resized_frame = adjust_frame_size(frame, IMAGE_HEIGHT, IMAGE_WIDTH) input_frame = preprocess_image(resized_frame, input_is_bgr=True) input_data = np.concatenate([base_imgs, input_frame], axis=0) # inference preds_ailia = net.predict(input_data) # postprocessing fe_1 = np.concatenate([preds_ailia[0], preds_ailia[1]], axis=0) fe_2 = np.concatenate([preds_ailia[2], preds_ailia[3]], axis=0) sim = cosin_metric(fe_1, fe_2) bool_sim = False if THRESHOLD > sim else True frame = draw_result_on_img( frame, texts=[f"Similarity: {sim:06.3f}", f"SAME FACE: {bool_sim}"]) cv2.imshow('frame', frame) capture.release() cv2.destroyAllWindows() print('Script finished successfully.')
def recognize_tag_from_video(): # net initialize tag_net = ailia.Net(TAG_MODEL_PATH, TAG_WEIGHT_PATH, env_id=args.env_id) capture = webcamera_utils.get_capture(args.video) if check_file_existance(TAG_PATH): tags = np.array(json.loads(open(TAG_PATH, 'r').read())) assert len(tags) == 1539 # create video writer if savepath is specified as video format if args.savepath != SAVE_IMAGE_PATH: writer = webcamera_utils.get_writer( args.savepath, IMAGE_HEIGHT, IMAGE_WIDTH ) else: writer = None while(True): ret, frame = capture.read() if (cv2.waitKey(1) & 0xFF == ord('q')) or not ret: break _, frame = webcamera_utils.adjust_frame_size( frame, IMAGE_HEIGHT, IMAGE_WIDTH ) input_data = prepare_input_data(frame, bgr=True) input_dict = {'data': input_data} tag_net.set_input_shape(input_data.shape) # inference preds_ailia = tag_net.predict(input_dict)[0] prob = preds_ailia.reshape(preds_ailia.shape[0], -1) preds = estimate_top_tags(prob, tags, 512) print('==============================================================') pprint(apply_threshold(preds, THRESHOLD)) cv2.imshow('frame', frame) time.sleep(SLEEP_TIME) # save results if writer is not None: writer.write(frame) capture.release() cv2.destroyAllWindows() if writer is not None: writer.release() print('Script finished successfully.')
def recognize_from_video(): # net initialize env_id = ailia.get_gpu_environment_id() print(f'env_id: {env_id}') pose = ailia.PoseEstimator(MODEL_PATH, WEIGHT_PATH, env_id=env_id, algorithm=ALGORITHM) baseline = ailia.Net(BASELINE_MODEL_PATH, BASELINE_WEIGHT_PATH, env_id=env_id) baseline.set_input_shape((1, 32)) 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 = adjust_frame_size( frame, IMAGE_HEIGHT, IMAGE_WIDTH, ) input_data = cv2.cvtColor(input_data, cv2.COLOR_BGR2BGRA) # inferece _ = pose.compute(input_data) # postprocessing display_result(input_image, pose, baseline) cv2.imshow('frame', input_image) # display 3d pose plt.pause(0.01) capture.release() cv2.destroyAllWindows() print('Script finished successfully.')
def recognize_from_video(): # net initialize classifier = ailia.Classifier( MODEL_PATH, WEIGHT_PATH, env_id=args.env_id, format=ailia.NETWORK_IMAGE_FORMAT_RGB, range=ailia.NETWORK_IMAGE_RANGE_S_FP32, ) capture = webcamera_utils.get_capture(args.video) # create video writer if savepath is specified as video format if args.savepath is not None: 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 _, resized_frame = webcamera_utils.adjust_frame_size( frame, IMAGE_HEIGHT, IMAGE_WIDTH ) input_data = cv2.cvtColor( resized_frame.astype(np.float32), cv2.COLOR_RGB2BGRA ).astype(np.uint8) classifier.compute(input_data, MAX_CLASS_COUNT) # count = classifier.get_class_count() # show results plot_results(frame, classifier, efficientnet_labels.imagenet_category) cv2.imshow('frame', frame) time.sleep(SLEEP_TIME) # save results if writer is not None: writer.write(frame) 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}') detector = ailia.Detector( MODEL_PATH, WEIGHT_PATH, len(FACE_CATEGORY), format=ailia.NETWORK_IMAGE_FORMAT_RGB, channel=ailia.NETWORK_IMAGE_CHANNEL_FIRST, range=RANGE, algorithm=ALGORITHM, 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 _, resized_img = adjust_frame_size(frame, IMAGE_HEIGHT, IMAGE_WIDTH) img = cv2.cvtColor(resized_img, cv2.COLOR_BGR2BGRA) detector.compute(img, THRESHOLD, IOU) detections = [] for idx in range(detector.get_object_count()): obj = detector.get_object(idx) detections.append(obj) detections=nms_between_categories(detections,frame.shape[1],frame.shape[0],categories=[0,1],iou_threshold=IOU) res_img = plot_results(detections, resized_img, FACE_CATEGORY, False) cv2.imshow('frame', res_img) capture.release() cv2.destroyAllWindows() print('Script finished successfully.')
def recognize_from_video(): # net initialize categories = 80 threshold = 0.4 iou = 0.45 detector = ailia.Detector( MODEL_PATH, WEIGHT_PATH, categories, format=ailia.NETWORK_IMAGE_FORMAT_RGB, channel=ailia.NETWORK_IMAGE_CHANNEL_FIRST, range=ailia.NETWORK_IMAGE_RANGE_U_FP32, algorithm=ailia.DETECTOR_ALGORITHM_SSD, 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: writer = webcamera_utils.get_writer( args.savepath, IMAGE_HEIGHT, IMAGE_WIDTH ) else: writer = None while(True): ret, frame = capture.read() if (cv2.waitKey(1) & 0xFF == ord('q')) or not ret: break _, resized_img = webcamera_utils.adjust_frame_size( frame, IMAGE_HEIGHT, IMAGE_WIDTH ) img = cv2.cvtColor(resized_img, cv2.COLOR_BGR2BGRA) detector.compute(img, threshold, iou) res_img = plot_results(detector, resized_img, VOC_CATEGORY, False) cv2.imshow('frame', res_img) # 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 pose = ailia.PoseEstimator(MODEL_PATH, WEIGHT_PATH, env_id=args.env_id, algorithm=ALGORITHM) if args.threshold != THRESHOLD_DEFAULT: pose.set_threshold(args.threshold) 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.adjust_frame_size( frame, IMAGE_HEIGHT, IMAGE_WIDTH, ) input_data = cv2.cvtColor(input_data, cv2.COLOR_BGR2BGRA) # inference _ = pose.compute(input_data) # postprocessing display_result(input_image, pose) 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_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 # prepare frame img, resized_img = adjust_frame_size(frame, IMAGE_HEIGHT, IMAGE_WIDTH) data = cv2.cvtColor(resized_img, cv2.COLOR_BGR2GRAY) data = cv2.equalizeHist(data) if platform.system() == 'Darwin': data = data[np.newaxis, np.newaxis, :, :] / 255.0 - 0.5 else: data = data[np.newaxis, np.newaxis, :, :] / 127.5 - 1.0 eyeI = np.concatenate((data, data), axis=0) eyeI = eyeI.reshape(2, IMAGE_HEIGHT, IMAGE_WIDTH, 1) # inference preds_ailia = net.predict(eyeI) preds_ailia = net.get_blob_data( net.find_blob_index_by_name(OUTPUT_BLOB_NAME) ) # postprocessing img = plot_on_image(img, preds_ailia) cv2.imshow('frame', img) capture.release() cv2.destroyAllWindows() print('Script finished successfully.')
def extract_feature_vec_from_video(): # net initialize fe_net = ailia.Net(FE_MODEL_PATH, FE_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: writer = webcamera_utils.get_writer( args.savepath, IMAGE_HEIGHT, IMAGE_WIDTH ) else: writer = None while(True): ret, frame = capture.read() if (cv2.waitKey(1) & 0xFF == ord('q')) or not ret: break _, frame = webcamera_utils.adjust_frame_size( frame, IMAGE_HEIGHT, IMAGE_WIDTH ) input_data = prepare_input_data(frame, bgr=True) input_dict = {'data': input_data} fe_net.set_input_shape(input_data.shape) # inference _ = fe_net.predict(input_dict)[0] # Extracting the output of a specifc layer idx = fe_net.find_blob_index_by_name('encode1') preds_ailia = fe_net.get_blob_data(idx) print('==============================================================') print(preds_ailia.reshape(preds_ailia.shape[0], -1)) cv2.imshow('frame', frame) time.sleep(SLEEP_TIME) # save results if writer is not None: writer.write(frame) capture.release() cv2.destroyAllWindows() if writer is not None: writer.release() print('Script finished successfully.')
def recognize_tag_from_video(): # net initialize env_id = ailia.get_gpu_environment_id() print(f'env_id: {env_id}') tag_net = ailia.Net(TAG_MODEL_PATH, TAG_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) if check_file_existance(TAG_PATH): tags = np.array(json.loads(open(TAG_PATH, 'r').read())) assert (len(tags) == 1539) while (True): ret, frame = capture.read() _, frame = adjust_frame_size(frame, IMAGE_HEIGHT, IMAGE_WIDTH) if cv2.waitKey(1) & 0xFF == ord('q'): break if not ret: continue input_data = prepare_input_data(frame, bgr=True) input_dict = {'data': input_data} tag_net.set_input_shape(input_data.shape) # inference preds_ailia = tag_net.predict(input_dict)[0] prob = preds_ailia.reshape(preds_ailia.shape[0], -1) preds = estimate_top_tags(prob, tags, 512) print('==============================================================') pprint(apply_threshold(preds, THRESHOLD)) cv2.imshow('frame', frame) time.sleep(SLEEP_TIME) capture.release() cv2.destroyAllWindows() print('Script finished successfully.')
def recognize_from_video(): # net initialize net = ailia.Net(MODEL_PATH, WEIGHT_PATH, env_id=args.env_id) flag_set_shape = False 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.adjust_frame_size( frame, IMAGE_HEIGHT, IMAGE_WIDTH) input_data = cv2.cvtColor(input_data, cv2.COLOR_BGR2RGB) / 255.0 input_data = input_data[np.newaxis, :, :, :] if not flag_set_shape: net.set_input_shape(input_data.shape) flag_set_shape = True preds_ailia = net.predict(input_data) pred = preds_ailia.reshape((IMAGE_HEIGHT, IMAGE_WIDTH)) dst = transfer(input_image, pred) cv2.imshow('frame', dst) # save results if writer is not None: writer.write(dst) capture.release() cv2.destroyAllWindows() if writer is not None: writer.release() 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 is not None: 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 # prepare input data _, input_data = webcamera_utils.adjust_frame_size( frame, IMAGE_HEIGHT, IMAGE_WIDTH ) input_data = preprocess_input(input_data, IMAGE_HEIGHT, IMAGE_WIDTH) # inference preds = net.predict(input_data) preds = softmax(preds) # get result print_results(preds, imagenet_category) cv2.imshow('frame', frame) time.sleep(SLEEP_TIME) # save results if writer is not None: writer.write(frame) 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}') 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_S_FP32, algorithm=ailia.DETECTOR_ALGORITHM_YOLOV2, env_id=env_id ) detector.set_anchors(ANCHORS) 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 _, resized_img = adjust_frame_size(frame, IMAGE_HEIGHT, IMAGE_WIDTH) img = cv2.cvtColor(resized_img, cv2.COLOR_RGB2BGRA) detector.compute(img, THRESHOLD, IOU) res_img = plot_results(detector, resized_img, COCO_CATEGORY, False) cv2.imshow('frame', res_img) capture.release() cv2.destroyAllWindows() print('Script finished successfully.')
def recognize_from_video(net): 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) * int(args.scale)) f_w = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH) * int(args.scale)) 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 IMAGE_HEIGHT, IMAGE_WIDTH = frame.shape[0], frame.shape[1] # resize with keep aspect frame, resized_img = webcamera_utils.adjust_frame_size( frame, IMAGE_HEIGHT, IMAGE_WIDTH) img = np.ascontiguousarray(resized_img.transpose((2, 0, 1))) img = img[np.newaxis, :, :, :] # (batch_size, channel, h, w) img = img.astype(np.float32) net.set_input_shape((1, 3, IMAGE_HEIGHT, IMAGE_WIDTH)) output = net.run(img) out_img = quantize(output[0][0], 255) out_img = out_img.astype(np.uint8).transpose(1, 2, 0) cv2.imshow('output', out_img) # save results if writer is not None: writer.write(out_img) capture.release() cv2.destroyAllWindows() if writer is not None: writer.release() logger.info('Script finished successfully.')
def extract_feature_vec_from_video(): # net initialize env_id = ailia.get_gpu_environment_id() print(f'env_id: {env_id}') fe_net = ailia.Net(FE_MODEL_PATH, FE_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() _, frame = adjust_frame_size(frame, IMAGE_HEIGHT, IMAGE_WIDTH) if cv2.waitKey(1) & 0xFF == ord('q'): break if not ret: continue input_data = prepare_input_data(frame, bgr=True) input_dict = {'data': input_data} fe_net.set_input_shape(input_data.shape) # inference _ = fe_net.predict(input_dict)[0] # Extracting the output of a specifc layer idx = fe_net.find_blob_index_by_name('encode1') preds_ailia = fe_net.get_blob_data(idx) print('==============================================================') print(preds_ailia.reshape(preds_ailia.shape[0], -1)) cv2.imshow('frame', frame) time.sleep(SLEEP_TIME) capture.release() cv2.destroyAllWindows() print('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 _, resized_image = adjust_frame_size(frame, IMAGE_HEIGHT, IMAGE_WIDTH) if args.add_noise: resized_image = add_noise(resized_image) resized_image = resized_image / 255.0 input_data = resized_image.transpose(2, 0, 1) input_data.shape = (1, ) + input_data.shape # inference preds_ailia = net.predict(input_data) # postprocessing output_img = preds_ailia[0].transpose(1, 2, 0) cv2.imshow('frame', output_img) capture.release() cv2.destroyAllWindows() print('Script finished successfully.')