Coral Python object detection

Revision as of 15:26, 23 June 2020 by Registered User

This article explains how to experimentCoral Edge TPU[1] applications for object detection based on COCO SSD MobileNet v1 model using the TensorFlow Lite Python runtime.

1. Description[edit source]

The object detection[2] neural network model allows identification and localization of a known object within an image.

Python Coral Edge TPU object detection application

The application enables OpenCV camera streaming (or test data picture) and the Coral Edge TPU[1] TensorFlow Lite[3] interpreter runing the NN inference based on the camera (or test data pictures) inputs.
The user interface is implemented using Python GTK.

The model used with this application is the COCO SSD MobileNet v1 downloaded from the object detection overview[2] and converted for the Coral Edge TPU.

2. Installation[edit source]

2.1. Install from the OpenSTLinux AI package repository[edit source]

After having configured the AI OpenSTLinux package you can install X-LINUX-AI components for this application:

 apt-get install tflite-cv-apps-edgetpu-object-detection-python

And restart the demo launcher:

 systemctl restart weston@root

2.2. Source code location[edit source]

The objdetect_tfl_edgetpu.py Python script is available:

  • in the Openembedded OpenSTLinux distribution with X-LINUX-AI expansion package:
<Distribution Package installation directory>/layers/meta-st/meta-st-stm32mpu-ai/recipes-samples/tflite-cv-apps-edgetpu/files/object-detection/python/objdetect_tfl_edgetpu.py
  • on the target:
/usr/local/demo-ai/computer-vision/tflite-object-detection-edgetpu/python/objdetect_tfl_edgetpu.py
  • on GitHub:
https://github.com/STMicroelectronics/meta-st-stm32mpu-ai/tree/dunfell/recipes-samples/tflite-cv-apps-edgetpu/files/object-detection/python/objdetect_tfl_edgetpu.py

3. How to use it[edit source]

3.1. Launch application via the demo launcher[edit source]

Launch python tfl edgetpu object detection.png

3.2. Or execute with command line[edit source]

The Python script objdetect_tfl_edgetpu.py application is located in the userfs partition:

/usr/local/demo-ai/computer-vision/tflite-object-detection-edgetpu/python/objdetect_tfl_edgetpu.py

It accepts the following input parameters:

usage: objdetect_tfl_edgetpu.py [-h] [-i IMAGE] [-v VIDEO_DEVICE] [--frame_width FRAME_WIDTH] [--frame_height FRAME_HEIGHT]           
                                [--framerate FRAMERATE] [-m MODEL_FILE] [-l LABEL_FILE] [--lib_edgetpu {max,throttled}]               
                                [--input_mean INPUT_MEAN] [--input_std INPUT_STD] [--top_k TOP_K]                                     
                                                                                                                                      
optional arguments:                                                                                                                   
  -h, --help            show this help message and exit                                                                               
  -i IMAGE, --image IMAGE                                                                                                             
                        image directory with image to be classified                                                                   
  -v VIDEO_DEVICE, --video_device VIDEO_DEVICE                                                                                        
                        video device (default /dev/video0)                                                                            
  --frame_width FRAME_WIDTH                                                                                                           
                        width of the camera frame (default is 320)                                                                    
  --frame_height FRAME_HEIGHT                                                                                                         
                        height of the camera frame (default is 240)                                                                   
  --framerate FRAMERATE                                                                                                               
                        framerate of the camera (default is 15fps)                                                                    
  -m MODEL_FILE, --model_file MODEL_FILE                                                                                              
                        .tflite model to be executed                                                                                  
  -l LABEL_FILE, --label_file LABEL_FILE                                                                                              
                        name of file containing labels                                                                                
  --lib_edgetpu {max,throttled}                                                                                                       
                        Choose the version of your EdgeTPU runtime                                                                    
  --input_mean INPUT_MEAN                                                                                                             
                        input mean                                                                                                    
  --input_std INPUT_STD                                                                                                               
                        input standard deviation                                                                                      
  --top_k TOP_K         The top_k classes to show 

3.3. Testing with COCO SSD MobileNet V1[edit source]

The model used for test is the detect_edgetpu.tflite downloaded from the object detection overview[2] and converted for the Coral Edge TPU.

To ease launching of the application, two shell scripts are available:

  • launch object detection based on camera frame inputs
/usr/local/demo-ai/computer-vision/tflite-object-detection-edgetpu/python/launch_python_objdetect_tfl_edgetpu_coco_ssd_mobilenet.sh
  • launch object detection based on the pictures located in /usr/local/demo-ai/computer-vision/models/mobilenet/testdata directory
 /usr/local/demo-ai/computer-vision/tflite-object-detection-edgetpu/python/launch_python_objdetect_tfl_edgetpu_coco_ssd_mobilenet_testdata.sh
Info white.png Information
Note that you need to populate the testdata directory with your own data sets.

The pictures are then randomly read from the testdata directory

4. References[edit source]