Example #1
0
def get_pose(video_file, output_path):
    # data out
    output_data = []
    # Output location
    output_format = '.mp4'
    video = os.path.basename(video_file).split('.')[0]
    video_output = output_path + '/' + video + str(start_datetime) + output_format
    # Video reader
    cam = cv2.VideoCapture(video_file)
    input_fps = cam.get(cv2.CAP_PROP_FPS)
    ret_val, orig_image = cam.read()
    video_length = int(cam.get(cv2.CAP_PROP_FRAME_COUNT))
    ending_frame = video_length

    # Video writer
    output_fps = input_fps / frame_rate_ratio
    fourcc = cv2.VideoWriter_fourcc(*'mp4v')
    out = cv2.VideoWriter(video_output, fourcc, output_fps, (orig_image.shape[1], orig_image.shape[0]))

    # scale_search = [1, .5, 1.5, 2]  # [.5, 1, 1.5, 2]
    # scale_search = scale_search[0:process_speed]
    #
    # params['scale_search'] = scale_search

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

            input_image = cv2.cvtColor(orig_image, cv2.COLOR_RGB2BGR)

            tic = time.time()

            # generate image with body parts
            body_parts, all_peaks, subset, candidate = extract_parts(input_image, params, model, model_params)
            output_data.append({'frame_id': i, 'body_parts': body_parts, 'all_peaks': all_peaks,
                              'subset': subset, 'candidate': candidate})
            canvas = draw(orig_image, all_peaks, subset, candidate)

            print('Processing frame: ', i)
            toc = time.time()
            print('processing time is %.5f' % (toc - tic))

            out.write(canvas)

        ret_val, orig_image = cam.read()

        i += 1

    return output_data
Example #2
0
def std_main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--image', type=str, required=True, help='input image')
    parser.add_argument('--output',
                        type=str,
                        default='result.png',
                        help='output image')
    parser.add_argument('--model',
                        type=str,
                        default='model/keras/model.h5',
                        help='path to the weights file')

    args = parser.parse_args()
    image_path = args.image
    output = args.output
    keras_weights_file = args.model

    tic = time.time()
    print('start processing...')

    # load model

    # authors of original model don't use
    # vgg normalization (subtracting mean) on input images
    model = get_testing_model()
    model.load_weights(keras_weights_file)

    # load config
    params, model_params = config_reader()

    input_image = cv2.imread(image_path)  # B,G,R order

    all_peaks, subset, candidate = extract_parts(input_image, params, model,
                                                 model_params)
    canvas = draw(input_image, all_peaks, subset, candidate)

    toc = time.time()
    print('processing time is %.5f' % (toc - tic))

    cv2.imwrite(output, canvas)

    cv2.destroyAllWindows()
    # load model

    # authors of original model don't use
    # vgg normalization (subtracting mean) on input images
    model = get_testing_model()
    model.load_weights(keras_weights_file)

    # load config
    params, model_params = config_reader()

    #immagine da classificare
    input_image = cv2.imread('images.jpg')  # B,G,R order

    body_parts, all_peaks, subset, candidate = extract_parts(
        input_image, params, model, model_params)
    canvas, dict, lis1, lis2 = draw(input_image, all_peaks, subset, candidate)

    cv2.imwrite(output, canvas)

    Concatena.salva_csv_dist(lis1, lis2, 'none')

    dataframe = pandas.read_csv("dataset_dist.csv")

    dataset = dataframe.values

    riga = dataframe.shape[0]

    X = dataset[:, 0:93]

    toc = time.time()
    print('processing time is %.5f' % (toc - tic))
Example #4
0
    out = cv2.VideoWriter(video_output, fourcc, output_fps,
                          (orig_image.shape[1], orig_image.shape[0]))

    scale_search = [1, .5, 1.5, 2]  # [.5, 1, 1.5, 2]
    scale_search = scale_search[0:process_speed]

    params['scale_search'] = scale_search

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

            input_image = cv2.cvtColor(orig_image, cv2.COLOR_RGB2BGR)

            tic = time.time()

            # generate image with body parts
            body_parts, all_peaks, subset, candidate = extract_parts(
                input_image, params, model, model_params)
            canvas = draw(orig_image, all_peaks, subset, candidate)

            print('Processing frame: ', i)
            toc = time.time()
            print('processing time is %.5f' % (toc - tic))

            out.write(canvas)

        ret_val, orig_image = cam.read()

        i += 1
