[Python] How to append once instead of every loop [closed]

Discussão em 'Python' iniciado por Stack, Dezembro 8, 2024.

  1. Stack

    Stack Membro Participativo

    sorry for the whole code

    I'm unsure how to simplify unfortunately

    Context


    Pose Keypoint extraction of picture/every frame in video

    keypoint coordinates & calculated angles export to .csv for later analysis

    import csv
    import pandas as pd
    from ultralytics import YOLO
    from pathlib import Path
    import numpy as np

    model = YOLO("yolo11n-pose.pt")
    results = model.track(source='file.png', save=False)

    def calculate_angle(p1, p2, p3):
    # p1, p2, p3 are the points in format [x, y]
    # Calculate the vectors
    v1 = np.array(p1) - np.array(p2)
    v2 = np.array(p3) - np.array(p2)

    # Calculate the angle in radians
    angle_rad = np.arccos(np.dot(v1, v2) / (np.linalg.norm(v1) * np.linalg.norm(v2)))

    # Convert to degrees
    angle_deg = np.degrees(angle_rad)

    return angle_deg


    ja_list = [[9,7,5], [10,8,6], [7,5,11], [8,6,12], [5,11,13], [6,12,14], [11,13,15], [12,14,16]]

    # Initialize an empty list to store data
    data = []

    for frame_id, (frame_results) in enumerate(results):
    for person_id, person in enumerate(frame_results.keypoints):
    xy = person.xyn.numpy() # keypoints coordinates
    conf = person.conf.numpy() # confidence scores of keypoints
    for kp_id, (xy, kp_conf) in enumerate(zip(xy, conf)):
    xy_str = ', '.join(map(str, xy)) # Convert the array to a comma-separated string

    data.append({"frame_id": frame_id, "person_id": person_id, "xy": xy_str})

    for ja in ja_list:
    angle = calculate_angle(xy[ja[0]],xy[ja[1]],xy[ja[2]])
    str_angle = angle

    data.append({"angles": str_angle})

    df = pd.DataFrame(data)
    df.to_csv('file.csv', escapechar=' ', quoting=csv.QUOTE_NONE, index=False)

    Current Output


    frame_id,person_id,nose , left eye , right eye , left ear , right ear , left shoulder , right shoulder , left elbow , right elbow , left wrist , right wrist , left hip , right hip , left knee , right knee , left ankle , right ankle,left elbow angle , right elbow angle , left shoulder angle , right shoulder angle , left hip angle , right hip angle , left knee angle , right knee angle
    0.0,0.0,[ 0.27054 0.32959] , [ 0.27079 0.32787] , [ 0.26698 0.32339] , [ 0 0] , [ 0.25605 0.33095] , [ 0.27429 0.39124] , [ 0.24764 0.34531] , [ 0.29295 0.40439] , [ 0.25213 0.2805] , [ 0.31294 0.41499] , [ 0.26839 0.21751] , [ 0.25684 0.49569] , [ 0.23976 0.47824] , [ 0.26361 0.59153] , [ 0.25314 0.57706] , [ 0.26301 0.68071] , [ 0.25716 0.68032],
    ,,,172.7757568359375
    ,,,169.49624633789062
    ,,,64.31809997558594
    ,,,179.4266357421875
    ,,,166.47421264648438
    ,,,168.8917999267578
    ,,,175.57078552246094
    ,,,174.5126953125

    Desired Output


    frame_id,person_id,nose , left eye , right eye , left ear , right ear , left shoulder , right shoulder , left elbow , right elbow , left wrist , right wrist , left hip , right hip , left knee , right knee , left ankle , right ankle,left elbow angle , right elbow angle , left shoulder angle , right shoulder angle , left hip angle , right hip angle , left knee angle , right knee angle
    0.0,0.0,[ 0.27054 0.32959] , [ 0.27079 0.32787] , [ 0.26698 0.32339] , [ 0 0] , [ 0.25605 0.33095] , [ 0.27429 0.39124] , [ 0.24764 0.34531] , [ 0.29295 0.40439] , [ 0.25213 0.2805] , [ 0.31294 0.41499] , [ 0.26839 0.21751] , [ 0.25684 0.49569] , [ 0.23976 0.47824] , [ 0.26361 0.59153] , [ 0.25314 0.57706] , [ 0.26301 0.68071] , [ 0.25716 0.68032], 172.7757568359375, 169.49624633789062, 64.31809997558594, 179.4266357421875, 166.47421264648438, 168.8917999267578, 175.57078552246094, 174.5126953125

    Problem:


    for ja in ja_list:
    angle = calculate_angle(xy[ja[0]],xy[ja[1]],xy[ja[2]])
    str_angle = angle

    data.append({angles})


    I think the problem is the data.append is being looped.

    I need a way to append all 8 angle values once

    Solution:


    for kp_id, (ja, xy, kp_conf) in enumerate(zip(ja_list, xy, conf)):
    xy_str = ', '.join(map(str, xy)) # Convert the array to a comma-separated string

    angles = [str(calculate_angle(xy[ja[0]], xy[ja[1]], xy[ja[2]])) for ja in ja_list]
    str_angles = ', '.join(map(str, angles)).replace("'", "")

    data.append({"frame_id": frame_id, "person_id": person_id, "xy": xy_str, "angles": str_angles})


    Desired Output achieved!

    Continue reading...

Compartilhe esta Página