Windows10下Object Detection API实战记录(5)——OpenCV调用模型检测实时视频(摄像头)和图片

本文详细介绍了在Windows10环境下,如何使用OpenCV4.0.0调用TensorFlow的FasterRCNN和MaskRCNN模型进行目标检测。包括模型转换、图片检测及实时视频检测的全过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Windows10下Object Detection API实战记录(5)——OpenCV实现模型调用检测视频和图片

1、OpenCV调用模型检测图片

建议使用opencv版本4.0.0,因为从这个版本开始,opencv开始支持tensorflow的 Faster RCNN 和 Mask RCNN 模型。Opencv调用tensorflow需要pb和pbtxt模型,所以我们要进行转化
下面介绍调用tensorflow模型步骤(一共需要4个文件):
1)下载opencv源码, samples/dnn 下的4个python脚本用来转换tensorflow模型:tf_text_graph_common.py、tf_text_graph_ssd.py、tf_text_graph_faster_rcnn.py、tf_text_graph_mask_rcnn.py。本次实战用的是SSD模型,所以只需要tf_text_graph_common.py、tf_text_graph_ssd.py两个文件就够了。
2)需要用到frozen_inference_graph.pb模型和ssd_inception_v2_coco.config配置文件,执行代码,可以得到pbtxt模型。

python tf_text_graph_ssd.py --input frozen_inference_graph.pb --output my.pbtxt --config ssd_inception_v2_coco.config

(3)运行OpenCV_model.py加载模型检测,OpenCV_model.py的具体代码如下:

import cv2 as cv

cvNet = cv.dnn.readNetFromTensorflow('frozen_inference_graph.pb', 'my.pbtxt')

img = cv.imread('2.jpg')
rows = img.shape[0]
cols = img.shape[1]
cvNet.setInput(cv.dnn.blobFromImage(img, size=(300, 300), swapRB=True, crop=False))
cvOut = cvNet.forward()
classNames = ["background", "sun", "huang"]
for detection in cvOut[0, 0, :, :]:
    score = float(detection[2])
    if score > 0.3:
        objectClass = int(detection[1])
        left = detection[3] * cols
        top = detection[4] * rows
        right = detection[5] * cols
        bottom = detection[6] * rows
        cv.rectangle(img, (int(left), int(top)), (int(right), int(bottom)), (225, 225, 0), thickness=2)

        label = classNames[objectClass]
        print(label)
        cv.putText(img, str(label) + ":" + str(score), (int(left), int(top)), cv.FONT_HERSHEY_SIMPLEX, 1,
                   (23, 230, 210), 2)

cv.imshow('img', img)
cv.waitKey()

输出结果:
在这里插入图片描述

2、OpenCV调用模型实时检测(视频)

利用OpenCV调用摄像头,加载tensorflow模型进行目标检测。
直接上代码:

import cv2 as cv
from PIL import Image
import numpy as np
# 加载模型
cvNet = cv.dnn.readNetFromTensorflow('frozen_inference_graph.pb', 'my.pbtxt')
# 类别列表,0为背景,1为sun,2为huang,与模型保持一致
classNames = ["background", "sun", "huang"]
# 调用摄像头
capture = cv.VideoCapture(0)

while(True):
    # 读取某一帧
    ref,frame = capture.read()
    rows = frame.shape[0]
    cols = frame.shape[1]
    # 格式转变,BGRtoRGB
    frame = cv.cvtColor(frame, cv.COLOR_BGR2RGB)
    # 转变成Image
    frame = Image.fromarray(np.uint8(frame))
    frame = np.array(frame)

    # 进行检测
    cvNet.setInput(cv.dnn.blobFromImage(frame, size=(300, 300), swapRB=True, crop=False))
    cvOut = cvNet.forward()
    for detection in cvOut[0, 0, :, :]:
        score = float(detection[2])
        if score > 0.3:
            objectClass = int(detection[1])
            left = detection[3] * cols
            top = detection[4] * rows
            right = detection[5] * cols
            bottom = detection[6] * rows
            cv.rectangle(frame, (int(left), int(top)), (int(right), int(bottom)), (90, 150, 210), thickness=2)

            label = classNames[objectClass]
            print(label)
            cv.putText(frame, str(label) + ":" + str(score), (int(left), int(top)), cv.FONT_HERSHEY_SIMPLEX, 1,
                       (23, 230, 210), 2)

    # RGBtoBGR满足opencv显示格式
    frame = cv.cvtColor(frame,cv.COLOR_RGB2BGR)
    cv.imshow("video",frame)
    c = cv.waitKey(30) & 0xff
    # 退出代码
    if c == 27:
        capture.release()
        break

在这里插入图片描述

大功告成!!!

利用tensorflow Object Detection API 快捷方便的训练出自己的目标检测模型,可以用tensorflow平台检测,也可以用Opencv调用模型进行检测。
到这里,Windows10下Object Detection API实战过程记录就结束了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值