Darknet YoloV3调用python接口统计检测个数(批量图片和视频)

前篇进阶:https://blog.csdn.net/qq_34717531/article/details/107483479

前篇进阶:https://blog.csdn.net/qq_34717531/article/details/107468262

 

一、批量图片计数:

# 将检测结果绘制到照片上并且保存
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:
        #统计计数detection
        detection = 0
        #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)
            #统计计数detection加一
            detection += 1
        #显示统计总数
        cv2.putText(im,"num: %s" % str(detection),(75,100),cv2.FONT_HERSHEY_SIMPLEX,2,[0,255,255],3)
        #保存画过框,打上标签的图片
        cv2.imwrite(save_dir +str(count) +'.jpg', im)
       
        #图片计数+1
        count += 1

 

二、视频计数

if __name__ == "__main__":
    #加载模型
    net = load_net('cfg/yolov3.cfg'.encode('utf-8'), 'yolov3.weights'.encode('utf-8'), 0)
    meta = load_meta('cfg/coco.data'.encode('utf-8'))
    #读取视频
    vid = cv2.VideoCapture('1.ts')
    #FourCC全称Four-Character Codes,代表四字符代码 (four character code), 它是一个32位的标示符,其实就是typedef unsigned int FOURCC;是一种独立标示视频数据流格式的四字符代码。
    fourcc = cv2.VideoWriter_fourcc('M','P','4','2') #opencv3.0
    #保存检测后的视频cv2.VideoWriter()参数:视频,fourcc指定编码器,保存视频的fps,保存视频的尺寸
    videoWriter = cv2.VideoWriter('11.avi', fourcc, 25, (1920,1080))
    
    while True:
        detection = 0
        return_value,arr=vid.read()
        if not return_value:
            break 
        im=nparray_to_image(arr)
        boxes= detect(net, meta, im)
       
        for i in range(len(boxes)):
            score=boxes[i][1]
            label=boxes[i][0]
            xmin=boxes[i][2][0]-boxes[i][2][2]/2
            ymin=boxes[i][2][1]-boxes[i][2][3]/2
            xmax=boxes[i][2][0]+boxes[i][2][2]/2
            ymax=boxes[i][2][1]+boxes[i][2][3]/2
            #画框
            cv2.rectangle(arr,(int(xmin),int(ymin)),(int(xmax),int(ymax)),(0,255,0),2)
            #打标签
            cv2.putText(arr,str(label),(int(xmin),int(ymin)),fontFace=cv2.FONT_HERSHEY_SIMPLEX,fontScale=0.8,color=(0,255,255),thickness=2)
            detection += 1   
        cv2.putText(arr,"num: %s" % str(detection),(75,100),cv2.FONT_HERSHEY_SIMPLEX,2,[0,255,255],3)
        cv2.imshow("Canvas", arr)
        videoWriter.write(arr) 
        cv2.waitKey(1) 
    cv2.destroyAllWindows()
    

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

学术菜鸟小晨

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

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

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

打赏作者

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

抵扣说明:

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

余额充值