示例#1
0
def recognize_from_image():
    # prepare input data
    input_data = load_image(args.input, (IMAGE_HEIGHT, IMAGE_WIDTH),
                            normalize_type='None',
                            gen_input_ailia=False)
    input_data = cv2.cvtColor(input_data.astype(np.float32),
                              cv2.COLOR_RGB2BGRA).astype(np.uint8)

    # 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)

    # inference
    print('Start inference...')
    if args.benchmark:
        print('BENCHMARK mode')
        for i in range(5):
            start = int(round(time.time() * 1000))
            classifier.compute(input_data, MAX_CLASS_COUNT)
            # count = classifier.get_class_count()
            end = int(round(time.time() * 1000))
            print(f'\tailia processing time {end - start} ms')
    else:
        classifier.compute(input_data, MAX_CLASS_COUNT)
        # count = classifier.get_class_count()

    # show results
    print_results(classifier, efficientnet_labels.imagenet_category)

    print('Script finished successfully.')
示例#2
0
def recognize_from_image():
    # prepare input data
    img = cv2.imread(args.input, cv2.IMREAD_UNCHANGED)
    img = preprocess_image(img)

    # 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=IMAGE_RANGE)

    # compute execution time
    for i in range(5):
        start = int(round(time.time() * 1000))
        classifier.compute(img, MAX_CLASS_COUNT)
        end = int(round(time.time() * 1000))
        print(f'ailia processing time {end - start} ms')

    # get result
    count = classifier.get_class_count()
    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"[ {resnet50_labels.imagenet_category[info.category]} ]")
        print(f"  prob={info.prob}")
    print('Script finished successfully.')
示例#3
0
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.')
示例#4
0
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.')
示例#5
0
def recognize_from_image():
    # net initialize
    classifier = ailia.Classifier(
        MODEL_PATH,
        WEIGHT_PATH,
        env_id=args.env_id,
        format=ailia.NETWORK_IMAGE_FORMAT_RGB,
        range=IMAGE_RANGE,
    )

    # input image loop
    for image_path in args.input:
        # prepare input data
        logger.info(image_path)
        img = cv2.imread(image_path, cv2.IMREAD_UNCHANGED)
        img = preprocess_image(img)

        # inference
        logger.info('Start inference...')
        if args.benchmark:
            logger.info('BENCHMARK mode')
            for i in range(args.benchmark_count):
                start = int(round(time.time() * 1000))
                classifier.compute(img, MAX_CLASS_COUNT)
                end = int(round(time.time() * 1000))
                logger.info(f'\tailia processing time {end - start} ms')
        else:
            classifier.compute(img, MAX_CLASS_COUNT)

        # show results
        print_results(classifier, resnet50_labels.imagenet_category)
示例#6
0
def recognize_from_image():
    # prepare input data
    etl_word = codecs.open(ETL_PATH, 'r', 'utf-8').readlines()
    img = cv2.imread(args.input, cv2.IMREAD_UNCHANGED)
    img = preprocess_image(img)
    
    # 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
    )

    # compute execution time
    for i in range(5):
        start = int(round(time.time() * 1000))
        classifier.compute(img, MAX_CLASS_COUNT)
        end = int(round(time.time() * 1000))
        print(f'ailia processing time {end - start} ms')

    # get result
    count = classifier.get_class_count()
    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}")
示例#7
0
def recognize_from_image():
    # prepare input data
    input_img = load_image(
        args.input,
        (IMAGE_HEIGHT, IMAGE_WIDTH),
        normalize_type='None',
    )
    input_data = cv2.cvtColor(input_img, cv2.COLOR_BGR2BGRA)

    # 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)

    # compute execution time
    for i in range(5):
        start = int(round(time.time() * 1000))
        classifier.compute(input_data, MAX_CLASS_COUNT)
        count = classifier.get_class_count()
        end = int(round(time.time() * 1000))
        print(f'ailia processing time {end - start} ms')

    # postprocessing
    for idx in range(count):
        # print result
        print(f'+ idx={idx}')
        info = classifier.get_class(idx)
        print(f'  category={info.category}' +\
              f'[ {inceptionv3_labels.imagenet_category[info.category]} ]')
        print(f'  prob={info.prob}')
    print('Script finished successfully.')
