Esempio n. 1
0
 def load_model(self):
     """
     加载模型
     """
     model = get_testing_model()
     model.load_weights(self.weights_path)
     params, model_params = config_reader(self.config_path)
     return model, params, model_params
Esempio n. 2
0
def process_one_img():
    img_path = os.path.join(DATA_DIR, 'test_img.jpg')
    keras_weights_file = os.path.join(ROOT_DIR, "model/keras/model.h5")
    model = get_testing_model()
    model.load_weights(keras_weights_file)

    output_img = os.path.join(img_path + ".r.jpg")
    output_pos = os.path.join(img_path + ".p.txt")
    predict_img(img_path, model, output_img, output_pos)
Esempio n. 3
0
def get_open_pose():

    #load model
    model = get_testing_model()
    model.load_weights(keras_weights_file)

    # load config
    params, model_params = config_reader()

    return model, params, model_params
Esempio n. 4
0
def process_img_batch():
    keras_weights_file = os.path.join(ROOT_DIR, "model/keras/model.h5")
    output_img_dir = os.path.join(DATA_DIR, "results")
    output_pos_dir = os.path.join(DATA_DIR, "pos")
    img_dir = os.path.join(DATA_DIR, 'frames-p')

    model = get_testing_model()
    model.load_weights(keras_weights_file)

    paths_list, names_list = traverse_dir_files(img_dir)

    for path, name in zip(paths_list, names_list):
        print('[Info] name: {}'.format(name))
        output_img = os.path.join(output_img_dir, name + ".r.jpg")
        output_pos = os.path.join(output_pos_dir, name + ".p.txt")
        predict_img(path, model, output_img, output_pos)
def generate_model_blobs(in_video_file, starting_frame, ending_frame):
    from model.cmu_model import get_testing_model
    from processing_action import get_model_blob
    # 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)

    # Video reader
    cam = cv2.VideoCapture(in_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))

    if ending_frame is None:
        ending_frame = video_length

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

    if ending_frame is None:
        ending_frame = video_length

    i = 0
    if starting_frame > 0:
        while (cam.isOpened()) and ret_val is True and i < starting_frame:
            ret_val, orig_image = cam.read()
            i += 1

    blobs = {}
    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()
            blobs[i] = get_model_blob(input_image, params, model, model_params)
            toc = time.time()
            print(f"Generate model blob, frame: {i}, took {toc - tic} seconds")

        ret_val, orig_image = cam.read()
        i += 1
    return blobs
Esempio n. 6
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()
                        default='model/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()

    #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')
