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 ...为我的视频中的每个图像/帧创建一行数据的数据帧
bbox_xyxy是在我的对象检测器被deepsort循环后创建的,并且deepsort识别出每个检测到的对象并将其标识为具有位置的对象。
然后,我将np.array分解并计算这些对象的中心点,以便将它们视为单个点而不是边界框矩形。
Pandas接受我的输入,并创建一个带有对象ID(在本例中为1),中心x y坐标的DataFrame,并将其放置在与每个帧相对应的行中
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)
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]
因此,我可以比较这些对象在帧之间的位置,从而为我提供一个对象计数,该对象可以对对象进行计数并将其存储在两个数据库中,“ UP”和“ DOWN”
答案 0 :(得分:0)
您的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)
希望这行得通。
答案 1 :(得分:0)
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))] =“”行创建了列,并在特殊情况下运行了它。