Skip to content

Commit 232350d

Browse files
committed
update 126_130 Face&Pose&YOLO
1 parent 6367c91 commit 232350d

35 files changed

+1136
-2
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,9 @@ code_121 | [DNN- Read the information of each layer of the model](python/code_12
163163
code_122 | [DNN- Realize image classification](python/code_122) | ✔️
164164
code_123 | [DNN- Model runs to set the target device and compute the background](python/code_123) | ✔️
165165
code_124 | [DNN- SSD Single Image Detection](python/code_124) | ✔️
166-
code_125 | [DNN- SSD Real-time Video Detection](python/code_125) | ✔️
166+
code_125 | [DNN- SSD Real-time Video Detection](python/code_125) | ✔️
167+
code_126 | [DNN- Face Detection based on Residual Network](python/code_126) | ✔️
168+
code_127 | [DNN- Video Face Detection based on Residual Network](python/code_127) | ✔️
169+
code_128 | [DNN- Call the Detection Model of Tensorflow](python/code_128) | ✔️
170+
code_129 | [DNN- Call the Openpose Implementation Attitude Assessment](python/code_129) | ✔️
171+
code_130 | [DNN- Call YOLO Object Detection Network](python/code_130) | ✔️

README_CN.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,9 @@ code_121 | [DNN- 读取模型各层信息](python/code_121) | ✔️
162162
code_122 | [DNN- DNN实现图像分类](python/code_122) | ✔️
163163
code_123 | [DNN- 模型运行设置目标设备与计算后台](python/code_123) | ✔️
164164
code_124 | [DNN- SSD单张图片检测](python/code_124) | ✔️
165-
code_125 | [DNN- SSD实时视频检测](python/code_125) | ✔️
165+
code_125 | [DNN- SSD实时视频检测](python/code_125) | ✔️
166+
code_126 | [DNN- 基于残差网络的人脸检测](python/code_126) | ✔️
167+
code_127 | [DNN- 基于残差网络的视频人脸检测](python/code_127) | ✔️
168+
code_128 | [DNN- 调用tensorflow的检测模型](python/code_128) | ✔️
169+
code_129 | [DNN- 调用openpose模型实现姿态评估](python/code_129) | ✔️
170+
code_130 | [DNN- 调用YOLO对象检测网络](python/code_130) | ✔️

python/code_126/README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
## OpenCV 基于残差网络的人脸检测
2+
3+
### 概述
4+
5+
✔️ ️OpenCV在DNN模块中提供了基于残差SSD网络训练的人脸检测模型,该模型分别提供了tensorflow版本,caffe版本,torch版本模型文件。
6+
7+
✔️ 其中tensorflow版本的模型做了更加进一步的压缩优化,大小只有2MB左右,非常适合移植到移动端使用,实现人脸检测功能,而caffe版本的是fp16的浮点数模型,精准度更好。
8+
9+
✔️ 对比传统人脸检测,同样一张图像,在OpenCV HAAR与LBP级联检测器中必须通过不断调整参数才可以检测出全部人脸,而通过使用该模型,基本在Python语言中基于OpenCV后台的推断,在25毫秒均可以检测出结果,网络支持输入size大小为300x300。
10+
11+
### 代码
12+
```python
13+
import cv2
14+
15+
model_bin = "../model/face_detector/opencv_face_detector_uint8.pb";
16+
config_text = "../model/face_detector/opencv_face_detector.pbtxt";
17+
18+
# load tensorflow model
19+
net = cv2.dnn.readNetFromTensorflow(model_bin, config=config_text)
20+
image = cv2.imread("people.jpg")
21+
h = image.shape[0]
22+
w = image.shape[1]
23+
24+
# 人脸检测
25+
blobImage = cv2.dnn.blobFromImage(image, 1.0, (300, 300), (104.0, 177.0, 123.0), False, False);
26+
net.setInput(blobImage)
27+
Out = net.forward()
28+
29+
t, _ = net.getPerfProfile()
30+
label = 'Inference time: %.2f ms' % (t * 1000.0 / cv2.getTickFrequency())
31+
cv2.putText(image, label, (0, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
32+
33+
# 绘制检测矩形
34+
for detection in Out[0,0,:,:]:
35+
score = float(detection[2])
36+
objIndex = int(detection[1])
37+
if score > 0.5:
38+
left = detection[3]*w
39+
top = detection[4]*h
40+
right = detection[5]*w
41+
bottom = detection[6]*h
42+
43+
# 绘制
44+
cv2.rectangle(image, (int(left), int(top)), (int(right), int(bottom)), (255, 0, 0), thickness=2)
45+
cv2.putText(image, "score:%.2f"%score, (int(left), int(top)-1), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1)
46+
47+
cv2.imshow('demo', image)
48+
```
49+
>输出
50+
51+
<img src="./result.jpg">

python/code_126/opencv_126.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import cv2
2+
3+
model_bin = "../model/face_detector/opencv_face_detector_uint8.pb";
4+
config_text = "../model/face_detector/opencv_face_detector.pbtxt";
5+
6+
# load tensorflow model
7+
net = cv2.dnn.readNetFromTensorflow(model_bin, config=config_text)
8+
image = cv2.imread("face.jpg")
9+
h = image.shape[0]
10+
w = image.shape[1]
11+
12+
# 人脸检测
13+
blobImage = cv2.dnn.blobFromImage(image, 1.0, (300, 300), (104.0, 177.0, 123.0), False, False);
14+
net.setInput(blobImage)
15+
Out = net.forward()
16+
17+
t, _ = net.getPerfProfile()
18+
label = 'Inference time: %.2f ms' % (t * 1000.0 / cv2.getTickFrequency())
19+
cv2.putText(image, label, (0, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
20+
21+
# 绘制检测矩形
22+
for detection in Out[0,0,:,:]:
23+
score = float(detection[2])
24+
objIndex = int(detection[1])
25+
if score > 0.5:
26+
left = detection[3]*w
27+
top = detection[4]*h
28+
right = detection[5]*w
29+
bottom = detection[6]*h
30+
31+
# 绘制
32+
cv2.rectangle(image, (int(left), int(top)), (int(right), int(bottom)), (255, 0, 0), thickness=2)
33+
cv2.putText(image, "score:%.2f"%score, (int(left), int(top)-1), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1)
34+
35+
cv2.imshow('face-detection-demo', image)
36+
cv2.waitKey(0)
37+
cv2.destroyAllWindows()

python/code_126/people.jpg

32.3 KB
Loading

python/code_126/result.jpg

57.1 KB
Loading

python/code_127/README.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
## OpenCV 基于残差网络的视频人脸检测
2+
3+
### 概述
4+
5+
✔️ OpenCV在DNN模块中提供了基于残差SSD网络训练的人脸检测模型,它支持单精度的fp16的检测,准确度更好的Caffe模型加载与使用.
6+
7+
8+
### 代码
9+
10+
✔️ 这里实现了一个基于Caffe Model的视频实时人脸监测模型,基于Python, 在CPU运行,可以达到fps16以上。
11+
12+
```python
13+
import cv2
14+
15+
model_bin = "../model/face_detector/res10_300x300_ssd_iter_140000_fp16.caffemodel";
16+
config_text = "../model/face_detector/deploy.prototxt";
17+
18+
# load caffe model
19+
net = cv2.dnn.readNetFromCaffe(config_text, model_bin)
20+
21+
# set back-end
22+
net.setPreferableBackend(cv2.dnn.DNN_BACKEND_OPENCV)
23+
net.setPreferableTarget(cv2.dnn.DNN_TARGET_CPU)
24+
25+
cap = cv2.VideoCapture(0)
26+
while True:
27+
ret, image = cap.read()
28+
image = cv2.flip(image, 1)
29+
if ret is False:
30+
break
31+
# 人脸检测
32+
h, w = image.shape[:2]
33+
blobImage = cv2.dnn.blobFromImage(image, 1.0, (300, 300), (104.0, 177.0, 123.0), False, False);
34+
net.setInput(blobImage)
35+
cvOut = net.forward()
36+
37+
# Put efficiency information.
38+
t, _ = net.getPerfProfile()
39+
fps = 1000 / (t * 1000.0 / cv2.getTickFrequency())
40+
label = 'FPS: %.2f' % fps
41+
cv2.putText(image, label, (0, 15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0))
42+
43+
# 绘制检测矩形
44+
for detection in cvOut[0,0,:,:]:
45+
score = float(detection[2])
46+
objIndex = int(detection[1])
47+
if score > 0.5:
48+
left = detection[3]*w
49+
top = detection[4]*h
50+
right = detection[5]*w
51+
bottom = detection[6]*h
52+
53+
# 绘制
54+
cv2.rectangle(image, (int(left), int(top)), (int(right), int(bottom)), (255, 0, 0), thickness=2)
55+
cv2.putText(image, "score:%.2f"%score, (int(left), int(top)), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1)
56+
cv2.imshow('face-detection-demo', image)
57+
c = cv2.waitKey(2)
58+
if c == 27:
59+
break
60+
cv2.waitKey(0)
61+
cv2.destroyAllWindows()
62+
```
63+
64+
<img src="./song.gif">

python/code_127/opencv_127.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import cv2
2+
3+
model_bin = "../model/face_detector/res10_300x300_ssd_iter_140000_fp16.caffemodel";
4+
config_text = "../model/face_detector/deploy.prototxt";
5+
6+
# load caffe model
7+
net = cv2.dnn.readNetFromCaffe(config_text, model_bin)
8+
9+
# set back-end
10+
net.setPreferableBackend(cv2.dnn.DNN_BACKEND_OPENCV)
11+
net.setPreferableTarget(cv2.dnn.DNN_TARGET_CPU)
12+
13+
cap = cv2.VideoCapture('song.mp4')
14+
while True:
15+
ret, image = cap.read()
16+
image = cv2.flip(image, 1)
17+
if ret is False:
18+
break
19+
# 人脸检测
20+
h, w = image.shape[:2]
21+
blobImage = cv2.dnn.blobFromImage(image, 1.0, (300, 300), (104.0, 177.0, 123.0), False, False);
22+
net.setInput(blobImage)
23+
cvOut = net.forward()
24+
25+
# Put efficiency information.
26+
t, _ = net.getPerfProfile()
27+
fps = 1000 / (t * 1000.0 / cv2.getTickFrequency())
28+
label = 'FPS: %.2f' % fps
29+
cv2.putText(image, label, (0, 15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0))
30+
31+
# 绘制检测矩形
32+
for detection in cvOut[0,0,:,:]:
33+
score = float(detection[2])
34+
objIndex = int(detection[1])
35+
if score > 0.5:
36+
left = detection[3]*w
37+
top = detection[4]*h
38+
right = detection[5]*w
39+
bottom = detection[6]*h
40+
41+
# 绘制
42+
cv2.rectangle(image, (int(left), int(top)), (int(right), int(bottom)), (255, 0, 0), thickness=2)
43+
cv2.putText(image, "score:%.2f"%score, (int(left), int(top)), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1)
44+
cv2.imshow('face-detection-demo', image)
45+
c = cv2.waitKey(2)
46+
if c == 27:
47+
break
48+
cv2.waitKey(0)
49+
cv2.destroyAllWindows()

python/code_127/song.gif

2.73 MB
Loading

python/code_127/song.mp4

393 KB
Binary file not shown.

0 commit comments

Comments
 (0)