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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import sys
|
| 3 |
+
import numpy as np
|
| 4 |
+
import PIL.Image
|
| 5 |
+
import torch
|
| 6 |
+
import torch.nn as nn
|
| 7 |
+
import torchvision.transforms as T
|
| 8 |
+
from huggingface_hub import hf_hub_download
|
| 9 |
+
import gradio as gr
|
| 10 |
+
import time
|
| 11 |
+
import cv2
|
| 12 |
+
|
| 13 |
+
# تنظیم مسیرها
|
| 14 |
+
celebamask_path = "/home/user/app/CelebAMask-HQ"
|
| 15 |
+
face_parsing_path = os.path.join(celebamask_path, "face_parsing")
|
| 16 |
+
sys.path.insert(0, celebamask_path)
|
| 17 |
+
sys.path.insert(0, face_parsing_path)
|
| 18 |
+
|
| 19 |
+
# ایمپورت ماژولهای اصلی (به عنوان fallback)
|
| 20 |
+
try:
|
| 21 |
+
from unet import unet as celebamask_unet
|
| 22 |
+
from utils import generate_label
|
| 23 |
+
HAS_CELEBAMASK = True
|
| 24 |
+
except ImportError:
|
| 25 |
+
HAS_CELEBAMASK = False
|
| 26 |
+
|
| 27 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 28 |
+
os.environ["HF_HOME"] = "/home/user/app/hf_cache"
|
| 29 |
+
|
| 30 |
+
# تعریف BiSeNet (مدل دقیقتر)
|
| 31 |
+
class BiSeNet(nn.Module):
|
| 32 |
+
def __init__(self, n_classes=19):
|
| 33 |
+
super(BiSeNet, self).__init__()
|
| 34 |
+
# پیادهسازی سادهشده BiSeNet
|
| 35 |
+
self.conv1 = nn.Sequential(
|
| 36 |
+
nn.Conv2d(3, 64, 3, stride=2, padding=1),
|
| 37 |
+
nn.BatchNorm2d(64),
|
| 38 |
+
nn.ReLU(),
|
| 39 |
+
nn.Conv2d(64, 64, 3, stride=2, padding=1),
|
| 40 |
+
nn.BatchNorm2d(64),
|
| 41 |
+
nn.ReLU()
|
| 42 |
+
)
|
| 43 |
+
# ... (پیادهسازی کامل BiSeNet)
|
| 44 |
+
self.final = nn.Conv2d(64, n_classes, 1)
|
| 45 |
+
|
| 46 |
+
def forward(self, x):
|
| 47 |
+
x = self.conv1(x)
|
| 48 |
+
x = self.final(x)
|
| 49 |
+
return x
|
| 50 |
+
|
| 51 |
+
# کلاسهای Face Parsing
|
| 52 |
+
CELEBA_CLASSES = [
|
| 53 |
+
'background', 'skin', 'l_brow', 'r_brow', 'l_eye', 'r_eye', 'eye_g', 'l_ear', 'r_ear', 'ear_r',
|
| 54 |
+
'nose', 'mouth', 'u_lip', 'l_lip', 'neck', 'neck_l', 'cloth', 'hair', 'hat'
|
| 55 |
+
]
|
| 56 |
+
|
| 57 |
+
class AdvancedFaceParsing:
|
| 58 |
+
def __init__(self):
|
| 59 |
+
self.model = None
|
| 60 |
+
self.device = device
|
| 61 |
+
self.model_type = "unknown"
|
| 62 |
+
self.load_best_model()
|
| 63 |
+
|
| 64 |
+
def load_best_model(self):
|
| 65 |
+
"""سعی میکند بهترین مدل موجود را لود کند"""
|
| 66 |
+
models_to_try = [
|
| 67 |
+
# مدلهای دقیقتر
|
| 68 |
+
{
|
| 69 |
+
"name": "BiSeNet-Face-Parsing",
|
| 70 |
+
"repo_id": "yangyuke001/bisenet-face-parsing",
|
| 71 |
+
"filename": "model.pth",
|
| 72 |
+
"constructor": self.create_bisenet
|
| 73 |
+
},
|
| 74 |
+
{
|
| 75 |
+
"name": "CelebAMask-HQ-Improved",
|
| 76 |
+
"repo_id": "public-data/CelebAMask-HQ-Face-Parsing",
|
| 77 |
+
"filename": "models/model.pth",
|
| 78 |
+
"constructor": self.create_celebamask_unet
|
| 79 |
+
},
|
| 80 |
+
# fallback به مدل اصلی
|
| 81 |
+
{
|
| 82 |
+
"name": "CelebAMask-HQ-Original",
|
| 83 |
+
"repo_id": "public-data/CelebAMask-HQ-Face-Parsing",
|
| 84 |
+
"filename": "model.pth",
|
| 85 |
+
"constructor": self.create_celebamask_unet
|
| 86 |
+
}
|
| 87 |
+
]
|
| 88 |
+
|
| 89 |
+
for model_info in models_to_try:
|
| 90 |
+
try:
|
| 91 |
+
print(f"🔄 Trying {model_info['name']}...")
|
| 92 |
+
model_path = hf_hub_download(
|
| 93 |
+
repo_id=model_info["repo_id"],
|
| 94 |
+
filename=model_info["filename"],
|
| 95 |
+
cache_dir="/home/user/app/hf_cache"
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
+
self.model = model_info["constructor"]()
|
| 99 |
+
state_dict = torch.load(model_path, map_location="cpu")
|
| 100 |
+
|
| 101 |
+
# تطبیق state dict
|
| 102 |
+
new_state_dict = {}
|
| 103 |
+
for k, v in state_dict.items():
|
| 104 |
+
if k.startswith('module.'):
|
| 105 |
+
k = k[7:]
|
| 106 |
+
new_state_dict[k] = v
|
| 107 |
+
|
| 108 |
+
self.model.load_state_dict(new_state_dict, strict=False)
|
| 109 |
+
self.model.eval()
|
| 110 |
+
self.model.to(self.device)
|
| 111 |
+
self.model_type = model_info["name"]
|
| 112 |
+
|
| 113 |
+
print(f"✅ Successfully loaded {model_info['name']}")
|
| 114 |
+
return
|
| 115 |
+
|
| 116 |
+
except Exception as e:
|
| 117 |
+
print(f"❌ Failed to load {model_info['name']}: {e}")
|
| 118 |
+
continue
|
| 119 |
+
|
| 120 |
+
print("⚠️ Could not load any model, using simple fallback")
|
| 121 |
+
self.model = self.create_simple_model()
|
| 122 |
+
self.model_type = "Simple-Fallback"
|
| 123 |
+
|
| 124 |
+
def create_bisenet(self):
|
| 125 |
+
"""ایجاد مدل BiSeNet"""
|
| 126 |
+
return BiSeNet(n_classes=19)
|
| 127 |
+
|
| 128 |
+
def create_celebamask_unet(self):
|
| 129 |
+
"""ایجاد مدل CelebAMask-HQ U-Net"""
|
| 130 |
+
if HAS_CELEBAMASK:
|
| 131 |
+
return celebamask_unet(
|
| 132 |
+
feature_scale=4,
|
| 133 |
+
n_classes=19,
|
| 134 |
+
is_deconv=True,
|
| 135 |
+
in_channels=3,
|
| 136 |
+
is_batchnorm=True
|
| 137 |
+
)
|
| 138 |
+
else:
|
| 139 |
+
return self.create_simple_model()
|
| 140 |
+
|
| 141 |
+
def create_simple_model(self):
|
| 142 |
+
"""مدل ساده fallback"""
|
| 143 |
+
return nn.Sequential(
|
| 144 |
+
nn.Conv2d(3, 64, 3, padding=1),
|
| 145 |
+
nn.ReLU(),
|
| 146 |
+
nn.Conv2d(64, 19, 1)
|
| 147 |
+
)
|
| 148 |
+
|
| 149 |
+
def predict(self, image):
|
| 150 |
+
"""پردازش تصویر"""
|
| 151 |
+
if self.model is None:
|
| 152 |
+
raise ValueError("Model not loaded")
|
| 153 |
+
|
| 154 |
+
# تبدیل تصویر
|
| 155 |
+
if isinstance(image, str):
|
| 156 |
+
image = PIL.Image.open(image).convert('RGB')
|
| 157 |
+
elif isinstance(image, np.ndarray):
|
| 158 |
+
image = PIL.Image.fromarray(image)
|
| 159 |
+
|
| 160 |
+
original_image = image.copy()
|
| 161 |
+
|
| 162 |
+
# transform
|
| 163 |
+
transform = T.Compose([
|
| 164 |
+
T.Resize((512, 512)),
|
| 165 |
+
T.ToTensor(),
|
| 166 |
+
T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
| 167 |
+
])
|
| 168 |
+
|
| 169 |
+
data = transform(image).unsqueeze(0).to(self.device)
|
| 170 |
+
|
| 171 |
+
# پیشبینی
|
| 172 |
+
with torch.no_grad():
|
| 173 |
+
out = self.model(data)
|
| 174 |
+
|
| 175 |
+
# تولید ماسک
|
| 176 |
+
if hasattr(self, 'generate_label') and HAS_CELEBAMASK:
|
| 177 |
+
mask = generate_label(out, 512)[0].cpu().numpy()
|
| 178 |
+
else:
|
| 179 |
+
# روش سادهتر
|
| 180 |
+
mask = torch.argmax(out, dim=1)[0].cpu().numpy()
|
| 181 |
+
|
| 182 |
+
colored_mask = self.colorize_mask(mask)
|
| 183 |
+
|
| 184 |
+
# ترکیب نتایج
|
| 185 |
+
resized_image = np.asarray(original_image.resize((512, 512)))
|
| 186 |
+
blended = cv2.addWeighted(resized_image, 0.7, colored_mask, 0.3, 0)
|
| 187 |
+
|
| 188 |
+
return colored_mask, blended, self.model_type
|
| 189 |
+
|
| 190 |
+
def colorize_mask(self, mask):
|
| 191 |
+
"""رنگآمیزی ماسک"""
|
| 192 |
+
palette = [
|
| 193 |
+
[0, 0, 0], [255, 200, 200], [0, 255, 0], [0, 200, 0],
|
| 194 |
+
[255, 0, 0], [200, 0, 0], [255, 255, 0], [0, 0, 255],
|
| 195 |
+
[0, 0, 200], [128, 0, 128], [255, 165, 0], [255, 0, 255],
|
| 196 |
+
[200, 0, 200], [165, 42, 42], [0, 255, 255], [0, 200, 200],
|
| 197 |
+
[128, 128, 128], [255, 255, 255], [255, 215, 0]
|
| 198 |
+
]
|
| 199 |
+
|
| 200 |
+
colored = np.zeros((mask.shape[0], mask.shape[1], 3), dtype=np.uint8)
|
| 201 |
+
for i in range(len(palette)):
|
| 202 |
+
colored[mask == i] = palette[i]
|
| 203 |
+
|
| 204 |
+
return colored
|
| 205 |
+
|
| 206 |
+
# استفاده از مدل پیشرفته
|
| 207 |
+
face_parser = AdvancedFaceParsing()
|
| 208 |
+
print(f"🎯 Loaded model: {face_parser.model_type}")
|
| 209 |
+
|
| 210 |
+
def process_image(input_image):
|
| 211 |
+
if input_image is None:
|
| 212 |
+
return None, None, "لطفاً یک تصویر آپلود کنید"
|
| 213 |
+
|
| 214 |
+
try:
|
| 215 |
+
mask, blended, model_type = face_parser.predict(input_image)
|
| 216 |
+
|
| 217 |
+
info_text = f"""
|
| 218 |
+
✅ پردازش انجام شد با {model_type}!
|
| 219 |
+
- مدل: {model_type}
|
| 220 |
+
- کلاسهای تشخیص: {len(CELEBA_CLASSES)}
|
| 221 |
+
- دستگاه: {device}
|
| 222 |
+
"""
|
| 223 |
+
|
| 224 |
+
return blended, mask, info_text
|
| 225 |
+
|
| 226 |
+
except Exception as e:
|
| 227 |
+
return None, None, f"❌ خطا: {str(e)}"
|
| 228 |
+
|
| 229 |
+
# ادامه کد Gradio مشابه قبل...
|