Пример #1
0
def video(args):
    config = load_config(args)
    model = create_model(args, config)

    cap = cv2.VideoCapture(0)
    # cap = cv2.VideoCapture("/home/fabian/Documents/dataset/videos/test4.mp4")
    if cap.isOpened() is False:
        print('Error opening video stream or file')
        exit(1)

    logger.info('camera will capture {} FPS'.format(cap.get(cv2.CAP_PROP_FPS)))
    if os.path.exists('mask.png'):
        mask = Image.open('mask.png')
        mask = mask.resize((200, 200))
    else:
        mask = None

    fps_time = 0
    degree = 0
    detection_thresh = config.getfloat('predict', 'detection_thresh')
    min_num_keypoints = config.getint('predict', 'min_num_keypoints')
    while cap.isOpened():
        degree += 5
        degree = degree % 360
        ret_val, image = cap.read()
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        image = cv2.resize(image, model.insize)
        with chainer.using_config('autotune', True):
            humans = estimate(model,
                              image.transpose(2, 0, 1).astype(np.float32),
                              detection_thresh, min_num_keypoints)
        pilImg = Image.fromarray(image)
        pilImg = draw_humans(
            model.keypoint_names,
            model.edges,
            pilImg,
            humans,
            mask=mask.rotate(degree) if mask else None,
            visbbox=config.getboolean('predict', 'visbbox'),
        )
        img_with_humans = cv2.cvtColor(np.asarray(pilImg), cv2.COLOR_RGB2BGR)
        msg = 'GPU ON' if chainer.backends.cuda.available else 'GPU OFF'
        msg += ' ' + config.get('model_param', 'model_name')
        cv2.putText(img_with_humans,
                    'FPS: % f' % (1.0 / (time.time() - fps_time)), (10, 10),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
        # img_with_humans = cv2.resize(img_with_humans, (3 * model.insize[0], 3 * model.insize[1]))
        img_with_humans = cv2.resize(
            img_with_humans, (1 * model.insize[0], 1 * model.insize[1]))
        cv2.imshow('Pose Proposal Network' + msg, img_with_humans)
        fps_time = time.time()
        # press Esc to exit
        if cv2.waitKey(1) == 27:
            break
Пример #2
0
def main():
    config = configparser.ConfigParser()
    config.read('config.ini', 'UTF-8')

    # load model
    model = create_model(config)

    cap = cv2.VideoCapture(0)
    if cap.isOpened() is False:
        print('Error opening video stream or file')
        exit(1)

    logger.info('camera will capture {} FPS'.format(cap.get(cv2.CAP_PROP_FPS)))
    if os.path.exists('mask.png'):
        mask = Image.open('mask.png')
        mask = mask.resize((200, 200))
    else:
        mask = None

    fps_time = 0
    degree = 0
    while cap.isOpened():
        degree += 5
        degree = degree % 360
        ret_val, image = cap.read()
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        image = cv2.resize(image, model.insize)
        with chainer.using_config('autotune', True):
            humans = estimate(model,
                              image.transpose(2, 0, 1).astype(np.float32))
        pilImg = Image.fromarray(image)
        pilImg = draw_humans(
            model.keypoint_names,
            model.edges,
            pilImg,
            humans,
            mask=mask.rotate(degree) if mask else None
        )
        img_with_humans = cv2.cvtColor(np.asarray(pilImg), cv2.COLOR_RGB2BGR)
        msg = 'GPU ON' if chainer.backends.cuda.available else 'GPU OFF'
        msg += ' ' + config.get('model_param', 'model_name')
        cv2.putText(img_with_humans, 'FPS: % f' % (1.0 / (time.time() - fps_time)),
                    (10, 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
        img_with_humans = cv2.resize(img_with_humans, (3 * model.insize[0], 3 * model.insize[1]))
        cv2.imshow('Pose Proposal Network' + msg, img_with_humans)
        fps_time = time.time()
        # press Esc to exit
        if cv2.waitKey(1) == 27:
            break
Пример #3
0
def high_speed(args):
    config = load_config(args)
    dataset_type = config.get('dataset', 'type')
    detection_thresh = config.getfloat('predict', 'detection_thresh')
    min_num_keypoints = config.getint('predict', 'min_num_keypoints')
    model = create_model(args, config)

    if os.path.exists('mask.png'):
        mask = Image.open('mask.png')
        mask = mask.resize((200, 200))
    else:
        mask = None
		

    capture = Capture(model.insize)
    predictor = Predictor(model=model, cap=capture)

    capture.start()
    predictor.start()

    fps_time = 0
    degree = 0

    main_event = threading.Event()

    try:
        while not main_event.is_set():
            degree += 5
            degree = degree % 360
            try:
                image, feature_map = predictor.get()
                humans = get_humans_by_feature(
                    model,
                    feature_map,
                    detection_thresh,
                    min_num_keypoints
                )
            except queue.Empty:
                continue
            except Exception:
                break
            pilImg = Image.fromarray(image)
            pilImg = draw_humans(
                model.keypoint_names,
                model.edges,
                pilImg,
                humans,
                mask=mask.rotate(degree) if mask else None,
                visbbox=config.getboolean('predict', 'visbbox'),
            )
            img_with_humans = cv2.cvtColor(np.asarray(pilImg), cv2.COLOR_RGB2BGR)
            msg = 'GPU ON' if chainer.backends.cuda.available else 'GPU OFF'
            msg += ' ' + config.get('model_param', 'model_name')
            cv2.putText(img_with_humans, 'FPS: %f' % (1.0 / (time.time() - fps_time)),
                        (10, 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
            img_with_humans = cv2.resize(img_with_humans, (3 * model.insize[0], 3 * model.insize[1]))
            cv2.imshow('Pose Proposal Network' + msg, img_with_humans)
            fps_time = time.time()
            # press Esc to exit
            if cv2.waitKey(1) == 27:
                main_event.set()
    except Exception as e:
        print(e)
    except KeyboardInterrupt:
        main_event.set()

    capture.stop()
    predictor.stop()

    capture.join()
    predictor.join()
Пример #4
0
    def video_handle(video_file, video_output):
        global FPS_list
        # Video reader
        cam = cv2.VideoCapture(video_file)
        input_fps = cam.get(cv2.CAP_PROP_FPS)
        ret_val, input_image = cam.read()
        video_length = int(cam.get(cv2.CAP_PROP_FRAME_COUNT))

        ending_frame = video_length

        # Video writer
        frame_rate_ratio = 1
        output_fps = input_fps / frame_rate_ratio
        fourcc = cv2.VideoWriter_fourcc(*'mp4v')
        # 后面两者是写入的图片大小
        out = cv2.VideoWriter(video_output, fourcc, output_fps, (672, 672))

        i = 0  # default is 0
        while (cam.isOpened()) and ret_val == True and i < ending_frame:
            if i % frame_rate_ratio == 0:

                tic = time.time()
                mask = None

                input_image = cv2.cvtColor(input_image, cv2.COLOR_BGR2RGB)
                input_image = cv2.resize(input_image, model.insize)
                humans = estimate(
                    model,
                    input_image.transpose(2, 0, 1).astype(np.float32))
                pilImg = Image.fromarray(input_image)
                pilImg = draw_humans(
                    model.keypoint_names,
                    model.edges,
                    pilImg,
                    humans,
                    mask=mask.rotate(degree) if mask else None)
                img_with_humans = cv2.cvtColor(np.asarray(pilImg),
                                               cv2.COLOR_RGB2BGR)
                msg = 'GPU ON' if chainer.backends.cuda.available else 'GPU OFF'
                msg += ' ' + config.get('model_param', 'model_name')
                FPS = round(1.0 / (time.time() - tic), 2)
                FPS_list.append(FPS)
                cv2.putText(img_with_humans,
                            'FPS: % f' % (1.0 / (time.time() - tic)), (10, 10),
                            cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
                img_with_humans = cv2.resize(
                    img_with_humans,
                    (3 * model.insize[0], 3 * model.insize[1]))
                #  cv2.imshow('Pose Proposal Network' + msg, img_with_humans)

                print('Processing frame: {}/{}'.format(
                    i, video_length / frame_rate_ratio))
                toc = time.time()
                print('processing time is %.5f' % (toc - tic))

                out.write(img_with_humans)
            # 每次在这里变化的
            ret_val, input_image = cam.read()
            i += 1
        # compute average FPS
        average_fps = sum(FPS_list) / len(FPS_list)
        print('total {} frame, {} frame per second\n\n\n'.format(
            len(FPS_list), round(average_fps, 1)))
Пример #5
0
def high_speed(args):
    config = load_config(args)
    dataset_type = config.get('dataset', 'type')
    detection_thresh = config.getfloat('predict', 'detection_thresh')
    min_num_keypoints = config.getint('predict', 'min_num_keypoints')
    model = create_model(args, config)

    if os.path.exists('mask.png'):
        mask = Image.open('mask.png')
        mask = mask.resize((200, 200))
    else:
        mask = None

    cap = cv2.VideoCapture(0)  # get input from usb camera
    # cap = cv2.VideoCapture("/home/fabian/Documents/dataset/videos/test4.mp4")
    if cap.isOpened() is False:
        print('Error opening video stream or file')
        exit(1)

    cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'))
    cap.set(cv2.CAP_PROP_FPS, 60)
    cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
    cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
    logger.info('camera will capture {} FPS'.format(cap.get(cv2.CAP_PROP_FPS)))

    capture = Capture(cap, model.insize)
    predictor = Predictor(model=model, cap=capture)

    capture.start()
    predictor.start()

    fps_time = 0
    degree = 0

    main_event = threading.Event()

    try:
        while not main_event.is_set() and cap.isOpened():
            degree += 5
            degree = degree % 360
            try:
                image, feature_map = predictor.get()
                humans = get_humans_by_feature(model, feature_map,
                                               detection_thresh,
                                               min_num_keypoints)
            except queue.Empty:
                continue
            except Exception:
                break
            pilImg = Image.fromarray(image)
            pilImg = draw_humans(
                model.keypoint_names,
                model.edges,
                pilImg,
                humans,
                mask=mask.rotate(degree) if mask else None,
                visbbox=config.getboolean('predict', 'visbbox'),
            )
            img_with_humans = cv2.cvtColor(np.asarray(pilImg),
                                           cv2.COLOR_RGB2BGR)
            msg = 'GPU ON' if chainer.backends.cuda.available else 'GPU OFF'
            msg += ' ' + config.get('model_param', 'model_name')
            cv2.putText(img_with_humans,
                        'FPS: %f' % (1.0 / (time.time() - fps_time)), (10, 10),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
            img_with_humans = cv2.resize(
                img_with_humans, (3 * model.insize[0], 3 * model.insize[1]))
            cv2.imshow('Pose Proposal Network' + msg, img_with_humans)
            fps_time = time.time()
            # press Esc to exit
            if cv2.waitKey(1) == 27:
                main_event.set()
    except Exception as e:
        print(e)
    except KeyboardInterrupt:
        main_event.set()

    capture.stop()
    predictor.stop()

    capture.join()
    predictor.join()
Пример #6
0
    while (cam.isOpened()) and ret_val == True and i < ending_frame:
        # 每隔几帧处理一次帧图片
        if i % frame_rate_ratio == 0:

            tic = time.time()
            mask = None

            input_image = cv2.cvtColor(input_image, cv2.COLOR_BGR2RGB)
            input_image = cv2.resize(input_image, model.insize)
            humans = estimate(
                model,
                input_image.transpose(2, 0, 1).astype(np.float32))
            pilImg = Image.fromarray(input_image)
            pilImg = draw_humans(model.keypoint_names,
                                 model.edges,
                                 pilImg,
                                 humans,
                                 mask=mask.rotate(degree) if mask else None)
            img_with_humans = cv2.cvtColor(np.asarray(pilImg),
                                           cv2.COLOR_RGB2BGR)
            msg = 'GPU ON' if chainer.backends.cuda.available else 'GPU OFF'
            msg += ' ' + config.get('model_param', 'model_name')
            cv2.putText(img_with_humans,
                        'FPS: % f' % (1.0 / (time.time() - tic)), (10, 10),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
            img_with_humans = cv2.resize(
                img_with_humans, (3 * model.insize[0], 3 * model.insize[1]))
            #  cv2.imshow('Pose Proposal Network' + msg, img_with_humans)

            print('Processing frame: {}/{}'.format(
                i, video_length / frame_rate_ratio))
Пример #7
0
def high_speed(args, viewer):
    config = load_config(args)
    dataset_type = config.get('dataset', 'type')
    detection_thresh = config.getfloat('predict', 'detection_thresh')
    min_num_keypoints = config.getint('predict', 'min_num_keypoints')
    model = create_model(args, config)
    svo_file_path = None  #"/home/adujardin/Downloads/5m.svo" #config.get('zed', 'svo_file_path')

    init_cap_params = sl.InitParameters()
    if svo_file_path:
        print("Loading SVO file " + svo_file_path)
        init_cap_params.svo_input_filename = svo_file_path
        init_cap_params.svo_real_time_mode = True
    init_cap_params.camera_resolution = sl.RESOLUTION.RESOLUTION_HD720
    init_cap_params.depth_mode = sl.DEPTH_MODE.DEPTH_MODE_ULTRA
    init_cap_params.coordinate_units = sl.UNIT.UNIT_METER
    init_cap_params.depth_stabilization = True
    init_cap_params.coordinate_system = sl.COORDINATE_SYSTEM.COORDINATE_SYSTEM_RIGHT_HANDED_Y_UP

    cap = sl.Camera()
    if not cap.is_opened():
        print("Opening ZED Camera...")
    status = cap.open(init_cap_params)
    if status != sl.ERROR_CODE.SUCCESS:
        print(repr(status))
        exit()

    py_transform = sl.Transform()
    tracking_parameters = sl.TrackingParameters(init_pos=py_transform)
    cap.enable_tracking(tracking_parameters)

    capture = Capture(cap, model.insize)
    predictor = Predictor(model=model, cap=capture)

    capture.start()
    predictor.start()

    fps_time = 0
    main_event = threading.Event()
    viewer.edges = model.edges

    try:
        while not main_event.is_set() and cap.is_opened():
            try:
                image, feature_map, depth = predictor.get()
                humans = get_humans_by_feature(model, feature_map,
                                               detection_thresh,
                                               min_num_keypoints)
                humans_3d = get_humans3d(humans, depth, model)
            except queue.Empty:
                continue
            except Exception as e:
                print(e)
                break
            pilImg = Image.fromarray(image)
            pilImg = draw_humans(
                model.keypoint_names,
                model.edges,
                pilImg,
                humans,
                None,
                visbbox=config.getboolean('predict', 'visbbox'),
            )
            img_with_humans = cv2.cvtColor(np.asarray(pilImg),
                                           cv2.COLOR_RGB2BGR)
            img_with_humans = cv2.resize(
                img_with_humans,
                (700, 400))  #(3 * model.insize[0], 3 * model.insize[1]))
            msg = 'GPU ON' if chainer.backends.cuda.available else 'GPU OFF'
            msg += ' ' + config.get('model_param', 'model_name')
            fps_display = 'FPS: %f' % (1.0 / (time.time() - fps_time))
            str_to_dsplay = msg + " " + fps_display
            cv2.putText(img_with_humans, fps_display, (10, 10),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
            cv2.imshow('Pose Proposal Network' + msg, img_with_humans)

            viewer.update_text(str_to_dsplay)
            viewer.update_humans(humans_3d)

            fps_time = time.time()
            key = cv2.waitKey(1)

            # press Esc to exit
            if key == 27:
                exit
                main_event.set()
    except Exception as e:
        print(e)
    except KeyboardInterrupt:
        main_event.set()

    capture.stop()
    predictor.stop()

    capture.join()
    predictor.join()
    cap.close()