PaddleHub體驗口罩識別

PaddleHub 口罩檢測示例

防控疫情,衆志成城。人工智能技術正被應用到疫情防控中來。
工程資源
工程md文件
百度積極響應號召,爲了助推全社會的力量將AI技術應用於防疫工作,決定免費開源自研的“口罩人臉識別”預訓練模型,該模型基於2018年百度在國際頂級計算機視覺會議ECCV 2018的論文PyramidBox而研發,可以在公共場景檢測大量的人臉同時,將佩戴口罩和未佩戴口罩的人臉快速識別標註。基於此預訓練模型,開發者僅需使用少量自有數據,便可快速完成自有場景模型開發。

飛槳預訓練模型管理與遷移學習工具PadddleHub已提供PyramidBox預訓練模型(pyramidbox_lite_mobile_mask/pyramidbox_lite_server_mask)用於一鍵檢測人們是否佩戴口罩。同時PaddleHub還提供了飛槳生態下的高質量預訓練模型,涵蓋了圖像分類、目標檢測、詞法分析、語義模型、情感分析、視頻分類、圖像生成、圖像分割、文本審覈、關鍵點檢測等主流模型。更多模型詳情請查看官網:https://www.paddlepaddle.org.cn/hub 和 PaddleHub repo:https://github.com/PaddlePaddle/PaddleHub

本示例利用目標檢測輕量化模型pyramidbox_lite_mobile_mask完成佩戴口罩檢測。

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import zipfile

1.解壓圖片

zip_file = zipfile.ZipFile('detection.zip')
zip_list = zip_file.namelist()
for f in zip_list:
    zip_file.extract(f, r'./data')
    # print(f)

2.待預測圖片

test_img_path = ["./data/detection/test_mask_detection.jpg"]
img = mpimg.imread(test_img_path[0])

3.展示待預測圖片

plt.figure(figsize=(10, 10))
plt.imshow(img)
plt.axis('off')
plt.show()

4.顯示內容

with open('./data/detection/test.txt', 'r') as f:
    test_img_path = []
    for line in f:
        test_img_path.append(line.strip())

加載預訓練模型

PaddleHub口罩檢測提供了兩種預訓練模型,pyramidbox_lite_mobile_mask和

pyramidbox_lite_server_mask。不同點在於,pyramidbox_lite_mobile_mask

是針對於移動端優化過的模型,適合部署於移動端或者邊緣檢測等算力受限的設備上。

import paddlehub as hub
import os

口罩檢測

module = hub.Module(name="pyramidbox_lite_server_mask")
test_img_path[0] = test_img_path[0].strip('.').strip('/')
test_img_path[0] = os.path.join('./data/detection/', test_img_path[0])
print(50 * '*')
print('test_img_path: %s', test_img_path[0])
print(50 * '*')

PaddleHub對於支持一鍵預測的module,可以調用module的相應預測API,完成預測功能。

input_dict = {"image": test_img_path}
print("input_dict %s", input_dict)
print(50 * '*')
results = module.face_detection(data=input_dict)
for result in results:
    print(result)

預測結果

img = mpimg.imread("./detection_result/test_mask_detection.jpg")
plt.figure(figsize=(10, 10))
plt.imshow(img)
plt.axis('off')
plt.show()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章