Open Devilben4 opened 7 hours ago
Actually i dodge it, but now my main problem is :
with this code :
# test_3ddfa.py
import sys
import os
import cv2
import torch
import numpy as np
from datetime import datetime
# Append the 3DDFA_V2 path
# Assurez-vous que le chemin ci-dessous pointe vers le répertoire racine de 3DDFA_V2
sys.path.append('C:/Users/benkl/OneDrive/Documents/face recognition/3DDFA_V2')
from TDDFA import TDDFA
from utils.render import render
from utils.functions import crop_img
from Sim3DR import RenderPipeline
def clamp_roi_box(roi_box, frame_width, frame_height):
"""Clamp the ROI box within the frame boundaries."""
if len(roi_box) != 4:
raise ValueError(f"Expected ROI box with 4 elements, got {len(roi_box)} elements.")
x, y, w, h = roi_box
x = max(0, x)
y = max(0, y)
w = max(1, min(w, frame_width - x)) # Ensure width is at least 1
h = max(1, min(h, frame_height - y)) # Ensure height is at least 1
return (x, y, w, h)
def detect_faces(frame):
"""Detect faces in the frame using Haar Cascades."""
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
print(f"Detected faces: {faces}")
return [face for face in faces] # Return list of (x, y, w, h)
def process_image(image_path, output_dir, tddfa, renderer):
"""Process a single image to generate and save the UV map."""
# Charger l'image
frame = cv2.imread(image_path)
if frame is None:
print(f"Error: Could not load image {image_path}")
return
frame_height, frame_width = frame.shape[:2]
print(f"Processing image: {image_path}")
print(f"Image dimensions: width={frame_width}, height={frame_height}")
# Détecter les visages
boxes = detect_faces(frame)
if not boxes:
print("No faces detected.")
return
# Traiter le premier visage détecté
box = boxes[0]
print(f"Detected face box: {box}")
# Clamp et valider la ROI box
clamped_box = clamp_roi_box(box, frame_width, frame_height)
print(f"Clamped ROI box: {clamped_box}")
# Générer les paramètres 3D
param_lst, roi_box_lst = tddfa(frame, [clamped_box])
print(f"3D parameters: {param_lst}")
# Clamp les ROI boxes de TDDFA
clamped_roi_box_lst = [clamp_roi_box(roi_box, frame_width, frame_height) for roi_box in roi_box_lst]
print(f"Clamped ROI boxes from TDDFA: {clamped_roi_box_lst}")
# Reconstruire les vertices 3D
vertices_lst = tddfa.recon_vers(param_lst, clamped_roi_box_lst)
print("3D vertices reconstructed.")
# Rendre le visage 3D
img = render(frame, [vertices_lst[0]], tddfa.tri, show_flag=False)
print("3D face rendered.")
# Générer la texture map en recadrant la région du visage
texture_map = crop_img(frame, clamped_roi_box_lst[0])
print(f"Cropped texture map shape: {texture_map.shape}")
if texture_map.size == 0:
print("Error: Cropped texture map is empty.")
return
# Sauvegarder la UV map
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
uv_map_filename = os.path.join(output_dir, f"uv_map_{timestamp}.png")
cv2.imwrite(uv_map_filename, texture_map)
print(f"UV map saved to {uv_map_filename}")
# Afficher la UV map
cv2.imshow("UV Map", texture_map)
cv2.waitKey(0)
cv2.destroyAllWindows()
def main():
# ============================
# === Définir les Chemins ===
# ============================
# 1. Chemin vers le fichier de configuration
# Assurez-vous que ce fichier existe dans le répertoire spécifié
CONFIG_FP = 'C:/Users/benkl/OneDrive/Documents/face recognition/3DDFA_V2/configs/mb1_120x120.yml' # Exemple : 'configs/mb1_120x120.yml'
# 2. Chemin vers le checkpoint (poids du modèle)
# Assurez-vous que ce fichier existe dans le répertoire spécifié
CHECKPOINT_FP = 'C:/Users/benkl/OneDrive/Documents/face recognition/3DDFA_V2/weights/mb1_120x120.pth' # Exemple : 'weights/mb1_120x120.pth'
# 3. Chemin vers l'image d'entrée
# Placez votre image dans le répertoire spécifié ou ajustez le chemin en conséquence
IMAGE_PATH = 'C:/Users/benkl/OneDrive/Documents/face recognition/UV_Maps/visage.png' # Exemple : 'images/trump_hillary.jpg'
# 4. Répertoire pour sauvegarder les UV maps
OUTPUT_DIR = 'C:/Users/benkl/OneDrive/Documents/face recognition/UV_Maps' # Vous pouvez choisir un autre nom ou chemin
# ===========================================
# === Vérifier et Créer les Répertoires ===
# ===========================================
# Créer le répertoire de sortie si nécessaire
os.makedirs(OUTPUT_DIR, exist_ok=True)
# Vérifier l'existence du fichier de configuration
if not os.path.isfile(CONFIG_FP):
print(f"Error: Configuration file not found at {CONFIG_FP}")
sys.exit(1)
# Vérifier l'existence du checkpoint
if not os.path.isfile(CHECKPOINT_FP):
print(f"Error: Checkpoint file not found at {CHECKPOINT_FP}")
sys.exit(1)
# Vérifier l'existence de l'image d'entrée
if not os.path.isfile(IMAGE_PATH):
print(f"Error: Input image not found at {IMAGE_PATH}")
sys.exit(1)
# ===================================
# === Configuration de l'Appareil ===
# ===================================
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(f"Using device: {DEVICE}")
# ===========================
# === Initialiser TDDFA ===
# ===========================
print("Initializing TDDFA...")
tddfa = TDDFA(
gpu_mode=torch.cuda.is_available(),
checkpoint_fp=CHECKPOINT_FP,
arch='mobilenet', # Assurez-vous que c'est l'architecture correcte
size=120 # Taille correspondant au checkpoint
)
print("TDDFA initialized.")
# =================================
# === Initialiser RenderPipeline ===
# =================================
print("Initializing RenderPipeline...")
renderer = RenderPipeline()
print("RenderPipeline initialized.")
# ================================
# === Traiter l'Image d'Exemple ===
# ================================
process_image(IMAGE_PATH, OUTPUT_DIR, tddfa, renderer)
if __name__ == "__main__":
main()
My python always crash
I just want to have the UV map ... :(
Hi,
with this code :
i got : An error occurred during 3DDFA processing: negative dimensions are not allowed
can someone tell me why ?