Classification and output

Assuming you perform the previously outlined environmental checks and setups outlined earlier in this chapter, all of the code up to this point should run flawlessly without producing any errors. The next section of code was updated from the original version of the project offered on GitHub due to changes IBM made to the newer version of the API.

If you use the notebook code provided with this book, the updates have already been made for you.

Finally, the next code section receives the results or the classifications back from the Watson Visual Recognition service into an object named results.  

From that information, a label is then constructed, and a rectangle shape is drawn around each object that was detected within the source image file:

# --- Classify images with Watson visual recognition
with open(full_path, 'rb') as images_file:
parameters = json.dumps({'threshold': 0.7, 'classifier_ids': ['default']})
results = visual_recognition.classify(images_file=images_file, parameters=parameters).get_result()
label = results['images'][0]['classifiers'][0]['classes'][0]['class']
ax.text(box_x + 5, box_y - 5, label, fontsize=10, color='white', bbox={'facecolor':COLORS[i % 8], 'edgecolor':'none'})
# --- Create a Rectangle patch
rect = patches.Rectangle((box_x, box_y), box_width, box_height, linewidth=2, edgecolor=COLORS[i % 8], facecolor='none')
ax.add_patch(rect)

If you examine the results object more closely (try using the Python type command on it), you will see that the results object is a Python dictionary object.

A Python dictionary object is similar to a list in that it is a collection of objects. 

Now try adding print(results) to the notebook code and you'll get a glimpse of the raw output returned in results. 

Using the print(results) command, the actual output is shown in the following screenshot:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset