Skip to content

Instantly share code, notes, and snippets.

@Namburger
Created November 19, 2020 15:26
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Namburger/b76f417a8e96052c7b78bcdbd824f57a to your computer and use it in GitHub Desktop.
Save Namburger/b76f417a8e96052c7b78bcdbd824f57a to your computer and use it in GitHub Desktop.
Minimal Dockerfile for Coral Dev Board with tflite API
FROM arm64v8/debian:latest
WORKDIR /home
ENV HOME /home
RUN cd ~
RUN apt-get update
RUN apt-get install -y git nano python3-pip python-dev pkg-config wget usbutils curl
RUN echo "deb https://packages.cloud.google.com/apt coral-edgetpu-stable main" \
| tee /etc/apt/sources.list.d/coral-edgetpu.list
RUN curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
RUN apt-get update
@Namburger
Copy link
Author

Namburger commented Nov 19, 2020

Instructions:

  1. Install docker in the dev board
sudo apt-get install apt-transport-https ca-certificates curl gnupg2 software-properties-common
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add -
echo "deb [arch=arm64] https://download.docker.com/linux/debian buster stable" | sudo tee /etc/apt/sources.list.d/docker.list
sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io
sudo groupadd docker
sudo usermod -aG docker $USER
sudo reboot now
  1. Create Dockerfile with above content
  2. Build docker image:
mendel@mocha-apple:~/docker_test$ docker build -t "coral" .
  1. Run the image while giving it apex device file permission:
mendel@mocha-apple:~/docker_test$ docker run -it --device /dev/apex_0:/dev/apex_0 coral /bin/bash
  1. install pycoral
root@c6457582d5fd:~# apt install python3-pycoral
  1. Clone tflite API repo
root@c6457582d5fd:~# git clone https://github.com/google-coral/tflite.git
  1. Download models
root@c6457582d5fd:~# cd tflite/python/examples/classification
root@c6457582d5fd:~# ./install_requirements.sh
  1. Run example:
root@c6457582d5fd:~/tflite/python/examples/classification# python3 classify_image.py \
>   --model models/mobilenet_v2_1.0_224_inat_bird_quant_edgetpu.tflite \
>   --labels models/inat_bird_labels.txt \
>   --input images/parrot.jpg
----INFERENCE TIME----
Note: The first inference on Edge TPU is slow because it includes loading the model into Edge TPU memory.
13.7ms
3.5ms
2.9ms
2.6ms
2.7ms
-------RESULTS--------
Ara macao (Scarlet Macaw): 0.77734

@alexcocinda
Copy link

That's awesome, thank you! I tried to read and display the stream from a usb webcam after running the container with a few extra options:
docker run -it --device /dev/apex_0:/dev/apex_0 --device /dev/bus/usb/:/dev/bus/usb -e DISPLAY=$DISPLAY -v /tmp/.X11-unix/:/tmp/.X11-unix --name test coral /bin/bash.

But I get the following error:
cv2.error: /build/opencv-tragD2/opencv-3.2.0+dfsg/modules/highgui/src/window.cpp:304: error: (-215) size.width>0 && size.height>0 in function imshow

This is the script I used:

import cv2

#Capture video from webcam
vid_capture = cv2.VideoCapture(1)
vid_cod = cv2.VideoWriter_fourcc(*'XVID')
output = cv2.VideoWriter("videos/cam_video.mp4", vid_cod, 20.0, (640,480))

while(True):
     # Capture each frame of webcam video
     ret,frame = vid_capture.read()
     cv2.imshow("My cam video", frame)
     output.write(frame)
     # Close and break the loop after pressing "x" key
     if cv2.waitKey(1) &0XFF == ord('x'):
         break

# close the already opened camera
vid_capture.release()
# close the already opened file
output.release()
# close the window and de-allocate any associated memory usage
cv2.destroyAllWindows()

I also installed python3-opencv. Do you have any idea about what the problem could be?

@Namburger
Copy link
Author

@alexcocinda I'm not 100% sure, but according to this tutorial:
https://www.learnopencv.com/install-opencv-docker-image-ubuntu-macos-windows/

You may want to specify the video device file as oppose to the usb devide it self, maybe?

–device=/dev/video0:/dev/video0

@alexcocinda
Copy link

Yes, you are right, thank you. I'll continue to describe the following issue if someone else tries this too. After I fixed my docker run, I got the following error:

Unable to init server: Could not connect: Connection refused

(My cam video:12): Gtk-WARNING **: 18:54:45.068: cannot open display: :0

I managed to fix it by executing the following commands:

sudo apt install -y x11-xserver-utils
xhost +

make sure that DISPLAY is set correctly and check your display number with w. If it's not, try the following:

w
export DISPLAY=<number-you-found-from-the-w-command-output>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment