37.OpenCV的人臉和目標的跟蹤——dlib庫
文章目錄
- 前言
- 一、基于dlib庫的人臉檢測(圖片)
- 二、基于dlib庫的人臉檢測(視頻)
- 三、OpenCV-Python資源下載
- 總結
前言
??dlib庫是一個十分好用的機器學習庫,其原始碼均由C++實作,并提供了Python 介面,可廣泛適用于很多場景,本實體僅簡單示范dlib庫中關于人臉檢測技術的Python應用,
一、基于dlib庫的人臉檢測(圖片)
??程式可以實作對圖片中的人臉進行檢測,
import cv2
import dlib
import numpy as np
def plot_rectangle(image, faces):
for face in faces:
cv2.rectangle(image, (face.left(), face.top()), (face.right(), face.bottom()), (255,0,0), 4)
return image
def main():
img = cv2.imread("family.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
detector = dlib.get_frontal_face_detector()
dets_result = detector(gray, 1)
img_result = plot_rectangle(img.copy(), dets_result)
cv2.imshow("face detection", img_result)
cv2.waitKey(0)
cv2.destroyAllWindows()
if __name__ == '__main__':
main()

二、基于dlib庫的人臉檢測(視頻)
??程式可以實作對視頻中的人臉進行檢測,
import cv2
import dlib
def plot_rectangle(image, faces):
for face in faces:
cv2.rectangle(image, (face.left(), face.top()), (face.right(), face.bottom()), (255,0,0), 4)
return image
def main():
capture = cv2.VideoCapture(0)
if capture.isOpened() is False:
print("Camera Error !")
while True:
ret, frame = capture.read()
if ret:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # BGR to GRAY
detector = dlib.get_frontal_face_detector()
det_result = detector(gray, 1)
dets_image = plot_rectangle(frame, det_result)
cv2.imshow("face detection with dlib", dets_image)
if cv2.waitKey(1) == 27:
break
capture.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
main()
三、OpenCV-Python資源下載
OpenCV-Python測驗用圖片、中文官方檔案、opencv-4.5.4原始碼
總結
??以上內容簡單的介紹了OpenCV-Python的人臉跟蹤和目標跟蹤,有關Python、資料科學、人工智能等文章后續會不定期發布,請大家多多關注,一鍵三連喲(●’?’●),
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/404223.html
標籤:其他
