[英]pd.DataFrame is overwriting columns, instead of saving data to 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... 创建我的 dataframe ,视频中的每个图像/帧都有一行
bbox_xyxy 是在我的 object 检测器被 deepsort 循环后创建的,并且 deepsort 已识别每个检测到的 object 并将其识别为具有位置的 object。
然后,我分解 np.arrays 并计算这些对象的中心点,以便可以将它们视为单个点,而不是边界框矩形。
Pandas 接受我的输入并创建一个 DataFrame 与 object id(在本例中为 1),中心 xy 坐标并将它们放置在与每个帧对应的行中
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 仅跟踪每列的最新坐标集。 如果我要生成两列,那么我的 dataframe 中只会出现每个 object 的最后一次出现(如上图所示,其中一个 object,标识为 1)
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]
所以我可以比较这些对象在帧之间的位置,给我一个 object 计数,对对象进行计数并将它们存储在 2 个数据库中,“UP”和“DOWN”
您的 DataFrame 只是添加最后一个原始数据,因为每次 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))] = "" 行,其中创建了列并在排他的情况下运行它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.