###### tags `edu` `sju`
> 最後更新 [name=Jed Hung - 2019/10/31]
###### 本頁連結 - https://hackmd.io/@jed/B1oO8fdqH
# YOLO 深度學習實務應用 (Part 2)
## Step 1. Server node
at Raspberry Pi 3
```
git clone https://github.com/burningion/poor-mans-deep-learning-camera
cd poor-mans-deep-learning-camera
cd Camera-Server
python app.py
```
Before continue, get your ip address at Raspberry Pi side first
```
ifconfig eth0
```
if you use Wi-Fi
```
ifconfig wlan0
```
## Step 2. Client node
at Computer or Notebook
> cat /proc/cpuinfo | grep "model name"
```=
model name : Intel(R) Core(TM) i3-8109U CPU @ 3.00GHz
model name : Intel(R) Core(TM) i3-8109U CPU @ 3.00GHz
model name : Intel(R) Core(TM) i3-8109U CPU @ 3.00GHz
model name : Intel(R) Core(TM) i3-8109U CPU @ 3.00GHz
```
> free -h
```=
total used free shared buff/cache available
Mem: 7.7G 1.5G 3.4G 414M 2.8G 5.5G
Swap: 2.0G 0B 2.0G
```
> cat /etc/os-release
```=
NAME="Ubuntu"
VERSION="18.04.3 LTS (Bionic Beaver)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 18.04.3 LTS"
VERSION_ID="18.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=bionic
UBUNTU_CODENAME=bionic
```
### Install YOLO v3
```
sudo apt update
sudo apt install python-opencv
sudo apt install libopencv-dev
sudo apt install git
sudo apt install python-pip
pip install requests
pip2 install Pillow
git clone https://github.com/pjreddie/darknet.git
cd darknet
make
```
### Get weights
```
wget https://pjreddie.com/media/files/yolov3-tiny.weights
```
### Get images
Test `http://<IP address for your Raspberry Pi>:5000/image.jpg` first
> vim rpi_video.py
```python=
from subprocess import Popen, PIPE
import threading
from io import BytesIO
from time import sleep
import os, fcntl
import cv2
import requests
from PIL import Image
import numpy as np
#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.jpg')
print(im.shape)
cv2.imshow('yolov3-tiny',im)
key = cv2.waitKey(1)
except Exception:
pass
r = requests.get('http://10.118.126.240:5000/image.jpg') # replace with your ip address
curr_img = Image.open(BytesIO(r.content))
#curr_img_cv2 = cv2.cvtColor(np.array(curr_img), cv2.COLOR_RGB2BGR)
curr_img.save('frame.jpg')
yolo_proc.stdin.write('frame.jpg\n')
if len(stdout.strip())>0:
print('get %s' % stdout)
except Exception:
pass
```
```
sudo python ./rpi_video.py
```