示例#8
0
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.')
示例#9
0
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.')
示例#10
0
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.')
示例#11
0
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.')
示例#12
0
def recognize_from_image():
    # 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,
    )

    # input image loop
    for image_path in args.input:
        # prepare input data
        logger.info(image_path)
        input_img = load_image(
            image_path,
            (IMAGE_HEIGHT, IMAGE_WIDTH),
            normalize_type='None',
        )
        input_data = cv2.cvtColor(input_img, cv2.COLOR_BGR2BGRA)

        # inference
        logger.info('Start inference...')
        if args.benchmark:
            logger.info('BENCHMARK mode')
            for i in range(5):
                start = int(round(time.time() * 1000))
                classifier.compute(input_data, MAX_CLASS_COUNT)
                end = int(round(time.time() * 1000))
                logger.info(f'\tailia processing time {end - start} ms')
        else:
            classifier.compute(input_data, MAX_CLASS_COUNT)

        count = classifier.get_class_count()

        # postprocessing
        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}')
    logger.info('Script finished successfully.')
示例#13
0
def recognize_from_image():
    # 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,
    )

    # input image loop
    for image_path in args.input:
        # prepare input data
        logger.info(image_path)
        etl_word = codecs.open(ETL_PATH, 'r', 'utf-8').readlines()
        img = cv2.imread(image_path, cv2.IMREAD_UNCHANGED)
        if img is None:
            logger.error("can not open " + image_path)
            continue
        img = preprocess_image(img)

        # inference
        logger.info('Start inference...')
        if args.benchmark:
            logger.info('BENCHMARK mode')
            for i in range(5):
                start = int(round(time.time() * 1000))
                classifier.compute(img, MAX_CLASS_COUNT)
                end = int(round(time.time() * 1000))
                logger.info(f'\tailia processing time {end - start} ms')
        else:
            classifier.compute(img, MAX_CLASS_COUNT)

        # get result
        count = classifier.get_class_count()
        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}")
示例#14
0
def recognize_from_image():
    # prepare input data

    input_data = load_image(args.input, (IMAGE_HEIGHT, IMAGE_WIDTH),
                            normalize_type='None',
                            gen_input_ailia=False)
    input_data = cv2.cvtColor(input_data.astype(np.float32),
                              cv2.COLOR_RGB2BGRA).astype(np.uint8)

    # 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)

    # inference
    print('Start inference...')
    if args.benchmark:
        print('BENCHMARK mode')
        for i in range(5):
            start = int(round(time.time() * 1000))
            classifier.compute(input_data, MAX_CLASS_COUNT)
            end = int(round(time.time() * 1000))
            print(f'\tailia processing time {end - start} ms')
    else:
        classifier.compute(input_data, MAX_CLASS_COUNT)

    count = classifier.get_class_count()

    # show results
    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}')

    print('Script finished successfully.')
