Skip to content

Commit

Permalink
add yolov8 face demo
Browse files Browse the repository at this point in the history
  • Loading branch information
Neutree committed Jun 21, 2024
1 parent 25282d2 commit 1a01ddf
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 20 deletions.
45 changes: 33 additions & 12 deletions docs/doc/en/vision/face_detection.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,44 @@
---
title: MaixPy Face Detection and Key Points Detection
title: MaixPy Face Detection and Keypoint Detection
---

## Introduction

Face detection can be used in many places, such as providing the step of face detection for face recognition, or applications related to face tracking, and more.
Face detection can be applied in many scenarios, such as providing the face detection step for face recognition, or for face tracking applications, etc.

The face detection provided here can not only detect faces but also detect 5 key points, including two eyes, one nose, and the two corners of a mouth.
The face detection provided here can not only detect faces but also detect 5 key points, including two eyes, one nose, and two corners of the mouth.

![face detection](../../assets/face_detection.jpg)

## Using Face Detection in MaixPy

MaixPy officially provides two face detection models, sourced from the open projects [face detector 1MB with landmark](https://github.com/biubug6/Face-Detector-1MB-with-landmark) and [Retinaface](https://github.com/biubug6/Pytorch_Retinaface).
MaixPy officially provides three face detection models from the open-source projects [Face Detector 1MB with landmark](https://github.com/biubug6/Face-Detector-1MB-with-landmark), [Retinaface](https://github.com/biubug6/Pytorch_Retinaface), and [YOLOv8-face](https://github.com/derronqi/yolov8-face).

To use them, first download a model, either one as there's not much difference between them:
* [face detector 1MB with landmark](https://maixhub.com/model/zoo/377)
* [Retinaface](https://maixhub.com/model/zoo/378)
All three models can be used. `YOLOv8-face` performs better but is slightly slower, so you can choose based on your testing.

Using `YOLOv8-face` (requires MaixPy version >= 4.3.8):

```python
from maix import camera, display, image, nn, app

Then copy the model file to your device, see [Using MaixVision](../basic/maixvision.md) for how to copy.
> The default image contains a file that can be used directly; if not available, you must download it yourself. The downloaded zip package contains multiple resolutions to choose from; the higher the resolution, the more precise but also more time-consuming.
detector = nn.YOLOv8(model="/root/models/yolov8n_face.mud")

Next, run the code. The following line of commented code is for loading the `Retinaface` model, choose which line of code to use based on the model you downloaded.
cam = camera.Camera(detector.input_width(), detector.input_height(), detector.input_format())
dis = display.Display()

while not app.need_exit():
img = cam.read()
objs = detector.detect(img, conf_th = 0.5, iou_th = 0.45, keypoint_th = 0.5)
for obj in objs:
img.draw_rect(obj.x, obj.y, obj.w, obj.h, color = image.COLOR_RED)
msg = f'{detector.labels[obj.class_id]}: {obj.score:.2f}'
img.draw_string(obj.x, obj.y, msg, color = image.COLOR_RED)
detector.draw_pose(img, obj.points, 2, image.COLOR_RED)
dis.show(img)
```

> To use this function, MaixPy must >= 4.1.4.
For the other two models:
Here, a line of commented-out code is used to load the `Retinaface` model. Choose which line of code to use based on the model you download.

```python
from maix import camera, display, image, nn, app
Expand All @@ -43,5 +58,11 @@ while not app.need_exit():
radius = math.ceil(obj.w / 10)
img.draw_keypoints(obj.points, image.COLOR_RED, size = radius if radius < 5 else 4)
dis.show(img)

```

## Model Downloads and Other Resolution Models

Download the models; the compressed package contains multiple resolutions to choose from. Higher resolution models are more accurate but take longer to process:
* [Face Detector 1MB with landmark](https://maixhub.com/model/zoo/377)
* [Retinaface](https://maixhub.com/model/zoo/378)
* [YOLOv8-face](https://maixhub.com/model/zoo/407)
37 changes: 29 additions & 8 deletions docs/doc/zh/vision/face_detection.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,33 @@ title: MaixPy 人脸检测和关键点检测

## MaixPy 中使用人脸检测

MaixPy 官方提供了两种人脸检测模型,分别来自开源项目 [face detector 1MB with landmark](https://github.com/biubug6/Face-Detector-1MB-with-landmark)[Retinafate](https://github.com/biubug6/Pytorch_Retinaface)
MaixPy 官方提供了三种人脸检测模型,分别来自开源项目 [face detector 1MB with landmark](https://github.com/biubug6/Face-Detector-1MB-with-landmark)[Retinafate](https://github.com/biubug6/Pytorch_Retinaface) 以及 [YOLOv8-face](https://github.com/derronqi/yolov8-face)

要使用需要先下载模型,选择一个即可,两者区别不大:
* [face detector 1MB with landmark](https://maixhub.com/model/zoo/377)
* [Retinafate](https://maixhub.com/model/zoo/378)
这三种模型都可以用,`YOLOv8-face` 效果比较好但是速度略微慢一些,可以自己实际测试选择使用。

使用`YOLOv8-face`:(需要 MaixPy 版本 >= 4.3.8)

然后拷贝模型文件到设备,拷贝方法见 [MaixVision 使用](../basic/maixvision.md)
> 默认镜像里面有一个文件,可以直接使用,如果没有则需要你自己下载,而且下载的压缩包里面有多个分辨率可以选择,分辨率越高越精准但耗时更长
```python
from maix import camera, display, image, nn, app

然后执行代码,这里有一行被注释了代码是加载`Retinafae`模型,根据你下载的模型选择使用哪一行代码
detector = nn.YOLOv8(model="/root/models/yolov8n_face.mud")

> 本功能需要 MaixPy >= 4.1.4 才能使用
cam = camera.Camera(detector.input_width(), detector.input_height(), detector.input_format())
dis = display.Display()

while not app.need_exit():
img = cam.read()
objs = detector.detect(img, conf_th = 0.5, iou_th = 0.45, keypoint_th = 0.5)
for obj in objs:
img.draw_rect(obj.x, obj.y, obj.w, obj.h, color = image.COLOR_RED)
msg = f'{detector.labels[obj.class_id]}: {obj.score:.2f}'
img.draw_string(obj.x, obj.y, msg, color = image.COLOR_RED)
detector.draw_pose(img, obj.points, 2, image.COLOR_RED)
dis.show(img)
```

另外两种模型使用方法:
这里有一行被注释了代码是加载`Retinafae`模型,根据你下载的模型选择使用哪一行代码

```python
from maix import camera, display, image, nn, app
Expand All @@ -49,6 +63,13 @@ while not app.need_exit():

```

## 模型下载和其它分辨率模型

下载模型,下载的压缩包里面有多个分辨率可以选择,分辨率越高越精准但耗时更长:
* [face detector 1MB with landmark](https://maixhub.com/model/zoo/377)
* [Retinafate](https://maixhub.com/model/zoo/378)
* [YOLOv8-face](https://maixhub.com/model/zoo/407)




16 changes: 16 additions & 0 deletions examples/vision/ai_vision/nn_yolov8_face.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from maix import camera, display, image, nn, app

detector = nn.YOLOv8(model="/root/models/yolov8n_face.mud")

cam = camera.Camera(detector.input_width(), detector.input_height(), detector.input_format())
dis = display.Display()

while not app.need_exit():
img = cam.read()
objs = detector.detect(img, conf_th = 0.5, iou_th = 0.45, keypoint_th = 0.5)
for obj in objs:
img.draw_rect(obj.x, obj.y, obj.w, obj.h, color = image.COLOR_RED)
msg = f'{detector.labels[obj.class_id]}: {obj.score:.2f}'
img.draw_string(obj.x, obj.y, msg, color = image.COLOR_RED)
detector.draw_pose(img, obj.points, 2, image.COLOR_RED)
dis.show(img)

0 comments on commit 1a01ddf

Please sign in to comment.