运行"demo.py"
,识别的结果会以json的形式保存在“examples/res”下,那么怎样可以将人体关键点的结果标注在图片中呢?
看了"opt.py"
可以看出–save_img和–vis分别是保存图片结果和显示图片结果的参数。
- 首先在
"demo.py"
中设置开启–save_img和–vis参数
args.vis = True
args.save_img = True
- 点击运行,出现了报错
查看一下报错的语句
transparency = max(0, min(1, 0.5*(kp_scores[start_p] + kp_scores[end_p])))
img = cv2.addWeighted(bg, transparency, img, 1-transparency, 0)
是因为addWeighted函数的权重参数应为’double’类型,而transparency 类型为’Tensor’’。可以通过transparency.item()获取Tensor的值。再次运行没有报错,同时examples/res/vis文件夹下保存了结果图片。
但是此时仍存在两个问题:
1)显示图片结果非常的快,基本上一闪而过;
2)保存的结果图不清晰,像是曝光过度了一样,如下图
解决方案
1)显示图片结果非常的快,应该是因为waitKey()时间设置较短,首先定位到显示图片的代码段,也就是"dataloader.py"
文件下,将cv2.waitKey( 30
.)修改为cv2.waitKey( 0
.)
if opt.save_img or opt.save_video or opt.vis:
img = orig_img
if opt.vis:
cv2.imshow("AlphaPose Demo", img)
cv2.waitKey(0)
...
if opt.save_img or opt.save_video or opt.vis:
img = vis_frame(orig_img, result)
if opt.vis:
cv2.imshow("AlphaPose Demo", img)
cv2.waitKey(0)
- 保存的结果图不清晰,主要是在绘制人体关键点和连接关键点时,使用了cv2.addWeighted()函数,每一次画点/椭圆时都进行一次权重叠加,导致最后保存的结果图不清晰,因此我把cv2.addWeighted()这步放到了最后。在
"fn.py"
文件下,190行开始:
for n in range(kp_scores.shape[0]):
if kp_scores[n] <= 0.05:
continue
cor_x, cor_y = int(kp_preds[n, 0]), int(kp_preds[n, 1])
part_line[n] = (int(cor_x/2), int(cor_y/2))
# bg = img.copy()
cv2.circle(bg, (int(cor_x/2), int(cor_y/2)), 2, p_color[n], -1)
# # Now create a mask of logo and create its inverse mask also
# transparency = max(0, min(1, kp_scores[n])).item()
# img = cv2.addWeighted(bg, transparency, img, 1-transparency, 2)
# Draw limbs
for i, (start_p, end_p) in enumerate(l_pair):
if start_p in part_line and end_p in part_line:
start_xy = part_line[start_p]
end_xy = part_line[end_p]
# bg = img.copy()
X = (start_xy[0], end_xy[0])
Y = (start_xy[1], end_xy[1])
mX = np.mean(X)
mY = np.mean(Y)
length = ((Y[0] - Y[1]) ** 2 + (X[0] - X[1]) ** 2) ** 0.5
angle = math.degrees(math.atan2(Y[0] - Y[1], X[0] - X[1]))
stickwidth = (kp_scores[start_p] + kp_scores[end_p]) + 1
polygon = cv2.ellipse2Poly((int(mX),int(mY)), (int(length/2), stickwidth), int(angle), 0, 360, 1)
cv2.fillConvexPoly(bg, polygon, line_color[i])
#cv2.line(bg, start_xy, end_xy, line_color[i], (2 * (kp_scores[start_p] + kp_scores[end_p])) + 1)
# transparency = max(0, min(1, 0.5*(kp_scores[start_p] + kp_scores[end_p]))).item()
# img = cv2.addWeighted(bg, transparency, img, 1-transparency, 0)
img = cv2.addWeighted(bg, 0.7, img, 0.3, 0)
img1 = cv2.resize(img,(width,height),interpolation=cv2.INTER_CUBIC)
return img1
- 再次运行
"demo.py"
,成功啦!!