> For the complete documentation index, see [llms.txt](https://davids-tutorials.gitbook.io/robotics/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://davids-tutorials.gitbook.io/robotics/cv-yolov5.md).

# cv (yolov5)

Note: [Drone Computer Vision Repo w/ Datasets](https://gitlab.com/drone_la/cv)

### 1 - Video/Roboflow: Gather images for dataset

| Logo                                                                | Description                                                                                                                                                                                                                           |
| ------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| <img src="/files/NGf5NuLzLQVeoV7oYmKl" alt="" data-size="original"> | We can film a video of the item we want to detect and then upload to Roboflow. On the Roboflow website, we can extract frames every X seconds to create a .jpg file each interval. Then we can begin labeling each image on Roboflow. |

> Roboflow is a website where you upload images, label them, and export a zip file (images & .xml files).&#x20;
>
> \
> Useful to share dataset and have 3 people label at the same time

### 1.1 - Unlabeled Images to Labeled Images&#x20;

| Unlabled                                                            | Labeled (Desired Outcome)                                           |
| ------------------------------------------------------------------- | ------------------------------------------------------------------- |
| <img src="/files/Kl0vuUBfQl9TXGM43H9o" alt="" data-size="original"> | <img src="/files/fLmM5SLM8DEXq1xDu7pb" alt="" data-size="original"> |

{% embed url="<https://www.youtube.com/watch?v=a3SBRtILjPI>" %}

***

### 2 - Colab: Train Custom Model

<table><thead><tr><th width="365">Logo</th><th>Description</th></tr></thead><tbody><tr><td><img src="/files/Xpn7RF2xVnUC4vf9sP8E" alt="" data-size="original"></td><td><p>-The following website uses Python </p><p>-here we set up libraries </p><p>     - collections of pre-written code that you </p><p>        can use to perform specific tasks</p><p>-Prepare the dataset</p><p>-Train the model</p><p>-Evaluate the model performance with validation data</p><p>-Export the model as .tflite file </p></td></tr></tbody></table>

Open up [Google Colab for Yolov5 Training](https://colab.research.google.com/drive/1K1-LqloTLq9r0UevUlp0_G7QOfKk5BXH)

Follow [this video ](https://www.youtube.com/watch?v=fBHvyiXE0RY)for more details

> <pre><code><strong>#made changes to /yolov5train/yolov5/dataset.yaml 
> </strong>train: ../data/images/train/
> val: ../data/images/valid/
>
> # number of classes
> nc: 1
>
> # class names
> names: ['Target']
> </code></pre>

### 3 - Export Model

> ```notebook-python
> #############################
> #                           #
> ##         RESULTS         ##
> #                           #
> #############################
>
> # 'download /yolov5/runs/train/exp/weights/best.pt'
> ```

> Download the best.pt file to upload to Raspberry Pi.&#x20;
>
> \
> Have a naming convention like '3-27-2024--300epochs-' [Demo here](https://youtu.be/fBHvyiXE0RY?t=1193)

### 4 - Set up Pi - Installation

Python Library [Versions located here](https://gitlab.com/drone_la/cv/-/blob/main/yolov5-versions-3-27-2024.txt?ref_type=heads)

<table><thead><tr><th width="737">Ctrl + Alt + T</th></tr></thead><tbody><tr><td><p>NOTE: (2:54)</p><p>-click top left</p><p>-preferences</p><p>-raspberry pi configuration</p><p>-tabs: interfaces</p><p>-enable camera</p><p>-reboot</p></td></tr><tr><td>uaYq12tCsfz3-hWdK8ivKExIY</td></tr></tbody></table>

| sudo apt-get update                                                                        |
| ------------------------------------------------------------------------------------------ |
| git clone <https://github.com/ultralytics/yolov5>                                          |
| cd yolov5                                                                                  |
| pip install -r requirements.txt                                                            |
| pip install -r requirements.txt --user                                                     |
| pip install torchvision                                                                    |
| mv installs to /.local/python3/dist-packages /usr/local/lib/python3.9/dist-packages/yolov5 |
|                                                                                            |
| cd .local/lib/python3.10/site-packages/                                                    |
| ls # check if packages installed                                                           |
| sudo mv ./\* /usr/local/lib/python3.10/dist-packages                                       |
|                                                                                            |
|                                                                                            |
|                                                                                            |
|                                                                                            |

### 5 - Run on Pi

| Ctrl + Alt + T                                                                                                                                                                                                                                                  |
| --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| (Note: if on virtual machine for testing): Devices > Webcams > Check Integrated cam box                                                                                                                                                                         |
| Download your best.pt file after training, you can use Github or Google Drive                                                                                                                                                                                   |
|                                                                                                                                                                                                                                                                 |
| cd yolov5                                                                                                                                                                                                                                                       |
| cp \~/Downloads/best.pt .                                                                                                                                                                                                                                       |
|                                                                                                                                                                                                                                                                 |
| <p>gedit detect.py<br></p><ul><li>(Note: make sure you use correct naming convention)</li><li><p>got to line 53 and type: </p><ul><li>weights = 'best.pt',</li></ul></li><li><p>go and comment line 54: </p><ul><li>weights = 'yolov5s.pt',</li></ul></li></ul> |
|                                                                                                                                                                                                                                                                 |
| sudo python3 detect.py                                                                                                                                                                                                                                          |
| <p>sudo python3 detect.py --weights 3-27-13-best.pt --source 0<br>or<br>sudo python3 detect.py --weights best.pt --source 0</p>                                                                                                                                 |
