TypeError:0-d张量上的迭代(python社交距离检测append())



我有一个检测人与人之间距离的项目。当质心和绘制线位于人的中心时,该项目运行顺利。但是,我想把质心和绘制线移动到人们的脚下,我已经成功地移动了质心,但绘制线没有沿着质心移动。这是代码:

utils.py(定义距离(

def distancing(people_coords, img, dist_thres_lim=(200,250)):
# Plot lines connecting people
already_red = dict() # dictionary to store if a plotted rectangle has already been labelled as high risk
centers = []
for i in people_coords:
centers.append(((int(i[0])+int(i[2]))//2, (int(max(i[3]), (i[1])))))

for j in centers:
already_red[j] = 0
x_combs = list(itertools.combinations(people_coords,2))

radius = 10
thickness = 5
for x in x_combs:
xyxy1, xyxy2 = x[0],x[1]
cntr1 = ((int(xyxy1[2])+int(xyxy1[0]))//2,(int(xyxy1[3])+int(xyxy1[1]))//2)
cntr2 = ((int(xyxy2[2])+int(xyxy2[0]))//2,(int(xyxy2[3])+int(xyxy2[1]))//2)
dist = ((cntr2[0]-cntr1[0])**2 + (cntr2[1]-cntr1[1])**2)**0.5

问题出现在people_coords(循环(xy坐标中。我试图用(int(max(I[3](,(I[1]((((来更改代码,但当我运行它时,我得到了一个错误(TypeError:在0-d张量上的迭代(。我应该怎么做才能沿着质心移动绘制线?

这是质心代码

def plot_dots_on_people(x, img):
# Plotting centers of people with green dot.
thickness = -1;
color = [0, 255, 0] # green
center = ((int(x[0])+int(x[2]))//2, (int(max(x[3], x[1]))))
radius = 10
cv2.circle(img, center, radius, color, thickness)

我希望有人能帮助我,谢谢。

我建议打印people_coords的值并运行people-coords.size((。错误来自对大小为torch.Size([])的张量的迭代。具有该大小的示例张量类似于torch.tensor(5)

解决这个错误的一种方法是通过使用上面所示的调试步骤来确保people_coords是您期望的值,或者您可以使用unsqueeze将torc.tensor(x(转换为torc.tentor([x](,从而使其可迭代。

Sarthak Jain

最新更新