Spaces:
Running
Running
import os | |
import subprocess | |
import sys | |
def install_requirements(): | |
try: | |
subprocess.check_call([sys.executable, "-m", "pip", "install", "opencv-python", "numpy", "scipy", "matplotlib"]) | |
print("Successfully installed OpenCV, numpy, scipy, and matplotlib.") | |
try: | |
subprocess.check_call([sys.executable, "-m", "pip", "install", "facemorpher"]) | |
print("Successfully installed facemorpher.") | |
except subprocess.CalledProcessError as e: | |
print("Failed to install facemorpher:", e) | |
print("Attempting fallback: manually installing stasm and docopt.") | |
try: | |
subprocess.check_call([sys.executable, "-m", "pip", "install", "stasm", "docopt"]) | |
print("Successfully installed stasm and docopt.") | |
except subprocess.CalledProcessError as e: | |
print("Fallback installation failed:", e) | |
print("Proceeding without facemorpher.") | |
except subprocess.CalledProcessError as e: | |
print("Installation of core dependencies failed:", e) | |
def create_and_run_main_py(): | |
main_code = ''' | |
import os | |
import cv2 | |
import numpy as np | |
import time | |
# Fallback implementation for morph_faces if facemorpher is not available | |
try: | |
from facemorpher import morpher | |
use_facemorpher = True | |
except ImportError: | |
print("facemorpher not found. Using fallback morphing.") | |
use_facemorpher = False | |
def morph_faces(img1_path, img2_path, alpha=0.5): | |
img1 = cv2.imread(img1_path) | |
img2 = cv2.imread(img2_path) | |
if img1 is None or img2 is None: | |
raise ValueError("Error reading input images.") | |
img1 = cv2.resize(img1, (img2.shape[1], img2.shape[0])) | |
return cv2.addWeighted(img1, alpha, img2, 1 - alpha, 0) | |
def get_phoneme_image(phoneme): | |
phoneme_dir = "phoneme_images" | |
phoneme_map = { | |
"AA": "aa.jpg", | |
"EE": "ee.jpg", | |
"OO": "oo.jpg", | |
"MM": "mm.jpg", | |
"WW": "ww.jpg", | |
"NA": "na.jpg" | |
} | |
filename = phoneme_map.get(phoneme.upper()) | |
if filename is None: | |
raise ValueError(f"Invalid phoneme: {phoneme}") | |
path = os.path.join(phoneme_dir, filename) | |
if not os.path.exists(path): | |
raise FileNotFoundError(f"Image for phoneme '{phoneme}' not found at {path}") | |
return path | |
def interpolate_faces(phoneme1, phoneme2, steps=10): | |
img1_path = get_phoneme_image(phoneme1) | |
img2_path = get_phoneme_image(phoneme2) | |
output_dir = "morph_output" | |
os.makedirs(output_dir, exist_ok=True) | |
for i in range(steps + 1): | |
alpha = i / steps | |
if use_facemorpher: | |
morpher.morpher( | |
img1_path, img2_path, | |
plot=True, | |
alpha=alpha, | |
out_folder=output_dir, | |
out_frames=1, | |
gif=False | |
) | |
else: | |
result = morph_faces(img1_path, img2_path, alpha) | |
frame_path = os.path.join(output_dir, f"frame_{i:02d}.jpg") | |
cv2.imwrite(frame_path, result) | |
print(f"Saved frame {i} at alpha {alpha:.2f} to {frame_path}") | |
time.sleep(0.1) | |
if __name__ == "__main__": | |
interpolate_faces("AA", "EE", steps=10) | |
''' | |
with open("main.py", "w") as f: | |
f.write(main_code) | |
print("main.py created. Running the script...") | |
os.system(f"{sys.executable} main.py") | |
if __name__ == "__main__": | |
install_requirements() | |
create_and_run_main_py() |