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

+6-1
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

+6-1
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

+51
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

+37
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

+64
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

+49
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.

python/code_128/README.md

+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
## OpenCV 调用tensorflow
2+
3+
### 概述
4+
5+
✔️OpenCV在DNN模块中支持直接调用tensorflow object detection训练导出的模型使用,支持的模型包括
6+
- SSD
7+
- Faster-RCNN
8+
- Mask-RCNN
9+
10+
✔️ 利用这三种经典的对象检测网络,这样就可以实现从tensorflow模型训练、导出模型、在OpenCV DNN调用模型网络实现自定义对象检测的技术。
11+
12+
✔️ OpenCV3.4.1以上版本支持tensorflow1.11版本以上的对象检测框架(object detetion)模型导出使用,当前支持的模型包括以下:
13+
14+
Model | Version | -| -
15+
---|---|---|---
16+
MobileNet-SSD v1|2017_11_17| [weights](http://download.tensorflow.org/models/object_detection/ssd_mobilenet_v1_coco_2017_11_17.tar.gz)| [config](https://github.com/opencv/opencv_extra/blob/master/testdata/dnn/ssd_mobilenet_v1_coco_2017_11_17.pbtxt)|
17+
MobileNet-SSD v1 PPN| 2018_07_03| [weights](http://download.tensorflow.org/models/object_detection/ssd_mobilenet_v1_ppn_shared_box_predictor_300x300_coco14_sync_2018_07_03.tar.gz)| [config](https://github.com/opencv/opencv_extra/blob/master/testdata/dnn/ssd_mobilenet_v1_ppn_coco.pbtxt)|
18+
MobileNet-SSD v2| 2018_03_29| [weights](http://download.tensorflow.org/models/object_detection/ssd_mobilenet_v2_coco_2018_03_29.tar.gz)| [config](https://github.com/opencv/opencv_extra/blob/master/testdata/dnn/ssd_mobilenet_v2_coco_2018_03_29.pbtxt)|
19+
Inception-SSD v2| 2017_11_17| [weights](http://download.tensorflow.org/models/object_detection/ssd_inception_v2_coco_2017_11_17.tar.gz)| [config](https://github.com/opencv/opencv_extra/blob/master/testdata/dnn/ssd_inception_v2_coco_2017_11_17.pbtxt)|
20+
Faster-RCNN Inception v2| 2018_01_28| [weights](http://download.tensorflow.org/models/object_detection/faster_rcnn_inception_v2_coco_2018_01_28.tar.gz)| [config](https://github.com/opencv/opencv_extra/blob/master/testdata/dnn/faster_rcnn_inception_v2_coco_2018_01_28.pbtxt)|
21+
Faster-RCNN ResNet-50| 2018_01_28| [weights](http://download.tensorflow.org/models/object_detection/faster_rcnn_resnet50_coco_2018_01_28.tar.gz)| [config](https://github.com/opencv/opencv_extra/blob/master/testdata/dnn/faster_rcnn_resnet50_coco_2018_01_28.pbtxt)|
22+
Mask-RCNN Inception v2| 2018_01_28| [weights](http://download.tensorflow.org/models/object_detection/mask_rcnn_inception_v2_coco_2018_01_28.tar.gz)| [config](https://github.com/opencv/opencv_extra/blob/master/testdata/dnn/mask_rcnn_inception_v2_coco_2018_01_28.pbtxt)|
23+
24+
✏️ 使用tensorflow object detection API框架进行迁移学习训练模型,导出预测图之后,然后通过OpenCV3.4.1以上版本提供几个python脚本导出graph配置文件,这样就可以在OpenCV DNN模块中使用tensorflow相关的模型了。
25+
26+
### 使用tensorflow
27+
28+
✔️使用tensorflow预测:
29+
```
30+
import tensorflow as tf
31+
import cv2
32+
33+
# Read the graph.
34+
model_dir = '../faster_rcnn_resnet50_coco_2018_01_28/frozen_inference_graph.pb'
35+
with tf.gfile.FastGFile(model_dir, 'rb') as f:
36+
graph_def = tf.GraphDef()
37+
graph_def.ParseFromString(f.read())
38+
39+
with tf.Session() as sess:
40+
# Restore session
41+
sess.graph.as_default()
42+
tf.import_graph_def(graph_def, name='')
43+
44+
# Read and preprocess an image.
45+
img = cv2.imread('cat.jpg')
46+
rows = img.shape[0]
47+
cols = img.shape[1]
48+
inp = cv2.resize(img, (300, 300))
49+
inp = inp[:, :, [2, 1, 0]] # BGR2RGB
50+
51+
# Run the model
52+
out = sess.run([sess.graph.get_tensor_by_name('num_detections:0'),
53+
sess.graph.get_tensor_by_name('detection_scores:0'),
54+
sess.graph.get_tensor_by_name('detection_boxes:0'),
55+
sess.graph.get_tensor_by_name('detection_classes:0')],
56+
feed_dict={'image_tensor:0': inp.reshape(1, inp.shape[0], inp.shape[1], 3)})
57+
58+
# Visualize detected bounding boxes.
59+
num_detections = int(out[0][0])
60+
for i in range(num_detections):
61+
classId = int(out[3][0][i])
62+
score = float(out[1][0][i])
63+
bbox = [float(v) for v in out[2][0][i]]
64+
if score > 0.8:
65+
x = bbox[1] * cols
66+
y = bbox[0] * rows
67+
right = bbox[3] * cols
68+
bottom = bbox[2] * rows
69+
cv2.rectangle(img, (int(x), int(y)), (int(right), int(bottom)), (125, 255, 51), thickness=2)
70+
```
71+
<img src="./cat.jpg">
72+
<img src="./cat_tensor.jpg">
73+
74+
### 调用tensorflow
75+
76+
✔️根据tensorflow中迁移学习或者下载预训练模型不同,OpenCV DNN 模块提供如下可以使用脚本生成对应的模型配置文件
77+
78+
```
79+
tf_text_graph_ssd.py
80+
81+
tf_text_graph_faster_rcnn.py
82+
83+
tf_text_graph_mask_rcnn.py
84+
```
85+
✔️这是因为OpenCV DNN需要根据text版本的模型描述文件来解析tensorflow的pb文件,实现网络模型加载。
86+
87+
✔️对检测模型,生成模型描述文件运行以下命令行:
88+
```
89+
python tf_text_graph_ssd.py
90+
91+
--input /path/to/model.pb
92+
93+
--config /path/to/example.config
94+
95+
--output /path/to/graph.pbtxt
96+
```
97+
98+
✔️采用faster_res50目标检测模型生成pbtxt的输出结果:
99+
100+
```
101+
python tf_text_graph_faster_rcnn.py \
102+
--input faster_rcnn_resnet50_coco_2018_01_28/frozen_inference_graph.pb \
103+
--output faster_rcnn_resnet50_coco_2018_01_28/graph.pbtxt \
104+
--config faster_rcnn_resnet50_coco_2018_01_28/pipeline.config
105+
106+
Number of classes: 90
107+
Scales: [0.25, 0.5, 1.0, 2.0]
108+
Aspect ratios: [0.5, 1.0, 2.0]
109+
Width stride: 16.000000
110+
Height stride: 16.000000
111+
Features stride: 16.000000
112+
```
113+
✔️opencv调用tensorflow预测目标:
114+
```python
115+
import cv2
116+
117+
inference_pb = "../faster_rcnn_resnet50_coco_2018_01_28/frozen_inference_graph.pb";
118+
graph_text = "../faster_rcnn_resnet50_coco_2018_01_28/graph.pbtxt";
119+
120+
# load tensorflow model
121+
net = cv2.dnn.readNetFromTensorflow(inference_pb, graph_text)
122+
image = cv2.imread("cat.jpg")
123+
h = image.shape[0]
124+
w = image.shape[1]
125+
126+
# 检测
127+
net.setInput(cv2.dnn.blobFromImage(image, size=(300, 300), swapRB=True, crop=False))
128+
cvOut = net.forward()
129+
for detection in cvOut[0,0,:,:]:
130+
score = float(detection[2])
131+
if score > 0.5:
132+
left = detection[3]*w
133+
top = detection[4]*h
134+
right = detection[5]*w
135+
bottom = detection[6]*h
136+
137+
# 绘制
138+
cv2.rectangle(image, (int(left), int(top)), (int(right), int(bottom)), (0, 255, 0), thickness=2)
139+
cv2.putText(image, "score:%.2f"%score, (int(left), int(top)-2), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 1)
140+
```
141+
<img src="./result_cat.jpg">

python/code_128/cat.jpg

101 KB
Loading

python/code_128/cat_tensor.jpg

154 KB
Loading

0 commit comments

Comments
 (0)