本文实例为大家分享了python3实现人脸识别的具体代码,供大家参考,具体内容如下
第一种:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
import cv2
import numpy as np
filename = 'test1.jpg'
path = r 'd:\face'
def detect(filename):
face_cascade = cv2.cascadeclassifier( 'haarcascade_frontalface_default.xml' )
face_cascade.load(path + '\haarcascade_frontalface_default.xml' )
img = cv2.imread(filename)
gray = cv2.cvtcolor(img, cv2.color_bgr2gray)
faces = face_cascade.detectmultiscale(gray, 1.3 , 5 )
for (x, y, w, h) in faces:
img = cv2.rectangle(img, (x, y), (x + w, y + h), ( 255 , 0 , 0 ), 2 )
cv2.namedwindow( "vikings detected" )
cv2.imshow( "vikings detected" , img)
cv2.waitkey( 0 )
detect(filename)
|
结果:
第二种 参考贾志刚opencv教程
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
# -*- coding:utf-8 -*-
import cv2 as cv
import numpy as np
src = cv.imread( 'test1.jpg' )
path = r 'd:\face'
def face_detect_demo():
gray = cv.cvtcolor(src,cv.color_bgr2gray)
face_detector = cv.cascadeclassifier( 'haarcascade_frontalface_default.xml' )
face_detector.load(path + '\haarcascade_frontalface_default.xml' )
faces = face_detector.detectmultiscale(gray, 1.3 , 5 )
for x,y,w,h in faces:
cv.rectangle(src,(x,y),(x + w,y + h),( 0 , 0 , 255 ), 2 )
cv.imshow( "result" ,src)
print ( "--------------python face detect-------------" )
cv.namedwindow( "input image" , 0 )
cv.namedwindow( "result" , 0 )
cv.imshow( "input image" ,src)
face_detect_demo()
cv.waitkey( 0 )
cv.destroyallwindows()
|
结果:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/weixin_42512266/article/details/89467643