Opencv python 人臉檢測

2021-09-03 02:37:13 字數 2109 閱讀 9740

import numpy as np

import cv2 as cv

cv.namedwindow("face detected")

cap = cv.videocapture(0)

success, frame = cap.read()

#  載入opencv識別器

face_cascade = cv.cascadeclassifier('haarcascade/haarcascade_frontalface_alt.xml')

eyes_cascade = cv.cascadeclassifier('haarcascade/haarcascade_eye.xml')

# 定義人臉矩形

[x, y, w, h] = [0, 0, 0, 0]

# 每一幀的影象進行處理

while success:

success, frame = cap.read()

image = np.zeros(size, dtype=np.float16) # 初始化乙個影象矩陣

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

# 直方圖均衡

image = cv.equalizehist(image)

im_h, im_w = size

minsize_1 = (im_w // 10, im_h // 10)

facerects = face_cascade.detectmultiscale(image, 1.05, 2, cv.cascade_scale_image, minsize_1)

if len(facerects) > 0:

for facerect in facerects:

x, y, w, h = facerect

face_im = np.zeros([w, h], dtype=np.float16) # 初始化乙個眼睛影象矩陣

temp_image = cv.cvtcolor(frame, cv.color_bgr2gray)

face_im = temp_image[y:y + h, x:x + w] # 從中摳出臉部

eyecicle = eyes_cascade.detectmultiscale(face_im, 1.05, 2, cv.cascade_scale_image, (w // 10, h // 10))

if len(eyecicle) == 2:

x1, y1, w1, h1 = eyecicle[0]

x2, y2, w2, h2 = eyecicle[1]

point_1 = [(2 * (x + x1) + w1) // 2, (2 * (y + y1) + h1) // 2]

point_2 = [(2 * (x + x2) + w2) // 2, (2 * (y + y2) + h2) // 2]

r1 = w1 // 2

r2 = w2 // 2

cv.circle(frame, (point_1[0], point_1[1]), r1, (255, 0, 255), 2)

cv.circle(frame, (point_2[0], point_2[1]), r2, (255, 0, 255), 2)

if (x2 > x1):

cv.line(frame, (point_1[0] + r1, point_1[1]), (point_2[0] - r2, point_2[1]), (255, 0, 255), 2)

else:

cv.line(frame, (point_2[0] + r2, point_2[1]), (point_1[0] - r1, point_1[1]), (255, 0, 255), 2)

cv.rectangle(frame, (x, y), (x + w, y + h), [255, 255, 0], 2)

cv.imshow("face detected", frame)

key = cv.waitkey(5)

if key == 32:

break

cv.destroywindow("face detection system")

OpenCV Python 人臉檢測

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!例項總結 下午的時候,配好了opencv的python環境,opencv的python環境搭建。於是迫不及待的想體驗一下opencv的人臉識別,如下文。haar like百科釋義。通俗的來講,就是作為人臉特徵即可。haar特徵值反映了影象的灰度變化...

OpenCV python 人臉檢測

1,人臉檢測簡介 人臉檢測的模型主要有兩類,一類是知識模型,根據眼睛 嘴 鼻子的相對位置或面部不同部位的顏色深度差異來檢測人臉,另一類是統計模型,把海量的人臉資料轉換成二維畫素矩陣,從統計的觀點出發構建人臉模式空間判斷人臉是否存在。2,特徵資料 在搜尋 github opencv 在 opencv ...

OpenCV Python 實現人臉識別

參考 1 2 haar特徵值反映了影象的灰度變化情況。例如 臉部的一些特徵能由矩形特徵簡單的描述,如 眼睛要比臉頰顏色要深,鼻樑兩側比鼻樑顏色要深,嘴巴比周圍顏色要深等。import cv2 image cv2.imread imagepath import cv2 gray cv2.cvtcolo...