OpenCV 動態 人臉檢測 python

2021-08-09 21:40:02 字數 1712 閱讀 1416

直接上**: 按q退出

import cv2  

import numpy as np

cv2.namedwindow("test")

cap = cv2.videocapture(0) #載入攝像頭錄製

success, frame = cap.read()

# classifier = cv2.cascadeclassifier("/users/yuki/anaconda/share/opencv/haarcascades/haarcascade_frontalface_alt.xml") # 確保此xml檔案與該py檔案在乙個資料夾下,否則將這裡改為絕對路徑

#haarcascade_frontalface_default.xml

classifier = cv2.cascadeclassifier("/users/yuki/anaconda/share/opencv/haarcascades/haarcascade_frontalface_default.xml") # 確保此xml檔案與該py檔案在乙個資料夾下,否則將這裡改為絕對路徑

while success:

success, frame = cap.read()

size = frame.shape[:2]

image = np.zeros(size, dtype=np.float16)

image = cv2.cvtcolor(frame, cv2.color_bgr2gray)

cv2.equalizehist(image, image)

divisor = 8

h, w = size

minsize = (w // divisor, h // divisor)

facerects = classifier.detectmultiscale(image, 1.2, 2, cv2.cascade_scale_image, minsize)

if len(facerects) > 0:

for facerect in facerects:

x, y, w, h = facerect

cv2.rectangle(frame,(x,y),(x+h,y+w),(0,255,0),2)

#鎖定 眼和嘴巴

# cv2.circle(frame, (x + w // 4, y + h // 4 + 30), min(w // 8, h // 8), (255, 0, 0)) # 左眼

# cv2.circle(frame, (x + 3 * w //4, y + h // 4 + 30), min(w // 8, h // 8), (255, 0, 0)) #右眼

# cv2.rectangle(frame, (x + 3 * w // 8, y + 3 * h // 4), (x + 5 * w // 8, y + 7 * h // 8), (255, 0, 0)) #嘴巴

cv2.imshow("test", frame)

key = cv2.waitkey(10)

c = chr(key & 255)

if c in ['q', 'q', chr(27)]:

break

cv2.destroywindow("test")

OpenCV人臉檢測

include include include include include include include include include include static cvmemstorage storage 0 建立乙個記憶體儲存器,來統一管理各種動態物件的記憶體 static cvhaar...

OpenCV人臉檢測

win7 32位 opencv3.0 vs2013 對資料夾中進行人臉檢測 在opencv中,人臉檢測用的是harr或lbp特徵,分類演算法用的是adaboost演算法。這種演算法需要提前訓練大量的,非常耗時,因此opencv已經訓練好了,把訓練結果存放在一些xml檔案裡面。在opencv3.0版本...

opencv人臉檢測

最近有空對學習下opencv的東西,本篇主要記錄對人臉檢測實現,而人臉檢測是為人臉識別做準備。opencv版本 3.3.0 環境 vs2015 void cascadeclassifier detectmultiscale inputarray image,vector objects,double...