# Object Detection with YOLOv5 on Android
## Introduction
[YOLO](https://pjreddie.com/darknet/yolo/) (You Only Look Once) is one of the fastest and most popular object detection models. [YOLOv5](https://github.com/ultralytics/yolov5) is an open-source implementation of the latest version of YOLO (for a quick test of loading YOLOv5 from PyTorch hub for inference, see [here](https://pytorch.org/hub/ultralytics_yolov5/#load-from-pytorch-hub)). This Object Detection with YOLOv5 Android sample app uses the PyTorch scripted YOLOv5 model to detect objects of the [80 classes](https://github.com/ultralytics/yolov5/blob/master/data/coco.yaml) trained with the model.
## Prerequisites
* PyTorch 1.7 or later (Optional)
* Python 3.8 (Optional)
* Android Pytorch library 1.7.0
* Android Studio 4.0.1 or later
## Quick Start
To Test Run the Object Detection Android App, follow the steps below:
### 1. Prepare the Model
If you don't have the PyTorch environment set up to run the script, you can download the model file [here](https://drive.google.com/file/d/1QOxNfpy_j_1KbuhN8INw2AgAC82nEw0F/view?usp=sharing) to the `android-demo-app/ObjectDetection/app/src/main/assets` folder, then skip the rest of this step and go to step 2 directly.
Be aware that the downloadable model file was created with PyTorch 1.7.0, matching the PyTorch Android library 1.7.0 specified in the project's `build.gradle` file as `implementation 'org.pytorch:pytorch_android:1.7.0'`. If you use a different version of PyTorch to create your model by following the instructions below, make sure you specify the same PyTorch Android library version in the `build.gradle` file to avoid possible errors caused by the version mismatch. Furthermore, if you want to use the latest PyTorch master code to create the model, follow the steps at [Building PyTorch Android from Source](https://pytorch.org/mobile/android/#building-pytorch-android-from-source) and [Using the PyTorch Android Libraries Built](https://pytorch.org/mobile/android/#using-the-pytorch-android-libraries-built-from-source-or-nightly) on how to use the model in Android.
The Python script `export.py` in the `models` folder of the [YOLOv5 repo](https://github.com/ultralytics/yolov5) is used to generate a TorchScript-formatted YOLOv5 model named `yolov5s.torchscript.pt` for mobile apps.
Open a Mac/Linux/Windows Terminal, run the following commands (note that we use the fork of the original YOLOv5 repo to make sure the code changes work, but feel free to use the original repo):
```
git clone https://github.com/jeffxtang/yolov5
cd yolov5
pip install -r requirements.txt
```
Then edit `models/export.py` to make two changes:
* Change the line 50 from `model.model[-1].export = True` to `model.model[-1].export = False`
* Add the following two lines of model optimization code after line 57, between `ts = torch.jit.trace(model, img)` and `ts.save(f)`:
```
from torch.utils.mobile_optimizer import optimize_for_mobile
ts = optimize_for_mobile(ts)
```
If you ignore this step, you can still create a TorchScript model for mobile apps to use, but the inference on a non-optimized model can take twice as long as the inference on an optimized model - using the Android app test images, the average inference time on an optimized and non-optimized model is 0.6 seconds and 1.18 seconds, respectively. See [SCRIPT AND OPTIMIZE FOR MOBILE RECIPE](https://pytorch.org/tutorials/recipes/script_optimized.html) for more details.
Finally, run the script below to generate the optimized TorchScript model and copy the generated model file `yolov5s.torchscript.pt` to the `android-demo-app/ObjectDetection/app/src/main/assets` folder:
```
python models/export.py
```
Note that small sized version of the YOLOv5 model, which runs faster but with less accuracy, is generated by default when running the `export.py`. You can also change the value of the `weights` parameter in the `export.py` to generate the medium, large, and extra large version of the model.
### 2. Build with Android Studio
Start Android Studio, then open the project located in `android-demo-app/ObjectDetection`
### 3. Run the app
Select an Android emulator or device to run the app. You can go through the included example test images to see the detection results. You can also select a picture from your Android device's Photos library, take a picture with the device camera, or even use live camera to do object detection - see this [video](https://drive.google.com/file/d/1-5AoRONUqZPZByM-fy0m7r8Ct11OnlIT/view) for a screencast of the app running.
Some example images and the detection results are as follows:
![](screenshot1.png)
![](screenshot2.png)
![](screenshot3.png)
![](screenshot4.png)
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
基于andriod开发的yolov5的红绿灯识别模型APP,可应用于嵌入式大赛。包括程序和数据集。 (657个子文件)
0d2C7fg4Crt+WrLOxCFYL6cqdig= 62KB
0d2C7fg4Crt+WrLOxCFYL6cqdig= 62KB
3j0+gVBm_K7qzyI9Gq27iZMYaPk= 442KB
3j0+gVBm_K7qzyI9Gq27iZMYaPk= 442KB
5AKSCTT8BkLWGow9iq4C1fWoTT8= 54KB
5AKSCTT8BkLWGow9iq4C1fWoTT8= 54KB
5ZIDfGnSjIkjyd+w1FlXV0nTdp8= 45KB
5ZIDfGnSjIkjyd+w1FlXV0nTdp8= 45KB
_hw5NZbNVEdggZQzyFfnMeMSLXU= 33KB
_hw5NZbNVEdggZQzyFfnMeMSLXU= 33KB
androidResources 21KB
resources-debug.ap_ 874KB
app-debug.apk 118.66MB
gradlew.bat 2KB
executionHistory.bin 1.24MB
classAnalysis.bin 962KB
sha1-checksums.bin 125KB
fileHashes.bin 121KB
outputFiles.bin 92KB
jarAnalysis.bin 82KB
taskHistory.bin 41KB
md5-checksums.bin 28KB
resourceHashesCache.bin 18KB
last-build.bin 1B
MainActivity.class 12KB
AbstractCameraXActivity.class 7KB
ObjectDetectionActivity.class 6KB
PrePostProcessor.class 4KB
ResultView.class 3KB
MainActivity$1.class 3KB
MainActivity$4.class 3KB
MainActivity$2.class 2KB
MainActivity$2$1.class 2KB
BaseModuleActivity.class 2KB
PrePostProcessor$1.class 1KB
MainActivity$3.class 1KB
ObjectDetectionActivity$AnalysisResult.class 999B
BuildConfig.class 722B
Result.class 574B
cU4jnEj57mfv6qYCFxpzQylS+eU= 33KB
cU4jnEj57mfv6qYCFxpzQylS+eU= 33KB
classes.dex 3.01MB
classes.dex 315KB
MainActivity.dex 10KB
AbstractCameraXActivity.dex 6KB
ObjectDetectionActivity.dex 5KB
PrePostProcessor.dex 4KB
ResultView.dex 3KB
MainActivity$4.dex 3KB
MainActivity$1.dex 3KB
MainActivity$2$1.dex 2KB
MainActivity$2.dex 2KB
BaseModuleActivity.dex 2KB
PrePostProcessor$1.dex 1KB
MainActivity$3.dex 1KB
ObjectDetectionActivity$AnalysisResult.dex 1KB
BuildConfig.dex 992B
Result.dex 776B
DFLw0uDdDOxHqEBk_cjbqYm92A0= 42KB
DFLw0uDdDOxHqEBk_cjbqYm92A0= 42KB
Eur3OwdrQuHYNyLFF2bJzdacra8= 4KB
Eur3OwdrQuHYNyLFF2bJzdacra8= 4KB
Exvr614c_U5LQNzYOkjEsLYYGJw= 7KB
Exvr614c_U5LQNzYOkjEsLYYGJw= 7KB
values_values.arsc.flat 258KB
mipmap-xxxhdpi_ic_launcher.png.flat 167KB
mipmap-xxhdpi_ic_launcher.png.flat 115KB
mipmap-xhdpi_ic_launcher.png.flat 70KB
mipmap-xxxhdpi_ic_launcher_round.png.flat 49KB
mipmap-xxhdpi_ic_launcher_round.png.flat 33KB
mipmap-mdpi_ic_launcher.png.flat 28KB
mipmap-xhdpi_ic_launcher_round.png.flat 20KB
values-v21_values-v21.arsc.flat 20KB
mipmap-hdpi_ic_launcher_round.png.flat 14KB
values-en-rXC_values-en-rXC.arsc.flat 11KB
drawable_ic_launcher_background.xml.flat 10KB
mipmap-hdpi_ic_launcher.png.flat 9KB
mipmap-mdpi_ic_launcher_round.png.flat 8KB
layout_activity_main.xml.flat 6KB
values-v17_values-v17.arsc.flat 4KB
values-v23_values-v23.arsc.flat 3KB
values-ne_values-ne.arsc.flat 3KB
values-my_values-my.arsc.flat 3KB
values-ml_values-ml.arsc.flat 3KB
values-ta_values-ta.arsc.flat 3KB
values-or_values-or.arsc.flat 3KB
drawable-v24_ic_launcher_foreground.xml.flat 3KB
values-kn_values-kn.arsc.flat 3KB
values-te_values-te.arsc.flat 3KB
values-bn_values-bn.arsc.flat 3KB
values-as_values-as.arsc.flat 3KB
values-si_values-si.arsc.flat 3KB
values-ka_values-ka.arsc.flat 3KB
values-km_values-km.arsc.flat 3KB
values-el_values-el.arsc.flat 3KB
values-mr_values-mr.arsc.flat 3KB
values-lo_values-lo.arsc.flat 3KB
values-hi_values-hi.arsc.flat 3KB
values-gu_values-gu.arsc.flat 3KB
values-bg_values-bg.arsc.flat 3KB
共 657 条
- 1
- 2
- 3
- 4
- 5
- 6
- 7
资源评论
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功