15行python代码实现人脸识别

方法一:face_recognition

import cv2
import face_recognition
img_path = "C:/Users/CJK/Desktop/1.jpg"#图片目录
im_fr = face_recognition.load_image_file(img_path)
face_ = face_recognition.face_locations(im_fr,number_of_times_to_upsample=1,model="cnn")
print(face_) 
im = cv2.imread(img_path)
print("img size ",im.shape)
cv2.namedWindow("result",cv2.WINDOW_NORMAL)
for _ in face_:
    top, right, bottom, left = _
    cv2.rectangle(im,(left, top), (right, bottom),(0,200,0),2)    
cv2.imshow("result",im)
cv2.imwrite("C:/Users/CJK/Desktop/1_cnn.jpg", im)#图片目录加上_cnn后缀
cv2.waitKey(0)

方法二:opencv

import cv2
img_path = "C:/Users/CJK/Desktop/1.jpg"
pathf='D:/Anaconda3/Lib/site-packages/cv2/data/haarcascade_frontalface_default.xml'
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
face_cascade.load(pathf)
im = cv2.imread(img_path)
face_ = face_cascade.detectMultiScale(im,scaleFactor=1.1,minNeighbors=10,flags=0|cv2.CASCADE_SCALE_IMAGE,minSize=(1,1))
cv2.namedWindow("result",cv2.WINDOW_NORMAL)
for _ in face_:
    lt = (_[0],_[1])
    rb = (_[0]+_[2],_[1]+_[3])
    cv2.rectangle(im,lt,rb,(0,200,0),2)   
cv2.imshow("result",im)
cv2.imwrite("C:/Users/CJK/Desktop/1_harr.jpg",im)
cv2.waitKey(0)

posted @ 2022-06-08 19:34  CJK'sBLOG  阅读(543)  评论(0编辑  收藏  举报