Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Beginner
177 Views

Writing Python script for the NCS-2

Hello everyone

I'm a new user of the NCS-2 and need a help on the creation of a testing script to use it with NCS-2 on Raspberry Pi. To start by creating a CNN to classify a Digit from 0 to 9, using the example described in the link below.

https://www.youtube.com/watch?v=y1ZrOs9s2QA

I have saved a Keras model, next, I turn Keras to TensorFlow model. By using the model optimizer I have converted the Tensorflow model to an IR file (.bin and .xml files) that we can use on the Neural Compute Stick 2.

I have run the following Python script on CPU and he is working as expected,  

 

###################################################

import numpy as np

import cv2

from keras.models import load_model

########### PARAMETERS ##############

 

threshold = 0.65 # MINIMUM PROBABILITY TO CLASSIFY

 

#####################################

 

#%%

#### LOAD THE TRAINNED MODEL 

model = load_model('./keras_model/model_keras.h5')

 

#%%

#### PREPORCESSING FUNCTION

def preProcessing(img):

  img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

  img = cv2.equalizeHist(img)

  img = img/255

  return img

 

#%%

 

imgOriginal = cv2.imread('cap5.png')

img = np.asarray(imgOriginal)

img = cv2.resize(img,(32,32))

img = preProcessing(img)

cv2.imshow("Processed Image", img)

img = img.reshape(1,32,32,1)

 

 

#### PREDICT

classIndex = int(model.predict_classes(img))

print(classIndex)

 

predictions = model.predict(img)

print(predictions)

probVal= np.amax(predictions)

print(classIndex,probVal)

 

if probVal> threshold:

  cv2.putText(imgOriginal,str(classIndex) + "  "+str(probVal),

        (50,50),cv2.FONT_HERSHEY_COMPLEX,

        1,(0,0,255),1)

 

  cv2.imwrite('cap5_pre.png',imgOriginal)

 

###################################################

 

Now I need help with writing the testing script for the NCS-2 using the Openvino inference engine functions (IENetwork, IEPlugin).

Can anyone help me to do it?

 

Thanks

 

0 Kudos
3 Replies
Highlighted
Employee
53 Views

Hi Bbill14,

 

Thanks for reaching out.

You can check our python samples and demos here, using our pre-trained models, and start from there.

Additionally, check the Inference Engine Python API Reference for more information.

 

Let us know if you have more questions.

 

Best regards,

 

David C.

Intel Customer Support Technician

A Contingent Worker at Intel

0 Kudos
Highlighted
New Contributor I
53 Views

As the Above comment may solve your issues, but just for complete understanding for the workflow. Here is my Template Code:

import time

import cv2

from openvino.inference_engine import IENetwork,IEPlugin

 

def check_result(y):

pass

 

def preprocess(frame):

pass

 

def postprocess(outputs):

pass

 

def run():

#global plugin used for loading model to MYRIAD Device

plugin = IEPlugin("MYRIAD")

 

# assuming IR Files is: "./text_detection.xml" & "./text_detection.bin"

# Load the model.

# root="./text_detection.%s"

 

model1 = "<model_path.%s>"

 

num_requests = 1

######### MODEL ###########

net = IENetwork(model%'xml',model%'bin')

device_model = plugin.load(network = net,num_requests=num_requests)

input_blob = next(iter(net.inputs))

out_blob = next(iter(net.outputs))

# get the input image

input_img = None

 

# preprocess the inputs

preprocessed_img = preprocess(input_img)

# Run on NCS

device_model.infer({input_blob: preprocessed_img})

# Get the outputs

# print("",result[out_blob].shape)

result = device_model.requests[0].outputs

 

# postprocess the outputs

output = postprocess(result)

if __name__ == "__main__":

run()

 

 

 

 

0 Kudos
Highlighted
Beginner
53 Views

Hi

thank you DavidC_intel and SSola8

I will try to follow your instruction to write the correct python script.

Thanks again.

0 Kudos