Skip to content

Instantly share code, notes, and snippets.

@Kartik-cl
Created May 7, 2021 08:32
Show Gist options
  • Save Kartik-cl/b52ebe00212ff540ff8777160af50471 to your computer and use it in GitHub Desktop.
Save Kartik-cl/b52ebe00212ff540ff8777160af50471 to your computer and use it in GitHub Desktop.
import sys
import os
import cv2
import numpy as np
import logging as log
from openvino.inference_engine import IECore
import time
model_xml = 'model.xml'
model_bin = 'model.bin'
ie = IECore()
net = ie.read_network(model=model_xml, weights=model_bin)
input_blob = next(iter(net.input_info)) out_blob = next(iter(net.outputs)) net.batch_size = 1
n, c, h, w = net.input_info[input_blob].input_data.shape
images = np.ndarray(shape=(n, c, h, w))
image = cv2.imread('dog.4001.jpg') image = cv2.resize(image, (w, h)) image = image.transpose((2, 0, 1)) images[0] = image
exec_net = ie.load_network(network=net, device_name='CPU') res = exec_net.infer(inputs={input_blob: images})
res = res[out_blob]
for i, probs in enumerate(res):
probs = np.squeeze(probs)
top_ind = np.argmax(probs)
if(top_ind == 0):
print("Cat detected")
elif(top_ind == 1):
print("Dog detected")
else:
print("Failed to detect either a dog or a cat")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment