Try   HackMD

Facemask_Detection人臉口罩偵測(python)

Facemask detection 模組 介紹

  1. 模組功能: 偵測圖片中人臉的表情
  2. 安裝參考: https://pypi.org/project/facemask-detection/
  3. 安裝指令: !pip install Facemask detection
  4. 官方網站: https://github.com/ternaus/retinaface
  5. 注意事項:
  • 必須在GPU下執行
!pip install facemask_detection

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  1. 從 facemask_detection.pre_trained_models 匯入 get_model 模組 來取得 模型
  • 使用預先已經訓練的 model 來取得分類器
  • 以 get_classifer
from facemask_detection.pre_trained_models import get_model as get_classifer
  1. 必須匯入以下的模組

    import albumentation as A

    import torch

    import cv2

    import numpy
# albumentations 負責處理圖片增強的一個模組 import albumentations as A # 負責 針對 tenosr 運算的模組 import torch import cv2 import numpy as np
  1. 載入 已經訓練好的 acemask_detection model
  • 模組名稱: tf_efficientnet_b0_ns_2020-07-29
  • 語法:
    model變數 = get_model("tf_efficientnet_b0_ns_2020-07-29")

    model變數.eval()
model = get_classifer("tf_efficientnet_b0_ns_2020-07-29") model.eval()
  1. 以 openCV 讀取要偵測的圖片 並轉為 RGB 格式

    圖片變數 = cv2.cvtColor(cv2.imread(圖片路徑),cv2.COLOR_BGR2RGB)
img1 = cv2.cvtColor(cv2.imread("angry1.jpg"), cv2.COLOR_BGR2RGB) img2 = cv2.cvtColor(cv2.imread("mask1.jpg"), cv2.COLOR_BGR2RGB)
  1. 將圖片變數轉換成 facemask-detection 模組可以辨識的格式
transform = A.Compose( [ A.SmallestMaxSize(max_size=256, p=1), A.CenterCrop(height=244, width=244, p=1), A.Normalize(p=1) ] ) trans_image = transform(image=img1)["image"] input = torch.from_numpy( np.transpose(trans_image, (2, 0, 1)) ).unsqueeze(0)
  1. 將轉換後的圖片輸入資料(張量: tensor) 傳給 facemask-detection的 model來偵測
model(input)[0].item()

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

偵測圖片中的人臉是否有戴口罩完整程式碼

#### 匯入所需的所有模組 #### from facemask_detection.pre_trained_models import get_model as get_classifer # albumentations 負責處理圖片增強的一個模組 import albumentations as A # 負責 針對 tenosr 運算的模組 import torch import cv2 import numpy as np #### 載入 facemask_detection 的辨識 model ##### model = get_classifer("tf_efficientnet_b0_ns_2020-07-29") model.eval() #### 設計圖片轉換格式的轉換樣式 #### transform = A.Compose( [ A.SmallestMaxSize(max_size=256, p=1), A.CenterCrop(height=244, width=244, p=1), A.Normalize(p=1) ] ) #### 讀取圖片並轉換成的轉換樣式並以 tensor 作為辨識輸入的資料 #### # 讀取圖片並轉為 RGB 格式 image = cv2.imread("mask1.jpg") image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # 利用 transform 轉換成所需的格式 trans_image = transform(image=image)["image"] #取得image欄位的資料(numpy陣列資料) # 利用 torch 將 numpy 陣列資料作為輸入辨識的 tensor input = torch.from_numpy( np.transpose(trans_image, (2, 0, 1)) ).unsqueeze(0) # 使用 model 進行偵測取得辨識結果 score = model(input)[0].item() print(f"有戴口罩的機率是:{score}") from google.colab.patches import cv2_imshow image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) cv2_imshow(image)

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

#### 匯入所需的所有模組 #### from facemask_detection.pre_trained_models import get_model as get_classifer # albumentations 負責處理圖片增強的一個模組 import albumentations as A # 負責 針對 tenosr 運算的模組 import torch import cv2 import numpy as np #### 載入 facemask_detection 的辨識 model ##### model = get_classifer("tf_efficientnet_b0_ns_2020-07-29") model.eval() #### 設計圖片轉換格式的轉換樣式 #### transform = A.Compose( [ A.SmallestMaxSize(max_size=256, p=1), A.CenterCrop(height=244, width=244, p=1), A.Normalize(p=1) ] ) #### 讀取圖片並轉換成的轉換樣式並以 tensor 作為辨識輸入的資料 #### # 讀取圖片並轉為 RGB 格式 IMG_FILES = ["mask1.jpg", "mask2.jpg", "mask3.jpg"] for idx, file in enumerate(IMG_FILES): image = cv2.imread(file) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # 利用 transform 轉換成所需的格式 trans_image = transform(image=image)["image"] #取得image欄位的資料(numpy陣列資料) # 利用 torch 將 numpy 陣列資料作為輸入辨識的 tensor input = torch.from_numpy( np.transpose(trans_image, (2, 0, 1)) ).unsqueeze(0) # 使用 model 進行偵測取得辨識結果 score = model(input)[0].item() print(f"有戴口罩的機率是:{score}") from google.colab.patches import cv2_imshow image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) cv2_imshow(image)



import math # 匯入數學函式庫 import cv2 # 匯入openCV FH, FW = 480, 480 def resizeImgAndShow(img): h, w = img.shape[ : 2] if h < w: img = cv2.resize(img, (FW, math.floor(h/(w/FW)))) else: cv2.resize(img, (math.floor(w/(h/FH)), FH)) cv2_imshow(img) cv2.imwrite("mask5.jpg", img) image = cv2.imread("mask1.jpg") resizeImgAndShow(image)