Example #5
0
    output = args.output
    keras_weights_file = args.model

    tic = time.time()
    print('start processing...')

    # load model

    # authors of original model don't use
    # vgg normalization (subtracting mean) on input images
    model = get_testing_model()
    model.load_weights(keras_weights_file)

    # load config
    params, model_params = config_reader()
    print("params", params, "\n")
    print(output)
    input_image = cv2.imread(image_path)  # B,G,R order

    all_peaks, subset, candidate = extract_parts(input_image, params, model,
                                                 model_params)
    canvas = draw(input_image, all_peaks, subset, candidate)
    toc = time.time()
    cv2.imwrite(output, canvas)
    cwd = os.getcwd()
    print(all_peaks)
    des_dir = cwd + '/RESULTS/'
    with open((des_dir + 'parameters.data'), 'wb') as file:
        pickle.dump(all_peaks, file)
    cv2.destroyAllWindows()
Example #6
0
def video_cap(Excersise):
    path_model_h5 = 'model.h5'
    keras_weights_file = path_model_h5
    #Analysis for the every n frames
    frame_rate_ratio = 1

    #Int 1 (fastest, lowest quality) to 4 (slowest, highest quality)
    process_speed = 1
    #ending_frame = args.end
    print('start processing...')

    # Video input
    video_file = '/home/saireddy/SaiReddy/Desk/Flask/OrbitPose/videos/push.mp4'
    print(video_file)

    # Output location
    video_output = '/home/saireddy/SaiReddy/Desk/Flask/OrbitPose/videos/output/push4.avi'

    model = get_testing_model()
    model.load_weights(keras_weights_file)

    # load config
    params, model_params = config_reader()

    # Video reader
    #cam = cv2.VideoCapture(video_file)
    cam = cv2.VideoCapture(video_file)

    input_fps = cam.get(cv2.CAP_PROP_FPS)
    print("input frames per second:-", input_fps)

    ret_val, orig_image = cam.read()

    video_length = int(cam.get(cv2.CAP_PROP_FRAME_COUNT))
    print("total frames in a video:-", video_length)

    # Video writer
    output_fps = input_fps / frame_rate_ratio
    print("out put frames:-", output_fps)

    frame_width = int(cam.get(3))
    frame_height = int(cam.get(4))

    print("width:-", frame_width)
    print("Height:-", frame_height)

    out = cv2.VideoWriter(video_output,
                          cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'),
                          output_fps, (frame_width, frame_height))

    #out = cv2.VideoWriter(video_output, cv2.VideoWriter_fourcc(*'DIVX'),output_fps, (frame_width, frame_height))

    scale_search = [1, .5, 1.5, 2]  # [.5, 1, 1.5, 2]
    scale_search = scale_search[0:process_speed]

    params['scale_search'] = scale_search

    i = 0  # default is 0
    count = 0
    while (cam.isOpened()) and ret_val is True:
        if ret_val is None:
            break
        if i % frame_rate_ratio == 0:
            input_image = cv2.cvtColor(orig_image, cv2.COLOR_RGB2BGR)
            tic = time.time()

            if Excersise == 'Normal':
                all_peaks, subset, candidate = extract_parts(
                    input_image, params, model, model_params)
                canvas, theta, theta1, theta2, theta3, Angle1, Angle2, Angle3, Angle4 = draw(
                    orig_image, all_peaks, subset, candidate)
                cv2.rectangle(canvas, (0, 0), (265, 35),
                              color=(0, 255, 0),
                              thickness=2)
                cv2.putText(canvas,
                            "right Hand angle :- {0:.2f}".format(float(theta)),
                            (30, 15), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
                            (255, 255, 255))
                cv2.putText(canvas,
                            "right leg angle :- {0:.2f}".format(float(theta2)),
                            (30, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
                            (255, 255, 255))
                cv2.rectangle(canvas, (645, 0), (900, 35),
                              color=(0, 255, 0),
                              thickness=2)
                cv2.putText(canvas,
                            "left Hand angle :- {0:.2f}".format(float(theta1)),
                            (650, 15), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
                            (255, 255, 225))
                cv2.putText(canvas,
                            "left leg angle :- {0:.2f}".format(float(theta3)),
                            (650, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
                            (255, 255, 255))

            elif Excersise == 'Fat':
                all_peaks1, subset1, candidate1 = extract_parts_angle(
                    input_image, params, model, model_params)
                canvas, theta5, theta6, Angle5, Angle6 = draw_angle(
                    orig_image, all_peaks1, subset1, candidate1)
                cv2.rectangle(canvas, (645, 0), (900, 35),
                              color=(0, 255, 0),
                              thickness=2)
                cv2.putText(canvas, "Left  :- {0:.2f}".format(float(theta5)),
                            (650, 15), cv2.FONT_HERSHEY_SIMPLEX, 1,
                            (0, 0, 225))
                cv2.putText(canvas, "Right :- {0:.2f}".format(float(theta6)),
                            (650, 30), cv2.FONT_HERSHEY_SIMPLEX, 1,
                            (0, 0, 255))

            elif Excersise == 'Pushups':
                print("HIIIIIII")
                all_peaks2, subset2, candidate2 = extract_parts_push(
                    input_image, params, model, model_params)
                canvas, theta7, theta8, Angle7, Angle8 = draw_push(
                    orig_image, all_peaks2, subset2, candidate2)
                print("THeta 7, theta8 ", theta7, theta8)
                if float(theta7) > 170.0 or float(theta8) > 170.0:
                    count = count + 1
                    cv2.rectangle(canvas, (645, 0), (900, 60),
                                  color=(255, 25, 100),
                                  thickness=2)
                    cv2.putText(canvas,
                                "Left  :- {0:.2f}".format(float(theta7)),
                                (650, 30), cv2.FONT_HERSHEY_DUPLEX, 1,
                                (0, 0, 225))
                    cv2.putText(canvas,
                                "Right :- {0:.2f}".format(float(theta8)),
                                (650, 45), cv2.FONT_HERSHEY_DUPLEX, 1,
                                (0, 0, 255))
                    cv2.putText(canvas,
                                f"count :- {count}".format(float(theta8)),
                                (300, 35), cv2.FONT_HERSHEY_DUPLEX, 1,
                                (0, 0, 255))

                else:
                    cv2.rectangle(canvas, (645, 0), (900, 60),
                                  color=(255, 25, 100),
                                  thickness=2)
                    cv2.putText(canvas,
                                "Left  :- {0:.2f}".format(float(theta7)),
                                (650, 30), cv2.FONT_HERSHEY_DUPLEX, 1,
                                (0, 255, 0))
                    cv2.putText(canvas,
                                "Right :- {0:.2f}".format(float(theta8)),
                                (650, 45), cv2.FONT_HERSHEY_DUPLEX, 1,
                                (0, 255, 0))

            else:
                print("Sorry Wrong Excersise was given")

            print('Processing frame: ', i)
            toc = time.time()

            out.write(canvas)

        ret_val, orig_image = cam.read()

        i += 1

        if cv2.waitKey(25) & 0xFF == ord('q'):
            break

    cv2.destroyAllWindows()
    cam.release()

    convert_video(
        '/home/saireddy/SaiReddy/Desk/Flask/OrbitPose/videos/output/pose14.avi',
        '/home/saireddy/SaiReddy/Desk/Flask/OrbitPose/videos/output/pose14.mp4'
    )

    return Angle1, Angle2, Angle3, Angle4
Example #7
0
            break

        if mirror:
            orig_image = cv2.flip(orig_image, 1)

        tic = time.time()

        cropped = crop(orig_image, width, factor)

        input_image = cv2.resize(cropped, (0, 0), fx=1/resize_fac, fy=1/resize_fac, interpolation=cv2.INTER_CUBIC)

        input_image = cv2.cvtColor(input_image, cv2.COLOR_RGB2BGR)

        # generate image with body parts
        body_parts, all_peaks, subset, candidate = extract_parts(input_image, params, model, model_params)
        canvas = draw(cropped, all_peaks, subset, candidate, resize_fac=resize_fac)

        print('Processing frame: ', i)
        toc = time.time()
        print('processing time is %.5f' % (toc - tic))

        if out is not None:
            out.write(canvas)

        # canvas = cv2.resize(canvas, (0, 0), fx=4, fy=4, interpolation=cv2.INTER_CUBIC)

        cv2.imshow('frame', canvas)

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
Example #8
0
    def cameraLoad():
        parser = argparse.ArgumentParser()
        parser.add_argument('--device',
                            type=int,
                            default=0,
                            help='ID of the device to open')  #parser에서 받는거
        parser.add_argument('--model',
                            type=str,
                            default='model/keras/model.h5',
                            help='path to the weights file')  #모델경로
        parser.add_argument('--frame_ratio',
                            type=int,
                            default=7,
                            help='analyze every [n] frames')
        # --process_speed changes at how many times the model analyzes each frame at a different scale
        parser.add_argument(
            '--process_speed',
            type=int,
            default=1,
            help=
            'Int 1 (fastest, lowest quality) to 4 (slowest, highest quality)')
        parser.add_argument('--out_name',
                            type=str,
                            default=None,
                            help='name of the output file to write')
        parser.add_argument('--mirror',
                            type=bool,
                            default=True,
                            help='whether to mirror the camera')

        # 받은 값들 저장하는것
        args = parser.parse_args()
        device = args.device
        keras_weights_file = args.model
        frame_rate_ratio = args.frame_ratio
        process_speed = args.process_speed
        out_name = args.out_name
        mirror = args.mirror

        print('start processing...')

        # load model
        # authors of original model don't use
        # vgg normalization (subtracting mean) on input images
        model = get_testing_model()
        model.load_weights(keras_weights_file)

        # load config
        params, model_params = config_reader()

        # Video reader
        cam = cv2.VideoCapture(device)  # 디바이스=0은 카메라, 파일넣고싶으면 파일 경로 넣으면 됨
        # CV_CAP_PROP_FPS
        input_fps = cam.get(cv2.CAP_PROP_FPS)  # 해당카메라의 프레임으로 추측
        print("Running at {} fps.".format(input_fps))

        ret_val, orig_image = cam.read(
        )  #ret = 제대로 read됐는지확인하는 부울타입, orig = 실질적인 프레임단위의 이미지

        width = orig_image.shape[1]
        height = orig_image.shape[0]
        factor = 0.3

        out = None
        # Output location
        if out_name is not None and ret_val is not None:  # out_name이 parser 40번째줄/ out_name이 잇으면 파일생성/
            output_path = 'videos/outputs/'
            output_format = '.mp4'
            video_output = output_path + out_name + output_format

            # Video writer
            output_fps = input_fps / frame_rate_ratio

            tmp = crop(orig_image, width, factor)

            fourcc = cv2.VideoWriter_fourcc(*'mp4v')
            out = cv2.VideoWriter(video_output, fourcc, output_fps,
                                  (tmp.shape[1], tmp.shape[0]))

            del tmp

        scale_search = [0.22, 0.25, .5, 1, 1.5, 2]  # [.5, 1, 1.5, 2]
        scale_search = scale_search[0:process_speed]

        params['scale_search'] = scale_search

        i = 0  # default is 0
        resize_fac = 8
        # while(cam.isOpened()) and ret_val is True:
        while True:

            cv2.waitKey(10)

            if cam.isOpened() is False or ret_val is False:
                break

            if mirror:
                orig_image = cv2.flip(orig_image, 1)

            tic = time.time()

            cropped = crop(orig_image, width, factor)  # 파일 자름
            #opencv함수로 사이즈 조절하는 것
            input_image = cv2.resize(cropped, (0, 0),
                                     fx=1 / resize_fac,
                                     fy=1 / resize_fac,
                                     interpolation=cv2.INTER_CUBIC)

            input_image = cv2.cvtColor(input_image, cv2.COLOR_RGB2BGR)

            # generate image with body parts
            # extract_part
            all_peaks, subset, candidate = extract_parts(
                input_image, params, model, model_params)
            canvas = draw(cropped,
                          all_peaks,
                          subset,
                          candidate,
                          resize_fac=resize_fac)

            print('Processing frame: ', i)
            toc = time.time()
            print('processing time is %.5f' % (toc - tic))

            if out is not None:  # 이게 있으면 계속 출력하는 것
                out.write(canvas)  # 이미지 위에 만들어진 스켈레톤!

            # canvas = cv2.resize(canvas, (0, 0), fx=4, fy=4, interpolation=cv2.INTER_CUBIC)

            cv2.imshow('frame', canvas)

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

            ret_val, orig_image = cam.read()

            i += 1
        cropped = crop(orig_image, width, factor)

        input_image = cv2.resize(cropped, (0, 0),
                                 fx=1 / resize_fac,
                                 fy=1 / resize_fac,
                                 interpolation=cv2.INTER_CUBIC)

        input_image = cv2.cvtColor(input_image, cv2.COLOR_RGB2BGR)

        # generate image with body parts
        body_parts, all_peaks, subset, candidate = extract_parts(
            input_image, params, model, model_params)
        canvas, dict, lis1, lis2 = draw(cropped,
                                        all_peaks,
                                        subset,
                                        candidate,
                                        resize_fac=resize_fac)

        print('Processing frame: ', i)
        toc = time.time()
        print('processing time is %.5f' % (toc - tic))

        Concatena.salva_csv_dist(lis1, lis2, 'none')

        dataframe = pandas.read_csv("dataset_dist.csv")

        dataset = dataframe.values

        riga = dataframe.shape[0]
Example #10
0
        input_image = cv2.resize(cropped, (0, 0),
                                 fx=1 / resize_fac,
                                 fy=1 / resize_fac,
                                 interpolation=cv2.INTER_CUBIC)

        input_image = cv2.cvtColor(input_image, cv2.COLOR_RGB2BGR)

        print(cropped.shape, input_image.shape)

        # generate image with body parts
        all_peaks, subset, candidate = extract_parts(input_image, params,
                                                     model, model_params)
        canvas = draw(cropped,
                      all_peaks,
                      subset,
                      candidate,
                      resize_fac=resize_fac)

        # add remove background only skeleton
        numpy_tmp = np.zeros((canvas.shape[0], canvas.shape[1], 3),
                             dtype=np.uint8)
        canvas_test = draw(numpy_tmp,
                           all_peaks,
                           subset,
                           candidate,
                           resize_fac=resize_fac)

        print('Processing frame: ', i)
        toc = time.time()
        print('processing time is %.5f' % (toc - tic))