Initial clean commit

This commit is contained in:
Andre Neto
2025-04-12 03:19:13 +01:00
commit 62ff4d6c25
96 changed files with 104109 additions and 0 deletions
+43
View File
@@ -0,0 +1,43 @@
'''
Filename: magface.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
from insightface.app import FaceAnalysis
class MagFaceIQA:
"""MagFaceIQA"""
def __init__(self, device='cpu'):
"""
Initialize the MagFace model using InsightFace.
:param device: 'cpu' or 'cuda'
"""
provider = ['CUDAExecutionProvider'] if device == 'cuda' else ['CPUExecutionProvider']
self.model = FaceAnalysis(name='buffalo_l', providers=provider)
self.model.prepare(ctx_id=0, det_size=(224, 224))
def predict(self, pil_image: Image.Image) -> float:
"""
Compute the quality score based on embedding norm (L2 magnitude).
:param pil_image: Input PIL image in RGB.
:return: Quality score (float). Returns NaN if no face detected.
"""
img = np.array(pil_image.convert("RGB"))[..., ::-1] # RGB → BGR for OpenCV
faces = self.model.get(img)
if len(faces) == 0:
return np.nan
# Use the largest face if multiple detected
face = max(faces, key=lambda x: x.bbox[2] - x.bbox[0])
embedding = face.embedding
return float(np.linalg.norm(embedding))