示例#1
0
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

        frame = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
        input_data = preprocess(frame)
        net.set_input_shape(input_data.shape)

        boxes, labels, scores, masks = net.predict([input_data])

        display_objdetect_image(frame, boxes, labels, scores, masks)
        plt.pause(.01)

    capture.release()
    cv2.destroyAllWindows()
    print('Script finished successfully.')
示例#2
0
def recognize_from_video(video, detector):
    if 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

        x = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        detect_object = detect_objects(x, detector)
        res_img = plot_results(detect_object, frame, category)
        cv2.imshow('frame', res_img)

    capture.release()
    cv2.destroyAllWindows()
    print('Script finished successfully.')
示例#3
0
def get_capture(video):
    """
    Get cv2.VideoCapture

    * TODO: maybe get capture & writer at the same time?
    *       then, you can use capture frame size directory

    Parameters
    ----------
    video : str
        webcamera-id or video path

    Returns
    -------
    capture : cv2.VideoCapture
    """
    try:
        video_id = int(video)

        # webcamera-mode
        capture = cv2.VideoCapture(video_id)
        if not capture.isOpened():
            print(f"[ERROR] webcamera (ID - {video_id}) not found")
            sys.exit(0)

    except ValueError:
        # if file path is given, open video file
        if check_file_existance(video):
            capture = cv2.VideoCapture(video)

    return capture
示例#4
0
def process_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.face_recognition:
        locator = FaceLocator()
    else:
        locator = None

    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

        img = process_frame(net, locator, frame)

        cv2.imshow('frame', img[..., ::-1])

    capture.release()
    cv2.destroyAllWindows()
    print('Script finished successfully.')
def recognize_tag_from_image():
    # prepare input data
    input_img = load_image(
        args.input,
        (IMAGE_HEIGHT, IMAGE_WIDTH),
        normalize_type='None',
    )
    input_data = prepare_input_data(input_img)

    # net initialize
    tag_net = ailia.Net(TAG_MODEL_PATH, TAG_WEIGHT_PATH, env_id=args.env_id)
    tag_net.set_input_shape(input_data.shape)

    if check_file_existance(TAG_PATH):
        tags = np.array(json.loads(open(TAG_PATH, 'r').read()))
        assert(len(tags) == 1539)

    input_dict = {'data': input_data}

    # inference
    print('Start inference...')
    if args.benchmark:
        print('BENCHMARK mode')
        for i in range(5):
            start = int(round(time.time() * 1000))
            preds_ailia = tag_net.predict(input_dict)[0]
            end = int(round(time.time() * 1000))
            print(f'\tailia processing time {end - start} ms')
    else:
        preds_ailia = tag_net.predict(input_dict)[0]

    prob = preds_ailia.reshape(preds_ailia.shape[0], -1)
    preds = estimate_top_tags(prob, tags, 512)  # TODO how to decide n_tag?
    pprint(apply_threshold(preds, THRESHOLD))
    print('Script finished successfully.')
示例#6
0
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.')
示例#7
0
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.')
示例#8
0
def recognize_from_video():
    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)

    # # 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, 3, args.duration, IMAGE_HEIGHT, IMAGE_WIDTH))

    # prepare input data
    original_queue = deque([])
    input_blob = np.empty((1, 3, args.duration, IMAGE_HEIGHT, IMAGE_WIDTH))
    for i in range(args.duration - 1):
        ret, frame = capture.read()
        if not ret:
            continue
        original_queue.append(frame)
        input_blob[0, :, i, :, :] = convert_input_frame(frame)

    next_input_index = args.duration - 1
    input_frame_size = capture.get(cv2.CAP_PROP_FRAME_COUNT)

    while (next_input_index <= input_frame_size or input_frame_size == 0):
        ret, frame = capture.read()
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
        if not ret:
            continue
        original_queue.append(frame)
        input_blob[0, :, args.duration - 1, :, :] = convert_input_frame(frame)

        if args.ailia:
            result = net.predict(input_blob)
        else:
            result = session.run(
                [output_name], {input_name: input_blob.astype(np.float32)})[0]
        print_mars_result(result)

        preview_img = original_queue.popleft()
        cv2.imshow('preview', preview_img)

        for i in range(args.duration - 1):
            input_blob[0, :, i, :, :] = input_blob[0, :, i + 1, :, :]

        next_input_index += 1

    capture.release()
    print('Script finished successfully.')
