这里,我的代码:
#distance of pose
if results.pose_landmarks:
for pose in poses:
landmark_attribute = getattr(mp_pose.PoseLandmark, pose)
landmark_values = []
for suffix in dot_suffixes:
landmark_final = getattr(results.pose_landmarks.landmark[landmark_attribute], suffix)
if suffix == 'x' or suffix == 'z':
landmark_final *= 640
elif suffix == 'y':
landmark_final *= 320
landmark_values += (landmark_final,)
distance_data[pose].append(landmark_values)
with open('distance_pose.csv', 'a', newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerow([item for sublist in [distance_data[pose] for pose in poses] for item in sublist])
我正在开发一个项目,需要实现特定的输出布局,如链接中的图片所示:https://drive.google.com/file/d/1qX5Hs7PaQaUf8O7Y_2y2HX9pQiU2uV2q/view?usp=sharing
我已经尝试过,但没有得到想要的结果。有人可以指导我如何实现如图所示的效果吗?
你可以用 Pandas 来做到这一点。
在您的相应的输出中,您的数据将行拆分到列“F”处的新行,并且在您的绘制的图像中它扩展到L和“依此类推”。不知道你是不是也想吐槽
此代码示例保持行完整。输入 CSV 文件中的每行有 33 列,在输出 CSV 中扩展到 99 列。
我已经包含了 CSV 的标题行,其中包含 Excel 使用的列字母,因此标题来自“A”-“CU”。如果您不需要标题,您可以像“to_excel”示例一样设置
header=False
。您还可以删除重命名标题的行。
import pandas as pd
import openpyxl # Use Openpyxl util to get letter for column number
### Creates a list of letters per Excel column naming to size specified.
def headers(size):
return [openpyxl.utils.cell.get_column_letter(x) for x in range(1, size+1)]
input_csv_file = 'distance_pose.csv'
output_csv_file = 'desired_output.csv'
### Create dataframe from the input CSV file
df = pd.read_csv(input_csv_file, header=None)
### Remove the open or close bracket from every cell where they exist
df.replace(r'\(|\)', '', regex=True, inplace=True)
### Create a new dataframe df2 from expanding each cell to 3 numbers
df2 = pd.DataFrame()
df.columns = [i for i in range(df.shape[1])]
for x in range(len(df.columns)):
df2 = pd.concat([df2, df[x].str.split(',', expand=True)], axis=1)
### Change the Headers to letters (Excel Column Letters)
df2 = df2.set_axis(headers(df2.shape[1]), axis=1)
### Write to Excel sheet if that is desired
# df2.to_excel('distance_pose.xlsx', header=False, index=False)
### Write to CSV file
df2.to_csv(output_csv_file, index=False)