Pandas 在Keras中阅读Python词典
我正在尝试改编Kaggle面部关键点检测教程和代码,以解决类似的问题。我试图探测人体内的四个关键点;左肩、右肩、鼻子和脖子 我以前给图像贴标签,但现在excel文件的格式是两列,一列是图像名称,另一列是python字典,如下所示:Pandas 在Keras中阅读Python词典,pandas,keras,Pandas,Keras,我正在尝试改编Kaggle面部关键点检测教程和代码,以解决类似的问题。我试图探测人体内的四个关键点;左肩、右肩、鼻子和脖子 我以前给图像贴标签,但现在excel文件的格式是两列,一列是图像名称,另一列是python字典,如下所示: {'LS': [{'geometry': {'x': 45, 'y': 21}}], 'RS': [{'geometry': {'x': 28, 'y': 30}}], 'Neck': [{'geometry': {'x': 38, 'y': 25}}], 'N
{'LS': [{'geometry': {'x': 45, 'y': 21}}],
'RS': [{'geometry': {'x': 28, 'y': 30}}],
'Neck': [{'geometry': {'x': 38, 'y': 25}}],
'Nose': [{'geometry': {'x': 50, 'y': 15}}]}
而Kaggle数据集为每个点组件使用一列(一列代表x,另一列代表y)
是否有一种很好的方法将每个点的最终x和y值分离到单独的列中 用于将dict转换为正确的数据帧格式
from pandas.io.json import json_normalize
list_main=[]
for item in data:
temp = dict()
temp["tag"] = item
data[item][0].update(temp)
list_main.append(data[item][0])
final_df = json_normalize(list_main)
我使用了
pandas.io.json.json_normalize
,正如@tawab_shakel在下面的回答中所建议的,其中原始数据是从csv文件读取的
import ast
def extract_data(raw_data):
data_list=[]
items = ['LS', 'RS', 'Neck', 'Nose']
for row in raw_data.index:
#print(row)
x = raw_data ['Label'][row]
#print(type(x))
x = ast.literal_eval(x)
#print(type(x))
if'LS' not in x:
x= insert(x, {'fill_0':[{"geometry":{"x":45,"y":21}}]},0)
if'RS' not in x:
x= insert(x, {'fill_1':[{"geometry":{"x":45,"y":21}}]},1)
if'Neck' not in x:
x= insert(x, {'fill_2=':[{"geometry":{"x":45,"y":21}}]},2)
if'Nose' not in x:
x= insert(x, {'TEST_3':[{"geometry":{"x":45,"y":21}}]},3)
list_main=[]
point_index = 0
for item in x:
if item == items[point_index]:
temp = dict()
temp["tag"] = item
x[item][0].update(temp)
# print(list_main)
else:
x[item][0]['geometry']['x']=0
x[item][0]['geometry']['y']=0
x[item][0]['tag']= items[point_index]
list_main.append(x[item][0])
point_index +=1
final_df = json_normalize(list_main)
del final_df['tag']
a = np.asarray(final_df)
a = a.flatten()
data_list.append(a)
return data_list
谢谢你的回答,我怎样才能在一个完整的文件上做到这一点。您处理的单元格位于一行中,该行中有另一列指示图像名称。那么,有没有办法将两列[geometry.x geometry.y]展平到行中呢?我想我现在有了一个计划,我按照这个计划将excel工作表中的每一列都读入一个列表中。然后用您提供的代码迭代列表元素。@KaramAboGhalieh您的问题是否已解决或有任何困惑?仍在从代码开始工作,我需要达到以下格式列:LSX LSY RSX RSY NeckX NeckY NoseX NoseY ImageName我现在拥有的是列:Label ImageName,其中Label是问题中列出的格式。
import ast
def extract_data(raw_data):
data_list=[]
items = ['LS', 'RS', 'Neck', 'Nose']
for row in raw_data.index:
#print(row)
x = raw_data ['Label'][row]
#print(type(x))
x = ast.literal_eval(x)
#print(type(x))
if'LS' not in x:
x= insert(x, {'fill_0':[{"geometry":{"x":45,"y":21}}]},0)
if'RS' not in x:
x= insert(x, {'fill_1':[{"geometry":{"x":45,"y":21}}]},1)
if'Neck' not in x:
x= insert(x, {'fill_2=':[{"geometry":{"x":45,"y":21}}]},2)
if'Nose' not in x:
x= insert(x, {'TEST_3':[{"geometry":{"x":45,"y":21}}]},3)
list_main=[]
point_index = 0
for item in x:
if item == items[point_index]:
temp = dict()
temp["tag"] = item
x[item][0].update(temp)
# print(list_main)
else:
x[item][0]['geometry']['x']=0
x[item][0]['geometry']['y']=0
x[item][0]['tag']= items[point_index]
list_main.append(x[item][0])
point_index +=1
final_df = json_normalize(list_main)
del final_df['tag']
a = np.asarray(final_df)
a = a.flatten()
data_list.append(a)
return data_list