OpenCV人臉檢測 眼睛檢測之在人臉以外找眼睛

2021-10-03 21:23:09 字數 2307 閱讀 1984

今天在學習opencv人臉檢測時,發現眼睛檢測是在人臉以外尋找眼睛的,源**如下:

import cv2

def detect():

face_cascade = cv2.cascadeclassifier('../cascades/haarcascade_frontalface_default.xml')

eye_cascade = cv2.cascadeclassifier('../cascades/haarcascade_eye.xml')

camera = cv2.videocapture(0)

while (true):

ret , frame = camera.read()

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

faces = face_cascade.detectmultiscale(gray, 1.3, 5)

for (x,y,w,h) in faces:

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

roi_gray = gray[y: y+h, x: x+w]

eyes = eye_cascade.detectmultiscale(roi_gray, 1.8, 5, 0, (40,40))

for (ex,ey,ew,eh) in eyes:

cv2.rectangle(img, (ex, ey), (ex+ew, ey+eh), (0, 255, 0), 2)

cv2.imshow("camera", frame)

if cv2.waitkey(int(1000 / 12)) & 0xff == ord("q"):

break

camera.release()

cv2.destroyallwindows()

detect()

效果圖如下:

後來一檢查**才發現問題,

cv2.rectangle(img, (ex, ey), (ex+ew, ey+eh), (0, 255, 0), 2)
roi_color = img[y:y + h, x:x + w]
完整**如下:

import cv2

def detect():

face_cascade = cv2.cascadeclassifier('../cascades/haarcascade_frontalface_default.xml')

eye_cascade = cv2.cascadeclassifier('../cascades/haarcascade_eye.xml')

camera = cv2.videocapture(0)

while (true):

ret , frame = camera.read()

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

faces = face_cascade.detectmultiscale(gray, 1.3, 5)

for (x,y,w,h) in faces:

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

roi_gray = gray[y: y+h, x: x+w]

eyes = eye_cascade.detectmultiscale(roi_gray, 1.8, 5, 0, (40,40))

roi_color = img[y:y + h, x:x + w]

for (ex,ey,ew,eh) in eyes:

cv2.rectangle(roi_color, (ex, ey), (ex+ew, ey+eh), (0, 255, 0), 2)

cv2.imshow("camera", frame)

if cv2.waitkey(int(1000 / 12)) & 0xff == ord("q"):

break

camera.release()

cv2.destroyallwindows()

detect()

現在的效果圖如下:

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...