liamsch commited on
Commit
35578c3
·
1 Parent(s): df39e39

updates for hf space

Browse files
Files changed (6) hide show
  1. app.py +0 -9
  2. convert_flame.py +0 -68
  3. demo.py +6 -1
  4. gradio_demo.py +8 -2
  5. packages.txt +9 -0
  6. pyproject.toml +0 -52
app.py DELETED
@@ -1,9 +0,0 @@
1
- """
2
- Hugging Face Space entry point for SHeaP demo.
3
- This file imports and runs the gradio demo.
4
- """
5
-
6
- from gradio_demo import demo
7
-
8
- if __name__ == "__main__":
9
- demo.launch()
 
 
 
 
 
 
 
 
 
 
convert_flame.py DELETED
@@ -1,68 +0,0 @@
1
- """
2
- Converts FLAME pickle files to PyTorch .pt files.
3
- """
4
-
5
- import argparse
6
- from pathlib import Path
7
- from typing import Union
8
-
9
- import torch
10
-
11
- from sheap.load_flame_pkl import load_pkl_format_flame_model
12
-
13
-
14
- def convert_flame(flame_base_dir: Union[str, Path], overwrite: bool) -> None:
15
- """Convert FLAME pickle files to PyTorch .pt files.
16
-
17
- Searches for all .pkl files in the FLAME base directory and converts them to
18
- PyTorch .pt format, skipping certain mask files.
19
-
20
- Args:
21
- flame_base_dir: Path to the FLAME model directory containing pickle files.
22
- overwrite: Whether to overwrite existing .pt files if they already exist.
23
-
24
- Raises:
25
- AssertionError: If flame_base_dir does not exist.
26
- """
27
- flame_base_dir = Path(flame_base_dir)
28
- assert flame_base_dir is not None # for mypy
29
- assert flame_base_dir.exists(), (
30
- f"FLAME_BASE_DIR not found at {flame_base_dir}. "
31
- "Please set arg flame_base_dir to the FLAME model directory, "
32
- " or set the FLAME_BASE_DIR environment variable."
33
- )
34
- pickle_files = list(flame_base_dir.glob("**/**/*.pkl"))
35
- skip_files = ["FLAME_masks.pkl"]
36
- for model_path in pickle_files:
37
- if model_path.name in skip_files:
38
- continue
39
- print(f"Converting {model_path}...")
40
- data = load_pkl_format_flame_model(model_path)
41
- new_path = model_path.with_suffix(".pt")
42
- if new_path.exists() and not overwrite:
43
- print(f"Skipping {new_path} because it already exists.")
44
- continue
45
- torch.save(data, new_path)
46
- print(f"Saved {new_path}")
47
-
48
-
49
- def main() -> None:
50
- """Parse command-line arguments and convert FLAME pickle files to PyTorch format."""
51
- parser = argparse.ArgumentParser()
52
- parser.add_argument(
53
- "--flame_base_dir",
54
- type=str,
55
- help="Path to the FLAME model directory. "
56
- "Defaults to the FLAME_BASE_DIR environment variable.",
57
- default="FLAME2020/",
58
- )
59
- parser.add_argument(
60
- "--overwrite",
61
- action="store_true",
62
- help="Overwrite existing files if they already exist.",
63
- )
64
- convert_flame(**vars(parser.parse_args()))
65
-
66
-
67
- if __name__ == "__main__":
68
- main()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
demo.py CHANGED
@@ -32,7 +32,12 @@ def create_rendering_image(
32
  PIL Image with three views side-by-side (original, mesh, blended)
33
  """
34
  # Render the mesh
35
- color, depth = render_mesh(verts=verts, faces=faces, c2w=c2w)
 
 
 
 
 
36
 
37
  # Resize original to match output size
38
  original_resized = original_image.convert("RGB").resize((output_size, output_size))
 
32
  PIL Image with three views side-by-side (original, mesh, blended)
33
  """
34
  # Render the mesh
35
+ try:
36
+ color, depth = render_mesh(verts=verts, faces=faces, c2w=c2w)
37
+ except Exception as e:
38
+ print(f"WARNING: Rendering failed ({e}), returning original image only", flush=True)
39
+ # Return just the original image if rendering fails
40
+ return original_image.convert("RGB").resize((output_size, output_size))
41
 
42
  # Resize original to match output size
43
  original_resized = original_image.convert("RGB").resize((output_size, output_size))
gradio_demo.py CHANGED
@@ -4,6 +4,14 @@ Accepts video or image input and renders the SHEAP output overlayed.
4
  """
5
 
6
  import os
 
 
 
 
 
 
 
 
7
  import shutil
8
  import subprocess
9
  import tempfile
@@ -30,8 +38,6 @@ except ImportError:
30
  )
31
  from sheap.fa_landmark_utils import detect_face_and_crop
32
 
33
- os.environ["PYOPENGL_PLATFORM"] = "egl"
34
-
35
  # Global variables for models (load once)
36
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
37
  sheap_model = None
 
4
  """
5
 
6
  import os
7
+
8
+ # CRITICAL: Set PYOPENGL_PLATFORM before any OpenGL/pyrender imports
9
+ # This must happen before importing demo.py or sheap.render
10
+ # Note: HF Spaces with GPU will have EGL available, use osmesa for CPU-only environments
11
+ if "PYOPENGL_PLATFORM" not in os.environ:
12
+ # Default to egl (works on HF Spaces with GPU)
13
+ os.environ["PYOPENGL_PLATFORM"] = "egl"
14
+
15
  import shutil
16
  import subprocess
17
  import tempfile
 
38
  )