Esempio n. 8
0
def process(input_image, params, model_params):
    # load openpose model
    # vgg normalization (subtracting mean) on input images
    model = get_testing_model()
    model.load_weights('model/keras/model.h5')


    # for openpose
    # find connection in the specified sequence, center 29 is in the position 15
    limbSeq = [[2, 3], [2, 6], [3, 4], [4, 5], [6, 7], [7, 8], [2, 9], [9, 10], \
               [10, 11], [2, 12], [12, 13], [13, 14], [2, 1], [1, 15], [15, 17], \
               [1, 16], [16, 18], [3, 17], [6, 18]]

    # the middle joints heatmap correpondence
    mapIdx = [[31, 32], [39, 40], [33, 34], [35, 36], [41, 42], [43, 44], [19, 20], [21, 22], \
              [23, 24], [25, 26], [27, 28], [29, 30], [47, 48], [49, 50], [53, 54], [51, 52], \
              [55, 56], [37, 38], [45, 46]]

    # visualize
    '''
    colors = [[255, 0, 0], [255, 85, 0], [255, 170, 0], [255, 255, 0], [170, 255, 0], [85, 255, 0],
              [0, 255, 0], \
              [0, 255, 85], [0, 255, 170], [0, 255, 255], [0, 170, 255], [0, 85, 255], [0, 0, 255],
              [85, 0, 255], \
              [170, 0, 255], [255, 0, 255], [255, 0, 170], [255, 0, 85]]
              '''
    
    oriImg = input_image  # B,G,R order
    multiplier = [x * model_params['boxsize'] / oriImg.shape[0] for x in params['scale_search']]

    heatmap_avg = np.zeros((oriImg.shape[0], oriImg.shape[1], 19))
    paf_avg = np.zeros((oriImg.shape[0], oriImg.shape[1], 38))

    for m in range(len(multiplier)):
        scale = multiplier[m]

        imageToTest = cv2.resize(oriImg, (0, 0), fx=scale, fy=scale, interpolation=cv2.INTER_CUBIC)
        imageToTest_padded, pad = util.padRightDownCorner(imageToTest, model_params['stride'],
                                                          model_params['padValue'])

        input_img = np.transpose(np.float32(imageToTest_padded[:,:,:,np.newaxis]), (3,0,1,2)) # required shape (1, width, height, channels)

        output_blobs = model.predict(input_img)

        # extract outputs, resize, and remove padding
        heatmap = np.squeeze(output_blobs[1])  # output 1 is heatmaps
        heatmap = cv2.resize(heatmap, (0, 0), fx=model_params['stride'], fy=model_params['stride'],
                             interpolation=cv2.INTER_CUBIC)
        heatmap = heatmap[:imageToTest_padded.shape[0] - pad[2], :imageToTest_padded.shape[1] - pad[3],
                  :]
        heatmap = cv2.resize(heatmap, (oriImg.shape[1], oriImg.shape[0]), interpolation=cv2.INTER_CUBIC)

        paf = np.squeeze(output_blobs[0])  # output 0 is PAFs
        paf = cv2.resize(paf, (0, 0), fx=model_params['stride'], fy=model_params['stride'],
                         interpolation=cv2.INTER_CUBIC)
        paf = paf[:imageToTest_padded.shape[0] - pad[2], :imageToTest_padded.shape[1] - pad[3], :]
        paf = cv2.resize(paf, (oriImg.shape[1], oriImg.shape[0]), interpolation=cv2.INTER_CUBIC)

        heatmap_avg = heatmap_avg + heatmap / len(multiplier)
        paf_avg = paf_avg + paf / len(multiplier)

    all_peaks = []
    peak_counter = 0

    for part in range(18):
        map_ori = heatmap_avg[:, :, part]
        map = gaussian_filter(map_ori, sigma=3)

        map_left = np.zeros(map.shape)
        map_left[1:, :] = map[:-1, :]
        map_right = np.zeros(map.shape)
        map_right[:-1, :] = map[1:, :]
        map_up = np.zeros(map.shape)
        map_up[:, 1:] = map[:, :-1]
        map_down = np.zeros(map.shape)
        map_down[:, :-1] = map[:, 1:]

        peaks_binary = np.logical_and.reduce(
            (map >= map_left, map >= map_right, map >= map_up, map >= map_down, map > params['thre1']))
        peaks = list(zip(np.nonzero(peaks_binary)[1], np.nonzero(peaks_binary)[0]))  # note reverse
        peaks_with_score = [x + (map_ori[x[1], x[0]],) for x in peaks]
        id = range(peak_counter, peak_counter + len(peaks))
        peaks_with_score_and_id = [peaks_with_score[i] + (id[i],) for i in range(len(id))]

        all_peaks.append(peaks_with_score_and_id)
        peak_counter += len(peaks)

    connection_all = []
    special_k = []
    mid_num = 10

    for k in range(len(mapIdx)):
        score_mid = paf_avg[:, :, [x - 19 for x in mapIdx[k]]]
        candA = all_peaks[limbSeq[k][0] - 1]
        candB = all_peaks[limbSeq[k][1] - 1]
        nA = len(candA)
        nB = len(candB)
        indexA, indexB = limbSeq[k]
        if (nA != 0 and nB != 0):
            connection_candidate = []
            for i in range(nA):
                for j in range(nB):
                    vec = np.subtract(candB[j][:2], candA[i][:2])
                    norm = math.sqrt(vec[0] * vec[0] + vec[1] * vec[1])
                    # failure case when 2 body parts overlaps
                    if norm == 0:
                        continue
                    vec = np.divide(vec, norm)

                    startend = list(zip(np.linspace(candA[i][0], candB[j][0], num=mid_num), \
                                   np.linspace(candA[i][1], candB[j][1], num=mid_num)))

                    vec_x = np.array(
                        [score_mid[int(round(startend[I][1])), int(round(startend[I][0])), 0] \
                         for I in range(len(startend))])
                    vec_y = np.array(
                        [score_mid[int(round(startend[I][1])), int(round(startend[I][0])), 1] \
                         for I in range(len(startend))])

                    score_midpts = np.multiply(vec_x, vec[0]) + np.multiply(vec_y, vec[1])
                    score_with_dist_prior = sum(score_midpts) / len(score_midpts) + min(
                        0.5 * oriImg.shape[0] / norm - 1, 0)
                    criterion1 = len(np.nonzero(score_midpts > params['thre2'])[0]) > 0.8 * len(
                        score_midpts)
                    criterion2 = score_with_dist_prior > 0
                    if criterion1 and criterion2:
                        connection_candidate.append([i, j, score_with_dist_prior,
                                                     score_with_dist_prior + candA[i][2] + candB[j][2]])

            connection_candidate = sorted(connection_candidate, key=lambda x: x[2], reverse=True)
            connection = np.zeros((0, 5))
            for c in range(len(connection_candidate)):
                i, j, s = connection_candidate[c][0:3]
                if (i not in connection[:, 3] and j not in connection[:, 4]):
                    connection = np.vstack([connection, [candA[i][3], candB[j][3], s, i, j]])
                    if (len(connection) >= min(nA, nB)):
                        break

            connection_all.append(connection)
        else:
            special_k.append(k)
            connection_all.append([])

    # last number in each row is the total parts number of that person
    # the second last number in each row is the score of the overall configuration
    subset = -1 * np.ones((0, 20))
    candidate = np.array([item for sublist in all_peaks for item in sublist])

    for k in range(len(mapIdx)):
        if k not in special_k:
            partAs = connection_all[k][:, 0]
            partBs = connection_all[k][:, 1]
            indexA, indexB = np.array(limbSeq[k]) - 1

            for i in range(len(connection_all[k])):  # = 1:size(temp,1)
                found = 0
                subset_idx = [-1, -1]
                for j in range(len(subset)):  # 1:size(subset,1):
                    if subset[j][indexA] == partAs[i] or subset[j][indexB] == partBs[i]:
                        subset_idx[found] = j
                        found += 1

                if found == 1:
                    j = subset_idx[0]
                    if (subset[j][indexB] != partBs[i]):
                        subset[j][indexB] = partBs[i]
                        subset[j][-1] += 1
                        subset[j][-2] += candidate[partBs[i].astype(int), 2] + connection_all[k][i][2]
                elif found == 2:  # if found 2 and disjoint, merge them
                    j1, j2 = subset_idx
                    membership = ((subset[j1] >= 0).astype(int) + (subset[j2] >= 0).astype(int))[:-2]
                    if len(np.nonzero(membership == 2)[0]) == 0:  # merge
                        subset[j1][:-2] += (subset[j2][:-2] + 1)
                        subset[j1][-2:] += subset[j2][-2:]
                        subset[j1][-2] += connection_all[k][i][2]
                        subset = np.delete(subset, j2, 0)
                    else:  # as like found == 1
                        subset[j1][indexB] = partBs[i]
                        subset[j1][-1] += 1
                        subset[j1][-2] += candidate[partBs[i].astype(int), 2] + connection_all[k][i][2]

                # if find no partA in the subset, create a new subset
                elif not found and k < 17:
                    row = -1 * np.ones(20)
                    row[indexA] = partAs[i]
                    row[indexB] = partBs[i]
                    row[-1] = 2
                    row[-2] = sum(candidate[connection_all[k][i, :2].astype(int), 2]) + \
                              connection_all[k][i][2]
                    subset = np.vstack([subset, row])

    # delete some rows of subset which has few parts occur
    deleteIdx = [];
    for i in range(len(subset)):
        if subset[i][-1] < 4 or subset[i][-2] / subset[i][-1] < 0.4:
            deleteIdx.append(i)
    subset = np.delete(subset, deleteIdx, axis=0)
    '''
    for i in range(18):
        for j in range(len(all_peaks[i])):
            cv2.circle(input_image, all_peaks[i][j][0:2], 4, colors[i], thickness=-1)

    stickwidth = 4
    '''
    for n in range(len(subset)):
        right_shoulder_X = int(candidate[int(subset[n][2]), 0])
        right_shoulder_Y = int(candidate[int(subset[n][2]), 1])
        left_shoulder_X = int(candidate[int(subset[n][5]), 0])  
        left_shoulder_Y = int(candidate[int(subset[n][5]), 1])
        if abs(right_shoulder_X - left_shoulder_X) >= 100:
            body_ori = input_image[right_shoulder_Y:, right_shoulder_X:left_shoulder_X]
            body_RGB = cv2.cvtColor(body_ori, cv2.COLOR_BGR2RGB)
            # reshape the image to be a list of pixels
            body_reshape = body_RGB.reshape((body_RGB.shape[0] * body_RGB.shape[1], 3))
            # cluster the pixel intensities
            clt = KMeans(n_clusters = 5)
            # reshape the image to be a list of pixels
            clt.fit(body_reshape)
            # 找出各色之比重
            hist = utils.centroid_histogram(clt)
            #找出顏色比重最重的hist index
            color_idx = np.where(hist==np.max(hist))
            #主色RDB為: clt.cluster_centers_[color_idx]
            color = clt.cluster_centers_[color_idx].flatten().tolist()
        else:
            continue

    return color[0], color[1], color[2]
