如何用opencv dnn加载训练好的网络进行识别
下载训练好的网络模型和对应的配置文件(如caffe或tensorflow格式)。
加载模型和配置文件:
import cv2
net = cv2.dnn.readNetFromCaffe("model.prototxt", "model.caffemodel")
image = cv2.imread("image.jpg")
blob = cv2.dnn.blobFromImage(cv2.resize(image, (300, 300)), 0.007843, (300, 300), 127.5)
net.setInput(blob)
detections = net.forward()
for i in range(detections.shape[2]):
confidence = detections[0, 0, i, 2]
if confidence > 0.5:
box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
(startX, startY, endX, endY) = box.astype("int")
label = "{}: {:.2f}%".format(CLASSES[int(detections[0, 0, i, 1])], confidence * 100)
cv2.rectangle(image, (startX, startY), (endX, endY), (0, 255, 0), 2)
y = startY - 15 if startY - 15 > 15 else startY + 15
cv2.putText(image