python使用dlib進(jìn)行人臉檢測和關(guān)鍵點(diǎn)的示例
#!/usr/bin/env python # -*- coding:utf-8-*- # file: {NAME}.py # @author: jory.d # @contact: dangxusheng163@163.com # @time: 2020/04/10 19:42 # @desc: 使用dlib進(jìn)行人臉檢測和人臉關(guān)鍵點(diǎn) import cv2 import numpy as np import glob import dlib FACE_DETECT_PATH = '/home/build/dlib-v19.18/data/mmod_human_face_detector.dat' FACE_LANDMAKR_5_PATH = '/home/build/dlib-v19.18/data/shape_predictor_5_face_landmarks.dat' FACE_LANDMAKR_68_PATH = '/home/build/dlib-v19.18/data/shape_predictor_68_face_landmarks.dat' def face_detect(): root = '/media/dangxs/E/Project/DataSet/VGG Face Dataset/vgg_face_dataset/vgg_face_dataset/vgg_face_dataset' imgs = glob.glob(root + '/**/*.jpg', recursive=True) assert len(imgs) > 0 detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor(FACE_LANDMAKR_68_PATH) for f in imgs: img = cv2.imread(f) # The 1 in the second argument indicates that we should upsample the image # 1 time. This will make everything bigger and allow us to detect more # faces. dets = detector(img, 1) print("Number of faces detected: {}".format(len(dets))) for i, d in enumerate(dets): x1, y1, x2, y2 = d.left(), d.top(), d.right(), d.bottom() print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format( i, x1, y1, x2, y2)) cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 1) # Get the landmarks/parts for the face in box d. shape = predictor(img, d) print("Part 0: {}, Part 1: {} ...".format(shape.part(0), shape.part(1))) # # Draw the face landmarks on the screen. ''' # landmark 順序: 外輪廓 - 左眉毛 - 右眉毛 - 鼻子 - 左眼 - 右眼 - 嘴巴 ''' for i in range(shape.num_parts): x, y = shape.part(i).x, shape.part(i).y cv2.circle(img, (x, y), 2, (0, 0, 255), 1) cv2.putText(img, str(i), (x, y), cv2.FONT_HERSHEY_COMPLEX, 0.3, (0, 0, 255), 1) cv2.resize(img, dsize=None, dst=img, fx=2, fy=2) cv2.imshow('w', img) cv2.waitKey(0) def face_detect_mask(): root = '/media/dangxs/E/Project/DataSet/VGG Face Dataset/vgg_face_dataset/vgg_face_dataset/vgg_face_dataset' imgs = glob.glob(root + '/**/*.jpg', recursive=True) assert len(imgs) > 0 detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor(FACE_LANDMAKR_68_PATH) for f in imgs: img = cv2.imread(f) # The 1 in the second argument indicates that we should upsample the image # 1 time. This will make everything bigger and allow us to detect more # faces. dets = detector(img, 1) print("Number of faces detected: {}".format(len(dets))) for i, d in enumerate(dets): x1, y1, x2, y2 = d.left(), d.top(), d.right(), d.bottom() print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format( i, x1, y1, x2, y2)) cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 1) # Get the landmarks/parts for the face in box d. shape = predictor(img, d) print("Part 0: {}, Part 1: {} ...".format(shape.part(0), shape.part(1))) # # Draw the face landmarks on the screen. ''' # landmark 順序: 外輪廓 - 左眉毛 - 右眉毛 - 鼻子 - 左眼 - 右眼 - 嘴巴 ''' points = [] for i in range(shape.num_parts): x, y = shape.part(i).x, shape.part(i).y if i < 26: points.append([x, y]) # cv2.circle(img, (x, y), 2, (0, 0, 255), 1) # cv2.putText(img, str(i), (x,y),cv2.FONT_HERSHEY_COMPLEX, 0.3 ,(0,0,255),1) # 只把臉切出來 points[17:] = points[17:][::-1] points = np.asarray(points, np.int32).reshape(-1, 1, 2) img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) black_img = np.zeros_like(img) cv2.polylines(black_img, [points], 1, 255) cv2.fillPoly(black_img, [points], (1, 1, 1)) mask = black_img masked_bgr = img * mask # 位運(yùn)算時(shí)需要轉(zhuǎn)化成灰度圖像 mask_gray = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY) masked_gray = cv2.bitwise_and(img_gray, img_gray, mask=mask_gray) cv2.resize(img, dsize=None, dst=img, fx=2, fy=2) cv2.imshow('w', img) cv2.imshow('mask', mask) cv2.imshow('mask2', masked_gray) cv2.imshow('mask3', masked_bgr) cv2.waitKey(0) if __name__ == '__main__': face_detect()
以上就是python使用dlib進(jìn)行人臉檢測和關(guān)鍵點(diǎn)的示例的詳細(xì)內(nèi)容,更多關(guān)于python 人臉檢測的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- Python使用百度api做人臉對(duì)比的方法
- python基于opencv實(shí)現(xiàn)人臉識(shí)別
- python實(shí)現(xiàn)圖片,視頻人臉識(shí)別(dlib版)
- python實(shí)現(xiàn)圖片,視頻人臉識(shí)別(opencv版)
- python調(diào)用百度API實(shí)現(xiàn)人臉識(shí)別
- 使用python-cv2實(shí)現(xiàn)Harr+Adaboost人臉識(shí)別的示例
- Python用dilb提取照片上人臉的示例
- Python環(huán)境使用OpenCV檢測人臉實(shí)現(xiàn)教程
- python openCV實(shí)現(xiàn)攝像頭獲取人臉圖片
- 基于Python實(shí)現(xiàn)視頻的人臉融合功能
- python實(shí)現(xiàn)人臉簽到系統(tǒng)
- Python3 利用face_recognition實(shí)現(xiàn)人臉識(shí)別的方法
- python 使用百度AI接口進(jìn)行人臉對(duì)比的步驟
相關(guān)文章
詳解Python?itertools模塊中starmap函數(shù)的應(yīng)用
starmap是一個(gè)非常有用的函數(shù),它屬于itertools模塊中的一部分,本文將詳細(xì)介紹starmap函數(shù)的作用、用法以及實(shí)際應(yīng)用場景,希望對(duì)大家有所幫助2024-03-03使用pandas模塊讀取csv文件和excel表格,并用matplotlib畫圖的方法
今天小編就為大家分享一篇使用pandas模塊讀取csv文件和excel表格,并用matplotlib畫圖的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-06-06Python學(xué)習(xí)筆記之解析json的方法分析
這篇文章主要介紹了Python解析json的方法,結(jié)合實(shí)例形式分析了常見的Python解析與轉(zhuǎn)換json格式數(shù)據(jù)相關(guān)操作技巧,需要的朋友可以參考下2017-04-04Python內(nèi)置數(shù)據(jù)結(jié)構(gòu)列表與元組示例詳解
這篇文章主要給大家介紹了關(guān)于Python內(nèi)置數(shù)據(jù)結(jié)構(gòu)列表與元組的相關(guān)資料,列表是順序存儲(chǔ)的數(shù)據(jù)結(jié)構(gòu),類似于數(shù)據(jù)結(jié)構(gòu)中的順序表,在存儲(chǔ)上是相連的一大塊內(nèi)存空間,在物理和邏輯上都是連續(xù)的,需要的朋友可以參考下2021-08-08教你使用Pandas直接核算Excel中的快遞費(fèi)用
文中仔細(xì)說明了怎么根據(jù)賬單核算運(yùn)費(fèi).首先要確定運(yùn)費(fèi)規(guī)則,然后根據(jù)運(yùn)費(fèi)規(guī)則編寫代碼,生成核算列(快遞費(fèi) = 省份*重量),最后輸入賬單,進(jìn)行核算.將腳本件生成EXE文件,就可以使用啦,需要的朋友可以參考下2021-05-05python實(shí)現(xiàn)PID算法及測試的例子
今天小編就為大家分享一篇python實(shí)現(xiàn)PID算法及測試的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-08-08Python實(shí)現(xiàn)的簡單文件傳輸服務(wù)器和客戶端
這篇文章主要介紹了Python實(shí)現(xiàn)的簡單文件傳輸服務(wù)器和客戶端,本文直接給出Server和Client端的實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-04-04人工智能-Python實(shí)現(xiàn)多項(xiàng)式回歸
這篇文章主要介紹了人工智能-Python實(shí)現(xiàn)多項(xiàng)式回歸,上一次我們講解了線性回歸,這次我們重點(diǎn)分析多項(xiàng)式回歸,需要的小伙伴可以參考一下2022-01-01PyTorch搭建LSTM實(shí)現(xiàn)時(shí)間序列負(fù)荷預(yù)測
這篇文章主要為大家介紹了PyTorch搭建LSTM實(shí)現(xiàn)時(shí)間序列負(fù)荷預(yù)測,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05python實(shí)戰(zhàn)之利用pygame實(shí)現(xiàn)貪吃蛇游戲(二)
這篇文章主要介紹了python實(shí)戰(zhàn)之利用pygame實(shí)現(xiàn)貪吃蛇游戲(二),文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)python的小伙伴們有很好的幫助,需要的朋友可以參考下2021-05-05