Files
iqa-study/faceqnet.py
T
2025-04-12 03:19:13 +01:00

48 lines
1.5 KiB
Python

'''
Filename: faceqnet.py
Path: /home/netoa/Desktop/ISR/metrics/objective/no_reference
Created Date: Wednesday March 26th 2025
Author: Andre Neto
-----
Last Modified: Wed Mar 26 2025
Modified By: Andre Neto
-----
Copyright (c) 2025 Institute of Systems and Robotics
'''
import numpy as np
from PIL import Image
import tensorflow as tf
from tensorflow.keras.models import load_model
class FaceQnet:
""" FaceQnet: A Quality Assessment Model for Face Images """
def __init__(self, model_path='FaceQnet_v1.h5'):
"""
Load pre-trained FaceQnet model.
:param model_path: Path to FaceQnet Keras model (.h5)
"""
self.model = load_model(model_path, compile=False)
self.input_size = (224, 224)
def preprocess(self, pil_image: Image.Image) -> np.ndarray:
"""
Preprocess image for FaceQnet.
:param pil_image: PIL RGB image
:return: Preprocessed image as numpy array, shape (1, 224, 224, 3)
"""
img = pil_image.resize(self.input_size).convert('RGB')
img = np.asarray(img).astype('float32') / 255.0
return np.expand_dims(img, axis=0)
def predict(self, pil_image: Image.Image) -> float:
"""
Predict image quality using FaceQnet.
:param pil_image: PIL image (aligned face, RGB)
:return: Quality score (float)
"""
x = self.preprocess(pil_image)
score = self.model.predict(x, verbose=0)
return float(score[0][0])