【TensorFlow学习笔记】目标识别篇(二)运行demo

结合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()
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值