Update app_enhanced.py
Browse files- app_enhanced.py +55 -47
app_enhanced.py
CHANGED
|
@@ -49,9 +49,7 @@ def sanitize_json(obj):
|
|
| 49 |
# ======================================================
|
| 50 |
@spaces.GPU(duration=300)
|
| 51 |
def generate_comic_gpu(video_path, user_dir, frames_dir, metadata_path, target_pages):
|
| 52 |
-
|
| 53 |
-
import srt
|
| 54 |
-
import numpy as np
|
| 55 |
from backend.keyframes.keyframes import black_bar_crop
|
| 56 |
from backend.simple_color_enhancer import SimpleColorEnhancer
|
| 57 |
from backend.subtitles.subs_real import get_real_subtitles
|
|
@@ -64,6 +62,7 @@ def generate_comic_gpu(video_path, user_dir, frames_dir, metadata_path, target_p
|
|
| 64 |
duration = total_frames / fps
|
| 65 |
cap.release()
|
| 66 |
|
|
|
|
| 67 |
user_srt = os.path.join(user_dir, 'subs.srt')
|
| 68 |
try:
|
| 69 |
get_real_subtitles(video_path)
|
|
@@ -75,35 +74,31 @@ def generate_comic_gpu(video_path, user_dir, frames_dir, metadata_path, target_p
|
|
| 75 |
try: all_subs = list(srt.parse(f.read()))
|
| 76 |
except: all_subs = []
|
| 77 |
|
|
|
|
| 78 |
panels_per_page = 5
|
| 79 |
target_pages = int(target_pages)
|
| 80 |
total_needed = target_pages * panels_per_page
|
| 81 |
|
| 82 |
-
if
|
| 83 |
-
|
| 84 |
-
moments = [{'text': '', 'start': t} for t in times]
|
| 85 |
-
elif len(all_subs) <= total_needed:
|
| 86 |
-
moments = [{'text': s.content, 'start': s.start.total_seconds()} for s in all_subs]
|
| 87 |
-
while len(moments) < total_needed: moments.append({'text': '', 'start': duration/2})
|
| 88 |
-
else:
|
| 89 |
-
indices = np.linspace(0, len(all_subs) - 1, total_needed, dtype=int)
|
| 90 |
-
moments = [{'text': all_subs[i].content, 'start': all_subs[i].start.total_seconds()} for i in indices]
|
| 91 |
-
|
| 92 |
frame_metadata = {}
|
| 93 |
cap = cv2.VideoCapture(video_path)
|
| 94 |
frame_files = []
|
| 95 |
-
for i,
|
| 96 |
-
|
|
|
|
| 97 |
ret, frame = cap.read()
|
| 98 |
if ret:
|
| 99 |
fname = f"frame_{i:04d}.png"
|
| 100 |
p = os.path.join(frames_dir, fname)
|
| 101 |
cv2.imwrite(p, frame)
|
| 102 |
-
frame_metadata[fname] = {'dialogue':
|
| 103 |
frame_files.append(fname)
|
| 104 |
cap.release()
|
| 105 |
|
| 106 |
-
with open(metadata_path, 'w') as f:
|
|
|
|
|
|
|
| 107 |
try: black_bar_crop()
|
| 108 |
except: pass
|
| 109 |
|
|
@@ -133,7 +128,7 @@ def generate_comic_gpu(video_path, user_dir, frames_dir, metadata_path, target_p
|
|
| 133 |
return sanitize_json(pages_data)
|
| 134 |
|
| 135 |
# ======================================================
|
| 136 |
-
# 🔧
|
| 137 |
# ======================================================
|
| 138 |
app = Flask(__name__)
|
| 139 |
|
|
@@ -153,7 +148,7 @@ def uploader():
|
|
| 153 |
def task():
|
| 154 |
try:
|
| 155 |
with open(os.path.join(o_dir, 'status.json'), 'w') as f:
|
| 156 |
-
json.dump({'message': '
|
| 157 |
data = generate_comic_gpu(vid_p, u_dir, f_dir, os.path.join(f_dir, 'meta.json'), pages)
|
| 158 |
with open(os.path.join(o_dir, 'pages.json'), 'w') as f: json.dump(data, f)
|
| 159 |
with open(os.path.join(o_dir, 'status.json'), 'w') as f:
|
|
@@ -178,55 +173,67 @@ def get_frame(sid, filename): return send_from_directory(os.path.join(BASE_USER_
|
|
| 178 |
def get_output(sid, filename): return send_from_directory(os.path.join(BASE_USER_DIR, sid, 'output'), filename)
|
| 179 |
|
| 180 |
# ======================================================
|
| 181 |
-
# 🌐 HTML (
|
| 182 |
# ======================================================
|
| 183 |
INDEX_HTML = '''
|
| 184 |
<!DOCTYPE html><html lang="en">
|
| 185 |
<head>
|
| 186 |
-
<meta charset="UTF-8"><title>Geometric
|
| 187 |
<script src="https://cdnjs.cloudflare.com/ajax/libs/html-to-image/1.11.11/html-to-image.min.js"></script>
|
| 188 |
<link href="https://fonts.googleapis.com/css2?family=Comic+Neue:wght@700&family=Lato:wght@400;900&display=swap" rel="stylesheet">
|
| 189 |
<style>
|
| 190 |
:root {
|
| 191 |
-
--slant: 35px; /*
|
| 192 |
-
--hairline:
|
| 193 |
--page-border: 12px;
|
| 194 |
}
|
| 195 |
body { background: #111; font-family: 'Lato', sans-serif; margin: 0; padding: 20px; }
|
| 196 |
.setup-box { max-width: 450px; margin: 80px auto; background: white; padding: 40px; border-radius: 12px; color: black; text-align: center; }
|
| 197 |
|
| 198 |
-
/* THE PERFECT
|
| 199 |
.comic-page {
|
| 200 |
background: white; width: 1000px; height: 750px; margin: 40px auto;
|
| 201 |
-
border: var(--page-border) solid black; padding:
|
| 202 |
-
display: grid;
|
| 203 |
-
gap: var(--hairline); /* HAIRLINE GAP */
|
| 204 |
grid-template-columns: repeat(6, 1fr);
|
| 205 |
-
grid-template-rows: 1.35fr 1fr; /*
|
| 206 |
position: relative; overflow: hidden;
|
| 207 |
}
|
| 208 |
|
| 209 |
.panel { position: relative; background: #000; overflow: hidden; cursor: pointer; border: 1.5px solid black; }
|
|
|
|
| 210 |
|
| 211 |
-
/*
|
| 212 |
-
.panel
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
.panel:nth-child(2) {
|
|
|
|
|
|
|
|
|
|
| 217 |
|
| 218 |
-
/* ROW 2:
|
| 219 |
-
.panel:nth-child(3) {
|
| 220 |
-
|
| 221 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
|
| 223 |
-
.panel.selected { outline:
|
| 224 |
|
| 225 |
-
/* Bubble Style - Pro White Capsule */
|
| 226 |
.bubble {
|
| 227 |
position: absolute; background: white; border: 2.5px solid black; border-radius: 25px;
|
| 228 |
padding: 10px 20px; font-family: 'Comic Neue'; font-weight: bold; font-size: 15px;
|
| 229 |
color: black; min-width: 110px; text-align: center; cursor: move; z-index: 10;
|
|
|
|
| 230 |
}
|
| 231 |
.bubble::after { content: ""; position: absolute; bottom: -18px; left: 30px; width: 0; height: 0; border-left: 10px solid transparent; border-right: 10px solid transparent; border-top: 20px solid black; }
|
| 232 |
.bubble::before { content: ""; position: absolute; bottom: -13px; left: 31px; width: 0; height: 0; border-left: 9px solid transparent; border-right: 9px solid transparent; border-top: 17px solid white; z-index: 2; }
|
|
@@ -240,11 +247,12 @@ INDEX_HTML = '''
|
|
| 240 |
</head>
|
| 241 |
<body>
|
| 242 |
<div id="upload-zone" class="setup-box">
|
| 243 |
-
<h1>🎬 Elite Comic
|
| 244 |
-
<p>Consistent Straight &
|
| 245 |
<input type="file" id="vid" accept="video/mp4"><br><br>
|
| 246 |
-
<label>Pages: </label><input type="number" id="pg" value="2" style="width:50px"
|
| 247 |
-
<
|
|
|
|
| 248 |
<div id="loading" class="hidden"><div class="loader"></div><p id="st">Acquiring GPU...</p></div>
|
| 249 |
</div>
|
| 250 |
<div id="editor-zone" class="hidden">
|
|
@@ -298,15 +306,15 @@ INDEX_HTML = '''
|
|
| 298 |
document.onmousemove = (ev) => { b.style.left=(ev.clientX-ox)+'px'; b.style.top=(ev.clientY-oy)+'px'; };
|
| 299 |
document.onmouseup = () => { document.onmousemove = null; };
|
| 300 |
};
|
| 301 |
-
b.ondblclick = () => { let n = prompt("Edit:", b.innerText); if(n) b.innerText = n; };
|
| 302 |
return b;
|
| 303 |
}
|
| 304 |
-
function addB() { if(selP) selP.appendChild(createB("
|
| 305 |
async function exportPNG() {
|
| 306 |
const pgs = document.querySelectorAll('.comic-page');
|
| 307 |
for(let pg of pgs) {
|
| 308 |
const url = await htmlToImage.toPng(pg, {pixelRatio: 2});
|
| 309 |
-
const l = document.createElement('a'); l.download='
|
| 310 |
}
|
| 311 |
}
|
| 312 |
</script>
|
|
|
|
| 49 |
# ======================================================
|
| 50 |
@spaces.GPU(duration=300)
|
| 51 |
def generate_comic_gpu(video_path, user_dir, frames_dir, metadata_path, target_pages):
|
| 52 |
+
# Heavy AI imports inside function to avoid Startup Timeout
|
|
|
|
|
|
|
| 53 |
from backend.keyframes.keyframes import black_bar_crop
|
| 54 |
from backend.simple_color_enhancer import SimpleColorEnhancer
|
| 55 |
from backend.subtitles.subs_real import get_real_subtitles
|
|
|
|
| 62 |
duration = total_frames / fps
|
| 63 |
cap.release()
|
| 64 |
|
| 65 |
+
# 1. Subtitles
|
| 66 |
user_srt = os.path.join(user_dir, 'subs.srt')
|
| 67 |
try:
|
| 68 |
get_real_subtitles(video_path)
|
|
|
|
| 74 |
try: all_subs = list(srt.parse(f.read()))
|
| 75 |
except: all_subs = []
|
| 76 |
|
| 77 |
+
# 2. 5-Panel Math
|
| 78 |
panels_per_page = 5
|
| 79 |
target_pages = int(target_pages)
|
| 80 |
total_needed = target_pages * panels_per_page
|
| 81 |
|
| 82 |
+
indices = np.linspace(0, len(all_subs) - 1, total_needed, dtype=int) if all_subs else range(total_needed)
|
| 83 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
frame_metadata = {}
|
| 85 |
cap = cv2.VideoCapture(video_path)
|
| 86 |
frame_files = []
|
| 87 |
+
for i, idx in enumerate(indices):
|
| 88 |
+
t = all_subs[idx].start.total_seconds() if all_subs else (i * (duration/total_needed))
|
| 89 |
+
cap.set(cv2.CAP_PROP_POS_MSEC, t * 1000)
|
| 90 |
ret, frame = cap.read()
|
| 91 |
if ret:
|
| 92 |
fname = f"frame_{i:04d}.png"
|
| 93 |
p = os.path.join(frames_dir, fname)
|
| 94 |
cv2.imwrite(p, frame)
|
| 95 |
+
frame_metadata[fname] = {'dialogue': all_subs[idx].content if all_subs else "", 'time': t}
|
| 96 |
frame_files.append(fname)
|
| 97 |
cap.release()
|
| 98 |
|
| 99 |
+
with open(metadata_path, 'w') as f:
|
| 100 |
+
json.dump(sanitize_json(frame_metadata), f)
|
| 101 |
+
|
| 102 |
try: black_bar_crop()
|
| 103 |
except: pass
|
| 104 |
|
|
|
|
| 128 |
return sanitize_json(pages_data)
|
| 129 |
|
| 130 |
# ======================================================
|
| 131 |
+
# 🔧 APP ENGINE
|
| 132 |
# ======================================================
|
| 133 |
app = Flask(__name__)
|
| 134 |
|
|
|
|
| 148 |
def task():
|
| 149 |
try:
|
| 150 |
with open(os.path.join(o_dir, 'status.json'), 'w') as f:
|
| 151 |
+
json.dump({'message': 'Drafting Parallel Geometry...', 'progress': 30}, f)
|
| 152 |
data = generate_comic_gpu(vid_p, u_dir, f_dir, os.path.join(f_dir, 'meta.json'), pages)
|
| 153 |
with open(os.path.join(o_dir, 'pages.json'), 'w') as f: json.dump(data, f)
|
| 154 |
with open(os.path.join(o_dir, 'status.json'), 'w') as f:
|
|
|
|
| 173 |
def get_output(sid, filename): return send_from_directory(os.path.join(BASE_USER_DIR, sid, 'output'), filename)
|
| 174 |
|
| 175 |
# ======================================================
|
| 176 |
+
# 🌐 HTML (MATHEMATICALLY PARALLEL TILT TEMPLATE)
|
| 177 |
# ======================================================
|
| 178 |
INDEX_HTML = '''
|
| 179 |
<!DOCTYPE html><html lang="en">
|
| 180 |
<head>
|
| 181 |
+
<meta charset="UTF-8"><title>Geometric Consistency Generator</title>
|
| 182 |
<script src="https://cdnjs.cloudflare.com/ajax/libs/html-to-image/1.11.11/html-to-image.min.js"></script>
|
| 183 |
<link href="https://fonts.googleapis.com/css2?family=Comic+Neue:wght@700&family=Lato:wght@400;900&display=swap" rel="stylesheet">
|
| 184 |
<style>
|
| 185 |
:root {
|
| 186 |
+
--slant: 35px; /* Fixed angle for all panels */
|
| 187 |
+
--hairline: 1.5px; /* Minimal consistent gap */
|
| 188 |
--page-border: 12px;
|
| 189 |
}
|
| 190 |
body { background: #111; font-family: 'Lato', sans-serif; margin: 0; padding: 20px; }
|
| 191 |
.setup-box { max-width: 450px; margin: 80px auto; background: white; padding: 40px; border-radius: 12px; color: black; text-align: center; }
|
| 192 |
|
| 193 |
+
/* ⚡ THE PERFECT PARALLEL TEMPLATE ⚡ */
|
| 194 |
.comic-page {
|
| 195 |
background: white; width: 1000px; height: 750px; margin: 40px auto;
|
| 196 |
+
border: var(--page-border) solid black; padding: 3px; box-sizing: border-box;
|
| 197 |
+
display: grid; gap: var(--hairline);
|
|
|
|
| 198 |
grid-template-columns: repeat(6, 1fr);
|
| 199 |
+
grid-template-rows: 1.35fr 1fr; /* Straight horizontal line between tiers */
|
| 200 |
position: relative; overflow: hidden;
|
| 201 |
}
|
| 202 |
|
| 203 |
.panel { position: relative; background: #000; overflow: hidden; cursor: pointer; border: 1.5px solid black; }
|
| 204 |
+
.panel img { width: 115%; height: 115%; object-fit: cover; position: absolute; top: -7.5%; left: -7.5%; object-position: center 15%; pointer-events: none; }
|
| 205 |
|
| 206 |
+
/* ROW 1: Slant Left \ (Dividers are perfectly parallel) */
|
| 207 |
+
.panel:nth-child(1) {
|
| 208 |
+
grid-column: span 4;
|
| 209 |
+
clip-path: polygon(0 0, 100% 0, calc(100% - var(--slant)) 100%, 0 100%);
|
| 210 |
+
}
|
| 211 |
+
.panel:nth-child(2) {
|
| 212 |
+
grid-column: span 2;
|
| 213 |
+
clip-path: polygon(var(--slant) 0, 100% 0, 100% 100%, 0 100%);
|
| 214 |
+
}
|
| 215 |
|
| 216 |
+
/* ROW 2: Slant Right / (Dividers are perfectly parallel) */
|
| 217 |
+
.panel:nth-child(3) {
|
| 218 |
+
grid-column: span 2;
|
| 219 |
+
clip-path: polygon(0 0, calc(100% - var(--slant)) 0, 100% 100%, 0 100%);
|
| 220 |
+
}
|
| 221 |
+
.panel:nth-child(4) {
|
| 222 |
+
grid-column: span 2;
|
| 223 |
+
clip-path: polygon(var(--slant) 0, calc(100% - var(--slant)) 0, 100% 100%, var(--slant) 100%);
|
| 224 |
+
}
|
| 225 |
+
.panel:nth-child(5) {
|
| 226 |
+
grid-column: span 2;
|
| 227 |
+
clip-path: polygon(var(--slant) 0, 100% 0, 100% 100%, var(--slant) 100%);
|
| 228 |
+
}
|
| 229 |
|
| 230 |
+
.panel.selected { outline: 8px solid #00d2ff; z-index: 5; filter: brightness(1.1); }
|
| 231 |
|
|
|
|
| 232 |
.bubble {
|
| 233 |
position: absolute; background: white; border: 2.5px solid black; border-radius: 25px;
|
| 234 |
padding: 10px 20px; font-family: 'Comic Neue'; font-weight: bold; font-size: 15px;
|
| 235 |
color: black; min-width: 110px; text-align: center; cursor: move; z-index: 10;
|
| 236 |
+
box-shadow: 4px 4px 0 rgba(0,0,0,0.1);
|
| 237 |
}
|
| 238 |
.bubble::after { content: ""; position: absolute; bottom: -18px; left: 30px; width: 0; height: 0; border-left: 10px solid transparent; border-right: 10px solid transparent; border-top: 20px solid black; }
|
| 239 |
.bubble::before { content: ""; position: absolute; bottom: -13px; left: 31px; width: 0; height: 0; border-left: 9px solid transparent; border-right: 9px solid transparent; border-top: 17px solid white; z-index: 2; }
|
|
|
|
| 247 |
</head>
|
| 248 |
<body>
|
| 249 |
<div id="upload-zone" class="setup-box">
|
| 250 |
+
<h1>🎬 Elite Comic Maker</h1>
|
| 251 |
+
<p>Consistent Straight & Slanted Geometry</p>
|
| 252 |
<input type="file" id="vid" accept="video/mp4"><br><br>
|
| 253 |
+
<label>Total Pages: </label><input type="number" id="pg" value="2" style="width:50px">
|
| 254 |
+
<br><br>
|
| 255 |
+
<button onclick="start()" style="background:#e67e22; color:white;">GENERATE COMIC</button>
|
| 256 |
<div id="loading" class="hidden"><div class="loader"></div><p id="st">Acquiring GPU...</p></div>
|
| 257 |
</div>
|
| 258 |
<div id="editor-zone" class="hidden">
|
|
|
|
| 306 |
document.onmousemove = (ev) => { b.style.left=(ev.clientX-ox)+'px'; b.style.top=(ev.clientY-oy)+'px'; };
|
| 307 |
document.onmouseup = () => { document.onmousemove = null; };
|
| 308 |
};
|
| 309 |
+
b.ondblclick = () => { let n = prompt("Edit text:", b.innerText); if(n) b.innerText = n; };
|
| 310 |
return b;
|
| 311 |
}
|
| 312 |
+
function addB() { if(selP) selP.appendChild(createB("Dialogue", 60, 60)); }
|
| 313 |
async function exportPNG() {
|
| 314 |
const pgs = document.querySelectorAll('.comic-page');
|
| 315 |
for(let pg of pgs) {
|
| 316 |
const url = await htmlToImage.toPng(pg, {pixelRatio: 2});
|
| 317 |
+
const l = document.createElement('a'); l.download='Elite_Comic.png'; l.href=url; l.click();
|
| 318 |
}
|
| 319 |
}
|
| 320 |
</script>
|