Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,299 +1,872 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
-
import
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
| 24 |
def __init__(self):
|
| 25 |
-
self.
|
| 26 |
-
|
| 27 |
-
self.loaded_model_name = None
|
| 28 |
-
|
| 29 |
-
def preprocess_image_pil(self, image, target_size=512):
|
| 30 |
-
"""
|
| 31 |
-
ู
ุนุงูุฌุฉ ุงูุตูุฑุฉ ุจุงุณุชุฎุฏุงู
PIL ููุท (ุจุฏูู ูู OpenCV)
|
| 32 |
-
"""
|
| 33 |
-
if isinstance(image, np.ndarray):
|
| 34 |
-
image = Image.fromarray(image.astype('uint8'))
|
| 35 |
-
|
| 36 |
-
# ุชุบููุฑ ุงูุญุฌู
ู
ุน ุงูุญูุงุธ ุนูู ุงููุณุจุฉ
|
| 37 |
-
image.thumbnail((target_size, target_size), Image.Resampling.LANCZOS)
|
| 38 |
-
|
| 39 |
-
# ุฅูุดุงุก ุตูุฑุฉ ู
ุฑุจุนุฉ ู
ุน padding
|
| 40 |
-
new_image = Image.new('RGB', (target_size, target_size), (255, 255, 255))
|
| 41 |
-
paste_x = (target_size - image.width) // 2
|
| 42 |
-
paste_y = (target_size - image.height) // 2
|
| 43 |
-
new_image.paste(image, (paste_x, paste_y))
|
| 44 |
-
|
| 45 |
-
return np.array(new_image)
|
| 46 |
|
| 47 |
-
def
|
| 48 |
-
"""
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
model = torch.hub.load(
|
| 58 |
-
"bryandlee/animegan2-pytorch:main",
|
| 59 |
-
"generator",
|
| 60 |
-
pretrained=style,
|
| 61 |
-
device="cpu",
|
| 62 |
-
progress=True,
|
| 63 |
-
trust_repo=True
|
| 64 |
-
)
|
| 65 |
-
model.eval()
|
| 66 |
-
|
| 67 |
-
print(f"โ
ุชู
ุชุญู
ูู AnimeGANv2 ุจูุฌุงุญ!")
|
| 68 |
-
return model, None
|
| 69 |
-
|
| 70 |
-
except Exception as e:
|
| 71 |
-
error_msg = f"ุฎุทุฃ ูู ุชุญู
ูู AnimeGANv2: {str(e)}"
|
| 72 |
-
print(f"โ {error_msg}")
|
| 73 |
-
return None, error_msg
|
| 74 |
|
| 75 |
-
def
|
| 76 |
-
"""
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
image_tensor = torch.from_numpy(image).permute(2, 0, 1).unsqueeze(0).float()
|
| 88 |
-
image_tensor = image_tensor / 127.5 - 1.0 # Normalize to [-1, 1]
|
| 89 |
-
|
| 90 |
-
# ุงูุชุญููู
|
| 91 |
-
with torch.no_grad():
|
| 92 |
-
output = model(image_tensor)
|
| 93 |
-
|
| 94 |
-
# ุชุญููู ุงููุชูุฌุฉ ุฅูู ุตูุฑุฉ
|
| 95 |
-
output = output.squeeze(0).permute(1, 2, 0).cpu().numpy()
|
| 96 |
-
output = (output + 1.0) * 127.5
|
| 97 |
-
output = np.clip(output, 0, 255).astype(np.uint8)
|
| 98 |
-
|
| 99 |
-
return output, None
|
| 100 |
-
|
| 101 |
-
except Exception as e:
|
| 102 |
-
return None, f"ุฎุทุฃ ูู ุงูู
ุนุงูุฌุฉ: {str(e)}"
|
| 103 |
|
| 104 |
-
def
|
| 105 |
-
"""
|
| 106 |
-
|
| 107 |
-
"""
|
| 108 |
-
if image is None:
|
| 109 |
-
return None, "โ ๏ธ ู
ู ูุถูู ุงุฑูุน ุตูุฑุฉ ุฃููุงู"
|
| 110 |
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
|
| 169 |
-
|
| 170 |
-
|
|
|
|
|
|
|
|
|
|
| 171 |
|
| 172 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
|
| 178 |
-
|
| 179 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
)
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
)
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
4. ุงุถุบุท Commit ูุงูุชุธุฑ ุงูุจูุงุก!
|
| 270 |
-
|
| 271 |
-
โญ **ู
ูู
**: ุงูุชุญู
ูู ุงูุฃูู ูููู
ูุฐุฌ ูุฏ ูุณุชุบุฑู ุฏูููุฉ
|
| 272 |
-
""")
|
| 273 |
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 283 |
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
๐จ Professional AI Image Processing API - ENHANCED VERSION
|
| 3 |
+
Real AI models with advanced techniques for superior results
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import os
|
| 7 |
+
import cv2
|
| 8 |
+
import base64
|
| 9 |
import numpy as np
|
| 10 |
+
import uuid
|
| 11 |
+
import time
|
| 12 |
+
import torch
|
| 13 |
+
import torch.nn as nn
|
| 14 |
+
from flask import Flask, request, jsonify
|
| 15 |
+
from collections import OrderedDict
|
| 16 |
+
from threading import Thread, Lock
|
| 17 |
+
from queue import Queue, Empty
|
| 18 |
from PIL import Image
|
| 19 |
+
import requests
|
| 20 |
+
from io import BytesIO
|
| 21 |
+
from scipy.interpolate import UnivariateSpline
|
| 22 |
+
|
| 23 |
+
# ููู
ูุฏููุงุช ุงูุญุงููุฉ
|
| 24 |
+
from basicsr.archs.rrdbnet_arch import RRDBNet
|
| 25 |
+
from realesrgan import RealESRGANer
|
| 26 |
+
from gfpgan import GFPGANer
|
| 27 |
+
|
| 28 |
+
app = Flask(__name__)
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 32 |
+
# ๐จ Advanced Anime Style Transfer
|
| 33 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 34 |
+
|
| 35 |
+
class AdvancedAnimeConverter:
|
| 36 |
+
"""
|
| 37 |
+
ู
ุญุณูู ุฌุฏุงู - ุชูููุงุช ู
ุชูุฏู
ุฉ ูุชุญููู ุงุญุชุฑุงูู ููุฃูู
ู
|
| 38 |
+
"""
|
| 39 |
+
|
| 40 |
def __init__(self):
|
| 41 |
+
self.device = torch.device('cpu')
|
| 42 |
+
print("โ
Advanced Anime Converter initialized")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
+
def convert(self, img, style='hayao'):
|
| 45 |
+
"""ุชุญููู ู
ุชูุฏู
ุฅูู ุฃูู
ู"""
|
| 46 |
+
if style == 'hayao':
|
| 47 |
+
return self._hayao_style_advanced(img)
|
| 48 |
+
elif style == 'shinkai':
|
| 49 |
+
return self._shinkai_style_advanced(img)
|
| 50 |
+
elif style == 'paprika':
|
| 51 |
+
return self._paprika_style_advanced(img)
|
| 52 |
+
else:
|
| 53 |
+
return self._face_paint_style_advanced(img)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
+
def _adaptive_bilateral_filter(self, img):
|
| 56 |
+
"""Bilateral filter ู
ุชููู ุญุณุจ ู
ุญุชูู ุงูุตูุฑุฉ"""
|
| 57 |
+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 58 |
+
edges = cv2.Canny(gray, 50, 150)
|
| 59 |
+
edge_density = np.mean(edges) / 255.0
|
| 60 |
+
|
| 61 |
+
if edge_density > 0.15:
|
| 62 |
+
d, sigmaColor, sigmaSpace = 9, 60, 60
|
| 63 |
+
else:
|
| 64 |
+
d, sigmaColor, sigmaSpace = 11, 90, 90
|
| 65 |
+
|
| 66 |
+
return cv2.bilateralFilter(img, d, sigmaColor, sigmaSpace)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
+
def _advanced_color_quantization(self, img, n_colors=12):
|
| 69 |
+
"""ุชูููู ุฃููุงู ู
ุชูุฏู
ู
ุน ุงูุญูุงุธ ุนูู ุงูุชูุงุตูู ุงูู
ูู
ุฉ"""
|
| 70 |
+
from sklearn.cluster import MiniBatchKMeans
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
+
h, w, c = img.shape
|
| 73 |
+
img_float = img.astype(np.float32)
|
| 74 |
+
|
| 75 |
+
img_lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
|
| 76 |
+
img_reshaped = img_lab.reshape((-1, 3)).astype(np.float32)
|
| 77 |
+
|
| 78 |
+
kmeans = MiniBatchKMeans(
|
| 79 |
+
n_clusters=n_colors,
|
| 80 |
+
random_state=42,
|
| 81 |
+
batch_size=2000,
|
| 82 |
+
max_iter=100
|
| 83 |
+
)
|
| 84 |
+
labels = kmeans.fit_predict(img_reshaped)
|
| 85 |
+
quantized_lab = kmeans.cluster_centers_[labels].reshape((h, w, c))
|
| 86 |
+
|
| 87 |
+
quantized = cv2.cvtColor(quantized_lab.astype(np.uint8), cv2.COLOR_LAB2BGR)
|
| 88 |
+
|
| 89 |
+
return quantized
|
| 90 |
+
|
| 91 |
+
def _extract_lines_advanced(self, img, thick=False):
|
| 92 |
+
"""ุงุณุชุฎุฑุงุฌ ุฎุทูุท ู
ุชูุฏู
ุจุฌูุฏุฉ ุนุงููุฉ"""
|
| 93 |
+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 94 |
+
|
| 95 |
+
edges1 = cv2.Canny(gray, 30, 90)
|
| 96 |
+
edges2 = cv2.Canny(gray, 50, 150)
|
| 97 |
+
edges3 = cv2.Canny(gray, 70, 200)
|
| 98 |
+
|
| 99 |
+
edges = cv2.addWeighted(edges1, 0.3, edges2, 0.4, 0)
|
| 100 |
+
edges = cv2.addWeighted(edges, 1.0, edges3, 0.3, 0)
|
| 101 |
+
|
| 102 |
+
kernel_size = 3 if thick else 2
|
| 103 |
+
kernel = np.ones((kernel_size, kernel_size), np.uint8)
|
| 104 |
+
edges = cv2.morphologyEx(edges, cv2.MORPH_CLOSE, kernel)
|
| 105 |
+
|
| 106 |
+
edges = 255 - edges
|
| 107 |
+
edges_3ch = cv2.cvtColor(edges, cv2.COLOR_GRAY2BGR)
|
| 108 |
+
|
| 109 |
+
return edges_3ch
|
| 110 |
+
|
| 111 |
+
def _enhance_colors(self, img, saturation=1.3, brightness=1.1):
|
| 112 |
+
"""ุชุญุณูู ุงูุฃููุงู ุจุดูู ุงุญุชุฑุงูู"""
|
| 113 |
+
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV).astype(np.float32)
|
| 114 |
+
h, s, v = cv2.split(hsv)
|
| 115 |
+
|
| 116 |
+
s = np.clip(s * saturation, 0, 255)
|
| 117 |
+
v = np.clip(v * brightness, 0, 255)
|
| 118 |
+
|
| 119 |
+
hsv = cv2.merge([h, s, v]).astype(np.uint8)
|
| 120 |
+
result = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
|
| 121 |
+
|
| 122 |
+
return result
|
| 123 |
+
|
| 124 |
+
def _apply_anime_color_grading(self, img, style='warm'):
|
| 125 |
+
"""ุชุทุจูู Color Grading ุงุญุชุฑุงูู ููุฃูู
ู"""
|
| 126 |
+
img_float = img.astype(np.float32) / 255.0
|
| 127 |
+
|
| 128 |
+
if style == 'warm':
|
| 129 |
+
lut_r = self._create_curve([0, 64, 128, 192, 255], [0, 70, 140, 200, 255])
|
| 130 |
+
lut_g = self._create_curve([0, 64, 128, 192, 255], [0, 65, 135, 195, 255])
|
| 131 |
+
lut_b = self._create_curve([0, 64, 128, 192, 255], [0, 55, 120, 185, 250])
|
| 132 |
+
elif style == 'cool':
|
| 133 |
+
lut_r = self._create_curve([0, 64, 128, 192, 255], [0, 55, 120, 185, 250])
|
| 134 |
+
lut_g = self._create_curve([0, 64, 128, 192, 255], [0, 65, 135, 195, 255])
|
| 135 |
+
lut_b = self._create_curve([0, 64, 128, 192, 255], [0, 75, 145, 205, 255])
|
| 136 |
+
else:
|
| 137 |
+
lut_r = self._create_curve([0, 64, 128, 192, 255], [0, 75, 145, 210, 255])
|
| 138 |
+
lut_g = self._create_curve([0, 64, 128, 192, 255], [0, 75, 145, 210, 255])
|
| 139 |
+
lut_b = self._create_curve([0, 64, 128, 192, 255], [0, 70, 140, 205, 255])
|
| 140 |
+
|
| 141 |
+
b, g, r = cv2.split((img_float * 255).astype(np.uint8))
|
| 142 |
+
b = cv2.LUT(b, lut_b)
|
| 143 |
+
g = cv2.LUT(g, lut_g)
|
| 144 |
+
r = cv2.LUT(r, lut_r)
|
| 145 |
+
|
| 146 |
+
result = cv2.merge([b, g, r])
|
| 147 |
+
return result
|
| 148 |
|
| 149 |
+
def _create_curve(self, x_points, y_points):
|
| 150 |
+
"""ุฅูุดุงุก ู
ูุญูู LUT"""
|
| 151 |
+
spline = UnivariateSpline(x_points, y_points, k=3, s=0)
|
| 152 |
+
lut = spline(range(256))
|
| 153 |
+
return np.clip(lut, 0, 255).astype(np.uint8)
|
| 154 |
|
| 155 |
+
def _hayao_style_advanced(self, img):
|
| 156 |
+
"""ูู
ุท Hayao Miyazaki ู
ุญุณูู ุฌุฏุงู"""
|
| 157 |
+
h, w = img.shape[:2]
|
| 158 |
+
max_size = 1280
|
| 159 |
+
if max(h, w) > max_size:
|
| 160 |
+
scale = max_size / max(h, w)
|
| 161 |
+
img = cv2.resize(img, (int(w*scale), int(h*scale)), interpolation=cv2.INTER_AREA)
|
| 162 |
+
|
| 163 |
+
smooth = self._adaptive_bilateral_filter(img)
|
| 164 |
+
enhanced = self._enhance_colors(smooth, saturation=1.4, brightness=1.15)
|
| 165 |
+
graded = self._apply_anime_color_grading(enhanced, style='warm')
|
| 166 |
+
quantized = self._advanced_color_quantization(graded, n_colors=14)
|
| 167 |
+
lines = self._extract_lines_advanced(img, thick=False)
|
| 168 |
+
|
| 169 |
+
result = cv2.multiply(quantized.astype(np.float32) / 255.0,
|
| 170 |
+
lines.astype(np.float32) / 255.0)
|
| 171 |
+
result = (result * 255).astype(np.uint8)
|
| 172 |
+
|
| 173 |
+
result = cv2.convertScaleAbs(result, alpha=1.12, beta=8)
|
| 174 |
+
|
| 175 |
+
kernel = np.array([[-0.5, -0.5, -0.5],
|
| 176 |
+
[-0.5, 5.0, -0.5],
|
| 177 |
+
[-0.5, -0.5, -0.5]]) / 1.0
|
| 178 |
+
result = cv2.filter2D(result, -1, kernel)
|
| 179 |
+
result = np.clip(result, 0, 255).astype(np.uint8)
|
| 180 |
+
|
| 181 |
+
return result
|
| 182 |
|
| 183 |
+
def _shinkai_style_advanced(self, img):
|
| 184 |
+
"""ูู
ุท Makoto Shinkai ู
ุญุณูู ุฌุฏุงู"""
|
| 185 |
+
h, w = img.shape[:2]
|
| 186 |
+
max_size = 1280
|
| 187 |
+
if max(h, w) > max_size:
|
| 188 |
+
scale = max_size / max(h, w)
|
| 189 |
+
img = cv2.resize(img, (int(w*scale), int(h*scale)), interpolation=cv2.INTER_AREA)
|
| 190 |
+
|
| 191 |
+
smooth = cv2.bilateralFilter(img, d=7, sigmaColor=70, sigmaSpace=70)
|
| 192 |
+
enhanced = self._enhance_colors(smooth, saturation=1.6, brightness=1.2)
|
| 193 |
+
graded = self._apply_anime_color_grading(enhanced, style='cool')
|
| 194 |
+
quantized = self._advanced_color_quantization(graded, n_colors=18)
|
| 195 |
+
lines = self._extract_lines_advanced(img, thick=False)
|
| 196 |
+
|
| 197 |
+
result = cv2.multiply(quantized.astype(np.float32) / 255.0,
|
| 198 |
+
lines.astype(np.float32) / 255.0)
|
| 199 |
+
result = (result * 255).astype(np.uint8)
|
| 200 |
+
|
| 201 |
+
result = cv2.convertScaleAbs(result, alpha=1.25, beta=5)
|
| 202 |
+
|
| 203 |
+
kernel = np.array([[-1, -1, -1],
|
| 204 |
+
[-1, 9, -1],
|
| 205 |
+
[-1, -1, -1]]) / 1.0
|
| 206 |
+
result = cv2.filter2D(result, -1, kernel)
|
| 207 |
+
result = np.clip(result, 0, 255).astype(np.uint8)
|
| 208 |
+
|
| 209 |
+
return result
|
| 210 |
|
| 211 |
+
def _paprika_style_advanced(self, img):
|
| 212 |
+
"""ูู
ุท Paprika ู
ุญุณูู ุฌุฏุงู - ุฃููุงู ูุงุจุถุฉ"""
|
| 213 |
+
h, w = img.shape[:2]
|
| 214 |
+
max_size = 1280
|
| 215 |
+
if max(h, w) > max_size:
|
| 216 |
+
scale = max_size / max(h, w)
|
| 217 |
+
img = cv2.resize(img, (int(w*scale), int(h*scale)), interpolation=cv2.INTER_AREA)
|
| 218 |
+
|
| 219 |
+
smooth = cv2.bilateralFilter(img, d=7, sigmaColor=65, sigmaSpace=65)
|
| 220 |
+
enhanced = self._enhance_colors(smooth, saturation=1.8, brightness=1.3)
|
| 221 |
+
graded = self._apply_anime_color_grading(enhanced, style='vibrant')
|
| 222 |
+
quantized = self._advanced_color_quantization(graded, n_colors=12)
|
| 223 |
+
lines = self._extract_lines_advanced(img, thick=True)
|
| 224 |
+
|
| 225 |
+
result = cv2.multiply(quantized.astype(np.float32) / 255.0,
|
| 226 |
+
lines.astype(np.float32) / 255.0)
|
| 227 |
+
result = (result * 255).astype(np.uint8)
|
| 228 |
+
|
| 229 |
+
result = cv2.convertScaleAbs(result, alpha=1.3, beta=15)
|
| 230 |
+
|
| 231 |
+
kernel = np.array([[-0.7, -0.7, -0.7],
|
| 232 |
+
[-0.7, 6.6, -0.7],
|
| 233 |
+
[-0.7, -0.7, -0.7]]) / 1.0
|
| 234 |
+
result = cv2.filter2D(result, -1, kernel)
|
| 235 |
+
result = np.clip(result, 0, 255).astype(np.uint8)
|
| 236 |
+
|
| 237 |
+
return result
|
| 238 |
|
| 239 |
+
def _face_paint_style_advanced(self, img):
|
| 240 |
+
"""ูู
ุท Face Paint ู
ุญุณูู - ููุจูุฑุชุฑูู"""
|
| 241 |
+
h, w = img.shape[:2]
|
| 242 |
+
max_size = 1024
|
| 243 |
+
if max(h, w) > max_size:
|
| 244 |
+
scale = max_size / max(h, w)
|
| 245 |
+
img = cv2.resize(img, (int(w*scale), int(h*scale)), interpolation=cv2.INTER_AREA)
|
| 246 |
+
|
| 247 |
+
smooth = cv2.bilateralFilter(img, d=11, sigmaColor=80, sigmaSpace=80)
|
| 248 |
+
smooth = cv2.bilateralFilter(smooth, d=9, sigmaColor=70, sigmaSpace=70)
|
| 249 |
+
|
| 250 |
+
enhanced = self._enhance_colors(smooth, saturation=1.35, brightness=1.1)
|
| 251 |
+
graded = self._apply_anime_color_grading(enhanced, style='warm')
|
| 252 |
+
quantized = self._advanced_color_quantization(graded, n_colors=10)
|
| 253 |
+
lines = self._extract_lines_advanced(img, thick=False)
|
| 254 |
+
|
| 255 |
+
result = cv2.multiply(quantized.astype(np.float32) / 255.0,
|
| 256 |
+
lines.astype(np.float32) / 255.0)
|
| 257 |
+
result = (result * 255).astype(np.uint8)
|
| 258 |
+
|
| 259 |
+
result = cv2.convertScaleAbs(result, alpha=1.08, beta=5)
|
| 260 |
+
|
| 261 |
+
return result
|
| 262 |
+
|
| 263 |
+
|
| 264 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 265 |
+
# ๐จ Advanced Cartoon Style
|
| 266 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 267 |
+
|
| 268 |
+
class AdvancedCartoonConverter:
|
| 269 |
+
"""
|
| 270 |
+
ู
ุญุณูู ุฌุฏุงู - ูุฑุชูู ุงุญุชุฑุงูู
|
| 271 |
+
"""
|
| 272 |
+
|
| 273 |
+
def __init__(self):
|
| 274 |
+
self.device = torch.device('cpu')
|
| 275 |
+
print("โ
Advanced Cartoon Converter initialized")
|
| 276 |
+
|
| 277 |
+
def convert(self, img, style='default'):
|
| 278 |
+
"""ุชุญููู ู
ุชูุฏู
ุฅูู ูุฑุชูู"""
|
| 279 |
+
h, w = img.shape[:2]
|
| 280 |
+
max_size = 1280
|
| 281 |
+
if max(h, w) > max_size:
|
| 282 |
+
scale = max_size / max(h, w)
|
| 283 |
+
img = cv2.resize(img, (int(w*scale), int(h*scale)), interpolation=cv2.INTER_AREA)
|
| 284 |
+
|
| 285 |
+
if style == 'smooth':
|
| 286 |
+
return self._smooth_cartoon(img)
|
| 287 |
+
elif style == 'sharp':
|
| 288 |
+
return self._sharp_cartoon(img)
|
| 289 |
+
elif style == 'artistic':
|
| 290 |
+
return self._artistic_cartoon(img)
|
| 291 |
+
else:
|
| 292 |
+
return self._default_cartoon(img)
|
| 293 |
+
|
| 294 |
+
def _default_cartoon(self, img):
|
| 295 |
+
"""ูุฑุชูู ุนุงุฏู ู
ุญุณูู"""
|
| 296 |
+
smooth = cv2.bilateralFilter(img, d=9, sigmaColor=80, sigmaSpace=80)
|
| 297 |
+
|
| 298 |
+
from sklearn.cluster import MiniBatchKMeans
|
| 299 |
+
h, w, c = smooth.shape
|
| 300 |
+
img_reshaped = smooth.reshape((-1, 3)).astype(np.float32)
|
| 301 |
+
kmeans = MiniBatchKMeans(n_clusters=10, random_state=42, batch_size=2000)
|
| 302 |
+
labels = kmeans.fit_predict(img_reshaped)
|
| 303 |
+
quantized = kmeans.cluster_centers_[labels].reshape((h, w, c)).astype(np.uint8)
|
| 304 |
+
|
| 305 |
+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 306 |
+
edges = cv2.adaptiveThreshold(
|
| 307 |
+
gray, 255,
|
| 308 |
+
cv2.ADAPTIVE_THRESH_MEAN_C,
|
| 309 |
+
cv2.THRESH_BINARY,
|
| 310 |
+
blockSize=9,
|
| 311 |
+
C=7
|
| 312 |
+
)
|
| 313 |
+
edges = cv2.medianBlur(edges, 3)
|
| 314 |
+
edges_3ch = cv2.cvtColor(edges, cv2.COLOR_GRAY2BGR)
|
| 315 |
+
|
| 316 |
+
result = cv2.bitwise_and(quantized, edges_3ch)
|
| 317 |
+
result = cv2.convertScaleAbs(result, alpha=1.15, beta=10)
|
| 318 |
+
|
| 319 |
+
return result
|
| 320 |
+
|
| 321 |
+
def _smooth_cartoon(self, img):
|
| 322 |
+
"""ูุฑุชูู ูุงุนู
"""
|
| 323 |
+
smooth = cv2.bilateralFilter(img, d=11, sigmaColor=90, sigmaSpace=90)
|
| 324 |
+
smooth = cv2.bilateralFilter(smooth, d=9, sigmaColor=80, sigmaSpace=80)
|
| 325 |
+
|
| 326 |
+
from sklearn.cluster import MiniBatchKMeans
|
| 327 |
+
h, w, c = smooth.shape
|
| 328 |
+
img_reshaped = smooth.reshape((-1, 3)).astype(np.float32)
|
| 329 |
+
kmeans = MiniBatchKMeans(n_clusters=12, random_state=42, batch_size=2000)
|
| 330 |
+
labels = kmeans.fit_predict(img_reshaped)
|
| 331 |
+
quantized = kmeans.cluster_centers_[labels].reshape((h, w, c)).astype(np.uint8)
|
| 332 |
+
|
| 333 |
+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 334 |
+
edges = cv2.Canny(gray, 40, 120)
|
| 335 |
+
edges = cv2.dilate(edges, np.ones((2,2), np.uint8), iterations=1)
|
| 336 |
+
edges = 255 - edges
|
| 337 |
+
edges_3ch = cv2.cvtColor(edges, cv2.COLOR_GRAY2BGR)
|
| 338 |
+
|
| 339 |
+
result = cv2.bitwise_and(quantized, edges_3ch)
|
| 340 |
+
result = cv2.convertScaleAbs(result, alpha=1.1, beta=8)
|
| 341 |
+
|
| 342 |
+
return result
|
| 343 |
+
|
| 344 |
+
def _sharp_cartoon(self, img):
|
| 345 |
+
"""ูุฑุชูู ุญุงุฏ"""
|
| 346 |
+
smooth = cv2.bilateralFilter(img, d=7, sigmaColor=70, sigmaSpace=70)
|
| 347 |
+
|
| 348 |
+
from sklearn.cluster import MiniBatchKMeans
|
| 349 |
+
h, w, c = smooth.shape
|
| 350 |
+
img_reshaped = smooth.reshape((-1, 3)).astype(np.float32)
|
| 351 |
+
kmeans = MiniBatchKMeans(n_clusters=8, random_state=42, batch_size=2000)
|
| 352 |
+
labels = kmeans.fit_predict(img_reshaped)
|
| 353 |
+
quantized = kmeans.cluster_centers_[labels].reshape((h, w, c)).astype(np.uint8)
|
| 354 |
+
|
| 355 |
+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 356 |
+
edges = cv2.Canny(gray, 50, 150)
|
| 357 |
+
edges = cv2.dilate(edges, np.ones((3,3), np.uint8), iterations=1)
|
| 358 |
+
edges = 255 - edges
|
| 359 |
+
edges_3ch = cv2.cvtColor(edges, cv2.COLOR_GRAY2BGR)
|
| 360 |
+
|
| 361 |
+
result = cv2.bitwise_and(quantized, edges_3ch)
|
| 362 |
+
|
| 363 |
+
kernel = np.array([[-1, -1, -1],
|
| 364 |
+
[-1, 9, -1],
|
| 365 |
+
[-1, -1, -1]])
|
| 366 |
+
result = cv2.filter2D(result, -1, kernel)
|
| 367 |
+
result = np.clip(result, 0, 255).astype(np.uint8)
|
| 368 |
+
|
| 369 |
+
result = cv2.convertScaleAbs(result, alpha=1.2, beta=12)
|
| 370 |
+
|
| 371 |
+
return result
|
| 372 |
+
|
| 373 |
+
def _artistic_cartoon(self, img):
|
| 374 |
+
"""ูุฑุชูู ููู"""
|
| 375 |
+
try:
|
| 376 |
+
oil = cv2.xphoto.oilPainting(img, size=7, dynRatio=1)
|
| 377 |
+
except:
|
| 378 |
+
oil = img
|
| 379 |
+
|
| 380 |
+
smooth = cv2.bilateralFilter(oil, d=9, sigmaColor=75, sigmaSpace=75)
|
| 381 |
+
|
| 382 |
+
from sklearn.cluster import MiniBatchKMeans
|
| 383 |
+
h, w, c = smooth.shape
|
| 384 |
+
img_reshaped = smooth.reshape((-1, 3)).astype(np.float32)
|
| 385 |
+
kmeans = MiniBatchKMeans(n_clusters=11, random_state=42, batch_size=2000)
|
| 386 |
+
labels = kmeans.fit_predict(img_reshaped)
|
| 387 |
+
quantized = kmeans.cluster_centers_[labels].reshape((h, w, c)).astype(np.uint8)
|
| 388 |
+
|
| 389 |
+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 390 |
+
edges = cv2.Canny(gray, 45, 135)
|
| 391 |
+
edges = cv2.dilate(edges, np.ones((2,2), np.uint8))
|
| 392 |
+
edges = 255 - edges
|
| 393 |
+
edges_3ch = cv2.cvtColor(edges, cv2.COLOR_GRAY2BGR)
|
| 394 |
+
|
| 395 |
+
result = cv2.bitwise_and(quantized, edges_3ch)
|
| 396 |
+
result = cv2.convertScaleAbs(result, alpha=1.18, beta=10)
|
| 397 |
+
|
| 398 |
+
return result
|
| 399 |
+
|
| 400 |
+
|
| 401 |
+
# โโโโโโโโ๏ฟฝ๏ฟฝ๏ฟฝโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 402 |
+
# โ๏ธ Professional Sketch Converter - ALL TYPES
|
| 403 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 404 |
+
|
| 405 |
+
class ProfessionalSketchConverter:
|
| 406 |
+
"""ู
ุญูู ุฑุณู
ุงุญุชุฑุงูู ู
ุญุณูู ุจุฌู
ูุน ุงูุฃููุงุน"""
|
| 407 |
+
|
| 408 |
+
@staticmethod
|
| 409 |
+
def convert_to_sketch(img, style='pencil'):
|
| 410 |
+
"""ุชุญููู ุฅูู ุฑุณู
ุงุญุชุฑุงูู"""
|
| 411 |
+
if style == 'pencil':
|
| 412 |
+
return ProfessionalSketchConverter._pencil_sketch_enhanced(img)
|
| 413 |
+
elif style == 'colored':
|
| 414 |
+
return ProfessionalSketchConverter._colored_sketch_enhanced(img)
|
| 415 |
+
elif style == 'charcoal':
|
| 416 |
+
return ProfessionalSketchConverter._charcoal_sketch_enhanced(img)
|
| 417 |
+
else:
|
| 418 |
+
return ProfessionalSketchConverter._ink_sketch_enhanced(img)
|
| 419 |
+
|
| 420 |
+
@staticmethod
|
| 421 |
+
def _dodge_blend(image, mask):
|
| 422 |
+
"""Dodge blend mode ููุฑุณู
ุงูุงุญุชุฑุงูู"""
|
| 423 |
+
return cv2.divide(image, 255 - mask, scale=256)
|
| 424 |
+
|
| 425 |
+
@staticmethod
|
| 426 |
+
def _pencil_sketch_enhanced(img):
|
| 427 |
+
"""โ๏ธ ุฑุณู
ุจุงูููู
ุงูุฑุตุงุต ู
ุญุณูู - ูุงุถุญ ูููู"""
|
| 428 |
+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 429 |
+
|
| 430 |
+
denoised = cv2.fastNlMeansDenoising(gray, None, h=10)
|
| 431 |
+
inverted = 255 - denoised
|
| 432 |
+
blurred = cv2.GaussianBlur(inverted, (21, 21), sigmaX=0, sigmaY=0)
|
| 433 |
+
|
| 434 |
+
sketch = ProfessionalSketchConverter._dodge_blend(denoised, blurred)
|
| 435 |
+
sketch = cv2.convertScaleAbs(sketch, alpha=1.5, beta=15)
|
| 436 |
+
|
| 437 |
+
clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8,8))
|
| 438 |
+
sketch = clahe.apply(sketch)
|
| 439 |
+
|
| 440 |
+
kernel = np.array([[-1,-1,-1],
|
| 441 |
+
[-1, 10,-1],
|
| 442 |
+
[-1,-1,-1]])
|
| 443 |
+
sketch = cv2.filter2D(sketch, -1, kernel)
|
| 444 |
+
sketch = np.clip(sketch, 0, 255).astype(np.uint8)
|
| 445 |
+
|
| 446 |
+
sketch = cv2.convertScaleAbs(sketch, alpha=1.3, beta=5)
|
| 447 |
+
sketch_bgr = cv2.cvtColor(sketch, cv2.COLOR_GRAY2BGR)
|
| 448 |
+
|
| 449 |
+
return sketch_bgr
|
| 450 |
|
| 451 |
+
@staticmethod
|
| 452 |
+
def _colored_sketch_enhanced(img):
|
| 453 |
+
"""๐จ ุฑุณู
ู
ููู ู
ุญุณูู"""
|
| 454 |
+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 455 |
+
|
| 456 |
+
edges1 = cv2.Canny(gray, 30, 90)
|
| 457 |
+
edges2 = cv2.Canny(gray, 50, 150)
|
| 458 |
+
edges = cv2.addWeighted(edges1, 0.5, edges2, 0.5, 0)
|
| 459 |
+
|
| 460 |
+
edges = cv2.dilate(edges, np.ones((2,2), np.uint8), iterations=1)
|
| 461 |
+
edges = 255 - edges
|
| 462 |
+
|
| 463 |
+
smoothed = cv2.bilateralFilter(img, d=9, sigmaColor=80, sigmaSpace=80)
|
| 464 |
+
|
| 465 |
+
from sklearn.cluster import MiniBatchKMeans
|
| 466 |
+
h, w, c = smoothed.shape
|
| 467 |
+
img_reshaped = smoothed.reshape((-1, 3)).astype(np.float32)
|
| 468 |
+
kmeans = MiniBatchKMeans(n_clusters=16, random_state=42, batch_size=2000)
|
| 469 |
+
labels = kmeans.fit_predict(img_reshaped)
|
| 470 |
+
quantized = kmeans.cluster_centers_[labels].reshape((h, w, c)).astype(np.uint8)
|
| 471 |
+
|
| 472 |
+
edges_3ch = cv2.cvtColor(edges, cv2.COLOR_GRAY2BGR)
|
| 473 |
+
result = cv2.bitwise_and(quantized, edges_3ch)
|
| 474 |
+
|
| 475 |
+
hsv = cv2.cvtColor(result, cv2.COLOR_BGR2HSV).astype(np.float32)
|
| 476 |
+
h, s, v = cv2.split(hsv)
|
| 477 |
+
s = np.clip(s * 1.3, 0, 255)
|
| 478 |
+
hsv = cv2.merge([h, s, v]).astype(np.uint8)
|
| 479 |
+
result = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
|
| 480 |
+
|
| 481 |
+
result = cv2.convertScaleAbs(result, alpha=1.3, beta=15)
|
| 482 |
+
|
| 483 |
+
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
| 484 |
|
| 485 |
+
@staticmethod
|
| 486 |
+
def _charcoal_sketch_enhanced(img):
|
| 487 |
+
"""๐๏ธ ุฑุณู
ูุญู
ู
ุญุณูู - ููู ููุงุถุญ"""
|
| 488 |
+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 489 |
+
|
| 490 |
+
denoised = cv2.fastNlMeansDenoising(gray, None, h=10)
|
| 491 |
+
inverted = 255 - denoised
|
| 492 |
+
blurred = cv2.GaussianBlur(inverted, (31, 31), sigmaX=0)
|
| 493 |
+
|
| 494 |
+
sketch = ProfessionalSketchConverter._dodge_blend(denoised, blurred)
|
| 495 |
+
sketch = cv2.convertScaleAbs(sketch, alpha=1.2, beta=-30)
|
| 496 |
+
|
| 497 |
+
noise = np.random.normal(0, 5, sketch.shape).astype(np.int16)
|
| 498 |
+
sketch = np.clip(sketch.astype(np.int16) + noise, 0, 255).astype(np.uint8)
|
| 499 |
+
|
| 500 |
+
clahe = cv2.createCLAHE(clipLimit=2.5, tileGridSize=(8,8))
|
| 501 |
+
sketch = clahe.apply(sketch)
|
| 502 |
+
|
| 503 |
+
sketch = cv2.convertScaleAbs(sketch, alpha=1.1, beta=-10)
|
| 504 |
+
sketch_bgr = cv2.cvtColor(sketch, cv2.COLOR_GRAY2BGR)
|
| 505 |
+
|
| 506 |
+
return sketch_bgr
|
| 507 |
|
| 508 |
+
@staticmethod
|
| 509 |
+
def _ink_sketch_enhanced(img):
|
| 510 |
+
"""๐๏ธ ุฑุณู
ุจุงูุญุจุฑ ู
ุญุณูู - ุฎุทูุท ูุงุถุญุฉ"""
|
| 511 |
+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 512 |
+
|
| 513 |
+
denoised = cv2.fastNlMeansDenoising(gray, None, h=8)
|
| 514 |
+
|
| 515 |
+
edges1 = cv2.Canny(denoised, 40, 120)
|
| 516 |
+
edges2 = cv2.Canny(denoised, 60, 180)
|
| 517 |
+
edges = cv2.addWeighted(edges1, 0.6, edges2, 0.4, 0)
|
| 518 |
+
|
| 519 |
+
kernel = np.ones((2,2), np.uint8)
|
| 520 |
+
edges = cv2.dilate(edges, kernel, iterations=1)
|
| 521 |
+
|
| 522 |
+
adaptive = cv2.adaptiveThreshold(
|
| 523 |
+
denoised, 255,
|
| 524 |
+
cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
|
| 525 |
+
cv2.THRESH_BINARY,
|
| 526 |
+
blockSize=11,
|
| 527 |
+
C=8
|
| 528 |
+
)
|
| 529 |
+
|
| 530 |
+
combined = cv2.bitwise_and(adaptive, 255 - edges)
|
| 531 |
+
combined = cv2.medianBlur(combined, 3)
|
| 532 |
+
combined = cv2.convertScaleAbs(combined, alpha=1.2, beta=0)
|
| 533 |
+
|
| 534 |
+
result_bgr = cv2.cvtColor(combined, cv2.COLOR_GRAY2BGR)
|
| 535 |
+
|
| 536 |
+
return result_bgr
|
| 537 |
+
|
| 538 |
+
|
| 539 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 540 |
+
# ๐ Processing Queue (ู
ุญุฏุซ)
|
| 541 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 542 |
+
|
| 543 |
+
class ProcessingQueue:
|
| 544 |
+
def __init__(self, max_queue_size=15):
|
| 545 |
+
self.queue = Queue(maxsize=max_queue_size)
|
| 546 |
+
self.results = OrderedDict()
|
| 547 |
+
self.lock = Lock()
|
| 548 |
+
self.max_results_cache = 100
|
| 549 |
+
self.processing_thread = None
|
| 550 |
+
self.is_running = False
|
| 551 |
+
|
| 552 |
+
print("๐ Loading Enhanced AI Models...")
|
| 553 |
+
self.anime_converter = AdvancedAnimeConverter()
|
| 554 |
+
self.cartoon_converter = AdvancedCartoonConverter()
|
| 555 |
+
self.sketch_converter = ProfessionalSketchConverter()
|
| 556 |
+
print("โ
All Enhanced AI models loaded!")
|
| 557 |
+
|
| 558 |
+
def start(self):
|
| 559 |
+
if not self.is_running:
|
| 560 |
+
self.is_running = True
|
| 561 |
+
self.processing_thread = Thread(target=self._process_queue, daemon=True)
|
| 562 |
+
self.processing_thread.start()
|
| 563 |
+
|
| 564 |
+
def stop(self):
|
| 565 |
+
self.is_running = False
|
| 566 |
+
if self.processing_thread:
|
| 567 |
+
self.processing_thread.join(timeout=5)
|
| 568 |
+
|
| 569 |
+
def add_job(self, job_id, job_type, image_data, **params):
|
| 570 |
+
try:
|
| 571 |
+
self.queue.put({
|
| 572 |
+
'job_id': job_id,
|
| 573 |
+
'job_type': job_type,
|
| 574 |
+
'image_data': image_data,
|
| 575 |
+
'params': params,
|
| 576 |
+
'timestamp': time.time()
|
| 577 |
+
}, block=False)
|
| 578 |
+
|
| 579 |
+
with self.lock:
|
| 580 |
+
self.results[job_id] = {
|
| 581 |
+
'status': 'queued',
|
| 582 |
+
'position': self.queue.qsize(),
|
| 583 |
+
'message': 'ูู ุงูุทุงุจูุฑ',
|
| 584 |
+
'job_type': job_type
|
| 585 |
+
}
|
| 586 |
+
return True
|
| 587 |
+
except:
|
| 588 |
+
return False
|
| 589 |
+
|
| 590 |
+
def get_job_status(self, job_id):
|
| 591 |
+
with self.lock:
|
| 592 |
+
return self.results.get(job_id, {'status': 'not_found'})
|
| 593 |
+
|
| 594 |
+
def _process_queue(self):
|
| 595 |
+
while self.is_running:
|
| 596 |
+
try:
|
| 597 |
+
job = self.queue.get(timeout=1)
|
| 598 |
+
job_id = job['job_id']
|
| 599 |
+
job_type = job['job_type']
|
| 600 |
+
image_data = job['image_data']
|
| 601 |
+
params = job.get('params', {})
|
| 602 |
+
|
| 603 |
+
with self.lock:
|
| 604 |
+
self.results[job_id] = {
|
| 605 |
+
'status': 'processing',
|
| 606 |
+
'message': f'ุฌุงุฑู ุงูู
ุนุงูุฌุฉ...',
|
| 607 |
+
'job_type': job_type
|
| 608 |
+
}
|
| 609 |
+
|
| 610 |
+
try:
|
| 611 |
+
if job_type == 'upscale':
|
| 612 |
+
result = self._process_upscale(image_data, params)
|
| 613 |
+
elif job_type == 'anime':
|
| 614 |
+
result = self._process_anime(image_data, params)
|
| 615 |
+
elif job_type == 'cartoon':
|
| 616 |
+
result = self._process_cartoon(image_data, params)
|
| 617 |
+
elif job_type == 'sketch':
|
| 618 |
+
result = self._process_sketch(image_data, params)
|
| 619 |
+
else:
|
| 620 |
+
raise ValueError(f"Unknown job type: {job_type}")
|
| 621 |
+
|
| 622 |
+
with self.lock:
|
| 623 |
+
self.results[job_id] = {
|
| 624 |
+
'status': 'completed',
|
| 625 |
+
'message': 'ุชู
ุช ุงูู
ุนุงูุฌุฉ ุจูุฌุงุญ',
|
| 626 |
+
'result': result,
|
| 627 |
+
'job_type': job_type
|
| 628 |
+
}
|
| 629 |
+
if len(self.results) > self.max_results_cache:
|
| 630 |
+
self.results.popitem(last=False)
|
| 631 |
+
|
| 632 |
+
except Exception as e:
|
| 633 |
+
with self.lock:
|
| 634 |
+
self.results[job_id] = {
|
| 635 |
+
'status': 'failed',
|
| 636 |
+
'error': str(e),
|
| 637 |
+
'job_type': job_type
|
| 638 |
+
}
|
| 639 |
+
|
| 640 |
+
self.queue.task_done()
|
| 641 |
+
|
| 642 |
+
except Empty:
|
| 643 |
+
continue
|
| 644 |
+
except Exception as e:
|
| 645 |
+
print(f"Queue error: {e}")
|
| 646 |
+
continue
|
| 647 |
+
|
| 648 |
+
def _decode_image(self, image_data):
|
| 649 |
+
if ',' in image_data:
|
| 650 |
+
image_data = image_data.split(',')[1]
|
| 651 |
+
image_bytes = base64.b64decode(image_data)
|
| 652 |
+
nparr = np.frombuffer(image_bytes, np.uint8)
|
| 653 |
+
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
|
| 654 |
+
if img is None:
|
| 655 |
+
raise ValueError('ูุดู ูู ูุฑุงุกุฉ ุงูุตูุฑุฉ')
|
| 656 |
+
return img
|
| 657 |
+
|
| 658 |
+
def _encode_image(self, img):
|
| 659 |
+
_, buffer = cv2.imencode('.png', img, [cv2.IMWRITE_PNG_COMPRESSION, 6])
|
| 660 |
+
output_base64 = base64.b64encode(buffer).decode('utf-8')
|
| 661 |
+
return f"data:image/png;base64,{output_base64}"
|
| 662 |
+
|
| 663 |
+
def _process_anime(self, image_data, params):
|
| 664 |
+
img = self._decode_image(image_data)
|
| 665 |
+
original_h, original_w = img.shape[:2]
|
| 666 |
+
style = params.get('style', 'hayao')
|
| 667 |
+
result = self.anime_converter.convert(img, style)
|
| 668 |
+
result_h, result_w = result.shape[:2]
|
| 669 |
+
return {
|
| 670 |
+
'success': True,
|
| 671 |
+
'original_size': f"{original_w}x{original_h}",
|
| 672 |
+
'result_size': f"{result_w}x{result_h}",
|
| 673 |
+
'style': style,
|
| 674 |
+
'result_image': self._encode_image(result)
|
| 675 |
+
}
|
| 676 |
+
|
| 677 |
+
def _process_cartoon(self, image_data, params):
|
| 678 |
+
img = self._decode_image(image_data)
|
| 679 |
+
original_h, original_w = img.shape[:2]
|
| 680 |
+
style = params.get('style', 'default')
|
| 681 |
+
result = self.cartoon_converter.convert(img, style)
|
| 682 |
+
result_h, result_w = result.shape[:2]
|
| 683 |
+
return {
|
| 684 |
+
'success': True,
|
| 685 |
+
'original_size': f"{original_w}x{original_h}",
|
| 686 |
+
'result_size': f"{result_w}x{result_h}",
|
| 687 |
+
'style': style,
|
| 688 |
+
'result_image': self._encode_image(result)
|
| 689 |
+
}
|
| 690 |
+
|
| 691 |
+
def _process_sketch(self, image_data, params):
|
| 692 |
+
img = self._decode_image(image_data)
|
| 693 |
+
original_h, original_w = img.shape[:2]
|
| 694 |
+
sketch_type = params.get('type', 'pencil')
|
| 695 |
+
result = self.sketch_converter.convert_to_sketch(img, sketch_type)
|
| 696 |
+
result_h, result_w = result.shape[:2]
|
| 697 |
+
return {
|
| 698 |
+
'success': True,
|
| 699 |
+
'original_size': f"{original_w}x{original_h}",
|
| 700 |
+
'result_size': f"{result_w}x{result_h}",
|
| 701 |
+
'type': sketch_type,
|
| 702 |
+
'result_image': self._encode_image(result)
|
| 703 |
+
}
|
| 704 |
+
|
| 705 |
+
def _process_upscale(self, image_data, params):
|
| 706 |
+
img = self._decode_image(image_data)
|
| 707 |
+
original_h, original_w = img.shape[:2]
|
| 708 |
+
if original_w > 2000 or original_h > 2000:
|
| 709 |
+
raise ValueError('ุงูุตูุฑุฉ ูุจูุฑุฉ ุฌุฏุงู')
|
| 710 |
+
scale = params.get('scale', 2)
|
| 711 |
+
try:
|
| 712 |
+
_, _, restored_img = gfpgan.enhance(img, has_aligned=False, only_center_face=False, paste_back=True)
|
| 713 |
+
except:
|
| 714 |
+
restored_img = img
|
| 715 |
+
output, _ = upscaler.enhance(restored_img, outscale=scale)
|
| 716 |
+
upscaled_h, upscaled_w = output.shape[:2]
|
| 717 |
+
return {
|
| 718 |
+
'success': True,
|
| 719 |
+
'original_size': f"{original_w}x{original_h}",
|
| 720 |
+
'upscaled_size': f"{upscaled_w}x{upscaled_h}",
|
| 721 |
+
'scale': scale,
|
| 722 |
+
'upscaled_image': self._encode_image(output)
|
| 723 |
+
}
|
| 724 |
+
|
| 725 |
+
|
| 726 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 727 |
+
# ๐ Flask Routes
|
| 728 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 729 |
+
|
| 730 |
+
print("๐ Loading Upscale Models...")
|
| 731 |
+
gfpgan = GFPGANer(
|
| 732 |
+
model_path='https://github.com/TencentARC/GFPGAN/releases/download/v1.3.4/GFPGANv1.4.pth',
|
| 733 |
+
upscale=1,
|
| 734 |
+
device='cpu'
|
| 735 |
+
)
|
| 736 |
+
|
| 737 |
+
model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=4)
|
| 738 |
+
upscaler = RealESRGANer(
|
| 739 |
+
scale=4,
|
| 740 |
+
model_path='https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth',
|
| 741 |
+
model=model,
|
| 742 |
+
tile=256,
|
| 743 |
+
tile_pad=10,
|
| 744 |
+
pre_pad=0,
|
| 745 |
+
half=False,
|
| 746 |
+
device='cpu'
|
| 747 |
+
)
|
| 748 |
+
print("โ
Upscale models loaded!")
|
| 749 |
+
|
| 750 |
+
processing_queue = ProcessingQueue(max_queue_size=15)
|
| 751 |
+
processing_queue.start()
|
| 752 |
+
|
| 753 |
+
|
| 754 |
+
@app.route('/')
|
| 755 |
+
def home():
|
| 756 |
+
return jsonify({
|
| 757 |
+
'status': 'online',
|
| 758 |
+
'message': '๐จ Professional AI Image Processing - ENHANCED v2.0',
|
| 759 |
+
'version': '2.0',
|
| 760 |
+
'features': {
|
| 761 |
+
'upscale': 'GFPGAN + RealESRGAN',
|
| 762 |
+
'anime': 'Advanced Style Transfer (4 styles) - ENHANCED',
|
| 763 |
+
'cartoon': 'Professional Cartoonization (4 styles) - ENHANCED',
|
| 764 |
+
'sketch': 'Professional Sketch (4 types) - ENHANCED'
|
| 765 |
+
},
|
| 766 |
+
'anime_styles': ['hayao', 'shinkai', 'paprika', 'face_paint_v2'],
|
| 767 |
+
'cartoon_styles': ['default', 'smooth', 'sharp', 'artistic'],
|
| 768 |
+
'sketch_types': ['pencil', 'colored', 'charcoal', 'ink'],
|
| 769 |
+
'improvements': {
|
| 770 |
+
'anime': 'Multi-scale edge detection, LAB color quantization, professional color grading curves',
|
| 771 |
+
'cartoon': 'Adaptive bilateral filtering, advanced edge detection, oil painting effect',
|
| 772 |
+
'sketch': 'Dodge blending, CLAHE enhancement, multi-pass sharpening, realistic textures'
|
| 773 |
+
}
|
| 774 |
+
})
|
| 775 |
+
|
| 776 |
+
|
| 777 |
+
@app.route('/health', methods=['GET'])
|
| 778 |
+
def health():
|
| 779 |
+
return jsonify({
|
| 780 |
+
'status': 'healthy',
|
| 781 |
+
'models': 'All Enhanced AI Models Loaded',
|
| 782 |
+
'queue_size': processing_queue.queue.qsize()
|
| 783 |
+
}), 200
|
| 784 |
+
|
| 785 |
+
|
| 786 |
+
@app.route('/upscale', methods=['POST'])
|
| 787 |
+
def upscale_image():
|
| 788 |
+
try:
|
| 789 |
+
data = request.get_json()
|
| 790 |
+
if 'image' not in data:
|
| 791 |
+
return jsonify({'success': False, 'error': 'No image'}), 400
|
| 792 |
+
scale = int(data.get('scale', 2))
|
| 793 |
+
job_id = str(uuid.uuid4())
|
| 794 |
+
if processing_queue.add_job(job_id, 'upscale', data['image'], scale=scale):
|
| 795 |
+
return jsonify({'success': True, 'job_id': job_id, 'status_url': f'/status/{job_id}'}), 202
|
| 796 |
+
else:
|
| 797 |
+
return jsonify({'success': False, 'error': 'Queue full'}), 503
|
| 798 |
+
except Exception as e:
|
| 799 |
+
return jsonify({'success': False, 'error': str(e)}), 500
|
| 800 |
+
|
| 801 |
+
|
| 802 |
+
@app.route('/anime', methods=['POST'])
|
| 803 |
+
def convert_to_anime():
|
| 804 |
+
try:
|
| 805 |
+
data = request.get_json()
|
| 806 |
+
if 'image' not in data:
|
| 807 |
+
return jsonify({'success': False, 'error': 'No image'}), 400
|
| 808 |
+
style = data.get('style', 'hayao')
|
| 809 |
+
job_id = str(uuid.uuid4())
|
| 810 |
+
if processing_queue.add_job(job_id, 'anime', data['image'], style=style):
|
| 811 |
+
return jsonify({'success': True, 'job_id': job_id, 'status_url': f'/status/{job_id}'}), 202
|
| 812 |
+
else:
|
| 813 |
+
return jsonify({'success': False, 'error': 'Queue full'}), 503
|
| 814 |
+
except Exception as e:
|
| 815 |
+
return jsonify({'success': False, 'error': str(e)}), 500
|
| 816 |
+
|
| 817 |
+
|
| 818 |
+
@app.route('/cartoon', methods=['POST'])
|
| 819 |
+
def convert_to_cartoon():
|
| 820 |
+
try:
|
| 821 |
+
data = request.get_json()
|
| 822 |
+
if 'image' not in data:
|
| 823 |
+
return jsonify({'success': False, 'error': 'No image'}), 400
|
| 824 |
+
style = data.get('style', 'default')
|
| 825 |
+
job_id = str(uuid.uuid4())
|
| 826 |
+
if processing_queue.add_job(job_id, 'cartoon', data['image'], style=style):
|
| 827 |
+
return jsonify({'success': True, 'job_id': job_id, 'status_url': f'/status/{job_id}'}), 202
|
| 828 |
+
else:
|
| 829 |
+
return jsonify({'success': False, 'error': 'Queue full'}), 503
|
| 830 |
+
except Exception as e:
|
| 831 |
+
return jsonify({'success': False, 'error': str(e)}), 500
|
| 832 |
+
|
| 833 |
+
|
| 834 |
+
@app.route('/sketch', methods=['POST'])
|
| 835 |
+
def convert_to_sketch():
|
| 836 |
+
try:
|
| 837 |
+
data = request.get_json()
|
| 838 |
+
if 'image' not in data:
|
| 839 |
+
return jsonify({'success': False, 'error': 'No image'}), 400
|
| 840 |
+
sketch_type = data.get('type', 'pencil')
|
| 841 |
+
job_id = str(uuid.uuid4())
|
| 842 |
+
if processing_queue.add_job(job_id, 'sketch', data['image'], type=sketch_type):
|
| 843 |
+
return jsonify({'success': True, 'job_id': job_id, 'status_url': f'/status/{job_id}'}), 202
|
| 844 |
+
else:
|
| 845 |
+
return jsonify({'success': False, 'error': 'Queue full'}), 503
|
| 846 |
+
except Exception as e:
|
| 847 |
+
return jsonify({'success': False, 'error': str(e)}), 500
|
| 848 |
+
|
| 849 |
+
|
| 850 |
+
@app.route('/status/<job_id>', methods=['GET'])
|
| 851 |
+
def get_job_status(job_id):
|
| 852 |
+
status = processing_queue.get_job_status(job_id)
|
| 853 |
+
if status['status'] == 'not_found':
|
| 854 |
+
return jsonify({'success': False, 'error': 'Job not found'}), 404
|
| 855 |
+
return jsonify({'success': True, 'job_id': job_id, **status}), 200
|
| 856 |
+
|
| 857 |
+
|
| 858 |
+
@app.route('/queue/stats', methods=['GET'])
|
| 859 |
+
def queue_stats():
|
| 860 |
+
return jsonify({
|
| 861 |
+
'success': True,
|
| 862 |
+
'queue_size': processing_queue.queue.qsize(),
|
| 863 |
+
'total_jobs': len(processing_queue.results)
|
| 864 |
+
}), 200
|
| 865 |
+
|
| 866 |
+
|
| 867 |
+
if __name__ == '__main__':
|
| 868 |
+
port = int(os.environ.get('PORT', 7860))
|
| 869 |
+
try:
|
| 870 |
+
app.run(host='0.0.0.0', port=port, debug=False, threaded=True)
|
| 871 |
+
finally:
|
| 872 |
+
processing_queue.stop()
|