pd.DataFrame正在覆盖列,而不是将数据保存到pd.DataFrame



在这里,我试图创建一个数据帧来比较帧之间对象的位置:

Pcount = []
Pcountdb = []
framenumber = 0
frames_count = 0
frames_count = self.vdo.get(cv2.CAP_PROP_FRAME_COUNT)
df = pd.DataFrame(index=range(int(frames_count))) 
if len(outputs) > 0:
for i in range(len(outputs):
bbox_xyxy = outputs[:,:4]
identities = outputs[:,-1]
sx = outputs[:,0]
sy = outputs[:,1]
ex = outputs[:,2]
ey = outputs[:,3]
cx = ((sx + ex) /2)
cy = ((sy + ey) /2)
ct = (cx, cy)
cx2 = (cx.tolist())
cy2 = (cy.tolist())
P = identities[i]
df[str(P.astype(int))] = ""                              
#creates new column with an id number obtained through deepsort
df.at[int(framenumber), str(P.astype(int))] = [cx2[i], cy2[i]]  
#the i function from a for loop is necessary for multiple objects in the same frame

print(df)
if not P in Pcountdb:
global PcountT
Pcountdb.append(P)
PcountT = PcountT + 1
framenumber = framenumber + 1

编辑:上面的脚本以占位符开头

df=pd.DataFrame…为我的视频中的每个图像/帧创建一行数据帧

bboxxyxy是在我的对象检测器被深度排序循环后创建的,深度排序已经识别出每个检测到的对象,并将其识别为一个有位置的对象。

然后,我将np.array分解,计算这些对象的中心点,这样它们就可以被视为一个单独的点,而不是一个边界框矩形。

Pandas接受我的输入,创建一个带有对象id(在本例中为1(、中心x y坐标的DataFrame,并将它们放在与每个帧对应的行中

接下来,我们打印数据帧并查看结果

print(df(返回:

1
Frames                
3       [614.5, 632.0]
1
Frames                
3                     
4       [610.5, 624.0]
1
Frames                
3                     
4                     
5       [603.0, 618.0]
1
Frames                
3                     
4                     
5                     
6       [574.0, 615.5]
1
Frames                
3                     
4                     
5                     
6                     
7       [564.0, 610.0]
1
Frames                
3                     
4                     
5                     
6                     
7                     
8       [559.0, 597.0]

DataFrame只跟踪每列的最新坐标集。如果我要生成两列,那么只有最后一次看到每个对象才会出现在我的数据帧中(如上所示,其中一个对象被标识为1(

我需要将输出保存到我的pd.DataFrame=df中,而不是被覆盖

1
Frames                
3       [614.5, 632.0]
4       [610.5, 624.0]
5       [603.0, 618.0]
6       [574.0, 615.5]
7       [564.0, 610.0]
8       [559.0, 597.0]

所以我可以比较这些对象在帧之间的位置,给我一个对象计数,它对对象进行计数并将它们存储在2个数据库中,";UP和";向下";

您的DataFrame只是添加最后一个raw,因为每次for循环运行时,您都会将列重置为null。因此所有先前的值都被擦除。通过查看您的代码,我可以看到这一点,因为您的代码不需要处于for循环中。

解决方案

Pcount = []
Pcountdb = []
framenumber = 0
frames_count = 0
frames_count = self.vdo.get(cv2.CAP_PROP_FRAME_COUNT)
df = pd.DataFrame(index=range(int(frames_count))) 
if len(outputs) > 0:
bbox_xyxy = outputs[:,:4]
identities = outputs[:,-1]
sx = outputs[:,0]
sy = outputs[:,1]
ex = outputs[:,2]
ey = outputs[:,3]
cx = ((sx + ex) /2)
cy = ((sy + ey) /2)
ct = (cx, cy)
cx2 = (cx.tolist())
cy2 = (cy.tolist())
P = identities[i]
df[str(P.astype(int))] = ""
for i in range(len(outputs):
df.at[int(framenumber), str(P.astype(int))] = [cx2[i], cy2[i]]
print(df)

希望这能奏效。

for i in range(len(outputs)):
P = identities[i]
if not P in Pcountdb:
df[str(P.astype(int))] = ""
global PcountT
Pcountdb.append(P)
PcountT = PcountT + 1
else:
if P in Pcountdb:
df.at[int(framenumber), str(P.astype(int))] = [cx2[i], cy2[i]]

[222 rows x 1 columns]
1
Frames                
4       [610.5, 624.0]
5       [603.0, 618.0]
6       [574.0, 615.5]
7       [564.0, 610.0]
8       [559.0, 597.0]
...                ...
226     [640.5, 518.5]
227     [643.0, 525.0]
228     [646.0, 529.5]
229     [647.5, 529.5]
230     [650.5, 531.5]

感谢@Adarsh的回复,你是对的,我的列被覆盖了,因为我是从循环中创建的。

我取了df[str(p.astype(int((]="这行,其中创建了列,并在独占环境中运行它。

最新更新