python - 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 将 torch.tensor(x) 转换为 torch.tensor([x]) 和从而使其可迭代。
萨塔克耆那教
推荐阅读
- javascript - 返回值的数量 - REST API 角度
- c# - 每个请求创建全新的 HttpClient 是否便宜
- xml - 需要 JavaScript 代码帮助来解析 xml 响应
- rust - swagger 编辑器演示是否已过时?
- android - 应用程序在一段时间后没有响应
- c# - 在 asp .net core 3 中使用进程内托管设置请求超时
- azure-data-factory - 如何解码 html 编码的文本?
- java - 如何刷新单例类对象
- asp.net-core - Identity Server 4 - 保留返回 URL/重定向 Ui
- magento2 - Magento 2 为每个可定制选项添加基本价格