def recognize_from_image():
    # prepare input
    # load input image and convert to BGRA
    img = cv2.imread(args.input, cv2.IMREAD_UNCHANGED)
    if img.shape[2] == 3:
        img = cv2.cvtColor(img, cv2.COLOR_BGR2BGRA)
    elif img.shape[2] == 1:
        img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGRA)

    # net initialize
    emotion_classifier = ailia.Classifier(
        EMOTION_MODEL_PATH,
        EMOTION_WEIGHT_PATH,
        env_id=args.env_id,
        format=ailia.NETWORK_IMAGE_FORMAT_GRAY,
        range=ailia.NETWORK_IMAGE_RANGE_S_FP32,
        channel=ailia.NETWORK_IMAGE_CHANNEL_FIRST,
    )
    gender_classifier = ailia.Classifier(
        GENDER_MODEL_PATH,
        GENDER_WEIGHT_PATH,
        env_id=args.env_id,
        format=ailia.NETWORK_IMAGE_FORMAT_GRAY,
        range=ailia.NETWORK_IMAGE_RANGE_S_FP32,
        channel=ailia.NETWORK_IMAGE_CHANNEL_FIRST,
    )

    # inference emotion
    print('Start inference...')
    if args.benchmark:
        print('BENCHMARK mode')
        for i in range(5):
            start = int(round(time.time() * 1000))
            emotion_classifier.compute(img, EMOTION_MAX_CLASS_COUNT)
            end = int(round(time.time() * 1000))
            print(f'\t[EMOTION MODEL] ailia processing time {end - start} ms')
    else:
        emotion_classifier.compute(img, EMOTION_MAX_CLASS_COUNT)
    count = emotion_classifier.get_class_count()
    print(f'emotion_class_count={count}')

    # print result
    for idx in range(count):
        print(f'+ idx={idx}')
        info = emotion_classifier.get_class(idx)
        print(
            f'  category={info.category} [ {EMOTION_CATEGORY[info.category]} ]'
        )
        print(f'  prob={info.prob}')
    print('')

    # inference gender
    if args.benchmark:
        print('BENCHMARK mode')
        for i in range(5):
            start = int(round(time.time() * 1000))
            gender_classifier.compute(img, GENDER_MAX_CLASS_COUNT)
            end = int(round(time.time() * 1000))
            print(f'\t[EMOTION MODEL] ailia processing time {end - start} ms')
    else:
        gender_classifier.compute(img, GENDER_MAX_CLASS_COUNT)
    count = gender_classifier.get_class_count()
    print(f'gender_class_count={count}')

    # print reuslt
    for idx in range(count):
        print(f'+ idx={idx}')
        info = gender_classifier.get_class(idx)
        print(
            f'  category={info.category} [ {GENDER_CATEGORY[info.category]} ]'
        )
        print(f'  prob={info.prob}')
    print('Script finished successfully.')
