You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
1.3 KiB
45 lines
1.3 KiB
# https://juejin.cn/post/7019121556588593189
|
|
|
|
# https://github.com/InstantID/InstantID
|
|
# from insightface.app import FaceAnalysis
|
|
import cv2
|
|
from PIL import Image
|
|
|
|
import os
|
|
|
|
# 加载级联
|
|
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
|
|
|
|
|
|
def check(imgFile):
|
|
img = cv2.imread(imgFile)
|
|
|
|
# 检测人脸
|
|
faces = face_cascade.detectMultiScale(image=img, scaleFactor=1.1, minNeighbors=5)
|
|
|
|
# 在人脸周围绘制边界框
|
|
for (x, y, w, h) in faces:
|
|
cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
|
|
|
|
# 显示图像中检测到的人脸数量
|
|
print(len(faces), "faces detected!")
|
|
|
|
if len(faces)>1:
|
|
# 绘制检测到人脸的图像
|
|
finalimg = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
|
|
|
|
# 将numpy数组转换为PIL图像
|
|
outImage = imgFile.replace("/Upload/", "/Flag/")
|
|
image = Image.fromarray(finalimg)
|
|
image.save(outImage)
|
|
|
|
|
|
# 指定要遍历的目录路径
|
|
directory = r'C:/Users/Administrator/Desktop/Upload'
|
|
# 获取目录下的所有文件和目录名称列表
|
|
with os.scandir(directory) as entries:
|
|
for entry in entries:
|
|
if entry.is_file():
|
|
imgFile = directory + '/' + entry.name
|
|
check(imgFile)
|