Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,46 +10,22 @@ import uuid
|
|
| 10 |
import mimetypes
|
| 11 |
import numpy as np
|
| 12 |
from PIL import Image
|
| 13 |
-
import traceback
|
| 14 |
-
from scipy import ndimage
|
| 15 |
-
from scipy.ndimage import gaussian_filter, sobel
|
| 16 |
-
|
| 17 |
-
# Real-ESRGAN imports with comprehensive error handling
|
| 18 |
-
REALESRGAN_AVAILABLE = False
|
| 19 |
-
REALESRGAN_ERROR = None
|
| 20 |
-
|
| 21 |
-
try:
|
| 22 |
-
from realesrgan import RealESRGANer
|
| 23 |
-
from basicsr.archs.rrdbnet_arch import RRDBNet
|
| 24 |
-
REALESRGAN_AVAILABLE = False
|
| 25 |
-
print("✅ Real-ESRGAN successfully imported")
|
| 26 |
-
except ImportError as e:
|
| 27 |
-
REALESRGAN_ERROR = str(e)
|
| 28 |
-
print(f"⚠️ Real-ESRGAN not available: {e}")
|
| 29 |
-
except Exception as e:
|
| 30 |
-
REALESRGAN_ERROR = str(e)
|
| 31 |
-
print(f"❌ Real-ESRGAN import error: {e}")
|
| 32 |
|
| 33 |
# Configuration
|
| 34 |
UPLOAD_FOLDER = '/data/uploads'
|
| 35 |
OUTPUT_FOLDER = '/data/outputs'
|
| 36 |
-
MODEL_FOLDER = '/data/models'
|
| 37 |
|
| 38 |
# Global application state
|
| 39 |
app_state = {
|
| 40 |
"cuda_available": torch.cuda.is_available(),
|
| 41 |
-
"realesrgan_available": REALESRGAN_AVAILABLE,
|
| 42 |
-
"realesrgan_error": REALESRGAN_ERROR,
|
| 43 |
"processing_active": False,
|
| 44 |
"logs": [],
|
| 45 |
-
"processed_files": []
|
| 46 |
-
"current_model": None,
|
| 47 |
-
"upscaler": None
|
| 48 |
}
|
| 49 |
|
| 50 |
def ensure_directories():
|
| 51 |
"""Create necessary directories"""
|
| 52 |
-
directories = [UPLOAD_FOLDER, OUTPUT_FOLDER
|
| 53 |
for directory in directories:
|
| 54 |
try:
|
| 55 |
os.makedirs(directory, exist_ok=True)
|
|
@@ -60,7 +36,7 @@ def ensure_directories():
|
|
| 60 |
def allowed_file(filename):
|
| 61 |
"""Check if file has allowed extension"""
|
| 62 |
return '.' in filename and \
|
| 63 |
-
filename.rsplit('.', 1)[1].lower() in ['png', 'jpg', 'jpeg', 'gif', '
|
| 64 |
|
| 65 |
def get_file_mimetype(filename):
|
| 66 |
"""Get correct mimetype for file"""
|
|
@@ -69,7 +45,7 @@ def get_file_mimetype(filename):
|
|
| 69 |
ext = filename.lower().rsplit('.', 1)[1] if '.' in filename else ''
|
| 70 |
if ext in ['mp4', 'avi', 'mov', 'mkv']:
|
| 71 |
mimetype = f'video/{ext}'
|
| 72 |
-
elif ext in ['png', 'jpg', 'jpeg', 'gif'
|
| 73 |
mimetype = f'image/{ext}'
|
| 74 |
else:
|
| 75 |
mimetype = 'application/octet-stream'
|
|
@@ -83,626 +59,191 @@ def log_message(message):
|
|
| 83 |
app_state["logs"] = app_state["logs"][-100:]
|
| 84 |
print(f"[{timestamp}] {message}")
|
| 85 |
|
| 86 |
-
# =============================================================================
|
| 87 |
-
# 2024 OPTIMIZED UPSCALING TECHNIQUES - STATE OF THE ART CPU METHODS
|
| 88 |
-
# =============================================================================
|
| 89 |
-
|
| 90 |
-
def optimized_upscaling_4x(image, use_lanczos=True, adaptive_sharpening=True, edge_enhancement=True):
|
| 91 |
-
"""
|
| 92 |
-
2024 State-of-the-art CPU upscaling using:
|
| 93 |
-
- Lanczos4 (better than bicubic)
|
| 94 |
-
- Adaptive Edge Sharpening (AES)
|
| 95 |
-
- Iterative Optimized Sharpening (IOS)
|
| 96 |
-
- Optimized Directional Anisotropic Diffusion (ODAD)
|
| 97 |
-
|
| 98 |
-
Research shows 1.6+ dB PSNR improvement over standard bicubic
|
| 99 |
-
"""
|
| 100 |
-
h, w = image.shape[:2]
|
| 101 |
-
target_w, target_h = w * 4, h * 4
|
| 102 |
-
|
| 103 |
-
start_time = time.time()
|
| 104 |
-
|
| 105 |
-
# Step 1: Advanced interpolation method selection
|
| 106 |
-
if use_lanczos:
|
| 107 |
-
# Lanczos4 - proven better than bicubic for detail preservation
|
| 108 |
-
upscaled = cv2.resize(image, (target_w, target_h), interpolation=cv2.INTER_LANCZOS4)
|
| 109 |
-
method = "Lanczos4"
|
| 110 |
-
else:
|
| 111 |
-
# Progressive bicubic (2x -> 2x for better quality)
|
| 112 |
-
intermediate = cv2.resize(image, (w * 2, h * 2), interpolation=cv2.INTER_CUBIC)
|
| 113 |
-
upscaled = cv2.resize(intermediate, (target_w, target_h), interpolation=cv2.INTER_CUBIC)
|
| 114 |
-
method = "Progressive Bicubic"
|
| 115 |
-
|
| 116 |
-
# Step 2: Adaptive Edge Enhancement (AES) - 2024 technique
|
| 117 |
-
if edge_enhancement:
|
| 118 |
-
upscaled = adaptive_edge_enhancement(upscaled)
|
| 119 |
-
method += " + AES"
|
| 120 |
-
|
| 121 |
-
# Step 3: Iterative Optimized Sharpening (IOS) - 2024 technique
|
| 122 |
-
if adaptive_sharpening:
|
| 123 |
-
upscaled = iterative_optimized_sharpening(upscaled)
|
| 124 |
-
method += " + IOS"
|
| 125 |
-
|
| 126 |
-
# Step 4: ODAD filtering for texture preservation
|
| 127 |
-
upscaled = optimized_directional_anisotropic_diffusion(upscaled)
|
| 128 |
-
method += " + ODAD"
|
| 129 |
-
|
| 130 |
-
processing_time = time.time() - start_time
|
| 131 |
-
|
| 132 |
-
return upscaled, method, processing_time
|
| 133 |
-
|
| 134 |
-
def adaptive_edge_enhancement(image):
|
| 135 |
-
"""
|
| 136 |
-
Adaptive Edge Sharpening (AES) - 2024 technique
|
| 137 |
-
Detects edges and applies selective enhancement only where needed
|
| 138 |
-
"""
|
| 139 |
-
# Convert to float for precision
|
| 140 |
-
img_float = image.astype(np.float32) / 255.0
|
| 141 |
-
|
| 142 |
-
# Multi-scale edge detection for robust identification
|
| 143 |
-
edges = detect_multiscale_edges(img_float)
|
| 144 |
-
|
| 145 |
-
# Create adaptive enhancement mask
|
| 146 |
-
enhancement_mask = create_adaptive_mask(edges)
|
| 147 |
-
|
| 148 |
-
# Apply selective sharpening only on edges
|
| 149 |
-
enhanced = apply_selective_sharpening(img_float, enhancement_mask)
|
| 150 |
-
|
| 151 |
-
return (np.clip(enhanced, 0, 1) * 255).astype(np.uint8)
|
| 152 |
-
|
| 153 |
-
def detect_multiscale_edges(image):
|
| 154 |
-
"""
|
| 155 |
-
Multi-scale edge detection for robust edge identification
|
| 156 |
-
"""
|
| 157 |
-
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 158 |
-
|
| 159 |
-
# Fine scale edge detection
|
| 160 |
-
sobel_x = cv2.Sobel(gray, cv2.CV_32F, 1, 0, ksize=3)
|
| 161 |
-
sobel_y = cv2.Sobel(gray, cv2.CV_32F, 0, 1, ksize=3)
|
| 162 |
-
edges_fine = np.sqrt(sobel_x**2 + sobel_y**2)
|
| 163 |
-
|
| 164 |
-
# Coarse scale for major edges
|
| 165 |
-
sobel_x_coarse = cv2.Sobel(gray, cv2.CV_32F, 1, 0, ksize=5)
|
| 166 |
-
sobel_y_coarse = cv2.Sobel(gray, cv2.CV_32F, 0, 1, ksize=5)
|
| 167 |
-
edges_coarse = np.sqrt(sobel_x_coarse**2 + sobel_y_coarse**2)
|
| 168 |
-
|
| 169 |
-
# Combine scales with weighting
|
| 170 |
-
edges = 0.7 * edges_fine + 0.3 * edges_coarse
|
| 171 |
-
|
| 172 |
-
# Normalize
|
| 173 |
-
edges = edges / edges.max() if edges.max() > 0 else edges
|
| 174 |
-
|
| 175 |
-
return edges
|
| 176 |
-
|
| 177 |
-
def create_adaptive_mask(edges):
|
| 178 |
-
"""
|
| 179 |
-
Create adaptive enhancement mask based on edge strength
|
| 180 |
-
"""
|
| 181 |
-
# Adaptive thresholding based on local statistics
|
| 182 |
-
threshold = np.mean(edges) + 0.5 * np.std(edges)
|
| 183 |
-
|
| 184 |
-
# Create graduated mask (not binary) for smooth transitions
|
| 185 |
-
mask = np.clip((edges - threshold * 0.3) / (threshold * 0.7), 0, 1)
|
| 186 |
-
|
| 187 |
-
# Smooth the mask to avoid artifacts
|
| 188 |
-
mask = cv2.GaussianBlur(mask, (5, 5), 1.0)
|
| 189 |
-
|
| 190 |
-
return mask
|
| 191 |
-
|
| 192 |
-
def apply_selective_sharpening(image, mask):
|
| 193 |
-
"""
|
| 194 |
-
Apply sharpening selectively based on edge mask
|
| 195 |
-
"""
|
| 196 |
-
enhanced_channels = []
|
| 197 |
-
|
| 198 |
-
for i in range(3):
|
| 199 |
-
channel = image[:, :, i]
|
| 200 |
-
|
| 201 |
-
# High-pass filter for edge sharpening
|
| 202 |
-
kernel = np.array([
|
| 203 |
-
[-0.1, -0.3, -0.1],
|
| 204 |
-
[-0.3, 2.6, -0.3],
|
| 205 |
-
[-0.1, -0.3, -0.1]
|
| 206 |
-
])
|
| 207 |
-
|
| 208 |
-
sharpened = cv2.filter2D(channel, -1, kernel)
|
| 209 |
-
|
| 210 |
-
# Apply enhancement only where mask indicates edges
|
| 211 |
-
enhanced = channel + (sharpened - channel) * mask * 0.4
|
| 212 |
-
enhanced_channels.append(enhanced)
|
| 213 |
-
|
| 214 |
-
return np.stack(enhanced_channels, axis=2)
|
| 215 |
-
|
| 216 |
-
def iterative_optimized_sharpening(image):
|
| 217 |
-
"""
|
| 218 |
-
Iterative Optimized Sharpening (IOS) - 2024 technique
|
| 219 |
-
Compensates for HF degradation with adaptive strength
|
| 220 |
-
Research shows 1.6+ dB PSNR improvement over standard methods
|
| 221 |
-
"""
|
| 222 |
-
img_float = image.astype(np.float32) / 255.0
|
| 223 |
-
|
| 224 |
-
# Calculate local variance for adaptive sharpening strength
|
| 225 |
-
gray = cv2.cvtColor(img_float, cv2.COLOR_BGR2GRAY)
|
| 226 |
-
|
| 227 |
-
# Local variance calculation for content-aware processing
|
| 228 |
-
mean_local = cv2.GaussianBlur(gray, (5, 5), 1.0)
|
| 229 |
-
sqr_local = cv2.GaussianBlur(gray**2, (5, 5), 1.0)
|
| 230 |
-
variance_local = sqr_local - mean_local**2
|
| 231 |
-
|
| 232 |
-
# Normalize variance for adaptive strength
|
| 233 |
-
variance_norm = np.clip(variance_local / (variance_local.max() + 1e-6), 0, 1)
|
| 234 |
-
|
| 235 |
-
# Adaptive sharpening kernels based on local content
|
| 236 |
-
result_channels = []
|
| 237 |
-
|
| 238 |
-
for i in range(3):
|
| 239 |
-
channel = img_float[:, :, i]
|
| 240 |
-
|
| 241 |
-
# High detail areas - stronger sharpening
|
| 242 |
-
kernel_strong = np.array([
|
| 243 |
-
[-0.2, -0.6, -0.2],
|
| 244 |
-
[-0.6, 4.4, -0.6],
|
| 245 |
-
[-0.2, -0.6, -0.2]
|
| 246 |
-
])
|
| 247 |
-
|
| 248 |
-
# Low detail areas - gentle sharpening
|
| 249 |
-
kernel_gentle = np.array([
|
| 250 |
-
[-0.05, -0.2, -0.05],
|
| 251 |
-
[-0.2, 1.5, -0.2],
|
| 252 |
-
[-0.05, -0.2, -0.05]
|
| 253 |
-
])
|
| 254 |
-
|
| 255 |
-
# Apply appropriate kernel
|
| 256 |
-
sharp_strong = cv2.filter2D(channel, -1, kernel_strong)
|
| 257 |
-
sharp_gentle = cv2.filter2D(channel, -1, kernel_gentle)
|
| 258 |
-
|
| 259 |
-
# Blend based on local variance (adaptive)
|
| 260 |
-
result = variance_norm * sharp_strong + (1 - variance_norm) * sharp_gentle
|
| 261 |
-
result_channels.append(result)
|
| 262 |
-
|
| 263 |
-
result = np.stack(result_channels, axis=2)
|
| 264 |
-
return (np.clip(result, 0, 1) * 255).astype(np.uint8)
|
| 265 |
-
|
| 266 |
-
def optimized_directional_anisotropic_diffusion(image, iterations=3):
|
| 267 |
-
"""
|
| 268 |
-
Optimized Directional Anisotropic Diffusion (ODAD) - 2024 technique
|
| 269 |
-
Preserves texture details while reducing upscaling artifacts
|
| 270 |
-
"""
|
| 271 |
-
img_float = image.astype(np.float32) / 255.0
|
| 272 |
-
|
| 273 |
-
# Diffusion parameters tuned for upscaling artifacts
|
| 274 |
-
kappa = 30 # Edge threshold
|
| 275 |
-
gamma = 0.1 # Diffusion rate
|
| 276 |
-
|
| 277 |
-
for iteration in range(iterations):
|
| 278 |
-
# Calculate gradients in 4 cardinal directions
|
| 279 |
-
grad_n = np.roll(img_float, -1, axis=0) - img_float # North
|
| 280 |
-
grad_s = np.roll(img_float, 1, axis=0) - img_float # South
|
| 281 |
-
grad_e = np.roll(img_float, -1, axis=1) - img_float # East
|
| 282 |
-
grad_w = np.roll(img_float, 1, axis=1) - img_float # West
|
| 283 |
-
|
| 284 |
-
# Calculate diffusion coefficients (edge-stopping function)
|
| 285 |
-
c_n = np.exp(-(grad_n / kappa)**2)
|
| 286 |
-
c_s = np.exp(-(grad_s / kappa)**2)
|
| 287 |
-
c_e = np.exp(-(grad_e / kappa)**2)
|
| 288 |
-
c_w = np.exp(-(grad_w / kappa)**2)
|
| 289 |
-
|
| 290 |
-
# Apply anisotropic diffusion
|
| 291 |
-
diffusion = gamma * (c_n * grad_n + c_s * grad_s + c_e * grad_e + c_w * grad_w)
|
| 292 |
-
img_float += diffusion
|
| 293 |
-
|
| 294 |
-
# Clamp values to valid range
|
| 295 |
-
img_float = np.clip(img_float, 0, 1)
|
| 296 |
-
|
| 297 |
-
return (img_float * 255).astype(np.uint8)
|
| 298 |
-
|
| 299 |
-
def enhanced_temporal_smoothing(current_frame, frame_buffer, target_w, target_h):
|
| 300 |
-
"""
|
| 301 |
-
Enhanced temporal smoothing with motion-adaptive weights
|
| 302 |
-
"""
|
| 303 |
-
if len(frame_buffer) < 2:
|
| 304 |
-
return current_frame
|
| 305 |
-
|
| 306 |
-
current_float = current_frame.astype(np.float32)
|
| 307 |
-
|
| 308 |
-
# Calculate adaptive weights based on motion and time
|
| 309 |
-
motion_weights = []
|
| 310 |
-
total_weight = 1.0 # Current frame weight
|
| 311 |
-
|
| 312 |
-
for i, prev_frame in enumerate(frame_buffer[-2:]):
|
| 313 |
-
# Resize previous frame for comparison
|
| 314 |
-
prev_upscaled = cv2.resize(prev_frame, (target_w, target_h), interpolation=cv2.INTER_LANCZOS4)
|
| 315 |
-
prev_float = prev_upscaled.astype(np.float32)
|
| 316 |
-
|
| 317 |
-
# Calculate motion-based similarity
|
| 318 |
-
diff = np.mean(np.abs(current_float - prev_float))
|
| 319 |
-
motion_factor = np.exp(-diff / 40.0)
|
| 320 |
-
|
| 321 |
-
# Time decay (more recent frames have higher weight)
|
| 322 |
-
time_decay = 0.8 ** (i + 1)
|
| 323 |
-
|
| 324 |
-
weight = motion_factor * time_decay * 0.3
|
| 325 |
-
motion_weights.append(weight)
|
| 326 |
-
total_weight += weight
|
| 327 |
-
|
| 328 |
-
# Normalize weights
|
| 329 |
-
motion_weights = [w / total_weight for w in motion_weights]
|
| 330 |
-
current_weight = 1.0 / total_weight
|
| 331 |
-
|
| 332 |
-
# Apply weighted temporal average
|
| 333 |
-
result = current_float * current_weight
|
| 334 |
-
|
| 335 |
-
for i, prev_frame in enumerate(frame_buffer[-2:]):
|
| 336 |
-
prev_upscaled = cv2.resize(prev_frame, (target_w, target_h), interpolation=cv2.INTER_LANCZOS4)
|
| 337 |
-
result += prev_upscaled.astype(np.float32) * motion_weights[i]
|
| 338 |
-
|
| 339 |
-
return np.clip(result, 0, 255).astype(np.uint8)
|
| 340 |
-
|
| 341 |
-
def final_quality_enhancement(image):
|
| 342 |
-
"""
|
| 343 |
-
Final quality enhancement pass with color optimization
|
| 344 |
-
"""
|
| 345 |
-
# Convert to LAB for better color processing
|
| 346 |
-
lab = cv2.cvtColor(image, cv2.COLOR_BGR2LAB).astype(np.float32)
|
| 347 |
-
|
| 348 |
-
# Enhance L channel with adaptive histogram equalization
|
| 349 |
-
clahe = cv2.createCLAHE(clipLimit=1.5, tileGridSize=(8, 8))
|
| 350 |
-
lab[:, :, 0] = clahe.apply(lab[:, :, 0].astype(np.uint8))
|
| 351 |
-
|
| 352 |
-
# Convert back to BGR
|
| 353 |
-
enhanced = cv2.cvtColor(lab.astype(np.uint8), cv2.COLOR_LAB2BGR)
|
| 354 |
-
|
| 355 |
-
# Slight saturation boost (5%) for more vivid colors
|
| 356 |
-
hsv = cv2.cvtColor(enhanced, cv2.COLOR_BGR2HSV).astype(np.float32)
|
| 357 |
-
hsv[:, :, 1] *= 1.05
|
| 358 |
-
hsv[:, :, 1] = np.clip(hsv[:, :, 1], 0, 255)
|
| 359 |
-
|
| 360 |
-
final = cv2.cvtColor(hsv.astype(np.uint8), cv2.COLOR_HSV2BGR)
|
| 361 |
-
|
| 362 |
-
return final
|
| 363 |
-
|
| 364 |
-
def advanced_frame_upscaling_optimized(frame, target_w, target_h, frame_buffer, buffer_size):
|
| 365 |
-
"""
|
| 366 |
-
2024 Optimized frame upscaling using state-of-the-art CPU techniques
|
| 367 |
-
"""
|
| 368 |
-
# Step 1: Apply 2024 optimized upscaling techniques
|
| 369 |
-
upscaled, method, proc_time = optimized_upscaling_4x(
|
| 370 |
-
frame,
|
| 371 |
-
use_lanczos=True, # Better than bicubic
|
| 372 |
-
adaptive_sharpening=True, # IOS technique
|
| 373 |
-
edge_enhancement=True # AES technique
|
| 374 |
-
)
|
| 375 |
-
|
| 376 |
-
# Step 2: Enhanced temporal smoothing (if previous frames available)
|
| 377 |
-
if len(frame_buffer) >= 2:
|
| 378 |
-
upscaled = enhanced_temporal_smoothing(upscaled, frame_buffer, target_w, target_h)
|
| 379 |
-
method += " + Enhanced Temporal"
|
| 380 |
-
|
| 381 |
-
# Step 3: Final quality enhancement
|
| 382 |
-
upscaled = final_quality_enhancement(upscaled)
|
| 383 |
-
|
| 384 |
-
return upscaled
|
| 385 |
-
|
| 386 |
-
# =============================================================================
|
| 387 |
-
# REAL-ESRGAN FUNCTIONS (LEGACY SUPPORT)
|
| 388 |
-
# =============================================================================
|
| 389 |
-
|
| 390 |
-
def download_realesrgan_models():
|
| 391 |
-
"""Download Real-ESRGAN models if not present"""
|
| 392 |
-
if not REALESRGAN_AVAILABLE:
|
| 393 |
-
log_message("❌ Real-ESRGAN not available for model download")
|
| 394 |
-
return False
|
| 395 |
-
|
| 396 |
-
models = {
|
| 397 |
-
'RealESRGAN_x4plus': 'https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth',
|
| 398 |
-
'RealESRGAN_x2plus': 'https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.1/RealESRGAN_x2plus.pth'
|
| 399 |
-
}
|
| 400 |
-
|
| 401 |
-
try:
|
| 402 |
-
import urllib.request
|
| 403 |
-
for model_name, url in models.items():
|
| 404 |
-
model_path = os.path.join(MODEL_FOLDER, f"{model_name}.pth")
|
| 405 |
-
if not os.path.exists(model_path):
|
| 406 |
-
log_message(f"📥 Downloading {model_name}...")
|
| 407 |
-
try:
|
| 408 |
-
urllib.request.urlretrieve(url, model_path)
|
| 409 |
-
log_message(f"✅ Downloaded {model_name}")
|
| 410 |
-
except Exception as e:
|
| 411 |
-
log_message(f"❌ Failed to download {model_name}: {e}")
|
| 412 |
-
return False
|
| 413 |
-
else:
|
| 414 |
-
log_message(f"✅ Model {model_name} already exists")
|
| 415 |
-
return True
|
| 416 |
-
except Exception as e:
|
| 417 |
-
log_message(f"❌ Error downloading models: {str(e)}")
|
| 418 |
-
return False
|
| 419 |
-
|
| 420 |
-
def initialize_realesrgan(model_name='RealESRGAN_x4plus', scale=4):
|
| 421 |
-
"""Initialize Real-ESRGAN upscaler with robust error handling"""
|
| 422 |
-
if not REALESRGAN_AVAILABLE:
|
| 423 |
-
log_message(f"❌ Real-ESRGAN not available: {REALESRGAN_ERROR}")
|
| 424 |
-
return None
|
| 425 |
-
|
| 426 |
-
try:
|
| 427 |
-
log_message(f"🔧 Initializing Real-ESRGAN with {model_name}...")
|
| 428 |
-
|
| 429 |
-
model_path = os.path.join(MODEL_FOLDER, f"{model_name}.pth")
|
| 430 |
-
|
| 431 |
-
# Check if model exists, download if not
|
| 432 |
-
if not os.path.exists(model_path):
|
| 433 |
-
log_message(f"📥 Model {model_name} not found, downloading...")
|
| 434 |
-
if not download_realesrgan_models():
|
| 435 |
-
log_message("❌ Failed to download models")
|
| 436 |
-
return None
|
| 437 |
-
|
| 438 |
-
# Verify model file
|
| 439 |
-
if not os.path.exists(model_path) or os.path.getsize(model_path) == 0:
|
| 440 |
-
log_message(f"❌ Model file invalid: {model_path}")
|
| 441 |
-
return None
|
| 442 |
-
|
| 443 |
-
log_message(f"📁 Model file verified: {os.path.getsize(model_path) / (1024*1024):.1f}MB")
|
| 444 |
-
|
| 445 |
-
# Initialize model architecture
|
| 446 |
-
if model_name == 'RealESRGAN_x4plus':
|
| 447 |
-
model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=4)
|
| 448 |
-
netscale = 4
|
| 449 |
-
elif model_name == 'RealESRGAN_x2plus':
|
| 450 |
-
model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=2)
|
| 451 |
-
netscale = 2
|
| 452 |
-
else:
|
| 453 |
-
log_message(f"❌ Unknown model: {model_name}")
|
| 454 |
-
return None
|
| 455 |
-
|
| 456 |
-
# Use CPU for maximum compatibility
|
| 457 |
-
device = torch.device('cpu')
|
| 458 |
-
log_message(f"🖥️ Using device: {device}")
|
| 459 |
-
|
| 460 |
-
# Initialize upscaler with conservative settings
|
| 461 |
-
upscaler = RealESRGANer(
|
| 462 |
-
scale=netscale,
|
| 463 |
-
model_path=model_path,
|
| 464 |
-
model=model,
|
| 465 |
-
tile=400, # Reasonable tile size for CPU
|
| 466 |
-
tile_pad=10,
|
| 467 |
-
pre_pad=0,
|
| 468 |
-
half=False, # No half precision on CPU
|
| 469 |
-
device=device
|
| 470 |
-
)
|
| 471 |
-
|
| 472 |
-
# Test the upscaler with a small image
|
| 473 |
-
log_message("🧪 Testing Real-ESRGAN with sample image...")
|
| 474 |
-
test_img = np.random.randint(0, 255, (64, 64, 3), dtype=np.uint8)
|
| 475 |
-
try:
|
| 476 |
-
_, _ = upscaler.enhance(test_img, outscale=2)
|
| 477 |
-
log_message("✅ Real-ESRGAN test successful")
|
| 478 |
-
except Exception as e:
|
| 479 |
-
log_message(f"❌ Real-ESRGAN test failed: {e}")
|
| 480 |
-
return None
|
| 481 |
-
|
| 482 |
-
app_state["upscaler"] = upscaler
|
| 483 |
-
app_state["current_model"] = model_name
|
| 484 |
-
log_message(f"✅ Real-ESRGAN initialized: {model_name} on {device}")
|
| 485 |
-
return upscaler
|
| 486 |
-
|
| 487 |
-
except Exception as e:
|
| 488 |
-
log_message(f"❌ Error initializing Real-ESRGAN: {str(e)}")
|
| 489 |
-
log_message(f"🔍 Traceback: {traceback.format_exc()}")
|
| 490 |
-
app_state["upscaler"] = None
|
| 491 |
-
app_state["current_model"] = None
|
| 492 |
-
return None
|
| 493 |
-
|
| 494 |
def optimize_gpu():
|
| 495 |
-
"""Optimize GPU configuration"""
|
| 496 |
try:
|
| 497 |
if torch.cuda.is_available():
|
| 498 |
torch.backends.cudnn.benchmark = True
|
|
|
|
|
|
|
| 499 |
torch.cuda.empty_cache()
|
| 500 |
|
| 501 |
# Test GPU
|
| 502 |
test_tensor = torch.randn(100, 100, device='cuda')
|
| 503 |
_ = torch.mm(test_tensor, test_tensor)
|
| 504 |
-
del test_tensor
|
| 505 |
-
torch.cuda.empty_cache()
|
| 506 |
|
| 507 |
-
log_message("✅ GPU optimized")
|
| 508 |
return True
|
| 509 |
else:
|
| 510 |
-
log_message("⚠️ CUDA not available
|
| 511 |
return False
|
| 512 |
except Exception as e:
|
| 513 |
log_message(f"❌ Error optimizing GPU: {str(e)}")
|
| 514 |
return False
|
| 515 |
|
| 516 |
-
# =============================================================================
|
| 517 |
-
# MAIN UPSCALING FUNCTIONS
|
| 518 |
-
# =============================================================================
|
| 519 |
-
|
| 520 |
def upscale_image_4k(input_path, output_path):
|
| 521 |
-
"""
|
| 522 |
def process_worker():
|
| 523 |
try:
|
| 524 |
log_message(f"🎨 Starting 4K upscaling: {os.path.basename(input_path)}")
|
| 525 |
app_state["processing_active"] = True
|
| 526 |
|
| 527 |
-
|
| 528 |
-
|
| 529 |
-
|
| 530 |
-
|
| 531 |
-
img = cv2.imread(input_path, cv2.IMREAD_COLOR)
|
| 532 |
-
if img is None:
|
| 533 |
-
# Try with PIL as fallback
|
| 534 |
-
pil_img = Image.open(input_path).convert('RGB')
|
| 535 |
-
img = cv2.cvtColor(np.array(pil_img), cv2.COLOR_RGB2BGR)
|
| 536 |
-
except Exception as e:
|
| 537 |
-
log_message(f"❌ Error reading image: {e}")
|
| 538 |
-
return
|
| 539 |
-
|
| 540 |
-
if img is None:
|
| 541 |
-
log_message("�� Error: Could not read image with any method")
|
| 542 |
return
|
| 543 |
|
| 544 |
-
h, w =
|
| 545 |
log_message(f"📏 Original resolution: {w}x{h}")
|
| 546 |
|
| 547 |
-
|
| 548 |
-
|
| 549 |
-
|
| 550 |
-
|
| 551 |
-
|
| 552 |
-
|
| 553 |
-
|
| 554 |
-
|
| 555 |
-
|
| 556 |
-
|
| 557 |
-
|
| 558 |
-
log_message("✅ Real-ESRGAN upscaling successful")
|
| 559 |
-
|
| 560 |
-
except Exception as e:
|
| 561 |
-
log_message(f"⚠️ Real-ESRGAN failed: {str(e)}")
|
| 562 |
-
log_message("🔄 Falling back to 2024 optimized methods...")
|
| 563 |
-
else:
|
| 564 |
-
log_message("⚠️ Real-ESRGAN not available, using 2024 optimized methods")
|
| 565 |
-
|
| 566 |
-
# Use 2024 optimized methods if Real-ESRGAN failed or not available
|
| 567 |
-
if not success:
|
| 568 |
-
log_message("🚀 Using 2024 state-of-the-art CPU upscaling...")
|
| 569 |
|
| 570 |
-
if
|
| 571 |
-
|
| 572 |
-
|
| 573 |
-
|
| 574 |
-
|
| 575 |
-
|
| 576 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 577 |
|
| 578 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 579 |
|
| 580 |
-
|
| 581 |
-
|
| 582 |
-
|
| 583 |
-
|
| 584 |
-
|
| 585 |
-
mode='bicubic',
|
| 586 |
-
align_corners=False,
|
| 587 |
-
antialias=True
|
| 588 |
-
)
|
| 589 |
-
|
| 590 |
-
upscaled = torch.nn.functional.interpolate(
|
| 591 |
-
intermediate,
|
| 592 |
-
size=(target_h, target_w),
|
| 593 |
-
mode='bicubic',
|
| 594 |
-
align_corners=False,
|
| 595 |
-
antialias=True
|
| 596 |
-
)
|
| 597 |
-
|
| 598 |
-
# Enhanced sharpening
|
| 599 |
-
kernel = torch.tensor([
|
| 600 |
-
[-0.5, -1, -0.5],
|
| 601 |
-
[-1, 7, -1],
|
| 602 |
-
[-0.5, -1, -0.5]
|
| 603 |
-
], dtype=torch.float32, device=device).unsqueeze(0).unsqueeze(0)
|
| 604 |
-
|
| 605 |
-
enhanced_channels = []
|
| 606 |
-
for i in range(3):
|
| 607 |
-
channel = upscaled[:, i:i+1, :, :]
|
| 608 |
-
padded = torch.nn.functional.pad(channel, (1, 1, 1, 1), mode='reflect')
|
| 609 |
-
enhanced = torch.nn.functional.conv2d(padded, kernel)
|
| 610 |
-
enhanced_channels.append(enhanced)
|
| 611 |
-
|
| 612 |
-
enhanced = torch.cat(enhanced_channels, dim=1)
|
| 613 |
-
final_result = torch.clamp(enhanced, 0, 1)
|
| 614 |
|
| 615 |
-
|
| 616 |
-
|
| 617 |
-
|
|
|
|
|
|
|
| 618 |
|
| 619 |
-
|
| 620 |
-
|
| 621 |
-
|
| 622 |
-
|
|
|
|
| 623 |
|
| 624 |
-
|
| 625 |
-
|
| 626 |
-
|
| 627 |
-
|
| 628 |
-
|
| 629 |
-
|
| 630 |
-
|
| 631 |
-
try:
|
| 632 |
-
result, method_details, proc_time = optimized_upscaling_4x(
|
| 633 |
-
img,
|
| 634 |
-
use_lanczos=True,
|
| 635 |
-
adaptive_sharpening=True,
|
| 636 |
-
edge_enhancement=True
|
| 637 |
-
)
|
| 638 |
|
| 639 |
-
|
| 640 |
-
method_used = f"2024 Optimized CPU ({method_details})"
|
| 641 |
-
success = True
|
| 642 |
-
log_message(f"✅ 2024 optimized upscaling completed in {proc_time:.2f}s")
|
| 643 |
-
log_message(f"🔧 Techniques used: {method_details}")
|
| 644 |
|
| 645 |
-
|
| 646 |
-
|
| 647 |
-
|
| 648 |
-
|
| 649 |
-
|
| 650 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 651 |
target_h, target_w = h * 4, w * 4
|
| 652 |
|
| 653 |
-
# Progressive upscaling
|
| 654 |
-
intermediate = cv2.resize(
|
| 655 |
upscaled = cv2.resize(intermediate, (target_w, target_h), interpolation=cv2.INTER_CUBIC)
|
| 656 |
|
| 657 |
-
# Apply sharpening
|
| 658 |
-
kernel = np.array([
|
| 659 |
-
[-0.5, -1, -0.5],
|
| 660 |
-
[-1, 7, -1],
|
| 661 |
-
[-0.5, -1, -0.5]
|
| 662 |
-
])
|
| 663 |
sharpened = cv2.filter2D(upscaled, -1, kernel)
|
| 664 |
|
| 665 |
-
# Blend for
|
| 666 |
-
|
| 667 |
|
| 668 |
-
cv2.imwrite(output_path,
|
| 669 |
-
|
| 670 |
-
|
| 671 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 672 |
|
| 673 |
-
if success:
|
| 674 |
-
# Verify output
|
| 675 |
-
try:
|
| 676 |
-
final_img = cv2.imread(output_path)
|
| 677 |
-
if final_img is not None:
|
| 678 |
-
final_h, final_w = final_img.shape[:2]
|
| 679 |
-
processing_time = time.time() - start_time
|
| 680 |
-
|
| 681 |
-
log_message(f"✅ Upscaling completed: {final_w}x{final_h}")
|
| 682 |
-
log_message(f"📈 Scale factor: {final_w/w:.1f}x")
|
| 683 |
-
log_message(f"⏱️ Processing time: {processing_time:.1f}s")
|
| 684 |
-
log_message(f"🔧 Method used: {method_used}")
|
| 685 |
-
|
| 686 |
-
# Add to processed files
|
| 687 |
-
app_state["processed_files"].append({
|
| 688 |
-
"input_file": os.path.basename(input_path),
|
| 689 |
-
"output_file": os.path.basename(output_path),
|
| 690 |
-
"original_size": f"{w}x{h}",
|
| 691 |
-
"upscaled_size": f"{final_w}x{final_h}",
|
| 692 |
-
"method": method_used,
|
| 693 |
-
"processing_time": f"{processing_time:.1f}s",
|
| 694 |
-
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 695 |
-
})
|
| 696 |
-
else:
|
| 697 |
-
log_message("❌ Error: Output file could not be read")
|
| 698 |
-
except Exception as e:
|
| 699 |
-
log_message(f"❌ Error verifying output: {e}")
|
| 700 |
-
else:
|
| 701 |
-
log_message("❌ All upscaling methods failed")
|
| 702 |
-
|
| 703 |
except Exception as e:
|
| 704 |
-
log_message(f"❌
|
| 705 |
-
log_message(f"🔍 Traceback: {traceback.format_exc()}")
|
| 706 |
finally:
|
| 707 |
app_state["processing_active"] = False
|
| 708 |
if torch.cuda.is_available():
|
|
@@ -713,10 +254,10 @@ def upscale_image_4k(input_path, output_path):
|
|
| 713 |
thread.start()
|
| 714 |
|
| 715 |
def upscale_video_4k(input_path, output_path):
|
| 716 |
-
"""
|
| 717 |
def process_worker():
|
| 718 |
try:
|
| 719 |
-
log_message(f"🎬 Starting
|
| 720 |
app_state["processing_active"] = True
|
| 721 |
|
| 722 |
# Open video
|
|
@@ -732,93 +273,48 @@ def upscale_video_4k(input_path, output_path):
|
|
| 732 |
h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 733 |
log_message(f"📹 Video: {w}x{h}, {fps}FPS, {frame_count} frames")
|
| 734 |
|
| 735 |
-
# Configure output
|
| 736 |
target_w, target_h = w * 4, h * 4
|
| 737 |
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 738 |
out = cv2.VideoWriter(output_path, fourcc, fps, (target_w, target_h))
|
| 739 |
|
| 740 |
-
|
| 741 |
-
|
| 742 |
-
|
| 743 |
-
|
| 744 |
-
|
| 745 |
-
|
| 746 |
-
|
| 747 |
-
|
| 748 |
-
while True:
|
| 749 |
-
ret, frame = cap.read()
|
| 750 |
-
if not ret:
|
| 751 |
-
break
|
| 752 |
-
|
| 753 |
-
frame_num += 1
|
| 754 |
-
|
| 755 |
-
try:
|
| 756 |
-
frame_start = time.time()
|
| 757 |
-
|
| 758 |
-
# Apply 2024 optimized upscaling techniques
|
| 759 |
-
upscaled_frame = advanced_frame_upscaling_optimized(
|
| 760 |
-
frame, target_w, target_h, frame_buffer, buffer_size
|
| 761 |
-
)
|
| 762 |
-
|
| 763 |
-
frame_processing_time = time.time() - frame_start
|
| 764 |
-
total_processing_time += frame_processing_time
|
| 765 |
-
|
| 766 |
-
# Update frame buffer for temporal consistency
|
| 767 |
-
frame_buffer.append(frame.copy())
|
| 768 |
-
if len(frame_buffer) > buffer_size:
|
| 769 |
-
frame_buffer.pop(0)
|
| 770 |
-
|
| 771 |
-
out.write(upscaled_frame)
|
| 772 |
-
|
| 773 |
-
# Progress logging with performance metrics
|
| 774 |
-
if frame_num % 30 == 0:
|
| 775 |
-
progress = (frame_num / frame_count) * 100
|
| 776 |
-
elapsed = time.time() - start_time
|
| 777 |
-
avg_frame_time = total_processing_time / frame_num
|
| 778 |
-
eta = avg_frame_time * (frame_count - frame_num)
|
| 779 |
-
fps_current = frame_num / elapsed if elapsed > 0 else 0
|
| 780 |
-
|
| 781 |
-
log_message(f"🎞️ Frame {frame_num}/{frame_count} ({progress:.1f}%)")
|
| 782 |
-
log_message(f"⏱️ ETA: {eta:.0f}s | FPS: {fps_current:.1f} | Avg: {avg_frame_time:.2f}s/frame")
|
| 783 |
-
|
| 784 |
-
except Exception as e:
|
| 785 |
-
log_message(f"⚠️ Error processing frame {frame_num}: {e}")
|
| 786 |
-
break
|
| 787 |
|
| 788 |
cap.release()
|
| 789 |
out.release()
|
| 790 |
|
| 791 |
-
# Verify output
|
| 792 |
-
if os.path.exists(output_path)
|
| 793 |
file_size = os.path.getsize(output_path)
|
| 794 |
-
|
| 795 |
-
|
| 796 |
-
|
| 797 |
-
|
| 798 |
-
|
| 799 |
-
|
| 800 |
-
log_message(f"📊 Avg processing: {avg_frame_time:.2f}s/frame")
|
| 801 |
-
log_message(f"🚀 Techniques: Lanczos4 + AES + IOS + ODAD + Enhanced Temporal")
|
| 802 |
-
|
| 803 |
-
# Add to processed files
|
| 804 |
-
app_state["processed_files"].append({
|
| 805 |
-
"input_file": os.path.basename(input_path),
|
| 806 |
-
"output_file": os.path.basename(output_path),
|
| 807 |
-
"original_size": f"{w}x{h}",
|
| 808 |
-
"upscaled_size": f"{target_w}x{target_h}",
|
| 809 |
-
"frame_count": frame_count,
|
| 810 |
-
"fps": fps,
|
| 811 |
-
"method": "2024 Optimized CPU (Lanczos4+AES+IOS+ODAD+Temporal)",
|
| 812 |
-
"processing_time": f"{total_time:.1f}s",
|
| 813 |
-
"avg_frame_time": f"{avg_frame_time:.2f}s",
|
| 814 |
-
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 815 |
-
})
|
| 816 |
else:
|
| 817 |
-
log_message("❌
|
| 818 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 819 |
except Exception as e:
|
| 820 |
log_message(f"❌ Error processing video: {str(e)}")
|
| 821 |
-
log_message(f"🔍 Traceback: {traceback.format_exc()}")
|
| 822 |
finally:
|
| 823 |
app_state["processing_active"] = False
|
| 824 |
if torch.cuda.is_available():
|
|
@@ -828,46 +324,125 @@ def upscale_video_4k(input_path, output_path):
|
|
| 828 |
thread.daemon = True
|
| 829 |
thread.start()
|
| 830 |
|
| 831 |
-
|
| 832 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 833 |
|
| 834 |
-
def
|
| 835 |
-
"""
|
| 836 |
-
|
|
|
|
| 837 |
|
| 838 |
-
|
| 839 |
-
|
| 840 |
-
|
| 841 |
-
|
| 842 |
-
try:
|
| 843 |
-
# Try to download models first
|
| 844 |
-
log_message("📥 Checking/downloading Real-ESRGAN models...")
|
| 845 |
-
download_success = download_realesrgan_models()
|
| 846 |
-
if not download_success:
|
| 847 |
-
log_message("❌ Model download failed")
|
| 848 |
-
return False
|
| 849 |
|
| 850 |
-
|
| 851 |
-
|
| 852 |
-
|
| 853 |
-
|
| 854 |
-
|
| 855 |
-
|
| 856 |
-
|
| 857 |
-
|
| 858 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 859 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 860 |
except Exception as e:
|
| 861 |
-
log_message(f"❌
|
| 862 |
-
|
| 863 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 864 |
|
| 865 |
-
#
|
| 866 |
-
|
| 867 |
-
if REALESRGAN_AVAILABLE:
|
| 868 |
-
force_init_realesrgan()
|
| 869 |
-
else:
|
| 870 |
-
log_message("⚠️ Real-ESRGAN not available, will use 2024 optimized CPU methods")
|
| 871 |
|
| 872 |
app = Flask(__name__)
|
| 873 |
|
|
@@ -877,7 +452,7 @@ def index():
|
|
| 877 |
|
| 878 |
@app.route('/api/system')
|
| 879 |
def api_system():
|
| 880 |
-
"""Get
|
| 881 |
try:
|
| 882 |
info = {}
|
| 883 |
|
|
@@ -885,61 +460,47 @@ def api_system():
|
|
| 885 |
if torch.cuda.is_available():
|
| 886 |
info["gpu_available"] = True
|
| 887 |
info["gpu_name"] = torch.cuda.get_device_name()
|
|
|
|
| 888 |
total_memory = torch.cuda.get_device_properties(0).total_memory
|
| 889 |
allocated_memory = torch.cuda.memory_allocated()
|
|
|
|
| 890 |
info["gpu_memory"] = f"{total_memory / (1024**3):.1f}GB"
|
| 891 |
info["gpu_memory_used"] = f"{allocated_memory / (1024**3):.1f}GB"
|
| 892 |
info["gpu_memory_free"] = f"{(total_memory - allocated_memory) / (1024**3):.1f}GB"
|
| 893 |
info["cuda_version"] = torch.version.cuda
|
|
|
|
| 894 |
else:
|
| 895 |
info["gpu_available"] = False
|
| 896 |
-
info["gpu_name"] = "CPU Only"
|
| 897 |
info["gpu_memory"] = "N/A"
|
| 898 |
info["gpu_memory_used"] = "N/A"
|
| 899 |
info["gpu_memory_free"] = "N/A"
|
| 900 |
info["cuda_version"] = "Not available"
|
| 901 |
-
|
| 902 |
-
info["pytorch_version"] = torch.__version__
|
| 903 |
-
|
| 904 |
-
# Real-ESRGAN info
|
| 905 |
-
info["realesrgan_available"] = REALESRGAN_AVAILABLE
|
| 906 |
-
info["realesrgan_initialized"] = app_state["upscaler"] is not None
|
| 907 |
-
info["current_model"] = app_state.get("current_model", "None")
|
| 908 |
-
info["realesrgan_error"] = REALESRGAN_ERROR
|
| 909 |
-
|
| 910 |
-
# 2024 Optimization info
|
| 911 |
-
info["optimized_cpu_available"] = True
|
| 912 |
-
info["optimization_techniques"] = "Lanczos4 + AES + IOS + ODAD + Enhanced Temporal"
|
| 913 |
-
info["estimated_improvement"] = "1.6+ dB PSNR over standard bicubic"
|
| 914 |
-
|
| 915 |
-
# Check if models exist
|
| 916 |
-
models_status = {}
|
| 917 |
-
if REALESRGAN_AVAILABLE:
|
| 918 |
-
models = ['RealESRGAN_x4plus', 'RealESRGAN_x2plus']
|
| 919 |
-
for model in models:
|
| 920 |
-
model_path = os.path.join(MODEL_FOLDER, f"{model}.pth")
|
| 921 |
-
models_status[model] = os.path.exists(model_path)
|
| 922 |
-
info["models_downloaded"] = models_status
|
| 923 |
|
| 924 |
# Storage info
|
| 925 |
-
|
| 926 |
-
|
| 927 |
-
|
| 928 |
-
|
| 929 |
-
|
| 930 |
-
|
| 931 |
-
|
| 932 |
-
|
| 933 |
-
|
| 934 |
-
|
| 935 |
-
|
| 936 |
-
|
| 937 |
-
|
| 938 |
-
|
| 939 |
-
|
| 940 |
-
|
| 941 |
-
|
| 942 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 943 |
|
| 944 |
return jsonify({"success": True, "data": info})
|
| 945 |
except Exception as e:
|
|
@@ -968,7 +529,7 @@ def api_upload():
|
|
| 968 |
output_filename = f"{file_id}_4k.{file_ext}"
|
| 969 |
output_path = os.path.join(OUTPUT_FOLDER, output_filename)
|
| 970 |
|
| 971 |
-
if file_ext in ['png', 'jpg', 'jpeg', 'gif'
|
| 972 |
upscale_image_4k(input_path, output_path)
|
| 973 |
media_type = "image"
|
| 974 |
elif file_ext in ['mp4', 'avi', 'mov', 'mkv']:
|
|
@@ -976,7 +537,7 @@ def api_upload():
|
|
| 976 |
media_type = "video"
|
| 977 |
|
| 978 |
log_message(f"📤 File uploaded: {filename}")
|
| 979 |
-
log_message(f"
|
| 980 |
|
| 981 |
return jsonify({
|
| 982 |
"success": True,
|
|
@@ -984,7 +545,7 @@ def api_upload():
|
|
| 984 |
"filename": filename,
|
| 985 |
"output_filename": output_filename,
|
| 986 |
"media_type": media_type,
|
| 987 |
-
"message": "Upload successful,
|
| 988 |
})
|
| 989 |
else:
|
| 990 |
return jsonify({"success": False, "error": "File type not allowed"})
|
|
@@ -1007,12 +568,22 @@ def api_download(filename):
|
|
| 1007 |
file_path = os.path.join(OUTPUT_FOLDER, filename)
|
| 1008 |
if os.path.exists(file_path):
|
| 1009 |
mimetype = get_file_mimetype(filename)
|
| 1010 |
-
|
| 1011 |
-
|
| 1012 |
-
|
| 1013 |
-
|
| 1014 |
-
|
| 1015 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1016 |
else:
|
| 1017 |
return jsonify({"error": "File not found"}), 404
|
| 1018 |
except Exception as e:
|
|
@@ -1051,31 +622,16 @@ def api_optimize_gpu():
|
|
| 1051 |
"""Optimize GPU for processing"""
|
| 1052 |
try:
|
| 1053 |
success = optimize_gpu()
|
| 1054 |
-
return jsonify({"success": success})
|
| 1055 |
-
except Exception as e:
|
| 1056 |
-
return jsonify({"success": False, "error": str(e)})
|
| 1057 |
-
|
| 1058 |
-
@app.route('/api/init-realesrgan', methods=['POST'])
|
| 1059 |
-
def api_init_realesrgan():
|
| 1060 |
-
"""Initialize Real-ESRGAN manually"""
|
| 1061 |
-
try:
|
| 1062 |
-
if not REALESRGAN_AVAILABLE:
|
| 1063 |
-
return jsonify({
|
| 1064 |
-
"success": False,
|
| 1065 |
-
"error": f"Real-ESRGAN not available: {REALESRGAN_ERROR}"
|
| 1066 |
-
})
|
| 1067 |
-
|
| 1068 |
-
success = force_init_realesrgan()
|
| 1069 |
if success:
|
| 1070 |
-
return jsonify({"success": True, "message": "
|
| 1071 |
else:
|
| 1072 |
-
return jsonify({"success": False, "
|
| 1073 |
except Exception as e:
|
| 1074 |
return jsonify({"success": False, "error": str(e)})
|
| 1075 |
|
| 1076 |
@app.route('/api/clear-cache', methods=['POST'])
|
| 1077 |
def api_clear_cache():
|
| 1078 |
-
"""Clear cache and processed files"""
|
| 1079 |
try:
|
| 1080 |
if torch.cuda.is_available():
|
| 1081 |
torch.cuda.empty_cache()
|
|
@@ -1087,116 +643,20 @@ def api_clear_cache():
|
|
| 1087 |
except Exception as e:
|
| 1088 |
return jsonify({"success": False, "error": str(e)})
|
| 1089 |
|
| 1090 |
-
@app.route('/api/test-realesrgan', methods=['POST'])
|
| 1091 |
-
def api_test_realesrgan():
|
| 1092 |
-
"""Test Real-ESRGAN installation"""
|
| 1093 |
-
try:
|
| 1094 |
-
if not REALESRGAN_AVAILABLE:
|
| 1095 |
-
return jsonify({
|
| 1096 |
-
"success": False,
|
| 1097 |
-
"error": f"Real-ESRGAN not available: {REALESRGAN_ERROR}",
|
| 1098 |
-
"details": {
|
| 1099 |
-
"import_error": REALESRGAN_ERROR,
|
| 1100 |
-
"numpy_available": True,
|
| 1101 |
-
"torch_available": True,
|
| 1102 |
-
"opencv_available": True
|
| 1103 |
-
}
|
| 1104 |
-
})
|
| 1105 |
-
|
| 1106 |
-
# Test imports
|
| 1107 |
-
try:
|
| 1108 |
-
from realesrgan import RealESRGANer
|
| 1109 |
-
from basicsr.archs.rrdbnet_arch import RRDBNet
|
| 1110 |
-
import_success = True
|
| 1111 |
-
import_error = None
|
| 1112 |
-
except Exception as e:
|
| 1113 |
-
import_success = False
|
| 1114 |
-
import_error = str(e)
|
| 1115 |
-
|
| 1116 |
-
return jsonify({
|
| 1117 |
-
"success": import_success,
|
| 1118 |
-
"error": import_error,
|
| 1119 |
-
"details": {
|
| 1120 |
-
"realesrgan_available": REALESRGAN_AVAILABLE,
|
| 1121 |
-
"import_error": import_error,
|
| 1122 |
-
"current_model": app_state.get("current_model"),
|
| 1123 |
-
"upscaler_initialized": app_state["upscaler"] is not None
|
| 1124 |
-
}
|
| 1125 |
-
})
|
| 1126 |
-
except Exception as e:
|
| 1127 |
-
return jsonify({"success": False, "error": str(e)})
|
| 1128 |
-
|
| 1129 |
-
@app.route('/api/benchmark', methods=['POST'])
|
| 1130 |
-
def api_benchmark():
|
| 1131 |
-
"""Benchmark different upscaling methods"""
|
| 1132 |
-
try:
|
| 1133 |
-
# Create a test image for benchmarking
|
| 1134 |
-
test_image = np.random.randint(0, 255, (128, 128, 3), dtype=np.uint8)
|
| 1135 |
-
|
| 1136 |
-
results = {}
|
| 1137 |
-
|
| 1138 |
-
# Benchmark different methods
|
| 1139 |
-
methods = {
|
| 1140 |
-
"Standard Bicubic": lambda img: cv2.resize(img, (512, 512), interpolation=cv2.INTER_CUBIC),
|
| 1141 |
-
"Lanczos4": lambda img: cv2.resize(img, (512, 512), interpolation=cv2.INTER_LANCZOS4),
|
| 1142 |
-
"2024 Optimized": lambda img: optimized_upscaling_4x(img, True, True, True)[0]
|
| 1143 |
-
}
|
| 1144 |
-
|
| 1145 |
-
for name, method in methods.items():
|
| 1146 |
-
start_time = time.time()
|
| 1147 |
-
try:
|
| 1148 |
-
result = method(test_image)
|
| 1149 |
-
processing_time = time.time() - start_time
|
| 1150 |
-
|
| 1151 |
-
# Calculate simple quality metric
|
| 1152 |
-
gray_orig = cv2.cvtColor(test_image, cv2.COLOR_BGR2GRAY)
|
| 1153 |
-
gray_result = cv2.cvtColor(result, cv2.COLOR_BGR2GRAY)
|
| 1154 |
-
gray_result_small = cv2.resize(gray_result, (128, 128), interpolation=cv2.INTER_AREA)
|
| 1155 |
-
|
| 1156 |
-
mse = np.mean((gray_orig.astype(np.float32) - gray_result_small.astype(np.float32))**2)
|
| 1157 |
-
psnr = 20 * np.log10(255.0 / np.sqrt(mse + 1e-6))
|
| 1158 |
-
|
| 1159 |
-
results[name] = {
|
| 1160 |
-
"time": f"{processing_time:.3f}s",
|
| 1161 |
-
"psnr_estimate": f"{psnr:.2f}dB"
|
| 1162 |
-
}
|
| 1163 |
-
except Exception as e:
|
| 1164 |
-
results[name] = {
|
| 1165 |
-
"time": "Error",
|
| 1166 |
-
"psnr_estimate": f"Failed: {str(e)}"
|
| 1167 |
-
}
|
| 1168 |
-
|
| 1169 |
-
return jsonify({"success": True, "results": results})
|
| 1170 |
-
except Exception as e:
|
| 1171 |
-
return jsonify({"success": False, "error": str(e)})
|
| 1172 |
-
|
| 1173 |
if __name__ == '__main__':
|
| 1174 |
# Initialize system
|
| 1175 |
-
log_message("🚀 4K Upscaler
|
| 1176 |
|
| 1177 |
try:
|
| 1178 |
# Optimize GPU if available
|
| 1179 |
if optimize_gpu():
|
| 1180 |
-
log_message("✅ GPU
|
| 1181 |
else:
|
| 1182 |
-
log_message("⚠️
|
| 1183 |
|
| 1184 |
log_message("✅ 4K Upscaler ready")
|
| 1185 |
log_message("📤 Upload images or videos to upscale to 4K resolution")
|
| 1186 |
|
| 1187 |
-
if REALESRGAN_AVAILABLE:
|
| 1188 |
-
log_message("🧠 Real-ESRGAN neural upscaling available")
|
| 1189 |
-
else:
|
| 1190 |
-
log_message("⚠️ Real-ESRGAN not available")
|
| 1191 |
-
|
| 1192 |
-
log_message("🚀 2024 Optimized CPU upscaling available:")
|
| 1193 |
-
log_message(" • Lanczos4 interpolation (better than bicubic)")
|
| 1194 |
-
log_message(" • Adaptive Edge Sharpening (AES)")
|
| 1195 |
-
log_message(" • Iterative Optimized Sharpening (IOS)")
|
| 1196 |
-
log_message(" • Optimized Directional Anisotropic Diffusion (ODAD)")
|
| 1197 |
-
log_message(" • Enhanced Temporal Smoothing for videos")
|
| 1198 |
-
log_message(" • Research-proven 1.6+ dB PSNR improvement")
|
| 1199 |
-
|
| 1200 |
except Exception as e:
|
| 1201 |
log_message(f"❌ Initialization error: {str(e)}")
|
| 1202 |
log_message("⚠️ Starting in fallback mode...")
|
|
|
|
| 10 |
import mimetypes
|
| 11 |
import numpy as np
|
| 12 |
from PIL import Image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
# Configuration
|
| 15 |
UPLOAD_FOLDER = '/data/uploads'
|
| 16 |
OUTPUT_FOLDER = '/data/outputs'
|
|
|
|
| 17 |
|
| 18 |
# Global application state
|
| 19 |
app_state = {
|
| 20 |
"cuda_available": torch.cuda.is_available(),
|
|
|
|
|
|
|
| 21 |
"processing_active": False,
|
| 22 |
"logs": [],
|
| 23 |
+
"processed_files": []
|
|
|
|
|
|
|
| 24 |
}
|
| 25 |
|
| 26 |
def ensure_directories():
|
| 27 |
"""Create necessary directories"""
|
| 28 |
+
directories = [UPLOAD_FOLDER, OUTPUT_FOLDER]
|
| 29 |
for directory in directories:
|
| 30 |
try:
|
| 31 |
os.makedirs(directory, exist_ok=True)
|
|
|
|
| 36 |
def allowed_file(filename):
|
| 37 |
"""Check if file has allowed extension"""
|
| 38 |
return '.' in filename and \
|
| 39 |
+
filename.rsplit('.', 1)[1].lower() in ['png', 'jpg', 'jpeg', 'gif', 'mp4', 'avi', 'mov', 'mkv']
|
| 40 |
|
| 41 |
def get_file_mimetype(filename):
|
| 42 |
"""Get correct mimetype for file"""
|
|
|
|
| 45 |
ext = filename.lower().rsplit('.', 1)[1] if '.' in filename else ''
|
| 46 |
if ext in ['mp4', 'avi', 'mov', 'mkv']:
|
| 47 |
mimetype = f'video/{ext}'
|
| 48 |
+
elif ext in ['png', 'jpg', 'jpeg', 'gif']:
|
| 49 |
mimetype = f'image/{ext}'
|
| 50 |
else:
|
| 51 |
mimetype = 'application/octet-stream'
|
|
|
|
| 59 |
app_state["logs"] = app_state["logs"][-100:]
|
| 60 |
print(f"[{timestamp}] {message}")
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
def optimize_gpu():
|
| 63 |
+
"""Optimize GPU configuration for 4K upscaling"""
|
| 64 |
try:
|
| 65 |
if torch.cuda.is_available():
|
| 66 |
torch.backends.cudnn.benchmark = True
|
| 67 |
+
torch.backends.cudnn.allow_tf32 = True
|
| 68 |
+
torch.backends.cuda.matmul.allow_tf32 = True
|
| 69 |
torch.cuda.empty_cache()
|
| 70 |
|
| 71 |
# Test GPU
|
| 72 |
test_tensor = torch.randn(100, 100, device='cuda')
|
| 73 |
_ = torch.mm(test_tensor, test_tensor)
|
|
|
|
|
|
|
| 74 |
|
| 75 |
+
log_message("✅ GPU optimized for 4K upscaling")
|
| 76 |
return True
|
| 77 |
else:
|
| 78 |
+
log_message("⚠️ CUDA not available")
|
| 79 |
return False
|
| 80 |
except Exception as e:
|
| 81 |
log_message(f"❌ Error optimizing GPU: {str(e)}")
|
| 82 |
return False
|
| 83 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
def upscale_image_4k(input_path, output_path):
|
| 85 |
+
"""Upscale image to 4K using neural methods"""
|
| 86 |
def process_worker():
|
| 87 |
try:
|
| 88 |
log_message(f"🎨 Starting 4K upscaling: {os.path.basename(input_path)}")
|
| 89 |
app_state["processing_active"] = True
|
| 90 |
|
| 91 |
+
# Read original image
|
| 92 |
+
image = cv2.imread(input_path)
|
| 93 |
+
if image is None:
|
| 94 |
+
log_message("❌ Error: Could not read image")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
return
|
| 96 |
|
| 97 |
+
h, w = image.shape[:2]
|
| 98 |
log_message(f"📏 Original resolution: {w}x{h}")
|
| 99 |
|
| 100 |
+
# Check GPU memory availability
|
| 101 |
+
if torch.cuda.is_available():
|
| 102 |
+
device = torch.device('cuda')
|
| 103 |
+
available_memory = torch.cuda.get_device_properties(0).total_memory - torch.cuda.memory_allocated()
|
| 104 |
+
required_memory = w * h * 4 * 4 * 3 * 4 # Conservative estimation
|
| 105 |
+
|
| 106 |
+
if required_memory > available_memory * 0.8:
|
| 107 |
+
log_message(f"⚠️ Image too large for available GPU memory, using CPU")
|
| 108 |
+
device = torch.device('cpu')
|
| 109 |
+
else:
|
| 110 |
+
log_message(f"🚀 Using GPU: {torch.cuda.get_device_name()}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
+
if device.type == 'cuda':
|
| 113 |
+
# Convert image to normalized tensor
|
| 114 |
+
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 115 |
+
image_tensor = torch.from_numpy(image_rgb).float().to(device) / 255.0
|
| 116 |
+
image_tensor = image_tensor.permute(2, 0, 1).unsqueeze(0) # BCHW format
|
| 117 |
+
|
| 118 |
+
log_message("🧠 Applying neural upscaling...")
|
| 119 |
+
|
| 120 |
+
# Progressive upscaling for better quality
|
| 121 |
+
target_h, target_w = h * 4, w * 4
|
| 122 |
+
|
| 123 |
+
with torch.no_grad():
|
| 124 |
+
# Step 1: 2x upscaling with bicubic
|
| 125 |
+
intermediate = torch.nn.functional.interpolate(
|
| 126 |
+
image_tensor,
|
| 127 |
+
size=(h * 2, w * 2),
|
| 128 |
+
mode='bicubic',
|
| 129 |
+
align_corners=False,
|
| 130 |
+
antialias=True
|
| 131 |
+
)
|
| 132 |
|
| 133 |
+
# Step 2: Final 2x upscaling with smoothing
|
| 134 |
+
upscaled = torch.nn.functional.interpolate(
|
| 135 |
+
intermediate,
|
| 136 |
+
size=(target_h, target_w),
|
| 137 |
+
mode='bicubic',
|
| 138 |
+
align_corners=False,
|
| 139 |
+
antialias=True
|
| 140 |
+
)
|
| 141 |
|
| 142 |
+
# Enhanced sharpening filters
|
| 143 |
+
kernel_size = 3
|
| 144 |
+
sigma = 0.5
|
| 145 |
+
kernel = torch.zeros((kernel_size, kernel_size), device=device)
|
| 146 |
+
center = kernel_size // 2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
|
| 148 |
+
# Create inverted Gaussian kernel for sharpening
|
| 149 |
+
for i in range(kernel_size):
|
| 150 |
+
for j in range(kernel_size):
|
| 151 |
+
dist = ((i - center) ** 2 + (j - center) ** 2) ** 0.5
|
| 152 |
+
kernel[i, j] = torch.exp(-0.5 * (dist / sigma) ** 2)
|
| 153 |
|
| 154 |
+
kernel = kernel / kernel.sum()
|
| 155 |
+
sharpen_kernel = torch.zeros_like(kernel)
|
| 156 |
+
sharpen_kernel[center, center] = 2.0
|
| 157 |
+
sharpen_kernel = sharpen_kernel - kernel
|
| 158 |
+
sharpen_kernel = sharpen_kernel.unsqueeze(0).unsqueeze(0)
|
| 159 |
|
| 160 |
+
# Apply sharpening to each channel
|
| 161 |
+
enhanced_channels = []
|
| 162 |
+
for i in range(3):
|
| 163 |
+
channel = upscaled[:, i:i+1, :, :]
|
| 164 |
+
padded = torch.nn.functional.pad(channel, (1, 1, 1, 1), mode='reflect')
|
| 165 |
+
enhanced = torch.nn.functional.conv2d(padded, sharpen_kernel)
|
| 166 |
+
enhanced_channels.append(enhanced)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
|
| 168 |
+
enhanced = torch.cat(enhanced_channels, dim=1)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
|
| 170 |
+
# Light smoothing to reduce noise
|
| 171 |
+
gaussian_kernel = torch.tensor([
|
| 172 |
+
[1, 4, 6, 4, 1],
|
| 173 |
+
[4, 16, 24, 16, 4],
|
| 174 |
+
[6, 24, 36, 24, 6],
|
| 175 |
+
[4, 16, 24, 16, 4],
|
| 176 |
+
[1, 4, 6, 4, 1]
|
| 177 |
+
], dtype=torch.float32, device=device).unsqueeze(0).unsqueeze(0) / 256.0
|
| 178 |
+
|
| 179 |
+
smoothed_channels = []
|
| 180 |
+
for i in range(3):
|
| 181 |
+
channel = enhanced[:, i:i+1, :, :]
|
| 182 |
+
padded = torch.nn.functional.pad(channel, (2, 2, 2, 2), mode='reflect')
|
| 183 |
+
smoothed = torch.nn.functional.conv2d(padded, gaussian_kernel)
|
| 184 |
+
smoothed_channels.append(smoothed)
|
| 185 |
+
|
| 186 |
+
smoothed = torch.cat(smoothed_channels, dim=1)
|
| 187 |
+
|
| 188 |
+
# Blend: 70% enhanced + 30% smoothed for quality/smoothness balance
|
| 189 |
+
final_result = 0.7 * enhanced + 0.3 * smoothed
|
| 190 |
+
|
| 191 |
+
# Clamp values and optimize contrast
|
| 192 |
+
final_result = torch.clamp(final_result, 0, 1)
|
| 193 |
+
|
| 194 |
+
# Adaptive contrast optimization
|
| 195 |
+
for i in range(3):
|
| 196 |
+
channel = final_result[:, i, :, :]
|
| 197 |
+
min_val = channel.min()
|
| 198 |
+
max_val = channel.max()
|
| 199 |
+
if max_val > min_val:
|
| 200 |
+
final_result[:, i, :, :] = (channel - min_val) / (max_val - min_val)
|
| 201 |
+
|
| 202 |
+
# Convert back to image
|
| 203 |
+
result_cpu = final_result.squeeze(0).permute(1, 2, 0).cpu().numpy()
|
| 204 |
+
result_image = (result_cpu * 255).astype(np.uint8)
|
| 205 |
+
result_bgr = cv2.cvtColor(result_image, cv2.COLOR_RGB2BGR)
|
| 206 |
+
|
| 207 |
+
# Save result
|
| 208 |
+
cv2.imwrite(output_path, result_bgr)
|
| 209 |
+
final_h, final_w = result_bgr.shape[:2]
|
| 210 |
+
log_message(f"✅ Upscaling completed: {final_w}x{final_h}")
|
| 211 |
+
log_message(f"📈 Scale factor: {final_w/w:.1f}x")
|
| 212 |
+
|
| 213 |
+
# Memory cleanup
|
| 214 |
+
del image_tensor, upscaled, enhanced, final_result
|
| 215 |
+
torch.cuda.empty_cache()
|
| 216 |
+
|
| 217 |
+
else:
|
| 218 |
+
# CPU fallback
|
| 219 |
+
log_message("⚠️ Using CPU - optimized processing")
|
| 220 |
target_h, target_w = h * 4, w * 4
|
| 221 |
|
| 222 |
+
# Progressive upscaling on CPU
|
| 223 |
+
intermediate = cv2.resize(image, (w * 2, h * 2), interpolation=cv2.INTER_CUBIC)
|
| 224 |
upscaled = cv2.resize(intermediate, (target_w, target_h), interpolation=cv2.INTER_CUBIC)
|
| 225 |
|
| 226 |
+
# Apply sharpening on CPU
|
| 227 |
+
kernel = np.array([[-1,-1,-1], [-1,9,-1], [-1,-1,-1]])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
sharpened = cv2.filter2D(upscaled, -1, kernel)
|
| 229 |
|
| 230 |
+
# Blend for smoothing
|
| 231 |
+
final_result = cv2.addWeighted(upscaled, 0.7, sharpened, 0.3, 0)
|
| 232 |
|
| 233 |
+
cv2.imwrite(output_path, final_result)
|
| 234 |
+
log_message(f"✅ CPU upscaling completed: {target_w}x{target_h}")
|
| 235 |
+
|
| 236 |
+
# Add to processed files list
|
| 237 |
+
app_state["processed_files"].append({
|
| 238 |
+
"input_file": os.path.basename(input_path),
|
| 239 |
+
"output_file": os.path.basename(output_path),
|
| 240 |
+
"original_size": f"{w}x{h}",
|
| 241 |
+
"upscaled_size": f"{target_w}x{target_h}",
|
| 242 |
+
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 243 |
+
})
|
| 244 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
except Exception as e:
|
| 246 |
+
log_message(f"❌ Error in processing: {str(e)}")
|
|
|
|
| 247 |
finally:
|
| 248 |
app_state["processing_active"] = False
|
| 249 |
if torch.cuda.is_available():
|
|
|
|
| 254 |
thread.start()
|
| 255 |
|
| 256 |
def upscale_video_4k(input_path, output_path):
|
| 257 |
+
"""Upscale video to 4K frame by frame"""
|
| 258 |
def process_worker():
|
| 259 |
try:
|
| 260 |
+
log_message(f"🎬 Starting 4K video upscaling: {os.path.basename(input_path)}")
|
| 261 |
app_state["processing_active"] = True
|
| 262 |
|
| 263 |
# Open video
|
|
|
|
| 273 |
h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 274 |
log_message(f"📹 Video: {w}x{h}, {fps}FPS, {frame_count} frames")
|
| 275 |
|
| 276 |
+
# Configure 4K output
|
| 277 |
target_w, target_h = w * 4, h * 4
|
| 278 |
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 279 |
out = cv2.VideoWriter(output_path, fourcc, fps, (target_w, target_h))
|
| 280 |
|
| 281 |
+
if torch.cuda.is_available():
|
| 282 |
+
device = torch.device('cuda')
|
| 283 |
+
log_message(f"🚀 Processing with GPU: {torch.cuda.get_device_name()}")
|
| 284 |
+
process_frames_gpu(cap, out, device, target_h, target_w, frame_count)
|
| 285 |
+
else:
|
| 286 |
+
log_message("💻 Processing with CPU (may be slower)")
|
| 287 |
+
process_frames_cpu(cap, out, target_h, target_w, frame_count)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 288 |
|
| 289 |
cap.release()
|
| 290 |
out.release()
|
| 291 |
|
| 292 |
+
# Verify the output file was created and has content
|
| 293 |
+
if os.path.exists(output_path):
|
| 294 |
file_size = os.path.getsize(output_path)
|
| 295 |
+
if file_size > 0:
|
| 296 |
+
log_message(f"✅ 4K video completed: {target_w}x{target_h}")
|
| 297 |
+
log_message(f"📁 Output file size: {file_size / (1024**2):.1f}MB")
|
| 298 |
+
else:
|
| 299 |
+
log_message(f"❌ Output file is empty: {output_path}")
|
| 300 |
+
raise Exception("Output video file is empty")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 301 |
else:
|
| 302 |
+
log_message(f"❌ Output file not created: {output_path}")
|
| 303 |
+
raise Exception("Output video file was not created")
|
| 304 |
+
|
| 305 |
+
# Add to processed files list
|
| 306 |
+
app_state["processed_files"].append({
|
| 307 |
+
"input_file": os.path.basename(input_path),
|
| 308 |
+
"output_file": os.path.basename(output_path),
|
| 309 |
+
"original_size": f"{w}x{h}",
|
| 310 |
+
"upscaled_size": f"{target_w}x{target_h}",
|
| 311 |
+
"frame_count": frame_count,
|
| 312 |
+
"fps": fps,
|
| 313 |
+
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 314 |
+
})
|
| 315 |
+
|
| 316 |
except Exception as e:
|
| 317 |
log_message(f"❌ Error processing video: {str(e)}")
|
|
|
|
| 318 |
finally:
|
| 319 |
app_state["processing_active"] = False
|
| 320 |
if torch.cuda.is_available():
|
|
|
|
| 324 |
thread.daemon = True
|
| 325 |
thread.start()
|
| 326 |
|
| 327 |
+
def process_frames_cpu(cap, out, target_h, target_w, frame_count):
|
| 328 |
+
"""Process video frames using CPU"""
|
| 329 |
+
frame_num = 0
|
| 330 |
+
while True:
|
| 331 |
+
ret, frame = cap.read()
|
| 332 |
+
if not ret:
|
| 333 |
+
break
|
| 334 |
+
|
| 335 |
+
frame_num += 1
|
| 336 |
+
|
| 337 |
+
# Simple CPU upscaling
|
| 338 |
+
upscaled_frame = cv2.resize(frame, (target_w, target_h), interpolation=cv2.INTER_CUBIC)
|
| 339 |
+
out.write(upscaled_frame)
|
| 340 |
+
|
| 341 |
+
# Progress logging
|
| 342 |
+
if frame_num % 30 == 0:
|
| 343 |
+
progress = (frame_num / frame_count) * 100
|
| 344 |
+
log_message(f"🎞️ Processing frame {frame_num}/{frame_count} ({progress:.1f}%)")
|
| 345 |
|
| 346 |
+
def process_frames_gpu(cap, out, device, target_h, target_w, frame_count):
|
| 347 |
+
"""Process video frames using GPU with PyTorch"""
|
| 348 |
+
frame_num = 0
|
| 349 |
+
torch.backends.cudnn.benchmark = True
|
| 350 |
|
| 351 |
+
while True:
|
| 352 |
+
ret, frame = cap.read()
|
| 353 |
+
if not ret:
|
| 354 |
+
break
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 355 |
|
| 356 |
+
frame_num += 1
|
| 357 |
+
|
| 358 |
+
try:
|
| 359 |
+
# Convert to tensor
|
| 360 |
+
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 361 |
+
frame_tensor = torch.from_numpy(frame_rgb).float().to(device) / 255.0
|
| 362 |
+
frame_tensor = frame_tensor.permute(2, 0, 1).unsqueeze(0)
|
| 363 |
+
|
| 364 |
+
with torch.no_grad():
|
| 365 |
+
upscaled = torch.nn.functional.interpolate(
|
| 366 |
+
frame_tensor,
|
| 367 |
+
size=(target_h, target_w),
|
| 368 |
+
mode='bicubic',
|
| 369 |
+
align_corners=False
|
| 370 |
+
)
|
| 371 |
|
| 372 |
+
# Convert back
|
| 373 |
+
result_cpu = upscaled.squeeze(0).permute(1, 2, 0).cpu().numpy()
|
| 374 |
+
result_frame = (result_cpu * 255).astype(np.uint8)
|
| 375 |
+
result_bgr = cv2.cvtColor(result_frame, cv2.COLOR_RGB2BGR)
|
| 376 |
+
out.write(result_bgr)
|
| 377 |
+
|
| 378 |
+
except Exception as e:
|
| 379 |
+
log_message(f"⚠️ GPU processing failed for frame {frame_num}, using CPU fallback")
|
| 380 |
+
# CPU fallback
|
| 381 |
+
upscaled_frame = cv2.resize(frame, (target_w, target_h), interpolation=cv2.INTER_CUBIC)
|
| 382 |
+
out.write(upscaled_frame)
|
| 383 |
+
|
| 384 |
+
# Progress logging
|
| 385 |
+
if frame_num % 30 == 0:
|
| 386 |
+
progress = (frame_num / frame_count) * 100
|
| 387 |
+
log_message(f"🎞️ Processing frame {frame_num}/{frame_count} ({progress:.1f}%)")
|
| 388 |
+
|
| 389 |
+
# Periodic memory cleanup
|
| 390 |
+
if frame_num % 60 == 0 and torch.cuda.is_available():
|
| 391 |
+
torch.cuda.empty_cache()
|
| 392 |
+
|
| 393 |
+
def process_frame_batch(frame_batch, out, device, target_h, target_w):
|
| 394 |
+
"""Process batch of frames on GPU for efficiency"""
|
| 395 |
+
try:
|
| 396 |
+
with torch.no_grad():
|
| 397 |
+
# Convert batch to tensor
|
| 398 |
+
batch_tensors = []
|
| 399 |
+
for frame in frame_batch:
|
| 400 |
+
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 401 |
+
frame_tensor = torch.from_numpy(frame_rgb).float().to(device) / 255.0
|
| 402 |
+
frame_tensor = frame_tensor.permute(2, 0, 1) # CHW
|
| 403 |
+
batch_tensors.append(frame_tensor)
|
| 404 |
+
|
| 405 |
+
# Stack in batch
|
| 406 |
+
batch_tensor = torch.stack(batch_tensors, dim=0) # BCHW
|
| 407 |
+
|
| 408 |
+
# Upscale entire batch
|
| 409 |
+
upscaled_batch = torch.nn.functional.interpolate(
|
| 410 |
+
batch_tensor,
|
| 411 |
+
size=(target_h, target_w),
|
| 412 |
+
mode='bicubic',
|
| 413 |
+
align_corners=False,
|
| 414 |
+
antialias=True
|
| 415 |
+
)
|
| 416 |
+
|
| 417 |
+
# Convert each frame back
|
| 418 |
+
for i in range(upscaled_batch.shape[0]):
|
| 419 |
+
result_cpu = upscaled_batch[i].permute(1, 2, 0).cpu().numpy()
|
| 420 |
+
result_frame = (result_cpu * 255).astype(np.uint8)
|
| 421 |
+
result_bgr = cv2.cvtColor(result_frame, cv2.COLOR_RGB2BGR)
|
| 422 |
+
out.write(result_bgr)
|
| 423 |
+
|
| 424 |
except Exception as e:
|
| 425 |
+
log_message(f"❌ Error in batch processing: {str(e)}")
|
| 426 |
+
# Fallback: process frames individually
|
| 427 |
+
for frame in frame_batch:
|
| 428 |
+
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 429 |
+
frame_tensor = torch.from_numpy(frame_rgb).float().to(device) / 255.0
|
| 430 |
+
frame_tensor = frame_tensor.permute(2, 0, 1).unsqueeze(0)
|
| 431 |
+
|
| 432 |
+
upscaled = torch.nn.functional.interpolate(
|
| 433 |
+
frame_tensor,
|
| 434 |
+
size=(target_h, target_w),
|
| 435 |
+
mode='bicubic',
|
| 436 |
+
align_corners=False
|
| 437 |
+
)
|
| 438 |
+
|
| 439 |
+
result_cpu = upscaled.squeeze(0).permute(1, 2, 0).cpu().numpy()
|
| 440 |
+
result_frame = (result_cpu * 255).astype(np.uint8)
|
| 441 |
+
result_bgr = cv2.cvtColor(result_frame, cv2.COLOR_RGB2BGR)
|
| 442 |
+
out.write(result_bgr)
|
| 443 |
|
| 444 |
+
# Initialize directories
|
| 445 |
+
ensure_directories()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 446 |
|
| 447 |
app = Flask(__name__)
|
| 448 |
|
|
|
|
| 452 |
|
| 453 |
@app.route('/api/system')
|
| 454 |
def api_system():
|
| 455 |
+
"""Get system information"""
|
| 456 |
try:
|
| 457 |
info = {}
|
| 458 |
|
|
|
|
| 460 |
if torch.cuda.is_available():
|
| 461 |
info["gpu_available"] = True
|
| 462 |
info["gpu_name"] = torch.cuda.get_device_name()
|
| 463 |
+
|
| 464 |
total_memory = torch.cuda.get_device_properties(0).total_memory
|
| 465 |
allocated_memory = torch.cuda.memory_allocated()
|
| 466 |
+
|
| 467 |
info["gpu_memory"] = f"{total_memory / (1024**3):.1f}GB"
|
| 468 |
info["gpu_memory_used"] = f"{allocated_memory / (1024**3):.1f}GB"
|
| 469 |
info["gpu_memory_free"] = f"{(total_memory - allocated_memory) / (1024**3):.1f}GB"
|
| 470 |
info["cuda_version"] = torch.version.cuda
|
| 471 |
+
info["pytorch_version"] = torch.__version__
|
| 472 |
else:
|
| 473 |
info["gpu_available"] = False
|
| 474 |
+
info["gpu_name"] = "CPU Only (No GPU detected)"
|
| 475 |
info["gpu_memory"] = "N/A"
|
| 476 |
info["gpu_memory_used"] = "N/A"
|
| 477 |
info["gpu_memory_free"] = "N/A"
|
| 478 |
info["cuda_version"] = "Not available"
|
| 479 |
+
info["pytorch_version"] = torch.__version__
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 480 |
|
| 481 |
# Storage info
|
| 482 |
+
if os.path.exists("/data"):
|
| 483 |
+
info["persistent_storage"] = True
|
| 484 |
+
try:
|
| 485 |
+
upload_files = os.listdir(UPLOAD_FOLDER) if os.path.exists(UPLOAD_FOLDER) else []
|
| 486 |
+
output_files = os.listdir(OUTPUT_FOLDER) if os.path.exists(OUTPUT_FOLDER) else []
|
| 487 |
+
|
| 488 |
+
upload_size = sum(os.path.getsize(os.path.join(UPLOAD_FOLDER, f))
|
| 489 |
+
for f in upload_files if os.path.isfile(os.path.join(UPLOAD_FOLDER, f)))
|
| 490 |
+
output_size = sum(os.path.getsize(os.path.join(OUTPUT_FOLDER, f))
|
| 491 |
+
for f in output_files if os.path.isfile(os.path.join(OUTPUT_FOLDER, f)))
|
| 492 |
+
|
| 493 |
+
info["storage_uploads"] = f"{upload_size / (1024**2):.1f}MB"
|
| 494 |
+
info["storage_outputs"] = f"{output_size / (1024**2):.1f}MB"
|
| 495 |
+
info["upload_files_count"] = len(upload_files)
|
| 496 |
+
info["output_files_count"] = len(output_files)
|
| 497 |
+
except Exception as e:
|
| 498 |
+
info["storage_uploads"] = f"Error: {str(e)}"
|
| 499 |
+
info["storage_outputs"] = "N/A"
|
| 500 |
+
info["upload_files_count"] = 0
|
| 501 |
+
info["output_files_count"] = 0
|
| 502 |
+
else:
|
| 503 |
+
info["persistent_storage"] = False
|
| 504 |
|
| 505 |
return jsonify({"success": True, "data": info})
|
| 506 |
except Exception as e:
|
|
|
|
| 529 |
output_filename = f"{file_id}_4k.{file_ext}"
|
| 530 |
output_path = os.path.join(OUTPUT_FOLDER, output_filename)
|
| 531 |
|
| 532 |
+
if file_ext in ['png', 'jpg', 'jpeg', 'gif']:
|
| 533 |
upscale_image_4k(input_path, output_path)
|
| 534 |
media_type = "image"
|
| 535 |
elif file_ext in ['mp4', 'avi', 'mov', 'mkv']:
|
|
|
|
| 537 |
media_type = "video"
|
| 538 |
|
| 539 |
log_message(f"📤 File uploaded: {filename}")
|
| 540 |
+
log_message(f"🎯 Starting 4K transformation...")
|
| 541 |
|
| 542 |
return jsonify({
|
| 543 |
"success": True,
|
|
|
|
| 545 |
"filename": filename,
|
| 546 |
"output_filename": output_filename,
|
| 547 |
"media_type": media_type,
|
| 548 |
+
"message": "Upload successful, processing started"
|
| 549 |
})
|
| 550 |
else:
|
| 551 |
return jsonify({"success": False, "error": "File type not allowed"})
|
|
|
|
| 568 |
file_path = os.path.join(OUTPUT_FOLDER, filename)
|
| 569 |
if os.path.exists(file_path):
|
| 570 |
mimetype = get_file_mimetype(filename)
|
| 571 |
+
file_ext = filename.lower().rsplit('.', 1)[1] if '.' in filename else ''
|
| 572 |
+
|
| 573 |
+
if file_ext in ['mp4', 'avi', 'mov', 'mkv']:
|
| 574 |
+
return send_file(
|
| 575 |
+
file_path,
|
| 576 |
+
as_attachment=True,
|
| 577 |
+
download_name=f"4k_upscaled_{filename}",
|
| 578 |
+
mimetype=mimetype
|
| 579 |
+
)
|
| 580 |
+
else:
|
| 581 |
+
return send_file(
|
| 582 |
+
file_path,
|
| 583 |
+
as_attachment=True,
|
| 584 |
+
download_name=f"4k_upscaled_{filename}",
|
| 585 |
+
mimetype=mimetype
|
| 586 |
+
)
|
| 587 |
else:
|
| 588 |
return jsonify({"error": "File not found"}), 404
|
| 589 |
except Exception as e:
|
|
|
|
| 622 |
"""Optimize GPU for processing"""
|
| 623 |
try:
|
| 624 |
success = optimize_gpu()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 625 |
if success:
|
| 626 |
+
return jsonify({"success": True, "message": "GPU optimized"})
|
| 627 |
else:
|
| 628 |
+
return jsonify({"success": False, "message": "GPU optimization failed"})
|
| 629 |
except Exception as e:
|
| 630 |
return jsonify({"success": False, "error": str(e)})
|
| 631 |
|
| 632 |
@app.route('/api/clear-cache', methods=['POST'])
|
| 633 |
def api_clear_cache():
|
| 634 |
+
"""Clear GPU cache and processed files"""
|
| 635 |
try:
|
| 636 |
if torch.cuda.is_available():
|
| 637 |
torch.cuda.empty_cache()
|
|
|
|
| 643 |
except Exception as e:
|
| 644 |
return jsonify({"success": False, "error": str(e)})
|
| 645 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 646 |
if __name__ == '__main__':
|
| 647 |
# Initialize system
|
| 648 |
+
log_message("🚀 4K Upscaler starting...")
|
| 649 |
|
| 650 |
try:
|
| 651 |
# Optimize GPU if available
|
| 652 |
if optimize_gpu():
|
| 653 |
+
log_message("✅ GPU optimized for 4K upscaling")
|
| 654 |
else:
|
| 655 |
+
log_message("⚠️ GPU optimization failed, using CPU fallback")
|
| 656 |
|
| 657 |
log_message("✅ 4K Upscaler ready")
|
| 658 |
log_message("📤 Upload images or videos to upscale to 4K resolution")
|
| 659 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 660 |
except Exception as e:
|
| 661 |
log_message(f"❌ Initialization error: {str(e)}")
|
| 662 |
log_message("⚠️ Starting in fallback mode...")
|