日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 人工智能 > pytorch >内容正文

pytorch

人脸检测5种方法

發(fā)布時間:2023/12/8 pytorch 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 人脸检测5种方法 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

眾所周知,人臉識別是計算機(jī)視覺應(yīng)用的一個重大領(lǐng)域,在學(xué)習(xí)人臉識別之前,我們先來簡單學(xué)習(xí)下人臉檢測的幾種用法。

常見的人臉檢測方法大致有5種,Haar、Hog、CNN、SSD、MTCNN:

注:本文章圖片來源于網(wǎng)絡(luò)

相關(guān)構(gòu)造檢測器的文件:opencv/data at master · opencv/opencv · GitHub

基本步驟

  • 讀入圖片
  • 構(gòu)造檢測器
  • 獲取檢測結(jié)果
  • 解析檢測結(jié)果
  • 一、Haar

    # 調(diào)整參數(shù) img = cv2.imread('./images/001.jpg') cv_show('img',img)# 構(gòu)造harr檢測器 face_detector = cv2.CascadeClassifier('./weights/haarcascade_frontalface_default.xml')# 轉(zhuǎn)為灰度圖 img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) plt.imshow(img_gray,'gray')# 檢測結(jié)果 上圖4個人臉?biāo)?個方框坐標(biāo) # image # scaleFactor控制人臉尺寸 默認(rèn)1.1 detections = face_detector.detectMultiScale(img_gray,scaleFactor=1.3)# 解析 for x,y,w,h in detections:cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0)) plt.imshow(cv2.cvtColor(img,cv2.COLOR_BGR2RGB))

    # 調(diào)整參數(shù) img = cv2.imread('./images/004.jpeg') cv_show('img',img)# 構(gòu)造harr檢測器 face_detector = cv2.CascadeClassifier('./weights/haarcascade_frontalface_default.xml')# 轉(zhuǎn)為灰度圖 img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) plt.imshow(img_gray,'gray')# 檢測結(jié)果 上圖4個人臉?biāo)?個方框坐標(biāo) # image # scaleFactor控制人臉尺寸 默認(rèn)1.1 # minNeighbors 確定一個人臉框至少要有n個候選值 越高 質(zhì)量越好 # [, flags[, # minSize maxSize 人臉框的最大最小尺寸 如minSize=(40,40) detections = face_detector.detectMultiScale(img_gray,scaleFactor=1.2, minNeighbors=10)# 在質(zhì)量和數(shù)量上平衡# 解析 for x,y,w,h in detections:cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0)) plt.imshow(cv2.cvtColor(img,cv2.COLOR_BGR2RGB))

    ?

    上述過程中:

    • scaleFactor參數(shù):用來控制人臉框的大小,可以用它來排除一些錯誤檢測;?
    • minNeighbors參數(shù):我們給人臉框起來的時候,一般一張臉會框許多的框,假如這張臉框得越多,說明質(zhì)量越好,越是一張正確的“臉”。

    二、Hog

    對于第一次使用這個功能的同學(xué),要提前下載一下dlib。

    import dlib# 構(gòu)造HOG人臉檢測器 不需要參數(shù) hog_face_detetor = dlib.get_frontal_face_detector()# 檢測人臉獲取數(shù)據(jù) # img # scale類似haar的scalFactor detections = hog_face_detetor(img,1)# 解析獲取的數(shù)據(jù) for face in detections:# 左上角x = face.left()y = face.top()# 右下角r = face.right()b = face.bottom()cv2.rectangle(img,(x,y),(r,b),(0,255,0)) plt.imshow(cv2.cvtColor(img,cv2.COLOR_BGR2RGB))

    ?

    三、CNN

    import dlib# 構(gòu)造CNN人臉檢測器 cnn_face_detector = dlib.cnn_face_detection_model_v1("./weights/mmod_human_face_detector.dat")# 檢測人臉 參數(shù)與上一種相似 detections = cnn_face_detector(img,1)for face in detections:# 左上角x = face.rect.left()y = face.rect.top()# 右下角r = face.rect.right()b = face.rect.bottom()# 置信度c = face.confidenceprint(c)cv2.rectangle(img,(x,y),(r,b),(0,255,0))plt.imshow(cv2.cvtColor(img,cv2.COLOR_BGR2RGB))

    通過神經(jīng)網(wǎng)絡(luò)完成,這個過程中我們還可以查看每張臉檢測時的置信度。

    ?

    四、SSD

    # 加載模型 face_detector = cv2.dnn.readNetFromCaffe('./weights/deploy.prototxt.txt','./weights/res10_300x300_ssd_iter_140000.caffemodel')# 原圖尺寸 img_height = img.shape[0] img_width = img.shape[1]# 放縮至輸入尺寸 img_resized = cv2.resize(img,(500,300)) # 轉(zhuǎn)為2進(jìn)制 img_blob = cv2.dnn.blobFromImage(img_resized,1.0,(500,300),(104.0,177.0,123.0))# 輸入 face_detector.setInput(img_blob)# 推理 detections = face_detector.forward()

    此時

    detections.shape # (1, 1, 200, 7)

    說明有200個結(jié)果,后面的7則是我們做需要的一些數(shù)據(jù),繼續(xù)如下:

    # 查看人臉數(shù)量 num_of_detections = detections.shape[2]img_copy = img.copy()for index in range(num_of_detections):# 置信度detections_confidence = detections[0,0,index,2]# 通過置信度篩選if detections_confidence > 0.15:# 位置 乘以寬高恢復(fù)大小locations = detections[0,0,index,3:7] * np.array([img_width,img_height,img_width,img_height])# 打印print(detections_confidence)lx,ly,rx,ry = locations.astype('int')# 繪制cv2.rectangle(img_copy,(lx,ly),(rx,ry),(0,255,0),2)plt.imshow(cv2.cvtColor(img_copy,cv2.COLOR_BGR2RGB))

    ?

    五、MTCNN

    # 導(dǎo)入MTCNN from mtcnn.mtcnn import MTCNN# 記載模型 face_detetor = MTCNN()# 檢測人臉 detections = face_detetor.detect_faces(img_cvt) for face in detections:x,y,w,h = face['box']cv2.rectangle(img_cvt,(x,y),(x+w,y+h),(0,255,0),2) plt.imshow(img_cvt)

    ?

    對比

    優(yōu)勢劣勢
    Haar速度最快、清涼、適合算力較小的設(shè)備準(zhǔn)確度低、偶爾誤報、無旋轉(zhuǎn)不變性
    HOG+Dlib比Haar準(zhǔn)確率高速度比Haar低,計算量大、無旋轉(zhuǎn)不變性、Dlib兼容性問題
    SSD比Haar和hog準(zhǔn)確率高、深度學(xué)習(xí)、大小一般低光照片準(zhǔn)確率低,受膚色影響。
    CNN最準(zhǔn)確、誤報率低、輕量相對于其他方法慢、計算量大、Dlib兼容性問題

    總結(jié)

    以上是生活随笔為你收集整理的人脸检测5种方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。