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)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App