【问题标题】:TypeError: iteration over a 0-d tensor (python social distancing detection append())TypeError:迭代 0-d 张量(python 社交距离检测 append())
【发布时间】:2021-06-30 15:01:52
【问题描述】:

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

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)

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

【问题讨论】:

    标签: python tensorflow pytorch centroid


    【解决方案1】:

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

    解决此错误的一种方法是使用我上面说明的调试步骤确保 people_coords 是您期望的值,或者您可以使用 unsqueeze 将 torch.tensor(x) 转换为 torch.tensor([x ]) 并因此使其可迭代。

    萨塔克·耆那教

    【讨论】:

      猜你喜欢
      • 2018-07-16
      • 2014-02-07
      • 2016-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-12
      • 1970-01-01
      相关资源
      最近更新 更多