Skip to content

Instantly share code, notes, and snippets.

@smeschke
Last active April 7, 2023 07:08
Show Gist options
  • Select an option

  • Save smeschke/a3dcf7ce7d7d8bf684b26c8ec08d38f0 to your computer and use it in GitHub Desktop.

Select an option

Save smeschke/a3dcf7ce7d7d8bf684b26c8ec08d38f0 to your computer and use it in GitHub Desktop.

Revisions

  1. smeschke revised this gist Apr 17, 2019. 1 changed file with 25 additions and 46 deletions.
    71 changes: 25 additions & 46 deletions save_pose_data.py
    Original file line number Diff line number Diff line change
    @@ -1,18 +1,19 @@
    import cv2, numpy as np, csv

    #https://github.com/opencv/opencv/blob/master/samples/dnn/openpose.py
    outfile_path = '/home/stephen/Desktop/workout.csv'

    protoFile = "/home/stephen/Desktop/pose/mpi/pose_deploy_linevec_faster_4_stages.prototxt"
    weightsFile = "/home/stephen/Desktop/pose/mpi/pose_iter_160000.caffemodel"
    protoFile = "/home/stephen/pose/mpi/pose_deploy_linevec_faster_4_stages.prototxt"
    weightsFile = "/home/stephen/pose/mpi/pose_iter_160000.caffemodel"
    net = cv2.dnn.readNetFromCaffe(protoFile, weightsFile)

    data, input_width, input_height, threshold, frame_number = [], 368, 386, 0.1, 0

    input_source = "/home/stephen/Desktop/me.MP4"
    input_source = "/home/stephen/Desktop/workout.MP4"
    cap = cv2.VideoCapture(input_source)

    # use the previous location of the body part if the model is wrong
    previous_points = [(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0)]
    previous_x, previous_y = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

    while True:

    @@ -33,7 +34,7 @@
    output_width, output_height = output.shape[2], output.shape[3]

    # Empty list to store the detected keypoints
    points = []
    x_data, y_data = [], []

    # Iterate through the body parts
    for i in range(15):
    @@ -45,52 +46,30 @@
    x, y = (img_width * point[0]) / output_width, (img_height * point[1]) / output_height

    # Is the point likely to be correct?
    if prob > threshold: points.append((int(x), int(y)))
    if prob > threshold:
    x_data.append(x)
    y_data.append(y)
    xy = tuple(np.array([x,y], int))
    cv2.circle(img, xy, 5, (25,0,255), 5)
    # No? us the location in the previous frame
    else: points.append(previous_points[i])
    else:
    x_data.append(previous_x[i])
    y_data.append(previous_y[i])

    # add these points to the list of data
    data.append(points)
    previous_points = points
    data.append(x_data + y_data)
    previous_x, previous_y = x_data, y_data
    frame_number+=1
    # use this break statement to check your data before processing the whole video
    if frame_number == 10: break
    #if frame_number == 300: break
    print(frame_number)

    cv2.imshow('img', img)
    k = cv2.waitKey(1)
    if k == 27: break

    # write the data to a .csv file
    with open('/home/stephen/Desktop/open_pose_data.csv', 'w') as csvfile:
    fieldnames = []
    for i in range(30): fieldnames.append(str(i))
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
    writer.writeheader()
    for trick in data:
    writer.writerow({'0': trick[0][0],
    '1':trick[0][1],
    '2':trick[1][0],
    '3':trick[1][1],
    '4':trick[2][0],
    '5':trick[2][1],
    '6':trick[3][0],
    '7':trick[3][1],
    '8':trick[4][0],
    '9':trick[4][1],
    '10':trick[5][0],
    '11':trick[5][1],
    '12':trick[6][0],
    '13':trick[6][1],
    '14':trick[7][0],
    '15':trick[7][1],
    '16':trick[8][0],
    '17':trick[8][1],
    '18':trick[9][0],
    '19':trick[9][1],
    '20':trick[10][0],
    '21':trick[10][1],
    '22':trick[11][0],
    '23':trick[11][1],
    '24':trick[12][0],
    '25':trick[12][1],
    '26':trick[13][0],
    '27':trick[13][1],
    '28':trick[14][0],
    '29':trick[14][1]})
    import pandas as pd
    df = pd.DataFrame(data)
    df.to_csv(outfile_path, index = False)
    print('save complete')
  2. smeschke created this gist Sep 21, 2018.
    96 changes: 96 additions & 0 deletions save_pose_data.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,96 @@
    import cv2, numpy as np, csv

    #https://github.com/opencv/opencv/blob/master/samples/dnn/openpose.py

    protoFile = "/home/stephen/Desktop/pose/mpi/pose_deploy_linevec_faster_4_stages.prototxt"
    weightsFile = "/home/stephen/Desktop/pose/mpi/pose_iter_160000.caffemodel"
    net = cv2.dnn.readNetFromCaffe(protoFile, weightsFile)

    data, input_width, input_height, threshold, frame_number = [], 368, 386, 0.1, 0

    input_source = "/home/stephen/Desktop/me.MP4"
    cap = cv2.VideoCapture(input_source)

    # use the previous location of the body part if the model is wrong
    previous_points = [(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0)]

    while True:

    ret, img = cap.read()
    if not ret: break

    # get the image shape
    img_width, img_height = img.shape[1], img.shape[0]

    # get a blob from the image
    inputBlob = cv2.dnn.blobFromImage(img, 1.0 / 255, (input_width, input_height),(0, 0, 0), swapRB=False, crop=False)

    # set the input and perform a forward pass
    net.setInput(inputBlob)
    output = net.forward()

    # get the output shape
    output_width, output_height = output.shape[2], output.shape[3]

    # Empty list to store the detected keypoints
    points = []

    # Iterate through the body parts
    for i in range(15):

    # find probability that point is correct
    _, prob, _, point = cv2.minMaxLoc(output[0, i, :, :])

    # Scale the point to fit on the original image
    x, y = (img_width * point[0]) / output_width, (img_height * point[1]) / output_height

    # Is the point likely to be correct?
    if prob > threshold: points.append((int(x), int(y)))
    # No? us the location in the previous frame
    else: points.append(previous_points[i])

    # add these points to the list of data
    data.append(points)
    previous_points = points
    frame_number+=1
    # use this break statement to check your data before processing the whole video
    if frame_number == 10: break
    print(frame_number)

    # write the data to a .csv file
    with open('/home/stephen/Desktop/open_pose_data.csv', 'w') as csvfile:
    fieldnames = []
    for i in range(30): fieldnames.append(str(i))
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
    writer.writeheader()
    for trick in data:
    writer.writerow({'0': trick[0][0],
    '1':trick[0][1],
    '2':trick[1][0],
    '3':trick[1][1],
    '4':trick[2][0],
    '5':trick[2][1],
    '6':trick[3][0],
    '7':trick[3][1],
    '8':trick[4][0],
    '9':trick[4][1],
    '10':trick[5][0],
    '11':trick[5][1],
    '12':trick[6][0],
    '13':trick[6][1],
    '14':trick[7][0],
    '15':trick[7][1],
    '16':trick[8][0],
    '17':trick[8][1],
    '18':trick[9][0],
    '19':trick[9][1],
    '20':trick[10][0],
    '21':trick[10][1],
    '22':trick[11][0],
    '23':trick[11][1],
    '24':trick[12][0],
    '25':trick[12][1],
    '26':trick[13][0],
    '27':trick[13][1],
    '28':trick[14][0],
    '29':trick[14][1]})