| 94 | from tqdm import tqdm |
| 95 | |
| 96 | class LP_Engine: |
| 97 | pipeline = None |
| 98 | detect_model = None |
| 99 | mask_img = None |
| 100 | temp_img_idx = 0 |
| 101 | |
| 102 | def get_temp_img_name(self): |
| 103 | self.temp_img_idx += 1 |
| 104 | return "expression_edit_preview" + str(self.temp_img_idx) + ".png" |
| 105 | |
| 106 | def download_model(_, file_path, model_url): |
| 107 | print('AdvancedLivePortrait: Downloading model...') |
| 108 | response = requests.get(model_url, stream=True) |
| 109 | try: |
| 110 | if response.status_code == 200: |
| 111 | total_size = int(response.headers.get('content-length', 0)) |
| 112 | block_size = 1024 # 1 Kibibyte |
| 113 | |
| 114 | # tqdm will display a progress bar |
| 115 | with open(file_path, 'wb') as file, tqdm( |
| 116 | desc='Downloading', |
| 117 | total=total_size, |
| 118 | unit='iB', |
| 119 | unit_scale=True, |
| 120 | unit_divisor=1024, |
| 121 | ) as bar: |
| 122 | for data in response.iter_content(block_size): |
| 123 | bar.update(len(data)) |
| 124 | file.write(data) |
| 125 | |
| 126 | except requests.exceptions.RequestException as err: |
| 127 | print('AdvancedLivePortrait: Model download failed: {err}') |
| 128 | print(f'AdvancedLivePortrait: Download it manually from: {model_url}') |
| 129 | print(f'AdvancedLivePortrait: And put it in {file_path}') |
| 130 | except Exception as e: |
| 131 | print(f'AdvancedLivePortrait: An unexpected error occurred: {e}') |
| 132 | |
| 133 | def remove_ddp_dumplicate_key(_, state_dict): |
| 134 | state_dict_new = OrderedDict() |
| 135 | for key in state_dict.keys(): |
| 136 | state_dict_new[key.replace('module.', '')] = state_dict[key] |
| 137 | return state_dict_new |
| 138 | |
| 139 | def filter_for_model(_, checkpoint, prefix): |
| 140 | filtered_checkpoint = {key.replace(prefix + "_module.", ""): value for key, value in checkpoint.items() if |
| 141 | key.startswith(prefix)} |
| 142 | return filtered_checkpoint |
| 143 | |
| 144 | def load_model(self, model_config, model_type): |
| 145 | |
| 146 | device = get_device() |
| 147 | |
| 148 | if model_type == 'stitching_retargeting_module': |
| 149 | ckpt_path = os.path.join(get_model_dir("liveportrait"), "retargeting_models", model_type + ".pth") |
| 150 | else: |
| 151 | ckpt_path = os.path.join(get_model_dir("liveportrait"), "base_models", model_type + ".pth") |
| 152 | |
| 153 | is_safetensors = None |