AlphaPose+python识别人体关键点绘制图片

运行"demo.py",识别的结果会以json的形式保存在“examples/res”下,那么怎样可以将人体关键点的结果标注在图片中呢?
看了"opt.py"可以看出–save_img和–vis分别是保存图片结果和显示图片结果的参数。

  1. 首先在"demo.py"中设置开启–save_img和–vis参数
args.vis = True
args.save_img = True
  1. 点击运行,出现了报错
    在这里插入图片描述
    查看一下报错的语句
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)
  1. 保存的结果图不清晰,主要是在绘制人体关键点和连接关键点时,使用了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


  1. 再次运行"demo.py",成功啦!!
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Rena要努力

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值