Darknet YoloV3调用python接口进行批量图片检测并保存

前置:https://blog.csdn.net/qq_34717531/article/details/107466494

基础学习:https://blog.csdn.net/qq_34717531/article/details/107402545

将主函数替换即可。

# 将检测结果绘制到照片上并且保存
if __name__ == "__main__":
    #os.environ["CUDA_VISIBLE_DEVICES"] = "0"
    #加载模型
    net = load_net("./cfg/yolov3.cfg".encode('utf-8'), "./yolov3.weights".encode('utf-8'), 0)
    meta = load_meta("./cfg/coco.data".encode('utf-8'))
    # 测试数据集的路径
    test_dir = '/home/ycc/darknet-master/edata/VOC2008/JPEGImages/'
    # 检测结果保存路径
    save_dir = '/home/ycc/projects/results/'
    #如果保存检测图像文件夹不存在,生成它
    if not os.path.exists(save_dir):
        os.mkdir(save_dir)

    #os.listdir(path):列出path下的文件列表      
    pics = os.listdir(test_dir)
    #保存图片计数count
    count = 0
    #对test_dir列表下的所有图片pics进行循环检测
    for im in pics:
        #os.path.join():python路径拼接函数,img得到完整图片路径
        img = os.path.join(test_dir, im)
        #检测计时开始
        s = time.time()
        #detect检测
        r = detect(net, meta, img.encode('utf-8'))
        #检测计时结束
        print("一张图检测耗时:%.3f秒" % (time.time() - s))
        # 输出的检测结果中坐标信息为目标的中心点坐标和box的w和w
        im = cv2.imread(img)
        for res in r:
            #detect检测出来的是检测的中心点坐标x,y和宽高w,h,要画框所以做了转换
            x1 = int(res[2][0] - (res[2][2] / 2))
            x2 = int(res[2][0] + (res[2][2] / 2))
            y1 = int(res[2][1] - (res[2][3] / 2))
            y2 = int(res[2][1] + (res[2][3] / 2))
            #画矩形框cv2.rectangle()参数: 图像,左上角坐标,右下角坐标,颜色, 线条粗细。
            cv2.rectangle(im, (x1, y1), (x2, y2), (0, 255, 0), 2)
            #打标签(添加字体)cv2.putText()参数:图像,显示字符,左上角坐标,定义字体,字体大小,颜色,字体粗细
            #res[0]就是标签信息,res[2][0]代表的是第二维度第一个元素,就是x
            cv2.putText(im, str(res[0]).split("'")[1], (x1-10, y1-10), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
        #保存画过框,打上标签的图片
        cv2.imwrite(save_dir +str(count) +'.jpg', im)
        #图片计数+1
        count += 1

 

 

参考:https://blog.csdn.net/weixin_38106878/article/details/103295624

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

学术菜鸟小晨

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

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

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

打赏作者

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

抵扣说明:

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

余额充值