{ "metadata": { "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.5" }, "orig_nbformat": 4, "kernelspec": { "name": "python3", "display_name": "Python 3.8.5 64-bit" }, "interpreter": { "hash": "916dbcbb3f70747c44a77c7bcd40155683ae19c65e1c03b4aa3499c5328201f1" } }, "nbformat": 4, "nbformat_minor": 2, "cells": [ { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "['CPU', 'GNA', 'GPU']\nMKLDNNPlugin: 2.1.2021.3.0-2787-60059f2c755-releases/2021/3\n" ] } ], "source": [ "from openvino.inference_engine import IECore\n", "import cv2 as cv\n", "import numpy as np\n", "import time\n", "\n", "import sys\n", "from util import ResizeImage,CropImage,ToTensor,NormalizeImage\n", "from PIL import Image\n", "\n", "image_file = \"test/test1.png\"\n", "top_k = 5\n", "\n", "DEVICE = 'CPU'\n", "#初始化插件,输出插件版本号\n", "ie = IECore()\n", "devices = ie.available_devices\n", "print(devices)\n", "ver = ie.get_versions(DEVICE)[DEVICE]\n", "print(\"{descr}: {maj}.{min}.{num}\".format(descr=ver.description, maj=ver.major, min=ver.minor, num=ver.build_number))\n", "\n" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [], "source": [ "# 图像预处理,转为Tensor张量\n", "def preprocess(img):\n", " resize_op = ResizeImage(resize_short=224)\n", " img = resize_op(img)\n", " img_mean = [0.877, 0.877, 0.877]\n", " img_std = [0.200, 0.200, 0.200]\n", " img_scale = 1.0 / 255.0\n", " normalize_op = NormalizeImage(\n", " scale=img_scale, mean=img_mean, std=img_std)\n", " img = normalize_op(img)\n", " tensor_op = ToTensor()\n", " img = tensor_op(img)\n", " return img\n", "\n", "# 减少图像白边\n", "def cutImg(img):\n", " dst = 255- img\n", " gray = cv.cvtColor(dst,cv.COLOR_BGR2GRAY) \n", " ret, binary = cv.threshold(gray,127,255,cv.THRESH_BINARY) \n", " \n", " contours, hierarchy = cv.findContours(binary,cv.RETR_TREE,cv.CHAIN_APPROX_SIMPLE)\n", "\n", " minx = 65523\n", " miny = 65523\n", " maxx = 0\n", " maxy = 0\n", "\n", " for contour in contours:\n", " boundRect = cv.boundingRect(contour)\n", " (x,y,w,h)=boundRect\n", " if xmaxx:\n", " maxx=x+w\n", " if y+h>maxy:\n", " maxy=y+h\n", " cutw=maxx-minx\n", " cuth=maxy-miny\n", " # w>h\n", " # if cutw>cuth:\n", " # cuty1=int((maxy+miny)/2-cutw/2)\n", " # cuty2=int((maxy+miny)/2+cutw/2)\n", " # img = img[cuty1:cuty2,minx:maxx]\n", " # if cutw:7: DeprecationWarning: 'inputs' property of IENetwork class is deprecated. To access DataPtrs user need to use 'input_data' property of InputInfoPtr objects which can be accessed by 'input_info' property.\n", " input_blob = next(iter(net.inputs))\n" ] } ], "source": [ "#读取IR模型文件\n", "model_xml = \"onnx/handwriting_from_pytorch.onnx\"\n", "net = ie.read_network(model=model_xml, weights=None)\n", "\n", "#准备输入输出张量MYRIAD\n", "print(\"Preparing input blobs\")\n", "input_blob = next(iter(net.inputs))\n", "out_blob = next(iter(net.outputs))\n", "net.batch_size = 1\n" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Loading IR to the plugin...\n", "Batch size is 1\n", ":10: DeprecationWarning: 'inputs' property of IENetwork class is deprecated. To access DataPtrs user need to use 'input_data' property of InputInfoPtr objects which can be accessed by 'input_info' property.\n", " n, c, h, w = net.inputs[input_blob].shape\n" ] } ], "source": [ "#载入模型到AI推断计算设备\n", "print(\"Loading IR to the plugin...\")\n", "exec_net = ie.load_network(network=net, num_requests=1, device_name=DEVICE)\n", "frame = cv.imread(image_file)\n", "frame = cutImg(frame)\n", "frame = cv.cvtColor(frame,cv.COLOR_BGR2RGB)\n", "# frame = Image.fromarray(cv.cvtColor(img,cv.COLOR_BGR2RGB))\n", "# frame = cv.resize(frame, (64,64))\n", "# deal\n", "n, c, h, w = net.inputs[input_blob].shape\n", "initial_h, initial_w, channels = frame.shape\n", "#按照AI模型要求放缩图片\n", "image = cv.resize(frame, (w, h)) \n", "image = preprocess(image)\n", "\n", "print(\"Batch size is {}\".format(n))" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Starting inference in synchronous mode\nInfer Time:6.3915252685546875ms\n{'output': array([[-88.821846, -83.00881 , -87.61913 , ..., -90.74928 , -90.11325 ,\n -87.76955 ]], dtype=float32)}\nProcessing output blob\n[[-88.821846 -83.00881 -87.61913 ... -90.74928 -90.11325 -87.76955 ]]\n余\n朱\n杀\n朵\n会\n" ] } ], "source": [ "\n", "#执行推断计算\n", "print(\"Starting inference in synchronous mode\")\n", "start = time.time()\n", "res = exec_net.infer(inputs={input_blob: image})\n", "end = time.time()\n", "print(\"Infer Time:{}ms\".format((end-start)*1000))\n", "print(res)\n", "# 处理输出\n", "print(\"Processing output blob\")\n", "res = res[out_blob]\n", "print(res)\n", "index = np.argsort(-res[0])[0:top_k]\n", "for c in index:\n", " print(labels[c])\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ] }