示例#9
0
def estimate_from_video():
    # net initialize
    env_id = ailia.get_gpu_environment_id()
    print(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,
                                                   HEIGHT,
                                                   WIDTH,
                                                   data_rgb=False,
                                                   normalize_type='None')

        # inference
        preds_ailia = net.predict(input_data)

        # estimated crowd count
        et_count = int(np.sum(preds_ailia))

        # density map
        density_map = (255 * preds_ailia / np.max(preds_ailia))[0][0]
        density_map = cv2.resize(density_map,
                                 (input_image.shape[1], input_image.shape[0]))
        heatmap = cv2.applyColorMap(density_map.astype(np.uint8),
                                    cv2.COLORMAP_JET)
        cv2.putText(
            heatmap,
            f'Est Count: {et_count}',
            (40, 440),  # position
            cv2.FONT_HERSHEY_SIMPLEX,  # font
            0.8,  # fontscale
            (255, 255, 255),  # color
            2  # thickness
        )
        res_img = np.hstack((input_image, heatmap))
        cv2.imshow('frame', res_img)
    capture.release()
    cv2.destroyAllWindows()
    print('Script finished successfully.')
示例#10
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.')
示例#11
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.')
示例#12
0
def recognize_from_video():
    # [WARNING] This is test impl
    print('[WARNING] This is test implementation')
    # 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

        img = preProcess(frame)
        input_data = padCropImg(img)
        input_data = input_data.astype(np.float32) / 255.0

        ynum = input_data.shape[0]
        xnum = input_data.shape[1]

        preds_ailia = np.zeros((ynum, xnum, 128, 128, 3), dtype=np.float32)

        for j in range(ynum):
            for i in range(xnum):
                patchImg = input_data[j, i]
                patchImg = (patchImg - 0.5) / 0.5
                patchImg = patchImg.transpose((2, 0, 1))
                patchImg = patchImg[np.newaxis, :, :, :]
                out = net.predict(patchImg)
                out = out.transpose((0, 2, 3, 1))[0]
                out = (np.clip(out, 0, 1) * 255).astype(np.uint8)
                preds_ailia[j, i] = out

        resImg = composePatch(preds_ailia)
        resImg = postProcess(resImg)
        cv2.imshow('frame', img_as_ubyte(resImg))

    capture.release()
    cv2.destroyAllWindows()
    print('Script finished successfully.')
示例#13
0
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.')
示例#14
0
def recognize_from_video():
    # net initialize
    env_id = ailia.get_gpu_environment_id()
    print(f'env_id: {env_id}')
    enc_net = ailia.Net(ENC_MODEL_PATH, ENC_WEIGHT_PATH, env_id=env_id)
    dec_net = ailia.Net(DEC_MODEL_PATH, DEC_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)

    ret, frame = capture.read()
    org_height, org_width, _ = frame.shape

    while (True):
        ret, frame = capture.read()
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
        if not ret:
            continue

        _, input_data = preprocess_frame(frame, IMAGE_HEIGHT, IMAGE_WIDTH)

        # encoder
        enc_input_blobs = enc_net.get_input_blob_list()
        enc_net.set_input_blob_data(input_data, enc_input_blobs[0])
        enc_net.update()
        features = enc_net.get_results()

        # decoder
        dec_inputs_blobs = dec_net.get_input_blob_list()
        for f_idx in range(len(features)):
            dec_net.set_input_blob_data(features[f_idx],
                                        dec_inputs_blobs[f_idx])
        dec_net.update()
        preds_ailia = dec_net.get_results()

        # postprocessing
        disp = preds_ailia[-1]
        disp_resized, vmax = result_plot(disp, org_width, org_height)
        plt.imshow(disp_resized, cmap='magma', vmax=vmax)
        plt.pause(.01)

    capture.release()
    cv2.destroyAllWindows()
    print('Script finished successfully.')
示例#15
0
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)

    # 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 = postprocess(preds_ailia)
        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()
    print('Script finished successfully.')
示例#16
0
def segment_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)

    ailia_input_w = net.get_input_shape()[3]
    ailia_input_h = net.get_input_shape()[2]

    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, ailia_input_h, ailia_input_w, 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 = np.array(net.get_results())[0, 0]  # TODO why?
        
        # postprocessing
        seg_map = np.argmax(preds_ailia.transpose(1, 2, 0), axis=2)
        seg_image = label_to_color_image(seg_map).astype(np.uint8)

        # showing the segmented image (simple)
        seg_image = cv2.cvtColor(seg_image, cv2.COLOR_RGB2BGR)
        seg_image = cv2.resize(
            seg_image, (input_image.shape[1], input_image.shape[0])
        )

        cv2.imshow('frame', seg_image)

    capture.release()
    cv2.destroyAllWindows()
    print('Script finished successfully.')
