赞
踩
脑壳有包又花时间折腾了一下,其实之前也折腾过,主要是新看了一个方法
在下图中查找脸部
第一种方案:
使用了opencv 的cv2.FaceDetectorYN. ,完整代码如下:
- import numpy as np
- import cv2
-
- img=cv2.imread("00000523.jpg")
- # img=cv2.resize(img, new_shape)
- faceDetector=cv2.FaceDetectorYN.create("opencv_zoo/models/face_detection_yunet/face_detection_yunet_2023mar.onnx","",(img.shape[1],img.shape[0]))
- faces = faceDetector.detect(img)
- f = faces[1]
- for ps in f:
- x=int(ps[0])
- y=int(ps[1])
- w=int(ps[2])
- h=int(ps[3])
- s=ps[14]
- # print(x,y,w,h,s)
- cv2.rectangle(img, (x, y), (x+w, y+h), (0,0,255), 2)
- cv2.namedWindow("d",0);
- cv2.resizeWindow("d", int(img.shape[1]/2), int(img.shape[0]/2));
- cv2.imshow("d",img)
- cv2.waitKey()
- # cv2.destoryAllWindows()
- cv2.destroyAllWindows()

结果如下:
第二种方案:
使用了 cv2.CascadeClassifier("D:\\opencv-4.52\\data\\haarcascades\\haarcascade_frontalface_default.xml")
- import numpy as np
- import cv2
-
- img=cv2.imread("00000523.jpg")
- cas_default = cv2.CascadeClassifier("D:\\opencv-4.52\\data\\haarcascades\\haarcascade_frontalface_default.xml")
- faces_default = cas_default.detectMultiScale(img)
- for (x,y,w,h) in faces_default:
- print(x,y,w,h)
- cv2.rectangle(img, (x, y), (x+w, y+h), (0,255,255), 2)
- cv2.namedWindow("d",0);
- cv2.resizeWindow("d", int(img.shape[1]/2), int(img.shape[0]/2));
- cv2.imshow("d",img)
- cv2.waitKey()
- # cv2.destoryAllWindows()
- cv2.destroyAllWindows()
结果如下:
以上两种都是opencv的自带方案,要么找脸不全,要么错误的找脸
然后是insightface 的方案:
- import insightface
- import cv2
- import numpy as np
-
- model = insightface.app.FaceAnalysis()
- model.prepare(ctx_id=0, det_thresh=0.45)
-
- face_img = cv2.imread('00000523.jpg')
- rgb_small_frame = face_img[:, :, ::-1]
- faces = model.get(rgb_small_frame)
-
- for r in faces:
- box=r.bbox.astype(int)
- color=(0, 0, 255)
- cv2.rectangle(face_img, (box[0], box[1]), (box[2], box[3]), color, 2)
- cv2.namedWindow("d",0);
- cv2.resizeWindow("d", int(face_img.shape[1]/2), int(face_img.shape[0]/2));
- cv2.imshow("d",face_img)
- cv2.waitKey()
- cv2.destroyAllWindows()

汇总的就是: insightface 脸部的查找最好,稍微有点慢,不知道是不是因为推理了年龄和性别。用insightface 推理出来的脸部数据,来识别具体人物的准确度也很高。
这是原图:
对于推理出来的人脸进行分别标记,对应到一个数据集:
参照标记的脸部数据,对其他照片进行推理,以下是结果,就不贴代码了:
以下结果展示了不同角度下,都给了一个准确的结果。不要介意对于性别和年龄的推断,可能同亚洲和欧洲人种数据集有关系。
带了墨镜居然都有一个准确结果
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。