#!/bin/bash

# Datum: 28-08-2022
#
# Object Detection with 10 lines of code:
# https://towardsdatascience.com/object-detection-with-10-lines-of-code-d6cb4d86f606

# Train Object Detection AI with 6 lines of code:
# https://medium.com/deepquestai/train-object-detection-ai-with-6-lines-of-code-6d087063f6ff

# TensorFlow 2.2.0 is not supported by Bullseye due to Python 3.9.
# Only use the 32-bit Buster operating system with Python 3.7.
# Bullseye supports TensorFlow 2.5 and above on a 64-bit RPi.

#
# How to Install TensorFlow 2 and OpenCV on a Raspberry Pi
#
# Testen op Raspberry Pi 3B+
#

sudo timedatectl set-timezone Europe/Amsterdam
sudo apt update && sudo apt upgrade -y
sudo apt update --fix-missing -y
sudo apt full-upgrade -y
sudo apt dist-upgrade -y
sudo apt autoremove -y
clear
cat /etc/os-release
sleep 5
sudo apt update
sudo apt update --fix-missing -y
sudo apt full-upgrade -y
sudo apt dist-upgrade -y
sudo apt autoremove -y
# Overige software installeren o.a. voor de Trust Webcam:
sudo apt install mc xrdp fswebcam guvcview  -y
cat /etc/os-release
sleep 5


# Object detection:
# https://docs.opencv.org/3.0-beta/doc/tutorials/objdetect/table_of_content_objdetect/table_of_content_objdetect.html#table-of-content-objdetect

find . -name haarcascade_eye_tree_eyeglasses.xml
sudo cp /home/pi/project/env/lib/python3.9/site-packages/cv2/data/haarcascade_eye_tree_eyeglasses.xml /home/pi/Desktop/ObjectDetection
sudo cp /home/pi/project/env/lib/python3.9/site-packages/cv2/data/haarcascade_frontalface_alt.xml /home/pi/Desktop/ObjectDetection

# Installeer Python 3.7.6  - https://www.linuxcapable.com/how-to-install-python-3-8-on-debian-11-bullseye/
python -V
wget https://www.python.org/ftp/python/3.7.6/Python-3.7.6.tar.xz
tar -xf Python-3.7.6.tar.xz
sudo mv Python-3.7.6 /opt/Python-3.7.6
sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libsqlite3-dev libreadline-dev libffi-dev curl libbz2-dev -y
cd /opt/Python-3.7.6/
./configure --enable-optimizations --enable-shared
make
# It’s advised to use the 'make altinstall' command NOT to overwrite the default Python 3 binary system.
sudo make altinstall
#  configure the dynamic linker run-time bindings:
sudo ldconfig /opt/Python-3.7.6
# You will also need to replace the path with your directory name and version:
export PATH="/opt/Python-3.7.6:$PATH"
python 3.7.6 -version
mkdir ~/project && cd ~/project
python3.8 -m venv test_app_venv
source test_app_venv/bin/activate
deactivate
wget https://bootstrap.pypa.io/get-pip.py
python3.7.6 get-pip.py
python3.7.6 -m pip install --upgrade pip
pip3.7.6 --version


# Installeer pip
python -V
python -m ensurepip --upgrade
python get-pip.py
python -m pip install --upgrade pip
python -V

# Installeer Tensorflow 2.4.0
pip install tensorflow==2.4.0
# Dependencies
pip install keras==2.4.3 numpy==1.19.3 pillow==7.0.0 scipy==1.4.1 h5py==2.10.0 matplotlib==3.3.2 opencv-python keras-resnet==0.2.0
# ImageAI
pip install imageai --upgrade

mkdir project
cd project
# het is voldoende om imageai te installeren. Dit installeert automatisch de vereiste versies van keras, numpy, pillow, scipy enz. die eerder op de pagina genoemd worden.
pip install imageai --upgrade   # En nu even geduldig wachten tot de installatie klaar is. Met name de installatie van scipy duurt lang!


sudo wget https://github.com/OlafenwaMoses/ImageAI/releases/download/essentials-v5/resnet50_coco_best_v2.1.0.h5
sudo wget https://miro.medium.com/max/630/1*v0Bm-HQxWtpbQ0Yq463uqw.jpeg /home/pi/Desktop/ObjectDetection
sudo wget https://miro.medium.com/max/630/1*zoS8E7Gooe_-3Hi43_R_Yg.jpeg /home/pi/Desktop/ObjectDetection

# https://github.com/OlafenwaMoses/ImageAI
# https://github.com/OlafenwaMoses/ImageAI/blob/master/imageai/Classification/README.md
# https://imageai.readthedocs.io/en/latest/