Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Commit

Permalink
Fixes for wine detection tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
larroy committed Jan 16, 2019
1 parent 5b011b3 commit eca332c
Showing 1 changed file with 34 additions and 19 deletions.
53 changes: 34 additions & 19 deletions docs/tutorials/embedded/wine_detector.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Real-time Object Detection with MXNet On The Raspberry Pi
# Real-time Object Detection with MXNet On The Raspberry Pi

This tutorial shows developers who work with the Raspberry Pi or similar embedded ARM-based devices how to compile MXNet for those devices and run a pretrained deep network model. It also shows how to use AWS IoT to manage and monitor MXNet models running on your devices.

Expand Down Expand Up @@ -74,6 +74,7 @@ The next step is to create a python script to load the model, and run inference

import mxnet as mx
import numpy as np
import time
import cv2, os, urllib
from collections import namedtuple
Batch = namedtuple('Batch', ['data'])
Expand All @@ -83,13 +84,14 @@ with open('synset.txt', 'r') as f:
synsets = [l.rstrip() for l in f]

# Load the network parameters
sym, arg_params, aux_params = mx.model.load_checkpoint('Inception_BN', 0)
sym, arg_params, aux_params = mx.model.load_checkpoint('Inception-BN', 126)


# Load the network into an MXNet module and bind the corresponding parameters
mod = mx.mod.Module(symbol=sym, context=mx.cpu())
mod.bind(for_training=False, data_shapes=[('data', (1,3,224,224))])
mod.set_params(arg_params, aux_params)

'''
Function to predict objects by giving the model a pointer to an image file and running a forward pass through the model.
Expand All @@ -112,14 +114,14 @@ def predict(filename, mod, synsets, N=5):
img = np.swapaxes(img, 1, 2)
img = img[np.newaxis, :]
print "pre-processed image in "+str(time.time()-tic)

toc = time.time()
mod.forward(Batch([mx.nd.array(img)]))
prob = mod.get_outputs()[0].asnumpy()
prob = np.squeeze(prob)
print "forward pass in "+str(time.time()-toc)


topN = []
a = np.argsort(prob)[::-1]
for i in a[0:N]:
Expand All @@ -139,19 +141,32 @@ def predict_from_url(url, N=5):
return predict(filename, mod, synsets, N)

# Code to predict on a local file
def predict_from_local_file(filename, N=5):
def predict_from_local_file(filename, N=5):
return predict(filename, mod, synsets, N)
```

Now that we have defined inception_predict.py we can test that the model is running correctly. Open a Python REPL in your home directory and enter the following:

```bash
python
>>> import inception_predict
>>> predict_from_url("http://imgur.com/HzafyBA")
>>> from inception_predict import *
>>> predict_from_url("https://i.imgur.com/HzafyBA.jpg")
```

This should give a reasonable prediction for the fluffy cow in this [image](http://imgur.com/HzafyBA).
This should give a reasonable prediction for the fluffy cow in this [image](http://imgur.com/HzafyBA).

```
pre-processed image in 0.20366191864
forward pass in 63.2164611816
probability=0.718524, class=n02403003 ox
probability=0.176381, class=n02389026 sorrel
probability=0.095558, class=n03868242 oxcart
probability=0.002765, class=n02408429 water buffalo, water ox, Asiatic buffalo, Bubalus bubalis
probability=0.001262, class=n03935335 piggy bank, penny bank
[(0.71852392, 'n02403003 ox'), (0.17638102, 'n02389026 sorrel'), (0.09555836, 'n03868242 oxcart'),
(0.0027645244, 'n02408429 water buffalo, water ox, Asiatic buffalo, Bubalus bubalis'),
(0.0012616422, 'n03935335 piggy bank, penny bank')]
```


## Running an Inception on Real-Time Video From PiCamera
Expand All @@ -177,11 +192,11 @@ while True:
camera.start_preview()
camera.capture(filename)
camera.stop_preview()

# Run inception prediction on image
print "Predicting"
topn = inception_predict.predict_from_local_file(filename, N=5)

# Print the top N most likely objects in image (default set to 5, change this in the function call above)
print topn
```
Expand All @@ -192,7 +207,7 @@ You can then run this file by entering the following command:
python camera_test.py
```

If camera_test.py is working you should see a preview every few seconds of the image that is being captured and fed to the model, as well as predicted classes for objects in the image being written to the terminal.
If camera_test.py is working you should see a preview every few seconds of the image that is being captured and fed to the model, as well as predicted classes for objects in the image being written to the terminal.

Try pointing the PiCamera at a few different objects and see what predictions the network comes out with.

Expand Down Expand Up @@ -231,13 +246,13 @@ def customCallback(client, userdata, message):

# Usage
usageInfo = """Usage:
Use certificate based mutual authentication:
python wine_alerter.py -e <endpoint> -r <rootCAFilePath> -c <certFilePath> -k <privateKeyFilePath>
Use MQTT over WebSocket:
python wine_alerter.py -e <endpoint> -r <rootCAFilePath> -w
Type "python wine_alerter.py -h" for available options.
"""

Expand All @@ -255,7 +270,7 @@ helpInfo = """-e, --endpoint
-h, --help
Help information
"""

# Read in command-line parameters
useWebsocket = False
host = ""
Expand Down Expand Up @@ -350,10 +365,10 @@ while True:
camera.capture(filename)
camera.stop_preview()
topn = inception_predict.predict_from_local_file(filename, N=5)

# Check if either of the top two predictions are wine related and publish a message if it is
# you can change 'wine' here to anything you want to alert the server about detecting
if 'wine' in topn[0][1] or 'wine' in topn[1][1]:
if 'wine' in topn[0][1] or 'wine' in topn[1][1]:
myAWSIoTMQTTClient.publish("sdk/test/Python", "New Message: WINE DETECTED!", 0)
```

Expand Down

0 comments on commit eca332c

Please sign in to comment.