HEX
Server: Apache/2.4.66 (Ubuntu)
System: Linux nic2 5.15.0-177-generic #187-Ubuntu SMP Sat Apr 11 22:54:33 UTC 2026 x86_64
User: www-data (33)
PHP: 8.2.30
Disabled: NONE
Upload Files
File: //test_python/video/esperimento.py
import cv2
import numpy as np
import os

def esegui_esperimento_completo(video_input='cascata.mp4'):
    if not os.path.exists(video_input):
        print(f"Errore: {video_input} non trovato.")
        return

    # --- FASE 1: TRASMETTITORE (PORTRAIT POWER-UP) ---
    cap = cv2.VideoCapture(video_input)
    w_orig = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    h_orig = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
    
    # Manteniamo le proporzioni Portrait (es. 480 di larghezza, altezza proporzionale)
    scale_w = 480
    scale_h = int((h_orig / w_orig) * scale_w)
    
    print(f"Configurazione Portrait: {scale_w}x{scale_h}")
    flusso_stati = []
    
    for i in range(90): # 3 secondi
        ret, frame = cap.read()
        if not ret: break
        
        # Ridimensionamento coerente con la verticalità
        frame_res = cv2.resize(frame, (scale_w, scale_h))
        # Iniezione nel feedback con guadagno aumentato (x5.0)
        perturbazione = (frame_res.astype(np.float32) / 255.0) * 5.0
        flusso_stati.append(perturbazione.astype(np.float16))
    
    cap.release()
    print("Fase TX completata: segnale impresso nell'attrattore.")

    # --- FASE 2: RICEVITORE GEMELLO (RX COLORE & DINAMICA) ---
    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    out = cv2.VideoWriter('sasso_rivelato_finale.avi', fourcc, 30.0, (scale_w, scale_h))
    
    # Il Gemello RX usa i primi frame per calibrare la firma della cascata
    riferimento_rx = np.mean(flusso_stati[:5], axis=0)

    for frame in flusso_stati:
        # Sottrazione differenziale per estrarre il sasso (residuo)
        residuo = frame.astype(np.float32) - riferimento_rx
        
        # Amplificazione e pulizia del segnale (overdrive per il sasso)
        vis = np.abs(residuo)
        vis = np.clip(vis * 150, 0, 255).astype(np.uint8)
        
        out.write(vis)
    
    out.release()
    print("Fase RX completata: video 'sasso_rivelato_finale.avi' generato.")

if __name__ == "__main__":
    esegui_esperimento_completo()