FR-CNNs慢但是准确性高,YOLO很快但是精度差点。SSDs平衡了这两种算法。如果再结合Google提出的MobileNets优化网络结构,可以得到很理想的效果。
下面贴出源码:
import numpy as npimport cv2 imagefile = 'images/test02.jpg'deploy_prototxt = 'MobileNetSSD_deploy.prototxt'model = 'MobileNetSSD_deploy.caffemodel'confidence_default = 0.2CLASSES = ["background", "aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"] COLORS = np.random.uniform(0, 255, size=(len(CLASSES), 3)) print("[info] loading model...") net = cv2.dnn.readNetFromCaffe(deploy_prototxt, model) image = cv2.imread(imagefile) (h, w) = image.shape[:2] blob = cv2.dnn.blobFromImage(cv2.resize(image, (300, 300)), 0.007843, (300, 300), 127.5) print("[info] computing object detections...") net.setInput(blob) detections = net.forward()for i in np.arange(0, detections.shape[2]): confidence = detections[0, 0, i, 2] if confidence > confidence_default: idx = int(detections[0, 0, i, 1]) box = detections[0, 0, i, 3:7] * np.array([w, h, w, h]) (startX, startY, endX, endY) = box.astype("int") label = "{}: {:.2f}%".format(CLASSES[idx], confidence * 100) print("[info] {}".format(label)) cv2.rectangle(image, (startX, startY), (endX, endY), COLORS[idx], 2) y = startY - 15 if startY - 15 > 15 else startY + 15 cv2.putText(image, label, (startX, y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, COLORS[idx], 2) cv2.imshow("output", image) cv2.waitKey(0)
OpenCV需要3.3以上支持,因为代码中用到了cv2.dnn模块。
检测效果:
output
作者:飞天小小猫
链接:https://www.jianshu.com/p/e9a6c1eac2c9
x
點(diǎn)擊查看更多內(nèi)容
為 TA 點(diǎn)贊
評(píng)論
評(píng)論
共同學(xué)習(xí),寫下你的評(píng)論
評(píng)論加載中...
作者其他優(yōu)質(zhì)文章
正在加載中
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