Python implements AI inference program: less than twenty lines of code

  It is very simple to implement AI inference programs in Python. Except for module references and constant definitions, there are less than 20 lines of programs for realizing AI inference calculation! There are only five real functions for AI inference calculation!

  • The first step is to use cv.dnn.readNet () to read the IR model file in OpenVINO format
  • The second step, use net.setPreferableTarget (DEVICE) to specify the AI ​​inference calculation execution hardware
  • The third step is to use cv.dnn.blobFromImage () and net.setInput () to transfer the image data to the AI ​​model
  • The fourth step is to use net.forward () to implement AI inference calculation
  • The fifth step is to analyze the reasoning calculation results
The complete code for implementing AI inference calculation based on OpenCV and OpenVINO in Python is as follows
#导入opencv-openvino模块
import cv2 as cv
import time
#配置推断计算设备,IR文件路径,图片路径
DEVICE = cv.dnn.DNN_TARGET_CPU
model_xml = 'D:/tf_train/workspaces/cats_dogs/IR_model/cats_dogs_detector.xml'
model_bin = 'D:/tf_train/workspaces/cats_dogs/IR_model/cats_dogs_detector.bin'
image_file = 'D:/tf_train/workspaces/cats_dogs/images/test/3.jpg'

#读取IR模型文件
net = cv.dnn.readNet(model_xml, model_bin)

#指定AI推理计算执行硬件
net.setPreferableTarget(DEVICE)

#读取图片
img = cv.imread(image_file)

#将图片传入模型的输入张量
blob = cv.dnn.blobFromImage(img,ddepth=cv.CV_8U)
net.setInput(blob)

#执行推断计算
start = time.time()
out = net.forward()
end = time.time()
print("Infer Time:{}ms".format((end-start)*1000))

#处理推断计算结果
for detection in out.reshape(-1, 7):
    confidence = float(detection[2])
    xmin = int(detection[3] * img.shape[1])
    ymin = int(detection[4] * img.shape[0])
    xmax = int(detection[5] * img.shape[1])
    ymax = int(detection[6] * img.shape[0])

    if confidence>0.7:
        cv.rectangle(img, (xmin, ymin), (xmax, ymax), (0, 255, 0))
        conf = "{:.4f}".format(confidence)
        font = cv.FONT_HERSHEY_COMPLEX_SMALL
        cv.putText(img, conf, (xmin, ymin - 5), font, 1, (0, 0, 255))

#显示处理结果
cv.imshow("Detection results",img)
cv.waitKey(0)
cv.destroyAllWindows()
print("Inference is completed...")

How to train your own AI model and how to obtain OpenVINO IR model files?
Advanced learning: "Deep learning image recognition technology"

Published 8 original articles · Likes0 · Visits 138

Guess you like

Origin blog.csdn.net/qq_41802192/article/details/105305414