4. Installing darknet nnpack to run YOLOv3 on Raspberry pi 4 - HaroldSP/Harold GitHub Wiki

Build Instructions

1. Some of that you might need

1.1. Log in to Raspberry Pi using SSH. Always update upgrade first!

sudo apt-get update && sudo apt-get upgrade

1.2. Install PeachPy and confu

sudo apt-get install python-pip
sudo pip install --upgrade git+https://github.com/Maratyszcza/PeachPy
sudo pip install --upgrade git+https://github.com/Maratyszcza/confu

Usually I install with pip3, but the original was just pip

1.3. Install Ninja

git clone https://github.com/ninja-build/ninja.git
cd ninja
git checkout release
./configure.py --bootstrap
export NINJA_PATH=$PWD

Possible errors can be:

warning: A compatible version of re2c (>= 0.11.3) was not found; 
changes to src/*.in.cc will not affect your build.

A possible solution can be:

sudo apt-get install -y re2c

1.4. Install clang (I'm not sure why we need this, NNPACK doesn't use it unless you specifically target it)

sudo apt-get install clang

2. NNPACK

cd
git clone https://github.com/shizukachan/NNPACK.git
cd NNPACK
mkdir build && cd build
export PATH="${PATH}:~/ninja" 
cmake -G Ninja -DBUILD_SHARED_LIBS=on-DCMAKE_C_FLAGS=-march=armv6k ..
ninja
cd
sudo ninja install
git clone https://github.com/shizukachan/darknet-nnpack.git
cd darknet-nnpack
make

3. Dealing with errors

3.1. If cannot find opencv.pc

The full article

sudo find /  -iname opencv.pc
export PKG_CONFIG_PATH=/path/to/the/file

It’s better to add this line to your bashrc file so that you don’t have to do it every single time you reopen your terminal.

3.2. Some other cases

sudo apt-get install pkg-config
sudo apt-get install libopencv-dev

4. Download weights and test!

wget https://pjreddie.com/media/files/yolov3-tiny.weights
./darknet detector test cfg/coco.data cfg/yolov3-tiny.cfg yolov3-tiny.weights data/dog.jpg

5. Errors on startup

The full issue

./darknet: error while loading shared libraries: libnnpack.so: 
cannot open shared object file: No such file or directory

simply do:

sudo /bin/bash -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf'
sudo ldconfig

6. Run real-time stream!

./darknet detector demo cfg/coco.data cfg/yolov3-tiny.cfg yolov3-tiny.weights

Results (screenshot of video stream not included):

FPS:0.5
Objects:
person: 45% 

7. Trying to run rpi_video.py

run in the darknet-nnpack directory as:

python yolo_test.py

7.1 Error

picamera.exc.PiCameraMMALError: Failed to enable connection: Out of resources

solution:

sudo apt-get update && sudo apt-get upgrade
sudo rpi-update
#reboot

Also, you can rise GPU memory to 256 Mb

7.2 Error (if you run as python3 yolo_test.py)

Error: can only concatenate str (not "bytes") to str

I found some similar problems but didn't implement the solution in that code:

base64.b64encode produces a byte-stream, not a string. So for your concatenation to work, you have to convert it into a string first with str(base64.b64encode(addpermissions.encode('utf_16_le')))

b64cmd = base64.b64encode(cmd.encode('utf_16_le')).decode('utf-8')
os.system('powershell -enc ' + b64cmd)

EDIT: Normal string conversion didn't work with os.system, used decode('utf-8') instead

8. IF SOMEONE READING THIS

If it works on your Raspberry I am very glad, because it took me several days to finally make it work.

I would very much appreciate it if you could send me some nice code in python for the real-time detection from a raspberry pi cam because 0.5 fps is not enough.

For example, TensorFlow lite reaches 4-5 fps with active cooling and building it takes around 20 minutes.

In that case, I don't quite understand, what's the point in Yolo and darknet for the real-time detection purposes on raspberry.

Thanks for your attention.