如何处理人脸识别中的规格眩光?

0 投票
1 回答
38 浏览
提问于 2025-04-14 17:08

我想建立一个基于Python Flask的API应用程序,目的是发送两张人脸图片的路径(可以是网址或本地图片),然后希望得到一个回应。如果这两张脸是同一个人,就返回“是”;如果是不同的人,就返回“不是”。我使用了face_recognition这个库来实现这个功能。这个算法在处理没有眼镜的照片时效果很好,但一旦我引入了戴眼镜的人的照片,模型就无法识别出眼镜上的反光。请问我该怎么做?有没有什么预处理的方法可以帮助解决这个问题?还是说我应该换一个库?

import face_recognition
import cv2
import numpy as np
from datetime import datetime
from flask import Flask, request, jsonify
import face_recognition
import requests
from io import BytesIO
from datetime import datetime
from PIL import Image
app = Flask(__name__)

def preprocess_image(image_path,resize_height=400):
    if image_path.startswith(('http://', 'https://')):
            response = requests.get(image_path)
            image = Image.open(BytesIO(response.content))
    else:
        image = Image.open(image_path)
    exif_data = image.getexif()

    if exif_data:
        if exif_data.get(274) == 6:
            image = image.rotate(270) 
    width, length = image.size 
    ratio = resize_height / length
    new_width = int(width * ratio)
    print(image_path, image.size, ratio, new_width)
    
    new_width = int(width * ratio)
    resized_image = image.resize((new_width, resize_height))

    rgb_image = resized_image.convert("RGB")
    numpy_image = np.array(rgb_image)

    return numpy_image


@app.route('/', methods = ['GET','POST']) 
def home():
    return "Server is running"
@app.route('/compare', methods = ['GET','POST']) 
def face_compare():
    st = datetime.now()
    data = request.get_json()
    image_path1 = data['img1']
    image_path2 = data['img2']

    processed_image1 = preprocess_image(image_path1)
    processed_image2 = preprocess_image(image_path2)
    
    face_locations1 = face_recognition.face_locations(processed_image1)
    face_locations2 = face_recognition.face_locations(processed_image2)
    #show_bounding_box(face_locations1,processed_image1)
    #show_bounding_box(face_locations2,processed_image2)
    if not face_locations1:
        return jsonify({'result': 'Face 1 not found', 'time':str(datetime.now()-st)})
    if not face_locations2:
        return jsonify({'result': 'Face 2 not found', 'time':str(datetime.now()-st)})

    face_encoding1 = face_recognition.face_encodings(processed_image1, known_face_locations=face_locations1, model = 'small')[0]
    face_encoding2 = face_recognition.face_encodings(processed_image2, known_face_locations=face_locations2, model = 'small')[0]
    
    results = face_recognition.compare_faces([face_encoding1], face_encoding2, tolerance=0.47)
    return jsonify({'result': str(results[0]), 'time':str(datetime.now()-st)})

def show_bounding_box(face_loc, img):
    bounding_boxes = []
    for face_location in face_loc:
        top, right, bottom, left = face_location
        bounding_boxes.append((left, top, right, bottom))
    for (left, top, right, bottom) in bounding_boxes:
        cv2.rectangle(img, (left, top), (right, bottom), (0, 255, 0), 2)
    cv2.imshow("Faces with Bounding Boxes", img)
    cv2.waitKey(0)

if __name__ == '__main__':
    app.run(debug=True, threaded=True)

1 个回答

0

face_recognition 这个工具虽然很好用,但已经有点老了。你可以试试 insightface。这个新工具生成的是 512 维的人脸特征(而 dlib 只生成 128 维),所以它对一些人脸图像的干扰,比如眼镜、阴影、反光等,更加耐受。

撰写回答