示例#17
0
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_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.')
示例#19
0
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.')
示例#20
0
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.')
示例#21
0
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.')
示例#22
0
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)

    input_shape_set = False
    while (True):
        ret, frame = capture.read()
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
        if not ret:
            continue

        resized_img = midas_resize(frame, IMAGE_HEIGHT, IMAGE_WIDTH)
        resized_img = resized_img.transpose((2, 0, 1))  # channel first
        resized_img = resized_img[np.newaxis, :, :, :]

        if (not input_shape_set):
            net.set_input_shape(resized_img.shape)
            input_shape_set = True
        result = net.predict(resized_img)

        depth_min = result.min()
        depth_max = result.max()
        max_val = (2**16) - 1
        if depth_max - depth_min > np.finfo("float").eps:
            out = max_val * (result - depth_min) / (depth_max - depth_min)
        else:
            out = 0

        cv2.imshow('depth', out.transpose(1, 2, 0).astype("uint16"))

    capture.release()
    cv2.destroyAllWindows()
    print('Script finished successfully.')
示例#23
0
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)

    # create video writer if savepath is specified as video format
    f_h = int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT))
    f_w = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH))    
    if args.savepath != SAVE_IMAGE_PATH:    
        writer = webcamera_utils.get_writer(args.savepath, f_h, f_w, rgb=False)
    else:
        writer = None

    while(True):
        ret, frame = capture.read()
        if (cv2.waitKey(1) & 0xFF == ord('q')) or not ret:
            break

        input_data = transform(frame, IMAGE_SIZE)

        # inference
        preds_ailia = net.predict([input_data])

        # postprocessing
        pred = cv2.resize(norm(preds_ailia[0][0, 0, :, :]), (f_w, f_h))
        cv2.imshow('frame', pred)
        
        # save results
        if writer is not None:
            writer.write((pred * 255).astype(np.uint8))
        
    capture.release()
    cv2.destroyAllWindows()
    print('Script finished successfully.')
示例#24
0
def unwarp_from_video():
    # net initialize
    env_id = ailia.get_gpu_environment_id()
    print(f'env_id: {env_id}')
    bm_net = ailia.Net(BM_MODEL_PATH, BM_WEIGHT_PATH, env_id=env_id)
    wc_net = ailia.Net(WC_MODEL_PATH, WC_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

        org_image, input_data = preprocess_frame(frame,
                                                 WC_IMG_HEIGHT,
                                                 WC_IMG_WIDTH,
                                                 normalize_type='255')

        # inference
        wc_output = wc_net.predict(input_data)[0]
        pred_wc = np.clip(wc_output, 0, 1.0).transpose(1, 2, 0)
        bm_input = cv2.resize(pred_wc,
                              (BM_IMG_WIDTH, BM_IMG_HEIGHT)).transpose(
                                  2, 0, 1)
        bm_input = np.expand_dims(bm_input, 0)
        outputs_bm = bm_net.predict(bm_input)[0]
        uwpred = unwarp(org_image, outputs_bm)  # This is not on GPU!

        cv2.imshow('frame', uwpred)

    capture.release()
    cv2.destroyAllWindows()
    print('Script finished successfully.')
示例#25
0
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.')
示例#26
0
def recognize_from_video():
    # net initialize
    detector = None
    env_id = ailia.get_gpu_environment_id()
    print(f'env_id: {env_id}')
    detector = ailia.Net(MODEL_PATH, WEIGHT_PATH, env_id=env_id)
    if int(args.detection_width) != DETECTION_WIDTH:
        detector.set_input_shape(
            (1, 3, int(args.detection_width), int(args.detection_width)))

    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

        img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        img = cv2.resize(img, (IMAGE_WIDTH, IMAGE_HEIGHT))
        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 = yolov4_utils.post_processing(img, THRESHOLD, IOU,
                                                     output)
        res_img = plot_results(detect_object[0], frame, COCO_CATEGORY)

        cv2.imshow('frame', res_img)

    capture.release()
    cv2.destroyAllWindows()
    print('Script finished successfully.')
示例#27
0
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

        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.')
示例#29
0
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.')
示例#30
0
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,
                                                   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 = postprocess(preds_ailia)
        show_result(input_image, detections)
        cv2.imshow('frame', input_image)

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