Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleHub
提交
3e780056
P
PaddleHub
项目概览
PaddlePaddle
/
PaddleHub
大约 1 年 前同步成功
通知
281
Star
12117
Fork
2091
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
200
列表
看板
标记
里程碑
合并请求
4
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleHub
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
200
Issue
200
列表
看板
标记
里程碑
合并请求
4
合并请求
4
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
3e780056
编写于
2月 25, 2020
作者:
Z
Zeyu Chen
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update document and docs
上级
fe4a24a8
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
131 addition
and
11 deletion
+131
-11
demo/mask_detection/README.md
demo/mask_detection/README.md
+14
-11
demo/mask_detection/mask_detection.py
demo/mask_detection/mask_detection.py
+117
-0
未找到文件。
demo/mask_detection/README.md
浏览文件 @
3e780056
...
...
@@ -38,11 +38,18 @@ module = hub.Module(name="pyramidbox_lite_mobile_mask") #口罩检测模型
>以上语句paddlehub会自动下载口罩检测模型 "pyramidbox_lite_mobile_mask" 不需要提前下载模型
#### OpenCV打开摄像头或视频文件
下载测试视频
```
wget https://paddlehub.bj.bcebos.com/mask_detection/test_video.mp4
```
```
python
import
cv2
capture
=
cv2
.
VideoCapture
(
0
)
# 打开摄像头
# capture = cv2.VideoCapture('./
2
.mp4') # 打开视频文件
# capture = cv2.VideoCapture('./
test_video
.mp4') # 打开视频文件
while
(
1
):
ret
,
frame
=
capture
.
read
()
# frame即视频的一帧数据
if
ret
==
False
:
...
...
@@ -109,24 +116,22 @@ for result in results:
需要事先准备ttf/otf等格式的字体文件
```
python
def
paint_chinese_opencv
(
im
,
chinese
,
position
,
fontsize
,
color_bgr
):
#opencv输出中文
img_PIL
=
Image
.
fromarray
(
cv2
.
cvtColor
(
im
,
cv2
.
COLOR_BGR2RGB
))
# 图像从OpenCV格式转换成PIL格式
def
paint_chinese_opencv
(
im
,
chinese
,
position
,
fontsize
,
color_bgr
):
img_PIL
=
Image
.
fromarray
(
cv2
.
cvtColor
(
im
,
cv2
.
COLOR_BGR2RGB
))
# 图像从OpenCV格式转换成PIL格式
font
=
ImageFont
.
truetype
(
'思源黑体SC-Heavy.otf'
,
fontsize
,
encoding
=
"utf-8"
)
# 加载字体文件
#color = (255,0,0) # 字体颜色
#position = (100,100)# 文字输出位置
#position = (100,100)
# 文字输出位置
color
=
color_bgr
[::
-
1
]
draw
=
ImageDraw
.
Draw
(
img_PIL
)
draw
.
text
(
position
,
chinese
,
font
=
font
,
fill
=
color
)
# PIL图片上打印汉字 # 参数1:打印坐标,参数2:文本,参数3:字体颜色,参数4:字体
img
=
cv2
.
cvtColor
(
np
.
asarray
(
img_PIL
),
cv2
.
COLOR_RGB2BGR
)
# PIL图片转cv2 图片
# PIL图片上打印汉字 # 参数1:打印坐标,参数2:文本,参数3:字体颜色,参数4:字体
draw
.
text
(
position
,
chinese
,
font
=
font
,
fill
=
color
)
img
=
cv2
.
cvtColor
(
np
.
asarray
(
img_PIL
),
cv2
.
COLOR_RGB2BGR
)
# PIL图片转cv2图片
return
img
```
```
python
for
result
in
results
:
# print(result)
label
=
result
[
'data'
][
'label'
]
confidence
=
result
[
'data'
][
'confidence'
]
top
,
right
,
bottom
,
left
=
int
(
result
[
'data'
][
'top'
]),
int
(
result
[
'data'
][
'right'
]),
int
(
result
[
'data'
][
'bottom'
]),
int
(
result
[
'data'
][
'left'
])
color
=
(
0
,
255
,
0
)
...
...
@@ -183,12 +188,10 @@ with open("./result/2-mask_detection.json","w") as f:
更多信息可以参考
[
文档
](
./python/README.md
)
## 3. 高性能C++部署方案
更多信息可以参考
[
文档
](
./cpp/README.md
)
## 欢迎交流
**百度飞桨合作伙伴:**
...
...
demo/mask_detection/mask_detection.py
0 → 100644
浏览文件 @
3e780056
# -*- coding:utf-8 -*-
import
paddlehub
as
hub
import
cv2
from
PIL
import
Image
,
ImageDraw
,
ImageFont
import
numpy
as
np
import
json
import
os
module
=
hub
.
Module
(
name
=
"pyramidbox_lite_mobile_mask"
)
# opencv输出中文
def
paint_chinese
(
im
,
chinese
,
position
,
fontsize
,
color_bgr
):
# 图像从OpenCV格式转换成PIL格式
img_PIL
=
Image
.
fromarray
(
cv2
.
cvtColor
(
im
,
cv2
.
COLOR_BGR2RGB
))
font
=
ImageFont
.
truetype
(
'SourceHanSansSC-Medium.otf'
,
fontsize
,
encoding
=
"utf-8"
)
#color = (255,0,0) # 字体颜色
#position = (100,100)# 文字输出位置
color
=
color_bgr
[::
-
1
]
draw
=
ImageDraw
.
Draw
(
img_PIL
)
# PIL图片上打印汉字 # 参数1:打印坐标,参数2:文本,参数3:字体颜色,参数4:字体
draw
.
text
(
position
,
chinese
,
font
=
font
,
fill
=
color
)
img
=
cv2
.
cvtColor
(
np
.
asarray
(
img_PIL
),
cv2
.
COLOR_RGB2BGR
)
# PIL图片转cv2 图片
return
img
result_path
=
'./result'
if
not
os
.
path
.
exists
(
result_path
):
os
.
mkdir
(
result_path
)
name
=
"./result/1-mask_detection.mp4"
width
=
1920
height
=
1080
fps
=
30
fourcc
=
cv2
.
VideoWriter_fourcc
(
*
'mp4v'
)
writer
=
cv2
.
VideoWriter
(
name
,
fourcc
,
fps
,
(
width
,
height
))
maskIndex
=
0
index
=
0
data
=
[]
capture
=
cv2
.
VideoCapture
(
0
)
# 打开摄像头
# capture = cv2.VideoCapture('./test_video.mp4') # 打开视频文件
while
True
:
frameData
=
{}
ret
,
frame
=
capture
.
read
()
# frame即视频的一帧数据
if
ret
==
False
:
break
frame_copy
=
frame
.
copy
()
input_dict
=
{
"data"
:
[
frame
]}
results
=
module
.
face_detection
(
data
=
input_dict
)
maskFrameDatas
=
[]
for
result
in
results
:
label
=
result
[
'data'
][
'label'
]
confidence_origin
=
result
[
'data'
][
'confidence'
]
confidence
=
round
(
confidence_origin
,
2
)
confidence_desc
=
str
(
confidence
)
top
,
right
,
bottom
,
left
=
int
(
result
[
'data'
][
'top'
]),
int
(
result
[
'data'
][
'right'
]),
int
(
result
[
'data'
][
'bottom'
]),
int
(
result
[
'data'
][
'left'
])
#将当前帧保存为图片
img_name
=
"avatar_%d.png"
%
(
maskIndex
)
path
=
"./result/"
+
img_name
image
=
frame
[
top
-
10
:
bottom
+
10
,
left
-
10
:
right
+
10
]
cv2
.
imwrite
(
path
,
image
,
[
int
(
cv2
.
IMWRITE_PNG_COMPRESSION
),
9
])
maskFrameData
=
{}
maskFrameData
[
'top'
]
=
top
maskFrameData
[
'right'
]
=
right
maskFrameData
[
'bottom'
]
=
bottom
maskFrameData
[
'left'
]
=
left
maskFrameData
[
'confidence'
]
=
float
(
confidence_origin
)
maskFrameData
[
'label'
]
=
label
maskFrameData
[
'img'
]
=
img_name
maskFrameDatas
.
append
(
maskFrameData
)
maskIndex
+=
1
color
=
(
0
,
255
,
0
)
label_cn
=
"有口罩"
if
label
==
'NO MASK'
:
color
=
(
0
,
0
,
255
)
label_cn
=
"无口罩"
cv2
.
rectangle
(
frame_copy
,
(
left
,
top
),
(
right
,
bottom
),
color
,
3
)
# cv2.putText(frame, label, (left, top-10), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color, 2)
origin_point
=
(
left
,
top
-
36
)
frame_copy
=
paint_chinese
(
frame_copy
,
label_cn
,
origin_point
,
24
,
color
)
writer
.
write
(
frame_copy
)
cv2
.
imshow
(
'Mask Detection'
,
frame_copy
)
frameData
[
'frame'
]
=
index
# frameData['seconds'] = int(index/fps)
frameData
[
'data'
]
=
maskFrameDatas
data
.
append
(
frameData
)
print
(
json
.
dumps
(
frameData
))
index
+=
1
if
cv2
.
waitKey
(
1
)
&
0xFF
==
ord
(
'q'
):
break
with
open
(
"./result/2-mask_detection.json"
,
"w"
)
as
f
:
json
.
dump
(
data
,
f
)
writer
.
release
()
cv2
.
destroyAllWindows
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录