def recognize_from_video():
    # net initialize
    emotion_classifier = ailia.Classifier(
        EMOTION_MODEL_PATH,
        EMOTION_WEIGHT_PATH,
        env_id=args.env_id,
        format=ailia.NETWORK_IMAGE_FORMAT_GRAY,
        range=ailia.NETWORK_IMAGE_RANGE_S_FP32,
        channel=ailia.NETWORK_IMAGE_CHANNEL_FIRST,
    )
    gender_classifier = ailia.Classifier(
        GENDER_MODEL_PATH,
        GENDER_WEIGHT_PATH,
        env_id=args.env_id,
        format=ailia.NETWORK_IMAGE_FORMAT_GRAY,
        range=ailia.NETWORK_IMAGE_RANGE_S_FP32,
        channel=ailia.NETWORK_IMAGE_CHANNEL_FIRST,
    )
    detector = ailia.Net(FACE_MODEL_PATH, FACE_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:
        print('[WARNING] currently video results output feature '
              'is not supported in this model!')
        # TODO: shape should be debugged!
        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

        # detect face
        # WIP: FIXME: AiliaInvalidArgumentException error
        detections = compute_blazeface(
            detector,
            frame,
            anchor_path='../../face_detection/blazeface/anchors.npy',
        )

        for obj in detections:
            # get detected face
            crop_img, top_left, bottom_right = crop_blazeface(
                obj, FACE_MARGIN, frame
            )
            if crop_img.shape[0] <= 0 or crop_img.shape[1] <= 0:
                continue
            crop_img = cv2.cvtColor(crop_img, cv2.COLOR_BGR2BGRA)

            # emotion inference
            emotion_classifier.compute(crop_img, EMOTION_MAX_CLASS_COUNT)
            count = emotion_classifier.get_class_count()
            print('=' * 80)
            print(f'emotion_class_count={count}')

            # print result
            emotion_text = ""
            for idx in range(count):
                print(f'+ idx={idx}')
                info = emotion_classifier.get_class(idx)
                print(
                    f'  category={info.category} ' +
                    f'[ {EMOTION_CATEGORY[info.category]} ]'
                )
                print(f'  prob={info.prob}')
                if idx == 0:
                    emotion_text = (f'[ {EMOTION_CATEGORY[info.category]} ] '
                                    f'prob={info.prob:.3f}')
            print('')

            # gender inference
            gender_text = ""
            gender_classifier.compute(crop_img, GENDER_MAX_CLASS_COUNT)
            count = gender_classifier.get_class_count()
            # print reuslt
            for idx in range(count):
                print(f'+ idx={idx}')
                info = gender_classifier.get_class(idx)
                print(
                    f'  category={info.category} ' +
                    f'[ {GENDER_CATEGORY[info.category]} ]'
                )
                print(f'  prob={info.prob}')
                if idx == 0:
                    gender_text = (f'[ {GENDER_CATEGORY[info.category]} ] '
                                   f'prob={info.prob:.3f}')
            print('')

            # display label
            LABEL_WIDTH = 400
            LABEL_HEIGHT = 20
            color = (255, 255, 255)
            cv2.rectangle(frame, top_left, bottom_right, color, thickness=2)
            cv2.rectangle(
                frame,
                top_left,
                (top_left[0]+LABEL_WIDTH, top_left[1]+LABEL_HEIGHT),
                color,
                thickness=-1,
            )

            text_position = (top_left[0], top_left[1]+LABEL_HEIGHT//2)
            color = (0, 0, 0)
            fontScale = 0.5
            cv2.putText(
                frame,
                emotion_text + " " + gender_text,
                text_position,
                cv2.FONT_HERSHEY_SIMPLEX,
                fontScale,
                color,
                1,
            )

            # show result
            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}')
    emotion_classifier = ailia.Classifier(
        EMOTION_MODEL_PATH,
        EMOTION_WEIGHT_PATH,
        env_id=env_id,
        format=ailia.NETWORK_IMAGE_FORMAT_GRAY,
        range=ailia.NETWORK_IMAGE_RANGE_S_FP32,
        channel=ailia.NETWORK_IMAGE_CHANNEL_FIRST)
    gender_classifier = ailia.Classifier(
        GENDER_MODEL_PATH,
        GENDER_WEIGHT_PATH,
        env_id=env_id,
        format=ailia.NETWORK_IMAGE_FORMAT_GRAY,
        range=ailia.NETWORK_IMAGE_RANGE_S_FP32,
        channel=ailia.NETWORK_IMAGE_CHANNEL_FIRST)

    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 = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA)

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
        if not ret:
            continue

        # emotion inference
        emotion_classifier.compute(frame, EMOTION_MAX_CLASS_COUNT)
        count = emotion_classifier.get_class_count()
        print('===========================================================')
        print(f'emotion_class_count={count}')

        # print result
        for idx in range(count):
            print(f'+ idx={idx}')
            info = emotion_classifier.get_class(idx)
            print(
                f'  category={info.category} ' +\
                f'[ {EMOTION_CATEGORY[info.category]} ]'
            )
            print(f'  prob={info.prob}')
        print('')

        # gender inference
        gender_classifier.compute(frame, GENDER_MAX_CLASS_COUNT)
        count = gender_classifier.get_class_count()
        # print reuslt
        for idx in range(count):
            print(f'+ idx={idx}')
            info = gender_classifier.get_class(idx)
            print(
                f'  category={info.category} ' +\
                f'[ {GENDER_CATEGORY[info.category]} ]'
            )
            print(f'  prob={info.prob}')
        print('')
        cv2.imshow('frame', frame)
        time.sleep(SLEEP_TIME)

    capture.release()
    cv2.destroyAllWindows()
    print('Script finished successfully.')