1.OpenCV支持的深度学习框架
对于我们新手来说自己去实现一些深度学习目标检测算法难度很大,不过OpenCV的DNN(深度学习模块)里封装了各种 深度学习框架供我们使用。
比如我们常见的下面四种框架:
-TensorFlow -Caffe -Darknet -Pytorch
2.用到的API
2.1cv2.dnn.readNet(model,config)
我们可以通过cv2.dnn.readNet(model,config)函数来读取 网络,导入相应的模型。其中model为我们训练的模型文件比如yolo就是.weights文件,而conifg为神经网络的配置文件。
2.2cv2.dnn.blobFromImage(img, scalefactor, size, mean, swapRB, crop)
其中img为输入图像,scalefactor为缩放因子,size为输入图片大小,mean为图像RGB三个通道减去的均值 用来减少图片受光照影响,swapRB是否交换RB通道,crop代表是否裁剪。
2.3net.setInput(blob) net.forward()
net.setInput(blob)是把图像张量blob(由2.2得到)送入网络输入,net.forward()对blob进行网络的相应执行操作,比如卷积 、池化,最后得到输出结果。
3.Yolov3示例
具体过程不过多阐述,有机会后面文章出。
import cv2
import numpy as np
from matplotlib import pyplot as plt
# Load Yolo
net = cv2.dnn.readNet("yolov3.weights", "yolov3.cfg")
classes = []
with open("coco.names", "r") as f:
classes = [line.strip() for line in f.readlines()]
layer_names = net.getLayerNames()
print(len(layer_names))
output_layers = [layer_names[i-1] for i in net.getUnconnectedOutLayers()]
print(output_layers)
colors = np.random.uniform(0, 255, size=(len(classes), 3)) / 255
# Loading image
img = cv2.imread("traffic.jpg")
#img = cv2.resize(img, None, fx=0.4, fy=0.4)
height, width, channels = img.shape
# Detecting objects
blob = cv2.dnn.blobFromImage(img, 1.0 / 255.0, (320 , 320), (0, 0, 0), True, crop=False)
net.setInput(blob)
outs = net.forward(output_layers)
# Showing informations on the screen
class_ids = []
confidences = []
boxes = []
fig = plt.gcf()
fig.set_size_inches(20, 10)
plt_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.imshow(plt_img)
for out in outs:
for detection in out:
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]
if confidence > 0.5:
# Object detected
center_x = int(detection[0] * width)
center_y = int(detection[1] * height)
w = int(detection[2] * width)
h = int(detection[3] * height)
# Rectangle coordinates
x = int(center_x - w / 2)
y = int(center_y - h / 2)
boxes.append([x, y, w, h])
confidences.append(float(confidence))
class_ids.append(class_id)
indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)
for i in range(len(boxes)):
if i in indexes:
x, y, w, h = boxes[i]
label = str(classes[class_ids[i]])
color = colors[i]
plt.gca().add_patch(
plt.Rectangle((x, y), w,
h, fill=False,
edgecolor=color, linewidth=2)
)
plt.text(x, y - 10, label, color = color, fontsize=20)
plt.show()
现象如下: