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 @   CJK'sBLOG  阅读(565)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App
点击右上角即可分享
微信分享提示