### List Directory Contents Source: https://github.com/choyingw/synergynet/blob/main/synergy_demo.ipynb Lists the contents of the current directory after the setup steps. This is useful for verifying that files have been downloaded and built correctly. ```python % ls ``` -------------------------------- ### Basic CMake Project Setup Source: https://github.com/choyingw/synergynet/blob/main/Sim3DR/tests/CMakeLists.txt Sets the minimum CMake version, defines the target and project name, and configures C++ compiler flags for building the project. ```cmake cmake_minimum_required(VERSION 2.8) set(TARGET test) project(${TARGET}) ``` -------------------------------- ### Install SynergyNet Locally Source: https://github.com/choyingw/synergynet/blob/main/README.md Install SynergyNet in editable mode using pip. This allows importing the SynergyNet module from other directories. ```bash pip install -e . ``` -------------------------------- ### Run Benchmark Evaluation Source: https://github.com/choyingw/synergynet/blob/main/README.md Execute the benchmark script with a specified pretrained model. Results and visualizations for the first 50 examples are saved in the 'results/' directory. ```python benchmark.py -w pretrained/best.pth.tar ``` -------------------------------- ### Simplified API for SynergyNet Source: https://github.com/choyingw/synergynet/blob/main/README.md Demonstrates how to use the SynergyNet API to get 3D landmarks, mesh, and face pose from an image. Ensure you have the necessary data and pretrained weights. ```python import cv2 from synergy3DMM import SynergyNet model = SynergyNet() I = cv2.imread() # get landmark [[y, x, z], 68 (points)], mesh [[y, x, z], 53215 (points)], and face pose (Euler angles [yaw, pitch, roll] and translation [y, x, z]) lmks3d, mesh, pose = model.get_all_outputs(I) ``` -------------------------------- ### Activate Conda Environment Source: https://github.com/choyingw/synergynet/blob/main/README.md Activate the SynergyNet Conda environment before installing packages. ```conda conda activate SynergyNet ``` -------------------------------- ### Clone SynergyNet Repository Source: https://github.com/choyingw/synergynet/blob/main/synergy_demo.ipynb Clones the SynergyNet GitHub repository. This is the initial step to get the project files. ```python print('\n> Cloning the repo') !git clone https://github.com/choyingw/SynergyNet % cd SynergyNet ``` -------------------------------- ### Build Sim3DR Extensions Source: https://github.com/choyingw/synergynet/blob/main/Sim3DR/readme.md Builds the Cython extensions for Sim3DR in-place. Ensure you have the necessary build tools installed. ```shell python3 setup.py build_ext --inplace ``` -------------------------------- ### Import Core Libraries for SynergyNet Source: https://github.com/choyingw/synergynet/blob/main/synergy_demo.ipynb Imports essential libraries for SynergyNet, including PyTorch, torchvision, NumPy, OpenCV, and custom utilities for inference and model building. Ensure these libraries are installed in your environment. ```python import torch import torchvision.transforms as transforms import numpy as np import cv2 from utils.ddfa import ToTensor, Normalize from model_building import SynergyNet from utils.inference import crop_img, predict_sparseVert, draw_landmarks, predict_denseVert, predict_pose, draw_axis import argparse import torch.backends.cudnn as cudnn cudnn.benchmark = True import os import os.path as osp import glob from FaceBoxes import FaceBoxes from utils.render import render import scipy.io as sio ``` -------------------------------- ### Run Face Detection Demo Source: https://github.com/choyingw/synergynet/blob/main/FaceBoxes/readme.md Execute the FaceBoxes.py script using Python 3 to run the face detection demonstration. ```shell script python3 FaceBoxes.py ``` -------------------------------- ### Load and Display Image Outputs (Sample 1) Source: https://github.com/choyingw/synergynet/blob/main/synergy_demo.ipynb Loads the original image and its processed outputs (overlay, landmark, pose) for sample 1 using OpenCV and displays them. Requires `cv2_imshow` from `google.colab.patches`. ```python from google.colab.patches import cv2_imshow I_img = cv2.imread('img/sample_1.jpg', -1) I_overlay = cv2.imread('inference_output/rendering_overlay/sample_1.jpg', -1) I_landmark = cv2.imread('inference_output/landmarks/sample_1.jpg', -1) I_pose = cv2.imread('inference_output/poses/sample_1.jpg', -1) cv2_imshow(I_img) cv2_imshow(I_overlay) cv2_imshow(I_landmark) cv2_imshow(I_pose) ``` -------------------------------- ### Load and Display Image Outputs (Sample 2) Source: https://github.com/choyingw/synergynet/blob/main/synergy_demo.ipynb Loads the original image and its processed outputs (overlay, landmark, pose) for sample 2 using OpenCV and displays them. Requires `cv2_imshow` from `google.colab.patches`. ```python I_img = cv2.imread('img/sample_2.jpg', -1) I_overlay = cv2.imread('inference_output/rendering_overlay/sample_2.jpg', -1) I_landmark = cv2.imread('inference_output/landmarks/sample_2.jpg', -1) I_pose = cv2.imread('inference_output/poses/sample_2.jpg', -1) cv2_imshow(I_img) cv2_imshow(I_overlay) cv2_imshow(I_landmark) cv2_imshow(I_pose) ``` -------------------------------- ### Load and Display Image Outputs (Sample 3) Source: https://github.com/choyingw/synergynet/blob/main/synergy_demo.ipynb Loads the original image and its processed outputs (overlay, landmark, pose) for sample 3 using OpenCV and displays them. Requires `cv2_imshow` from `google.colab.patches`. ```python I_img = cv2.imread('img/sample_3.jpg', -1) I_overlay = cv2.imread('inference_output/rendering_overlay/sample_3.jpg', -1) I_landmark = cv2.imread('inference_output/landmarks/sample_3.jpg', -1) I_pose = cv2.imread('inference_output/poses/sample_3.jpg', -1) cv2_imshow(I_img) cv2_imshow(I_overlay) cv2_imshow(I_landmark) cv2_imshow(I_pose) ``` -------------------------------- ### Navigate to SynergyNet Directory Source: https://github.com/choyingw/synergynet/blob/main/README.md Change the current directory to the cloned SynergyNet repository. ```bash cd SynergyNet ``` -------------------------------- ### Execute Training Script Source: https://github.com/choyingw/synergynet/blob/main/README.md Run the training script. Hyperparameters like learning rate, epochs, and GPU device can be configured within the script. Adjust batch size and learning rate proportionally if GPU memory is limited. ```bash train_script.sh ``` -------------------------------- ### Load and Display Image Outputs (Sample 4) Source: https://github.com/choyingw/synergynet/blob/main/synergy_demo.ipynb Loads the original image and its processed outputs (overlay, landmark, pose) for sample 4 using OpenCV and displays them. Requires `cv2_imshow` from `google.colab.patches`. ```python I_img = cv2.imread('img/sample_4.jpg', -1) I_overlay = cv2.imread('inference_output/rendering_overlay/sample_4.jpg', -1) I_landmark = cv2.imread('inference_output/landmarks/sample_4.jpg', -1) I_pose = cv2.imread('inference_output/poses/sample_4.jpg', -1) cv2_imshow(I_img) cv2_imshow(I_overlay) cv2_imshow(I_landmark) cv2_imshow(I_pose) ``` -------------------------------- ### Build CPU Version of NMS Source: https://github.com/choyingw/synergynet/blob/main/FaceBoxes/readme.md Navigate to the utils directory and build the NMS extension in-place using Python 3. ```shell script cd utils python3 build.py build_ext --inplace ``` -------------------------------- ### Parse Command-Line Arguments for SynergyNet Source: https://github.com/choyingw/synergynet/blob/main/synergy_demo.ipynb Sets up argument parsing for the SynergyNet demo script. It defines options for input files, image format, image size, and batch size. ```python parser = argparse.ArgumentParser() parser.add_argument('-f', '--files', default='./img/', help='path to a single image or path to a folder containing multiple images') parser.add_argument("--png", action="store_true", help="if images are with .png extension") parser.add_argument('--img_size', default=120, type=int) parser.add_argument('-b', '--batch-size', default=1, type=int) args = parser.parse_args() args.files = 'img' main(args) ``` -------------------------------- ### Build CPU Version of NMS (Alternative) Source: https://github.com/choyingw/synergynet/blob/main/FaceBoxes/readme.md Alternatively, execute the build_cpu_nms.sh script to build the CPU version of NMS. ```shell script sh ./build_cpu_nms.sh ``` -------------------------------- ### Build Sim3DR and FaceBoxes Dependencies Source: https://github.com/choyingw/synergynet/blob/main/synergy_demo.ipynb Navigates into Sim3DR and FaceBoxes directories to build their respective dependencies using shell scripts. This step is crucial for the functionality of these components. ```python % cd Sim3DR !sh ./build_sim3dr.sh % cd ../FaceBoxes !sh ./build_cpu_nms.sh % cd .. ``` -------------------------------- ### Create Conda Environment Source: https://github.com/choyingw/synergynet/blob/main/README.md Create a new Conda environment named SynergyNet for project dependencies. ```conda conda create --name SynergyNet ``` -------------------------------- ### Process and Save Image Outputs Source: https://github.com/choyingw/synergynet/blob/main/synergy_demo.ipynb This section outlines the processing steps for each image, indicating the saving locations for mesh, landmark, and pose results. It is repeated for each sample image. ```text Process the image: img/sample_1.jpg Save mesh result to inference_output/rendering_overlay/sample_1.jpg Save landmark result to inference_output/landmarks/sample_1.jpg Save pose result to inference_output/poses/sample_1.jpg ``` ```text Process the image: img/sample_2.jpg Save mesh result to inference_output/rendering_overlay/sample_2.jpg Save landmark result to inference_output/landmarks/sample_2.jpg Save pose result to inference_output/poses/sample_2.jpg ``` ```text Process the image: img/sample_3.jpg Save mesh result to inference_output/rendering_overlay/sample_3.jpg Save landmark result to inference_output/landmarks/sample_3.jpg Save pose result to inference_output/poses/sample_3.jpg ``` ```text Process the image: img/sample_4.jpg Save mesh result to inference_output/rendering_overlay/sample_4.jpg Save landmark result to inference_output/landmarks/sample_4.jpg Save pose result to inference_output/poses/sample_4.jpg ``` -------------------------------- ### Generate Textured Artistic Face Meshes (Folder) Source: https://github.com/choyingw/synergynet/blob/main/README.md Process all images in a folder to generate textured artistic face meshes. Ensure artistic faces data and predicted UV maps are extracted in the root folder. ```python artistic.py -f art-all --png ``` -------------------------------- ### Compile FaceBoxes NMS Source: https://github.com/choyingw/synergynet/blob/main/README.md Compile the CPU NMS module for FaceBoxes. Navigate to the FaceBoxes directory first. ```bash cd ../FaceBoxes ``` ```bash ./build_cpu_nms.sh ``` ```bash cd .. ``` -------------------------------- ### SynergyNet Simplified API Usage Source: https://github.com/choyingw/synergynet/blob/main/README.md This snippet demonstrates how to import and use the SynergyNet model for single image inference. It shows how to load an image and obtain 3D landmarks, mesh, and face pose. ```APIDOC ## SynergyNet Simplified API We provide a simple API for convenient usage if you want to plug in this method into your work. ```python import cv2 from synergy3DMM import SynergyNet model = SynergyNet() I = cv2.imread() # get landmark [[y, x, z], 68 (points)], mesh [[y, x, z], 53215 (points)], and face pose (Euler angles [yaw, pitch, roll] and translation [y, x, z]) lmks3d, mesh, pose = model.get_all_outputs(I) ``` To use this API, ensure you have installed the library using `pip install -e .` and have downloaded the necessary 3DMM data and pretrained weights. You can then import `SynergyNet` from `synergy3DMM` in your Python projects. ``` -------------------------------- ### Run Single Image Inference Source: https://github.com/choyingw/synergynet/blob/main/README.md Execute the single image inference script with the 'img' directory as input. A compatible GPU is recommended. ```python python singleImage.py -f img ``` -------------------------------- ### Compile Sim3DR Source: https://github.com/choyingw/synergynet/blob/main/README.md Compile the Sim3DR module. Ensure you are in the Sim3DR directory. ```bash cd Sim3DR ``` ```bash ./build_sim3dr.sh ``` -------------------------------- ### Download Additional Data Source: https://github.com/choyingw/synergynet/blob/main/synergy_demo.ipynb Downloads another required data file using gdown. This is a supplementary data download step. ```python !gdown --id 1BVHbiLTfX6iTeJcNbh-jgHjWDoemfrzG ``` -------------------------------- ### Include Directories Source: https://github.com/choyingw/synergynet/blob/main/Sim3DR/tests/CMakeLists.txt Specifies private include directories for the target, adding the project's source directory to the include path. ```cmake target_include_directories(${TARGET} PRIVATE ${PROJECT_SOURCE_DIR}) ``` -------------------------------- ### Generate Textured Artistic Face Meshes (Single Image) Source: https://github.com/choyingw/synergynet/blob/main/README.md Process a single image to generate a textured artistic face mesh. Ensure artistic faces data and predicted UV maps are extracted in the root folder. ```python artistic.py -f art-all/122.png ``` -------------------------------- ### Run Inference on a Folder of Real Faces Source: https://github.com/choyingw/synergynet/blob/main/README.md Execute the inference script on an entire folder containing real face images. The results will be saved in the 'inference_output' directory. ```python uv_texture_realFaces.py -f texture_data/real --png ``` -------------------------------- ### Move Model Weights Source: https://github.com/choyingw/synergynet/blob/main/synergy_demo.ipynb Moves the downloaded 'best.pth.tar' model weights file to the 'pretrained' directory. This is a common step after downloading model checkpoints. ```python !mv best.pth.tar ./pretrained % ls ``` -------------------------------- ### Clone SynergyNet Repository Source: https://github.com/choyingw/synergynet/blob/main/README.md Clone the SynergyNet repository from GitHub. ```git git clone https://github.com/choyingw/SynergyNet ``` -------------------------------- ### Run Inference on a Single Real Face Image Source: https://github.com/choyingw/synergynet/blob/main/README.md Execute the inference script on a single real face image. The results will be saved in the 'inference_output' directory. ```python uv_texture_realFaces.py -f texture_data/real/image00002_real_A.png ``` -------------------------------- ### Compile CPU NMS Extension Source: https://github.com/choyingw/synergynet/blob/main/synergy_demo.ipynb Compiles the CPU Non-Maximum Suppression (NMS) extension using g++. This is often a necessary step for performance-critical components. ```bash x86_64-linux-gnu-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -g -fwrapv -O2 -Wl,-Bsymbolic-functions -g -fwrapv -O2 -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 build/temp.linux-x86_64-3.7/nms/cpu_nms.o -o /content/SynergyNet/FaceBoxes/utils/nms/cpu_nms.cpython-37m-x86_64-linux-gnu.so ``` -------------------------------- ### Download and Unzip 3DMM Data Source: https://github.com/choyingw/synergynet/blob/main/synergy_demo.ipynb Downloads the 3DMM data using gdown and then unzips the downloaded file. This data is required for 3D model fitting. ```python !gdown --id 1SQsMhvAmpD1O8Hm0yEGom0C0rXtA0qs8 !unzip -n 3dmm_data ``` -------------------------------- ### Run SynergyNet Face Analysis Pipeline Source: https://github.com/choyingw/synergynet/blob/main/synergy_demo.ipynb This function orchestrates the entire face analysis process, from loading the model to saving inference outputs. It requires a pre-trained model checkpoint and handles image loading, face detection, and result visualization. ```python # Following 3DDFA-V2, we also use 120x120 resolution IMG_SIZE = 120 def main(args): # load pre-tained model checkpoint_fp = 'pretrained/best.pth.tar' args.arch = 'mobilenet_v2' args.devices_id = [0] checkpoint = torch.load(checkpoint_fp, map_location=lambda storage, loc: storage)['state_dict'] model = SynergyNet(args) model_dict = model.state_dict() # because the model is trained by multiple gpus, prefix 'module' should be removed for k in checkpoint.keys(): model_dict[k.replace('module.', '')] = checkpoint[k] model.load_state_dict(model_dict, strict=False) model = model.cuda() model.eval() # face detector face_boxes = FaceBoxes() # preparation transform = transforms.Compose([ToTensor(), Normalize(mean=127.5, std=128)]) if osp.isdir(args.files): if not args.files[-1] == '/': args.files = args.files + '/' if not args.png: files = sorted(glob.glob(args.files+'*.jpg')) else: files = sorted(glob.glob(args.files+'*.png')) else: files = [args.files] def _to_ctype(arr): if not arr.flags.c_contiguous: return arr.copy(order='C') return arr tri = sio.loadmat('./3dmm_data/tri.mat')['tri'] - 1 for img_fp in files: print("Process the image: ", img_fp) img_ori = cv2.imread(img_fp) # crop faces rects = face_boxes(img_ori) # storage pts_res = [] poses = [] vertices_lst = [] for idx, rect in enumerate(rects): roi_box = rect # enlarge the bbox a little and do a square crop HCenter = (rect[1] + rect[3])/2 WCenter = (rect[0] + rect[2])/2 side_len = roi_box[3]-roi_box[1] margin = side_len * 1.2 // 2 roi_box[0], roi_box[1], roi_box[2], roi_box[3] = WCenter-margin, HCenter-margin, WCenter+margin, HCenter+margin img = crop_img(img_ori, roi_box) img = cv2.resize(img, dsize=(IMG_SIZE, IMG_SIZE), interpolation=cv2.INTER_LINEAR) # cv2.imwrite(f'validate_{idx}.png', img) input = transform(img).unsqueeze(0) with torch.no_grad(): input = input.cuda() param = model.forward_test(input) param = param.squeeze().cpu().numpy().flatten().astype(np.float32) # inferences lmks = predict_sparseVert(param, roi_box, transform=True) vertices = predict_denseVert(param, roi_box, transform=True) angles, translation = predict_pose(param, roi_box) pts_res.append(lmks) vertices_lst.append(vertices) poses.append([angles, translation, lmks]) if not osp.exists(f'inference_output/rendering_overlay/'): os.makedirs(f'inference_output/rendering_overlay/') if not osp.exists(f'inference_output/landmarks/'): os.makedirs(f'inference_output/landmarks/') if not osp.exists(f'inference_output/poses/'): os.makedirs(f'inference_output/poses/') name = img_fp.rsplit('/',1)[-1][:-4] img_ori_copy = img_ori.copy() # mesh render(img_ori, vertices_lst, alpha=0.6, wfp=f'inference_output/rendering_overlay/{name}.jpg', connectivity=tri) # landmarks draw_landmarks(img_ori_copy, pts_res, wfp=f'inference_output/landmarks/{name}.jpg') # face orientation img_axis_plot = img_ori_copy for angles, translation, lmks in poses: img_axis_plot = draw_axis(img_axis_plot, angles[0], angles[1], angles[2], translation[0], translation[1], size = 50, pts68=lmks) wfp = f'inference_output/poses/{name}.jpg' cv2.imwrite(wfp, img_axis_plot) print(f'Save pose result to {wfp}') ``` -------------------------------- ### Compiler Flags and Executable Definition Source: https://github.com/choyingw/synergynet/blob/main/Sim3DR/tests/CMakeLists.txt Configures C++ compiler flags to enable Position Independent Code (PIC) and C++11 standard, and adds source files to build the executable target. ```cmake #set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC -O3") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -std=c++11") add_executable(${TARGET} test.cpp rasterize_kernel.cpp io.cpp) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.