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实战过程记录就结束了。