39
  from sheap.fa_landmark_utils import detect_face_and_crop
40
 
 
 
41
  # Global variables for models (load once)
42
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
43
  sheap_model = None
packages.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ ffmpeg
2
+ libsm6
3
+ libxext6
4
+ libxrender1
5
+ libgomp1
6
+ libglib2.0-0
7
+ libgl1
8
+ libegl1
9
+ libgles2
pyproject.toml DELETED
@@ -1,52 +0,0 @@
1
- [build-system]
2
- requires = ["hatchling"]
3
- build-backend = "hatchling.build"
4
-
5
- [project]
6
- name = "sheap"
7
- version = "0.1.0"
8
- description = "SHeaP: Self-Supervised Head Geometry Predictor Learned via 2D Gaussians"
9
- readme = "README.md"
10
- requires-python = ">=3.11"
11
- license = { file = "LICENSE.txt" }
12
- authors = [
13
- { name = "Liam Schoneveld" }
14
- ]
15
- keywords = ["3d", "face", "flame", "head", "mesh", "reconstruction"]
16
- classifiers = [
17
- "Development Status :: 3 - Alpha",
18
- "Intended Audience :: Developers",
19
- "Intended Audience :: Science/Research",
20
- "Programming Language :: Python :: 3",
21
- "Programming Language :: Python :: 3.11",
22
- "Programming Language :: Python :: 3.12",
23
- "Topic :: Scientific/Engineering :: Artificial Intelligence",
24
- ]
25
- dependencies = [
26
- "chumpy @ git+https://github.com/nlml/chumpy.git",
27
- "numpy>=1.20.0",
28
- "pillow>=9.0.0",
29
- "pyrender>=0.1.45",
30
- "roma>=1.5.4",
31
- "scipy>=1.16.3",
32
- "torch>=2.0.0",
33
- "torchaudio>=2.0.0",
34
- "torchvision>=0.15.1",
35
- "tqdm>=4.67.1",
36
- "trimesh>=4.9.0",
37
- ]
38
-
39
- [project.urls]
40
- Homepage = "https://nlml.github.io/sheap"
41
- Repository = "https://github.com/nlml/sheap"
42
-
43
- [dependency-groups]
44
- dev = [
45
- "pre-commit>=4.3.0",
46
- ]
47
-
48
- [tool.hatch.metadata]
49
- allow-direct-references = true
50
-
51
- [tool.hatch.build.targets.wheel]
52
- packages = ["sheap"]