含源码 手把手教你使用LabVIEW OpenCV dnn实现图像分类( 二 )

2、使用opencv_dnn进行推理

  • 图像预处理(blob)
def get_preprocessed_img(img_path):# read the imageinput_img = cv2.imread(img_path, cv2.IMREAD_COLOR)input_img = input_img.astype(np.float32)# define preprocess parametersmean = np.array([1.0, 1.0, 1.0]) * 127.5scale = 1 / 127.5# prepare input blob to fit the model input:# 1. subtract mean# 2. scale to set pixel values from 0 to 1input_blob = cv2.dnn.blobFromImage(image=input_img,scalefactor=scale,size=(224, 224),# img target sizemean=mean,swapRB=True,# BGR -> RGBcrop=True# center crop)print("Input blob shape: {}\n".format(input_blob.shape))return input_blob
  • 调用pb模型进行推理
def get_tf_dnn_prediction(original_net, preproc_img, imagenet_labels):# inferencepreproc_img = preproc_img.transpose(0, 2, 3, 1)print("TF input blob shape: {}\n".format(preproc_img.shape))out = original_net(preproc_img)print("\nTensorFlow model prediction: \n")print("* shape: ", out.shape)# get the predicted class IDimagenet_class_id = np.argmax(out)print("* class ID: {}, label: {}".format(imagenet_class_id, imagenet_labels[imagenet_class_id]))# get confidenceconfidence = out[0][imagenet_class_id]print("* confidence: {:.4f}".format(confidence))3、实现图像分类 (代码汇总)import osimport cv2import numpy as npimport tensorflow as tffrom tensorflow.keras.applications import MobileNetfrom tensorflow.python.framework.convert_to_constants import convert_variables_to_constants_v2def get_tf_model_proto(tf_model):# define the directory for .pb modelpb_model_path = "models"# define the name of .pb modelpb_model_name = "mobilenet.pb"# create directory for further converted modelos.makedirs(pb_model_path, exist_ok=True)# get model TF graphtf_model_graph = tf.function(lambda x: tf_model(x))# get concrete functiontf_model_graph = tf_model_graph.get_concrete_function(tf.TensorSpec(tf_model.inputs[0].shape, tf_model.inputs[0].dtype))# obtain frozen concrete functionfrozen_tf_func = convert_variables_to_constants_v2(tf_model_graph)# get frozen graphfrozen_tf_func.graph.as_graph_def()# save full tf modeltf.io.write_graph(graph_or_graph_def=frozen_tf_func.graph,logdir=pb_model_path,name=pb_model_name,as_text=False)return os.path.join(pb_model_path, pb_model_name)def get_preprocessed_img(img_path):# read the imageinput_img = cv2.imread(img_path, cv2.IMREAD_COLOR)input_img = input_img.astype(np.float32)# define preprocess parametersmean = np.array([1.0, 1.0, 1.0]) * 127.5scale = 1 / 127.5# prepare input blob to fit the model input:# 1. subtract mean# 2. scale to set pixel values from 0 to 1input_blob = cv2.dnn.blobFromImage(image=input_img,scalefactor=scale,size=(224, 224),# img target sizemean=mean,swapRB=True,# BGR -> RGBcrop=True# center crop)print("Input blob shape: {}\n".format(input_blob.shape))return input_blobdef get_imagenet_labels(labels_path):with open(labels_path) as f:imagenet_labels = [line.strip() for line in f.readlines()]return imagenet_labelsdef get_opencv_dnn_prediction(opencv_net, preproc_img, imagenet_labels):# set OpenCV DNN inputopencv_net.setInput(preproc_img)# OpenCV DNN inferenceout = opencv_net.forward()print("OpenCV DNN prediction: \n")print("* shape: ", out.shape)# get the predicted class IDimagenet_class_id = np.argmax(out)# get confidenceconfidence = out[0][imagenet_class_id]print("* class ID: {}, label: {}".format(imagenet_class_id, imagenet_labels[imagenet_class_id]))print("* confidence: {:.4f}\n".format(confidence))def get_tf_dnn_prediction(original_net, preproc_img, imagenet_labels):# inferencepreproc_img = preproc_img.transpose(0, 2, 3, 1)print("TF input blob shape: {}\n".format(preproc_img.shape))out = original_net(preproc_img)print("\nTensorFlow model prediction: \n")print("* shape: ", out.shape)# get the predicted class IDimagenet_class_id = np.argmax(out)print("* class ID: {}, label: {}".format(imagenet_class_id, imagenet_labels[imagenet_class_id]))# get confidenceconfidence = out[0][imagenet_class_id]print("* confidence: {:.4f}".format(confidence))def main():# configure TF launching#set_tf_env()# initialize TF MobileNet modeloriginal_tf_model = MobileNet(include_top=True,weights="imagenet")# get TF frozen graph pathfull_pb_path = get_tf_model_proto(original_tf_model)print(full_pb_path)# read frozen graph with OpenCV APIopencv_net = cv2.dnn.readNetFromTensorflow(full_pb_path)print("OpenCV model was successfully read. Model layers: \n", opencv_net.getLayerNames())# get preprocessed imageinput_img = get_preprocessed_img("yaopin.png")# get ImageNet labelsimagenet_labels = get_imagenet_labels("classification_classes.txt")# obtain OpenCV DNN predictionsget_opencv_dnn_prediction(opencv_net, input_img, imagenet_labels)# obtain TF model predictionsget_tf_dnn_prediction(original_tf_model, input_img, imagenet_labels)if __name__ == "__main__":main()

经验总结扩展阅读