Esempio n. 9
0
    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') #model/keras/model.h5

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

    
    print('start processing...')

    # load model

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

    if False: # if model.h5 is trained on multi GPU
        from keras.utils import training_utils
        model = training_utils.multi_gpu_model(model,gpus=2)
    model.load_weights(keras_weights_file)

    # load config
    params, model_params = config_reader()

    tic = time.time()
    # generate image with body parts
    canvas = process(input_image, params, model_params)

    toc = time.time()
    print ('processing time is %.5f' % (toc - tic))
Esempio n. 10
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
Esempio n. 11
0
def load_m(model_path):
    keras_weights_file = "model.h5"
    global model
    model = get_testing_model()
    model.load_weights(keras_weights_file)
def video_cap():
    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/sit.mp4'
    print(video_file)

    # Output location
    video_output = '/home/saireddy/SaiReddy/Desk/Flask/OrbitPose/videos/output/sit.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
    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()
            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))
            print('Processing frame: ', i)
            toc = time.time()
            print(Angle5)
            print(Angle6)
            out.write(canvas)
        ret_val, orig_image = cam.read()
        i += 1
        if cv2.waitKey(25) & 0xFF == ord('q'):
            break

    return Angle5, Angle6
Esempio n. 13
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
Esempio n. 14
0
def load_model(keras_weights_file):
    model = get_testing_model()
    model.load_weights(keras_weights_file)
    params, model_params = config_reader()
    return model, params, model_params