import time import threading import traceback import tkinter as tk import cv2 import torch from tkinter import filedialog, messagebox from PIL import Image, ImageTk from src.config.inference_config import InferenceConfig from src.live_portrait_wrapper import LivePortraitWrapper from src.utils.camera import get_rotation_matrix # ============================================================ # CONFIGURAÇÃO # ============================================================ CAMERA_INDEX = 0 CAMERA_WIDTH = 640 CAMERA_HEIGHT = 480 DISPLAY_SIZE = 420 MODEL_SIZE = 256 # Preview da webcam ~25 FPS. CAMERA_PREVIEW_MS = 40 # ============================================================ # APLICAÇÃO # ============================================================ class DeepFakeLive: def __init__(self, root): self.root = root self.root.title( "LivePortrait — Demonstração Live de Conteúdo Sintético para o 4CFPQPE" ) self.root.geometry("1450x850") self.root.minsize(1150, 700) # Webcam self.cap = None self.camera_running = False self.last_frame = None # Live self.live_running = False # LivePortrait self.wrapper = None # Source self.source_path = None self.source_original = None self.source_photo = None self.source_info = None self.source_feature = None self.source_kp = None self.source_rotation = None self.source_canonical_kp = None # Primeiro frame da webcam self.driving_first_info = None self.driving_first_rotation = None # Resultado self.result_frame = None # Performance self.live_fps = 0.0 self.last_inference_time = 0.0 self.build_interface() self.root.after( 200, self.start_camera ) # ======================================================== # INTERFACE # ======================================================== def build_interface(self): self.root.configure( bg="#E5E7EB" ) # ---------------------------------------------------- # HEADER # ---------------------------------------------------- header = tk.Frame( self.root, bg="#111827", height=100 ) header.pack( fill="x" ) tk.Label( header, text="LIVEPORTRAIT - CFPQP", font=("Segoe UI", 28, "bold"), bg="#111827", fg="white" ).pack( pady=(14, 0) ) tk.Label( header, text="DEMONSTRAÇÃO LIVE — CFPQP", font=("Segoe UI", 12, "bold"), bg="#111827", fg="#F59E0B" ).pack() # ---------------------------------------------------- # ÁREA PRINCIPAL # ---------------------------------------------------- main = tk.Frame( self.root, bg="#E5E7EB" ) main.pack( fill="both", expand=True, padx=20, pady=20 ) for i in range(3): main.columnconfigure( i, weight=1 ) main.rowconfigure( 1, weight=1 ) titles = [ "1 — FOTOGRAFIA ORIGINAL", "2 — WEBCAM / REAL STREAM", "3 — DEEPFAKE LIVE" ] for column, title in enumerate(titles): tk.Label( main, text=title, font=("Segoe UI", 12, "bold"), bg="#E5E7EB", fg="#111827" ).grid( row=0, column=column, pady=5 ) # ---------------------------------------------------- # SOURCE # ---------------------------------------------------- self.source_panel = tk.Label( main, text="Seleciona uma fotografia", bg="#1F2937", fg="white", font=("Segoe UI", 14) ) self.source_panel.grid( row=1, column=0, sticky="nsew", padx=5, pady=5 ) # ---------------------------------------------------- # WEBCAM # ---------------------------------------------------- self.camera_panel = tk.Label( main, text="A iniciar webcam...", bg="black", fg="white", font=("Segoe UI", 14) ) self.camera_panel.grid( row=1, column=1, sticky="nsew", padx=5, pady=5 ) # ---------------------------------------------------- # RESULTADO # ---------------------------------------------------- self.result_panel = tk.Label( main, text="LIVE ainda não iniciado", bg="#111827", fg="white", font=("Segoe UI", 14) ) self.result_panel.grid( row=1, column=2, sticky="nsew", padx=5, pady=5 ) # ---------------------------------------------------- # CONTROLOS # ---------------------------------------------------- controls = tk.Frame( self.root, bg="white" ) controls.pack( fill="x", padx=20, pady=(0, 10) ) self.btn_photo = tk.Button( controls, text="FOTOGRAFIA", command=self.select_source, font=("Segoe UI", 11, "bold"), padx=20, pady=12 ) self.btn_photo.pack( side="left", padx=10, pady=10 ) self.btn_start = tk.Button( controls, text="▶ INICIAR LIVE STREAM", command=self.start_live, font=("Segoe UI", 11, "bold"), padx=25, pady=12 ) self.btn_start.pack( side="left", padx=10 ) self.btn_stop = tk.Button( controls, text="■ PARAR", command=self.stop_live, font=("Segoe UI", 11, "bold"), padx=25, pady=12, state="disabled" ) self.btn_stop.pack( side="left", padx=10 ) self.performance_label = tk.Label( controls, text="FPS: -- | -- ms/frame | VRAM: -- GB", font=("Consolas", 11, "bold"), bg="white", fg="#111827" ) self.performance_label.pack( side="right", padx=25 ) # ---------------------------------------------------- # STATUS # ---------------------------------------------------- self.status = tk.Label( self.root, text="Pronto.", anchor="w", bg="#111827", fg="white", font=("Consolas", 11), padx=15, pady=8 ) self.status.pack( fill="x" ) # ======================================================== # FOTOGRAFIA # ======================================================== def select_source(self): if self.live_running: return path = filedialog.askopenfilename( title="Selecionar fotografia", filetypes=[ ( "Imagens", "*.jpg *.jpeg *.png *.webp" ), ( "Todos os ficheiros", "*.*" ) ] ) if not path: return image_bgr = cv2.imread( path ) if image_bgr is None: messagebox.showerror( "Erro", "Não foi possível abrir a fotografia." ) return self.source_path = path self.source_original = image_bgr rgb = cv2.cvtColor( image_bgr, cv2.COLOR_BGR2RGB ) pil = Image.fromarray( rgb ) pil.thumbnail( ( DISPLAY_SIZE, 500 ) ) self.source_photo = ImageTk.PhotoImage( pil ) self.source_panel.configure( image=self.source_photo, text="" ) self.set_status( "Fotografia selecionada. " "Posiciona-te frontalmente na webcam." ) # ======================================================== # WEBCAM # ======================================================== def start_camera(self): self.cap = cv2.VideoCapture( CAMERA_INDEX, cv2.CAP_DSHOW ) if not self.cap.isOpened(): messagebox.showerror( "Webcam", "Não foi possível abrir a webcam." ) return self.cap.set( cv2.CAP_PROP_FRAME_WIDTH, CAMERA_WIDTH ) self.cap.set( cv2.CAP_PROP_FRAME_HEIGHT, CAMERA_HEIGHT ) self.cap.set( cv2.CAP_PROP_BUFFERSIZE, 1 ) self.camera_running = True self.update_camera() def update_camera(self): if not self.camera_running: return ret, frame = self.cap.read() if ret: self.last_frame = frame # Apenas a visualização fica em espelho. preview = cv2.flip( frame, 1 ) rgb = cv2.cvtColor( preview, cv2.COLOR_BGR2RGB ) pil = Image.fromarray( rgb ) pil.thumbnail( ( DISPLAY_SIZE, 500 ) ) photo = ImageTk.PhotoImage( pil ) self.camera_panel.configure( image=photo, text="" ) self.camera_panel.image = photo self.root.after( CAMERA_PREVIEW_MS, self.update_camera ) # ======================================================== # PREPARAR DRIVING FRAME # ======================================================== def prepare_driving_frame( self, frame ): h, w = frame.shape[:2] size = min( h, w ) x = ( w - size ) // 2 y = ( h - size ) // 2 crop = frame[ y:y + size, x:x + size ] crop = cv2.resize( crop, ( MODEL_SIZE, MODEL_SIZE ), interpolation=cv2.INTER_AREA ) crop = cv2.cvtColor( crop, cv2.COLOR_BGR2RGB ) return crop # ======================================================== # PREPARAR SOURCE # ======================================================== def prepare_source(self): self.set_status_threadsafe( "A preparar fotografia..." ) image = self.source_original h, w = image.shape[:2] size = min( h, w ) x = ( w - size ) // 2 y = ( h - size ) // 2 image = image[ y:y + size, x:x + size ] image = cv2.resize( image, ( MODEL_SIZE, MODEL_SIZE ), interpolation=cv2.INTER_AREA ) image_rgb = cv2.cvtColor( image, cv2.COLOR_BGR2RGB ) with torch.inference_mode(): I_s = self.wrapper.prepare_source( image_rgb ) self.source_info = ( self.wrapper.get_kp_info( I_s ) ) self.source_canonical_kp = ( self.source_info["kp"] ) self.source_rotation = ( get_rotation_matrix( self.source_info["pitch"], self.source_info["yaw"], self.source_info["roll"] ) ) self.source_feature = ( self.wrapper.extract_feature_3d( I_s ) ) self.source_kp = ( self.wrapper.transform_keypoint( self.source_info ) ) # ======================================================== # INICIAR LIVE # ======================================================== def start_live(self): if self.source_original is None: messagebox.showwarning( "Fotografia", "Seleciona primeiro uma fotografia." ) return if self.last_frame is None: messagebox.showwarning( "Webcam", "Ainda não existe imagem da webcam." ) return self.btn_start.configure( state="disabled" ) self.btn_photo.configure( state="disabled" ) self.live_fps = 0.0 self.last_inference_time = 0.0 self.set_status( "A carregar modelos LivePortrait..." ) threading.Thread( target=self.initialize_live, daemon=True ).start() def initialize_live(self): try: # ----------------------------------------------- # MODELOS SÃO CARREGADOS APENAS UMA VEZ # ----------------------------------------------- if self.wrapper is None: config = InferenceConfig() self.wrapper = LivePortraitWrapper( config ) # ----------------------------------------------- # SOURCE # ----------------------------------------------- self.prepare_source() # ----------------------------------------------- # PRIMEIRO FRAME = REFERÊNCIA NEUTRA # ----------------------------------------------- frame = self.last_frame.copy() driving_rgb = ( self.prepare_driving_frame( frame ) ) with torch.inference_mode(): I_d = ( self.wrapper.prepare_source( driving_rgb ) ) self.driving_first_info = ( self.wrapper.get_kp_info( I_d ) ) self.driving_first_rotation = ( get_rotation_matrix( self.driving_first_info["pitch"], self.driving_first_info["yaw"], self.driving_first_info["roll"] ) ) self.live_running = True self.root.after( 0, self.live_started ) self.live_loop() except Exception as e: traceback.print_exc() self.live_running = False self.root.after( 0, lambda err=str(e): messagebox.showerror( "LivePortrait", err ) ) self.root.after( 0, self.reset_buttons ) def live_started(self): self.btn_stop.configure( state="normal" ) self.set_status( "LIVE ATIVO — move a cabeça, pisca os olhos, " "sorri e fala." ) # ======================================================== # LOOP LIVE # ======================================================== def live_loop(self): # ==================================================== # Toda esta secção é inferência. # Não são construídos gradientes/autograd graphs. # ==================================================== with torch.inference_mode(): while self.live_running: if self.last_frame is None: time.sleep( 0.005 ) continue start = time.perf_counter() try: # --------------------------------------- # DRIVING FRAME # --------------------------------------- frame = self.last_frame.copy() driving_rgb = ( self.prepare_driving_frame( frame ) ) I_d = ( self.wrapper.prepare_source( driving_rgb ) ) # --------------------------------------- # MOTION EXTRACTION # --------------------------------------- d_info = ( self.wrapper.get_kp_info( I_d ) ) R_d = get_rotation_matrix( d_info["pitch"], d_info["yaw"], d_info["roll"] ) # --------------------------------------- # MOVIMENTO RELATIVO # --------------------------------------- R_new = ( R_d @ self.driving_first_rotation.permute( 0, 2, 1 ) @ self.source_rotation ) delta_new = ( self.source_info["exp"] + ( d_info["exp"] - self.driving_first_info["exp"] ) ) scale_new = ( self.source_info["scale"] * ( d_info["scale"] / self.driving_first_info["scale"] ) ) t_new = ( self.source_info["t"] + ( d_info["t"] - self.driving_first_info["t"] ) ) t_new = ( t_new.clone() ) t_new[ ..., 2 ].fill_(0) # --------------------------------------- # KEYPOINTS DO NOVO FRAME # --------------------------------------- x_d_new = ( scale_new * ( self.source_canonical_kp @ R_new + delta_new ) + t_new ) # --------------------------------------- # STITCHING # --------------------------------------- x_d_new = ( self.wrapper.stitching( self.source_kp, x_d_new ) ) # --------------------------------------- # SYNTHESIS # --------------------------------------- output = ( self.wrapper.warp_decode( self.source_feature, self.source_kp, x_d_new ) ) result = ( self.wrapper.parse_output( output["out"] )[0] ) # --------------------------------------- # RESULTADO # --------------------------------------- self.result_frame = ( cv2.cvtColor( result, cv2.COLOR_RGB2BGR ) ) # --------------------------------------- # PERFORMANCE # --------------------------------------- elapsed = ( time.perf_counter() - start ) if elapsed > 0: current_fps = ( 1.0 / elapsed ) if self.live_fps == 0: self.live_fps = ( current_fps ) else: # Média exponencial para evitar # FPS a saltar constantemente. self.live_fps = ( self.live_fps * 0.85 + current_fps * 0.15 ) self.last_inference_time = ( elapsed * 1000.0 ) # --------------------------------------- # INTERFACE # --------------------------------------- self.root.after( 0, self.update_result_panel ) except Exception: traceback.print_exc() self.live_running = False self.root.after( 0, lambda: messagebox.showerror( "LivePortrait", "Erro durante o processamento LIVE.\n\n" "Consulta o PowerShell para veres " "o erro completo." ) ) break self.root.after( 0, self.reset_buttons ) # ======================================================== # RESULTADO # ======================================================== def update_result_panel(self): if self.result_frame is None: return frame = self.result_frame.copy() # Mantemos o mesmo efeito de espelho da webcam. frame = cv2.flip( frame, 1 ) rgb = cv2.cvtColor( frame, cv2.COLOR_BGR2RGB ) pil = Image.fromarray( rgb ) pil = pil.resize( ( DISPLAY_SIZE, DISPLAY_SIZE ), Image.Resampling.LANCZOS ) photo = ImageTk.PhotoImage( pil ) self.result_panel.configure( image=photo, text="" ) self.result_panel.image = photo # ---------------------------------------------------- # VRAM # ---------------------------------------------------- if torch.cuda.is_available(): vram = ( torch.cuda.memory_allocated() / (1024 ** 3) ) else: vram = 0.0 # ---------------------------------------------------- # MÉTRICAS # ---------------------------------------------------- self.performance_label.configure( text=( f"FPS: {self.live_fps:.1f}" f" | " f"{self.last_inference_time:.0f} ms/frame" f" | " f"VRAM: {vram:.1f} GB" ) ) # ======================================================== # PARAR # ======================================================== def stop_live(self): self.live_running = False self.set_status( "A parar LIVE..." ) def reset_buttons(self): self.btn_start.configure( state="normal" ) self.btn_photo.configure( state="normal" ) self.btn_stop.configure( state="disabled" ) if not self.live_running: self.set_status( "LIVE parado." ) # ======================================================== # STATUS # ======================================================== def set_status( self, text ): self.status.configure( text=text ) def set_status_threadsafe( self, text ): self.root.after( 0, lambda: self.set_status( text ) ) def close(self): self.live_running = False self.camera_running = False if self.cap is not None: self.cap.release() self.root.destroy() if __name__ == "__main__": root = tk.Tk() app = DeepFakeLive( root ) root.protocol( "WM_DELETE_WINDOW", app.close ) root.mainloop()