【OpenCV】132 OpenCV DNN单张与多张图像的推断

132 OpenCV DNN单张与多张图像的推断

代码

import cv2 as cv
import numpy as np

bin_model = "../models/googlenet/bvlc_googlenet.caffemodel"
protxt = "../models/googlenet/bvlc_googlenet.prototxt"

# Load names of classes
classes = None
with open("classification_classes_ILSVRC2012.txt", rt) as f:
    classes = f.read().rstrip(
).split(
)

# load CNN model
net = cv.dnn.readNetFromCaffe(protxt, bin_model)

# read input data
image1 = cv.imread("../images/cat.jpg")
image2 = cv.imread("../images/aeroplane.jpg")
images = []
images.append(image1)
images.append(image2)
blobs = cv.dnn.blobFromImages(np.asarray(images), 1.0, (224, 224), (104, 117,123), False, crop=False)
print(blobs.shape)

# Run a model
net.setInput(blobs)
out = net.forward()
# Put efficiency information.
t, _ = net.getPerfProfile()
label = Inference time: %.2f ms % (t * 1000.0 / cv.getTickFrequency())
print(out.shape)

# Get a class with a highest score.
for i in range(len(out)):
    classId = np.argmax(out[i])
    confidence = out[i][classId]
    cv.putText(images[i], label, (0, 15), cv.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0))

    # Print predicted class.
    text_label = %s: %.4f % (classes[classId] if classes else Class #%d % classId, confidence)
    cv.putText(images[i], text_label, (50, 50), cv.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2)
    cv.imshow("googlenet-demo", images[i])
    cv.waitKey(0)
cv.destroyAllWindows()

实验结果

解释

OpenCV DNN中支持单张图像推断,同时还支持分批次方式的图像推断,对应的两个相关API分别为blobFromImage与blobFromImages,它们的返回对象都是一个四维的Mat对象-按照顺序分别为NCHW 其组织方式详解如下: N表示多张图像 C表示接受输入图像的通道数目 H表示接受输入图像的高度 W表示接受输入图像的宽度

retval	= cv.dnn.blobFromImage(image[, scalefactor[, size[, mean[, swapRB[, crop[, ddepth]]]]]])
retval = cv.dnn.blobFromImages(images[, scalefactor[, size[, mean[, swapRB[, crop[, ddepth]]]]]])

参数解释

    Images表示多张图像,image表示单张图像 Scalefactor = 1.0表示放缩 Size = Size()表示图像大小 Mean = Scalar()表示均值 swapRB = false是否交换通道 crop = false是否剪切 ddepth = CV_32F输出的类型,默认是浮点数格式

经验分享 程序员 微信小程序 职场和发展