###### tags `edu` `sju`
> 最後更新 [name=Jed Hung - 2019/10/31]
###### 本頁連結 - https://hackmd.io/@jed/rk4UbTB9S
# YOLO 深度學習實務應用 (Part 1)
> cat /etc/os-release
```=
PRETTY_NAME="Raspbian GNU/Linux 10 (buster)"
NAME="Raspbian GNU/Linux"
VERSION_ID="10"
VERSION="10 (buster)"
VERSION_CODENAME=buster
ID=raspbian
ID_LIKE=debian
HOME_URL="http://www.raspbian.org/"
SUPPORT_URL="http://www.raspbian.org/RaspbianForums"
BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"
```
## Step 0: Prepare Python and Pi Camera
```
sudo apt-get update
sudo apt-get install python-pip
sudo apt-get install python-opencv
sudo raspi-config # if you use Pi Camera
```
## Step 1: Install NNPACK
### Install Ninja (building tool)
Install PeachPy and confu
```
sudo pip install --upgrade git+https://github.com/Maratyszcza/PeachPy
sudo pip install --upgrade git+https://github.com/Maratyszcza/confu
```
Install Ninja
```
git clone https://github.com/ninja-build/ninja.git
cd ninja
git checkout release
./configure.py --bootstrap
export NINJA_PATH=$PWD
cd
```
### Install NNPACK
```
git clone https://github.com/shizukachan/NNPACK
cd NNPACK
confu setup
python ./configure.py --backend auto
```
**Build NNPACK with ninja (this might take * quie * a while, be patient. In fact my Pi crashed in the first time. Just reboot and run again)**
```
$NINJA_PATH/ninja
```
do a `ls` and you should be able to find the folders lib and include if all went well:
```
ls
```
Test if NNPACK is working:
```
bin/convolution-inference-smoketest
```
In my case, the test actually failed in the first time. But I just ran the test again and all items are passed. So if your test failed, don't panic, try one more time.
Copy the libraries and header files to the system environment:
```
sudo cp -a lib/* /usr/lib/
sudo cp include/nnpack.h /usr/include/
sudo cp deps/pthreadpool/include/pthreadpool.h /usr/include/
```
## Step 2. Install darknet-nnpack
```
cd
git clone -b yolov3 https://github.com/zxzhaixiang/darknet-nnpack
cd darknet-nnpack
git checkout yolov3
# modify Makefile: OPENCV=1
make
```
## Step 3. Test with YoloV3-tiny
To test it, simply run
```
sudo python rpi_video.py
```
if you use usb camera
> vim rpi_usb_video.py
```python=
#from picamera import PiCamera
from subprocess import Popen, PIPE
import threading
from time import sleep
import os, fcntl
import cv2
iframe = 0
#camera = PiCamera()
cap = cv2.VideoCapture(0)
#Yolo v3 is a full convolutional model. It does not care the size of input image, as long as h and w are multiplication of 32
#camera.resolution = (160,160)
#camera.resolution = (416, 416)
#camera.resolution = (544, 544)
#camera.resolution = (608, 608)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 608)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 608)
#camera.resolution = (608, 288)
#camera.capture('frame.jpg')
ret, frame = cap.read()
cv2.imwrite('frame.jpg', frame)
sleep(0.1)
#spawn darknet process
yolo_proc = Popen(["./darknet",
"detect",
"./cfg/yolov3-tiny.cfg",
"./yolov3-tiny.weights",
"-thresh","0.1"],
stdin = PIPE, stdout = PIPE)
fcntl.fcntl(yolo_proc.stdout.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)
while True:
try:
stdout = yolo_proc.stdout.read()
if 'Enter Image Path' in stdout:
try:
im = cv2.imread('predictions.png')
print(im.shape)
cv2.imshow('yolov3-tiny',im)
key = cv2.waitKey(5)
except Exception:
pass
#camera.capture('frame.jpg')
ret, frame = cap.read()
cv2.imwrite('frame.jpg', frame)
yolo_proc.stdin.write('frame.jpg\n')
if len(stdout.strip())>0:
print('get %s' % stdout)
except Exception:
pass
```
```
sudo python rpi_usb_video.py
```
## Reference
https://funofdiy.blogspot.com/2018/08/deep-learning-with-raspberry-pi-real.html
https://www.makeartwithpython.com/blog/poor-mans-deep-learning-camera