How to append once instead of every loop

sorry for the whole code

I’m very inexperienced and don’t know how to correctly simplify


Context

Pose Keypoint extraction of picture/video

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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, "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": xy_str})
for ja in ja_list:
angle = calculate_angle(xy[ja[0]],xy[ja[1]],xy[ja[2]])
str_angle = angle
data.append({"left elbow angle, right elbow angle, left shoulder angle, right shoulder angle, left hip angle, right hip angle, left knee angle, right knee angle": str_angle})
df = pd.DataFrame(data)
df.to_csv('file.csv', escapechar=' ', quoting=csv.QUOTE_NONE, index=False)
</code>
<code>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, "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": xy_str}) for ja in ja_list: angle = calculate_angle(xy[ja[0]],xy[ja[1]],xy[ja[2]]) str_angle = angle data.append({"left elbow angle, right elbow angle, left shoulder angle, right shoulder angle, left hip angle, right hip angle, left knee angle, right knee angle": str_angle}) df = pd.DataFrame(data) df.to_csv('file.csv', escapechar=' ', quoting=csv.QUOTE_NONE, index=False) </code>
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, "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": xy_str})

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

            data.append({"left elbow angle, right elbow angle, left shoulder angle, right shoulder angle, left hip angle, right hip angle, left knee angle, right knee angle": str_angle})

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

Current Output

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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
</code>
<code>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 </code>
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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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
</code>
<code>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 </code>
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>for ja in ja_list:
angle = calculate_angle(xy[ja[0]],xy[ja[1]],xy[ja[2]])
str_angle = angle
data.append({"left elbow angle, right elbow angle, left shoulder angle, right shoulder angle, left hip angle, right hip angle, left knee angle, right knee angle": str_angle})
</code>
<code>for ja in ja_list: angle = calculate_angle(xy[ja[0]],xy[ja[1]],xy[ja[2]]) str_angle = angle data.append({"left elbow angle, right elbow angle, left shoulder angle, right shoulder angle, left hip angle, right hip angle, left knee angle, right knee angle": str_angle}) </code>
for ja in ja_list:
            angle = calculate_angle(xy[ja[0]],xy[ja[1]],xy[ja[2]])
            str_angle = angle

            data.append({"left elbow angle, right elbow angle, left shoulder angle, right shoulder angle, left hip angle, right hip angle, left knee angle, right knee angle": str_angle})

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

I need a way to append all 8 angle values once

I don’t know how to integrate properly…


Sorry if this is trivial, I’ve been stuck on this for hours searching anywhere I could but I give up

New contributor

marc is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật