结合MobileNets和SSDs进行更快更高效的深度学习目标检测
如果我们把MobileNets和SSDs框架结合起来,我们可以实现更快速,更高效的基于深度学习的目标检测。这里使用的模型是original tensorflow impetension的Caffe版本,是由chuanqi305训练的。
MobileNets SSDs最初是在COCO dataset 上训练的,然后在PASCAL VOC进行调试并得到了72.7%的平均准确率。可以检测20种物体(1种是背景类的),包括飞机、单车、鸟、船、瓶子、公交车、汽车、猫、椅子、奶牛、餐桌、狗、马、摩托车、人、盆栽、羊、沙发、火车、和电视机。
import cv2 as cv
import numpy as np
model_path = 'D:/360MoveData/Users/ASUS/Desktop/ssd_mobilenet_v2_coco_2018_03_29/frozen_inference_graph.pb'
config_path = 'D:/360MoveData/Users/ASUS/Desktop/ssd_mobilenet_v2_coco_2018_03_29/ssd_mobilenet_v2_coco_2018_03_29.pbtxt'
CLASSES = ['backgound','person','bicycle','car','motorbike','aeroplane','bus','train','truck','boat','traffic light',
'fire hydrant','_','stop sign','parking meter','bench','bird','cat','dog','horse','sheep']
COLORS = np.random.uniform(0, 200, size=(len(CLASSES), 3))
net = cv.dnn.readNetFromTensorflow(model_path, config_path)
frame = cv.imread('demo.jpg')
rows = frame.shape[0]
cols = frame.shape[1]
net.setInput(cv.dnn.blobFromImage(frame, size=(300, 300), swapRB= True, crop= False))
cvOut = net.forward()
for detection in cvOut[ 0, 0,:,:]:
idx=int(detection[1])
score = float(detection[2])
if score > 0.3:
print(detection)
startX = detection[3] * cols
startY = detection[4] * rows
endX = detection[5] * cols
endY = detection[6] * rows
# display the prediction
label = "{}: {:.2f}%".format(CLASSES[idx], score * 100)
print("[INFO] {}".format(label))
cv.rectangle(frame, (int(startX), int(startY)), (int(endX), int(endY)), COLORS[idx], thickness= 2)
y = int(startY) - 15 if int(startY) - 15 > 15 else int(startY) + 15
cv.putText(frame, label, (int(startX), y), cv.FONT_HERSHEY_PLAIN, 1, COLORS[idx], 2)
cv.imshow( 'opencv-dnn-ssd-detect', frame)
cv.waitKey()