Update app_enhanced.py
Browse files- app_enhanced.py +49 -14
app_enhanced.py
CHANGED
|
@@ -8,10 +8,13 @@ import logging
|
|
| 8 |
import string
|
| 9 |
import random
|
| 10 |
import shutil
|
|
|
|
|
|
|
|
|
|
| 11 |
from flask import Flask, jsonify, request, send_from_directory, send_file
|
| 12 |
|
| 13 |
# ======================================================
|
| 14 |
-
# π ZEROGPU DETECTION
|
| 15 |
# ======================================================
|
| 16 |
@spaces.GPU
|
| 17 |
def gpu_warmup():
|
|
@@ -19,6 +22,27 @@ def gpu_warmup():
|
|
| 19 |
print(f"β
ZeroGPU Warmup: CUDA Available: {torch.cuda.is_available()}")
|
| 20 |
return True
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
# ======================================================
|
| 23 |
# π§ CONFIGURATION
|
| 24 |
# ======================================================
|
|
@@ -49,7 +73,6 @@ def generate_comic_gpu(video_path, user_dir, frames_dir, metadata_path):
|
|
| 49 |
from backend.subtitles.subs_real import get_real_subtitles
|
| 50 |
from backend.ai_bubble_placement import ai_bubble_placer
|
| 51 |
from backend.ai_enhanced_core import face_detector
|
| 52 |
-
from backend.class_def import bubble, panel, Page
|
| 53 |
|
| 54 |
# 1. Analyze Video
|
| 55 |
cap = cv2.VideoCapture(video_path)
|
|
@@ -79,8 +102,8 @@ def generate_comic_gpu(video_path, user_dir, frames_dir, metadata_path):
|
|
| 79 |
cap = cv2.VideoCapture(video_path)
|
| 80 |
count = 0
|
| 81 |
|
| 82 |
-
#
|
| 83 |
-
for i, moment in enumerate(key_moments[:32]):
|
| 84 |
mid_time = (moment['start'] + moment['end']) / 2
|
| 85 |
if mid_time > duration: continue
|
| 86 |
|
|
@@ -107,12 +130,11 @@ def generate_comic_gpu(video_path, user_dir, frames_dir, metadata_path):
|
|
| 107 |
print(f"π¨ Enhancing {len(frame_files)} frames...")
|
| 108 |
for f in frame_files:
|
| 109 |
p = os.path.join(frames_dir, f)
|
| 110 |
-
#
|
| 111 |
try: se.enhance_single(p, p)
|
| 112 |
-
except
|
| 113 |
-
|
| 114 |
try: qe.enhance_single(p, p)
|
| 115 |
-
except
|
| 116 |
|
| 117 |
# 5. Bubbles
|
| 118 |
print("π¬ Placing bubbles...")
|
|
@@ -124,16 +146,29 @@ def generate_comic_gpu(video_path, user_dir, frames_dir, metadata_path):
|
|
| 124 |
faces = face_detector.detect_faces(p)
|
| 125 |
lip = face_detector.get_lip_position(p, faces[0]) if faces else (-1, -1)
|
| 126 |
bx, by = ai_bubble_placer.place_bubble_ai(p, lip)
|
| 127 |
-
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
bubbles_list.append(bubble(dialog=dialogue))
|
| 130 |
|
| 131 |
# 6. Layout
|
| 132 |
print("π Generating layout...")
|
| 133 |
try:
|
| 134 |
from backend.fixed_12_pages_800x1080 import generate_12_pages_800x1080
|
|
|
|
| 135 |
pages = generate_12_pages_800x1080(frame_files, bubbles_list)
|
| 136 |
-
except:
|
|
|
|
| 137 |
pages = []
|
| 138 |
for i in range((len(frame_files)+3)//4):
|
| 139 |
start = i*4
|
|
@@ -144,8 +179,9 @@ def generate_comic_gpu(video_path, user_dir, frames_dir, metadata_path):
|
|
| 144 |
# Serialize
|
| 145 |
result = []
|
| 146 |
for pg in pages:
|
| 147 |
-
|
| 148 |
-
|
|
|
|
| 149 |
result.append({'panels': p_data, 'bubbles': b_data})
|
| 150 |
|
| 151 |
return result
|
|
@@ -173,7 +209,6 @@ def regen_frame_gpu(video_path, frames_dir, metadata_path, fname, direction):
|
|
| 173 |
if ret:
|
| 174 |
path = os.path.join(frames_dir, fname)
|
| 175 |
cv2.imwrite(path, frame)
|
| 176 |
-
# FIX: Pass output path
|
| 177 |
try: SimpleColorEnhancer().enhance_single(path, path)
|
| 178 |
except: pass
|
| 179 |
|
|
|
|
| 8 |
import string
|
| 9 |
import random
|
| 10 |
import shutil
|
| 11 |
+
import cv2
|
| 12 |
+
import numpy as np
|
| 13 |
+
import srt
|
| 14 |
from flask import Flask, jsonify, request, send_from_directory, send_file
|
| 15 |
|
| 16 |
# ======================================================
|
| 17 |
+
# π ZEROGPU DETECTION
|
| 18 |
# ======================================================
|
| 19 |
@spaces.GPU
|
| 20 |
def gpu_warmup():
|
|
|
|
| 22 |
print(f"β
ZeroGPU Warmup: CUDA Available: {torch.cuda.is_available()}")
|
| 23 |
return True
|
| 24 |
|
| 25 |
+
# ======================================================
|
| 26 |
+
# π§± ROBUST DATA CLASSES (Defined locally to prevent crashes)
|
| 27 |
+
# ======================================================
|
| 28 |
+
def bubble(dialog="", bubble_offset_x=50, bubble_offset_y=20, lip_x=-1, lip_y=-1, emotion='normal'):
|
| 29 |
+
return {
|
| 30 |
+
'dialog': dialog,
|
| 31 |
+
'bubble_offset_x': int(bubble_offset_x),
|
| 32 |
+
'bubble_offset_y': int(bubble_offset_y),
|
| 33 |
+
'lip_x': int(lip_x),
|
| 34 |
+
'lip_y': int(lip_y),
|
| 35 |
+
'emotion': emotion
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
def panel(image=""):
|
| 39 |
+
return {'image': image}
|
| 40 |
+
|
| 41 |
+
class Page:
|
| 42 |
+
def __init__(self, panels, bubbles):
|
| 43 |
+
self.panels = panels
|
| 44 |
+
self.bubbles = bubbles
|
| 45 |
+
|
| 46 |
# ======================================================
|
| 47 |
# π§ CONFIGURATION
|
| 48 |
# ======================================================
|
|
|
|
| 73 |
from backend.subtitles.subs_real import get_real_subtitles
|
| 74 |
from backend.ai_bubble_placement import ai_bubble_placer
|
| 75 |
from backend.ai_enhanced_core import face_detector
|
|
|
|
| 76 |
|
| 77 |
# 1. Analyze Video
|
| 78 |
cap = cv2.VideoCapture(video_path)
|
|
|
|
| 102 |
cap = cv2.VideoCapture(video_path)
|
| 103 |
count = 0
|
| 104 |
|
| 105 |
+
# Limit frames to prevent timeouts
|
| 106 |
+
for i, moment in enumerate(key_moments[:32]):
|
| 107 |
mid_time = (moment['start'] + moment['end']) / 2
|
| 108 |
if mid_time > duration: continue
|
| 109 |
|
|
|
|
| 130 |
print(f"π¨ Enhancing {len(frame_files)} frames...")
|
| 131 |
for f in frame_files:
|
| 132 |
p = os.path.join(frames_dir, f)
|
| 133 |
+
# Pass path twice (Input, Output)
|
| 134 |
try: se.enhance_single(p, p)
|
| 135 |
+
except: pass
|
|
|
|
| 136 |
try: qe.enhance_single(p, p)
|
| 137 |
+
except: pass
|
| 138 |
|
| 139 |
# 5. Bubbles
|
| 140 |
print("π¬ Placing bubbles...")
|
|
|
|
| 146 |
faces = face_detector.detect_faces(p)
|
| 147 |
lip = face_detector.get_lip_position(p, faces[0]) if faces else (-1, -1)
|
| 148 |
bx, by = ai_bubble_placer.place_bubble_ai(p, lip)
|
| 149 |
+
# Use local bubble function which is safe
|
| 150 |
+
b = bubble(
|
| 151 |
+
dialog=dialogue,
|
| 152 |
+
bubble_offset_x=bx,
|
| 153 |
+
bubble_offset_y=by,
|
| 154 |
+
lip_x=lip[0],
|
| 155 |
+
lip_y=lip[1],
|
| 156 |
+
emotion='normal'
|
| 157 |
+
)
|
| 158 |
+
bubbles_list.append(b)
|
| 159 |
+
except Exception as e:
|
| 160 |
+
print(f"Bubble placement error: {e}")
|
| 161 |
+
# Safe fallback
|
| 162 |
bubbles_list.append(bubble(dialog=dialogue))
|
| 163 |
|
| 164 |
# 6. Layout
|
| 165 |
print("π Generating layout...")
|
| 166 |
try:
|
| 167 |
from backend.fixed_12_pages_800x1080 import generate_12_pages_800x1080
|
| 168 |
+
# We need to ensure the backend accepts our dicts, or we use fallback
|
| 169 |
pages = generate_12_pages_800x1080(frame_files, bubbles_list)
|
| 170 |
+
except Exception as e:
|
| 171 |
+
print(f"Layout engine error: {e}. Using simple layout.")
|
| 172 |
pages = []
|
| 173 |
for i in range((len(frame_files)+3)//4):
|
| 174 |
start = i*4
|
|
|
|
| 179 |
# Serialize
|
| 180 |
result = []
|
| 181 |
for pg in pages:
|
| 182 |
+
# Handle if backend returned objects or dicts
|
| 183 |
+
p_data = [p if isinstance(p, dict) else p.__dict__ for p in pg.panels]
|
| 184 |
+
b_data = [b if isinstance(b, dict) else b.__dict__ for b in pg.bubbles]
|
| 185 |
result.append({'panels': p_data, 'bubbles': b_data})
|
| 186 |
|
| 187 |
return result
|
|
|
|
| 209 |
if ret:
|
| 210 |
path = os.path.join(frames_dir, fname)
|
| 211 |
cv2.imwrite(path, frame)
|
|
|
|
| 212 |
try: SimpleColorEnhancer().enhance_single(path, path)
|
| 213 |
except: pass
|
| 214 |
|