Update app_enhanced.py
Browse files- app_enhanced.py +46 -38
app_enhanced.py
CHANGED
|
@@ -29,7 +29,7 @@ BASE_USER_DIR = os.path.join(BASE_STORAGE_PATH, "userdata")
|
|
| 29 |
os.makedirs(BASE_USER_DIR, exist_ok=True)
|
| 30 |
|
| 31 |
# ======================================================
|
| 32 |
-
# 🔧 JSON SANITIZER
|
| 33 |
# ======================================================
|
| 34 |
def sanitize_json(obj):
|
| 35 |
if isinstance(obj, dict):
|
|
@@ -40,6 +40,8 @@ def sanitize_json(obj):
|
|
| 40 |
return int(obj)
|
| 41 |
elif isinstance(obj, (np.float64, np.float32)):
|
| 42 |
return float(obj)
|
|
|
|
|
|
|
| 43 |
return obj
|
| 44 |
|
| 45 |
# ======================================================
|
|
@@ -47,6 +49,9 @@ def sanitize_json(obj):
|
|
| 47 |
# ======================================================
|
| 48 |
@spaces.GPU(duration=300)
|
| 49 |
def generate_comic_gpu(video_path, user_dir, frames_dir, metadata_path, target_pages):
|
|
|
|
|
|
|
|
|
|
| 50 |
from backend.keyframes.keyframes import black_bar_crop
|
| 51 |
from backend.simple_color_enhancer import SimpleColorEnhancer
|
| 52 |
from backend.subtitles.subs_real import get_real_subtitles
|
|
@@ -64,7 +69,7 @@ def generate_comic_gpu(video_path, user_dir, frames_dir, metadata_path, target_p
|
|
| 64 |
get_real_subtitles(video_path)
|
| 65 |
if os.path.exists('test1.srt'): shutil.move('test1.srt', user_srt)
|
| 66 |
except:
|
| 67 |
-
with open(user_srt, 'w') as f: f.write("1\n00:00:01,000 --> 00:00:05,000\
|
| 68 |
|
| 69 |
with open(user_srt, 'r', encoding='utf-8') as f:
|
| 70 |
try: all_subs = list(srt.parse(f.read()))
|
|
@@ -73,21 +78,28 @@ def generate_comic_gpu(video_path, user_dir, frames_dir, metadata_path, target_p
|
|
| 73 |
panels_per_page = 5
|
| 74 |
target_pages = int(target_pages)
|
| 75 |
total_needed = target_pages * panels_per_page
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
frame_metadata = {}
|
| 79 |
cap = cv2.VideoCapture(video_path)
|
| 80 |
frame_files = []
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
t = all_subs[idx].start.total_seconds() if all_subs else (i * (duration/total_needed))
|
| 84 |
-
cap.set(cv2.CAP_PROP_POS_MSEC, t * 1000)
|
| 85 |
ret, frame = cap.read()
|
| 86 |
if ret:
|
| 87 |
fname = f"frame_{i:04d}.png"
|
| 88 |
p = os.path.join(frames_dir, fname)
|
| 89 |
cv2.imwrite(p, frame)
|
| 90 |
-
frame_metadata[fname] = {'dialogue':
|
| 91 |
frame_files.append(fname)
|
| 92 |
cap.release()
|
| 93 |
|
|
@@ -121,7 +133,7 @@ def generate_comic_gpu(video_path, user_dir, frames_dir, metadata_path, target_p
|
|
| 121 |
return sanitize_json(pages_data)
|
| 122 |
|
| 123 |
# ======================================================
|
| 124 |
-
# 🔧 APP
|
| 125 |
# ======================================================
|
| 126 |
app = Flask(__name__)
|
| 127 |
|
|
@@ -141,7 +153,7 @@ def uploader():
|
|
| 141 |
def task():
|
| 142 |
try:
|
| 143 |
with open(os.path.join(o_dir, 'status.json'), 'w') as f:
|
| 144 |
-
json.dump({'message': 'Generating
|
| 145 |
data = generate_comic_gpu(vid_p, u_dir, f_dir, os.path.join(f_dir, 'meta.json'), pages)
|
| 146 |
with open(os.path.join(o_dir, 'pages.json'), 'w') as f: json.dump(data, f)
|
| 147 |
with open(os.path.join(o_dir, 'status.json'), 'w') as f:
|
|
@@ -166,54 +178,51 @@ def get_frame(sid, filename): return send_from_directory(os.path.join(BASE_USER_
|
|
| 166 |
def get_output(sid, filename): return send_from_directory(os.path.join(BASE_USER_DIR, sid, 'output'), filename)
|
| 167 |
|
| 168 |
# ======================================================
|
| 169 |
-
#
|
| 170 |
# ======================================================
|
| 171 |
INDEX_HTML = '''
|
| 172 |
<!DOCTYPE html><html lang="en">
|
| 173 |
<head>
|
| 174 |
-
<meta charset="UTF-8"><title>
|
| 175 |
<script src="https://cdnjs.cloudflare.com/ajax/libs/html-to-image/1.11.11/html-to-image.min.js"></script>
|
| 176 |
<link href="https://fonts.googleapis.com/css2?family=Comic+Neue:wght@700&family=Lato:wght@400;900&display=swap" rel="stylesheet">
|
| 177 |
<style>
|
| 178 |
:root {
|
| 179 |
-
--slant:
|
| 180 |
-
--
|
| 181 |
-
--border
|
| 182 |
}
|
| 183 |
-
body { background: #
|
| 184 |
.setup-box { max-width: 450px; margin: 80px auto; background: white; padding: 40px; border-radius: 12px; color: black; text-align: center; }
|
| 185 |
|
| 186 |
-
/*
|
| 187 |
.comic-page {
|
| 188 |
background: white; width: 1000px; height: 750px; margin: 40px auto;
|
| 189 |
-
border: var(--border
|
| 190 |
-
display: grid;
|
|
|
|
| 191 |
grid-template-columns: repeat(6, 1fr);
|
| 192 |
-
grid-template-rows: 1.
|
| 193 |
position: relative; overflow: hidden;
|
| 194 |
}
|
| 195 |
|
| 196 |
.panel { position: relative; background: #000; overflow: hidden; cursor: pointer; border: 1.5px solid black; }
|
| 197 |
|
| 198 |
-
/*
|
| 199 |
-
.panel img {
|
| 200 |
-
width: 108%; height: 108%; object-fit: cover;
|
| 201 |
-
position: absolute; top: -4%; left: -4%;
|
| 202 |
-
object-position: center 10%; /* Ensures faces at the top aren't cut */
|
| 203 |
-
pointer-events: none;
|
| 204 |
-
}
|
| 205 |
|
| 206 |
-
/* ROW 1:
|
| 207 |
.panel:nth-child(1) { grid-column: span 4; clip-path: polygon(0 0, 100% 0, calc(100% - var(--slant)) 100%, 0 100%); }
|
| 208 |
.panel:nth-child(2) { grid-column: span 2; clip-path: polygon(var(--slant) 0, 100% 0, 100% 100%, 0 100%); }
|
| 209 |
|
| 210 |
-
/* ROW 2:
|
| 211 |
.panel:nth-child(3) { grid-column: span 2; clip-path: polygon(0 0, calc(100% - var(--slant)) 0, 100% 100%, 0 100%); }
|
| 212 |
.panel:nth-child(4) { grid-column: span 2; clip-path: polygon(var(--slant) 0, calc(100% - var(--slant)) 0, 100% 100%, var(--slant) 100%); }
|
| 213 |
.panel:nth-child(5) { grid-column: span 2; clip-path: polygon(var(--slant) 0, 100% 0, 100% 100%, var(--slant) 100%); }
|
| 214 |
|
| 215 |
-
.panel.selected { outline:
|
| 216 |
|
|
|
|
| 217 |
.bubble {
|
| 218 |
position: absolute; background: white; border: 2.5px solid black; border-radius: 25px;
|
| 219 |
padding: 10px 20px; font-family: 'Comic Neue'; font-weight: bold; font-size: 15px;
|
|
@@ -231,13 +240,12 @@ INDEX_HTML = '''
|
|
| 231 |
</head>
|
| 232 |
<body>
|
| 233 |
<div id="upload-zone" class="setup-box">
|
| 234 |
-
<h1>🎬 Elite Comic
|
| 235 |
-
<p>
|
| 236 |
<input type="file" id="vid" accept="video/mp4"><br><br>
|
| 237 |
-
<label>
|
| 238 |
-
<
|
| 239 |
-
<
|
| 240 |
-
<div id="loading" class="hidden"><div class="loader"></div><p id="st">Processing Composition...</p></div>
|
| 241 |
</div>
|
| 242 |
<div id="editor-zone" class="hidden">
|
| 243 |
<div id="output"></div>
|
|
@@ -293,7 +301,7 @@ INDEX_HTML = '''
|
|
| 293 |
b.ondblclick = () => { let n = prompt("Edit:", b.innerText); if(n) b.innerText = n; };
|
| 294 |
return b;
|
| 295 |
}
|
| 296 |
-
function addB() { if(selP) selP.appendChild(createB("
|
| 297 |
async function exportPNG() {
|
| 298 |
const pgs = document.querySelectorAll('.comic-page');
|
| 299 |
for(let pg of pgs) {
|
|
|
|
| 29 |
os.makedirs(BASE_USER_DIR, exist_ok=True)
|
| 30 |
|
| 31 |
# ======================================================
|
| 32 |
+
# 🔧 JSON SANITIZER (FIX FOR int64 ERROR)
|
| 33 |
# ======================================================
|
| 34 |
def sanitize_json(obj):
|
| 35 |
if isinstance(obj, dict):
|
|
|
|
| 40 |
return int(obj)
|
| 41 |
elif isinstance(obj, (np.float64, np.float32)):
|
| 42 |
return float(obj)
|
| 43 |
+
elif isinstance(obj, np.ndarray):
|
| 44 |
+
return sanitize_json(obj.tolist())
|
| 45 |
return obj
|
| 46 |
|
| 47 |
# ======================================================
|
|
|
|
| 49 |
# ======================================================
|
| 50 |
@spaces.GPU(duration=300)
|
| 51 |
def generate_comic_gpu(video_path, user_dir, frames_dir, metadata_path, target_pages):
|
| 52 |
+
import cv2
|
| 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
|
|
|
|
| 69 |
get_real_subtitles(video_path)
|
| 70 |
if os.path.exists('test1.srt'): shutil.move('test1.srt', user_srt)
|
| 71 |
except:
|
| 72 |
+
with open(user_srt, 'w') as f: f.write("1\n00:00:01,000 --> 00:00:05,000\nDialogue...\n")
|
| 73 |
|
| 74 |
with open(user_srt, 'r', encoding='utf-8') as f:
|
| 75 |
try: all_subs = list(srt.parse(f.read()))
|
|
|
|
| 78 |
panels_per_page = 5
|
| 79 |
target_pages = int(target_pages)
|
| 80 |
total_needed = target_pages * panels_per_page
|
| 81 |
+
|
| 82 |
+
if not all_subs:
|
| 83 |
+
times = np.linspace(1, max(1.1, duration-1), total_needed)
|
| 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, m in enumerate(moments):
|
| 96 |
+
cap.set(cv2.CAP_PROP_POS_MSEC, m['start'] * 1000)
|
|
|
|
|
|
|
| 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': m['text'], 'time': m['start']}
|
| 103 |
frame_files.append(fname)
|
| 104 |
cap.release()
|
| 105 |
|
|
|
|
| 133 |
return sanitize_json(pages_data)
|
| 134 |
|
| 135 |
# ======================================================
|
| 136 |
+
# 🔧 FLASK APP
|
| 137 |
# ======================================================
|
| 138 |
app = Flask(__name__)
|
| 139 |
|
|
|
|
| 153 |
def task():
|
| 154 |
try:
|
| 155 |
with open(os.path.join(o_dir, 'status.json'), 'w') as f:
|
| 156 |
+
json.dump({'message': 'Generating Geometry...', 'progress': 30}, f)
|
| 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 |
def get_output(sid, filename): return send_from_directory(os.path.join(BASE_USER_DIR, sid, 'output'), filename)
|
| 179 |
|
| 180 |
# ======================================================
|
| 181 |
+
# 🌐 HTML (CONSISTENT STRAIGHT HORIZONTAL + TILTED VERTICAL)
|
| 182 |
# ======================================================
|
| 183 |
INDEX_HTML = '''
|
| 184 |
<!DOCTYPE html><html lang="en">
|
| 185 |
<head>
|
| 186 |
+
<meta charset="UTF-8"><title>Geometric Precision Comic</title>
|
| 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; /* Angle offset */
|
| 192 |
+
--hairline: 1px; /* RAZOR THIN WHITE LINE */
|
| 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 GEOMETRIC LAYOUT */
|
| 199 |
.comic-page {
|
| 200 |
background: white; width: 1000px; height: 750px; margin: 40px auto;
|
| 201 |
+
border: var(--page-border) solid black; padding: 2px; box-sizing: border-box;
|
| 202 |
+
display: grid;
|
| 203 |
+
gap: var(--hairline); /* HAIRLINE GAP */
|
| 204 |
grid-template-columns: repeat(6, 1fr);
|
| 205 |
+
grid-template-rows: 1.35fr 1fr; /* Taller top row, straight horizontal divider */
|
| 206 |
position: relative; overflow: hidden;
|
| 207 |
}
|
| 208 |
|
| 209 |
.panel { position: relative; background: #000; overflow: hidden; cursor: pointer; border: 1.5px solid black; }
|
| 210 |
|
| 211 |
+
/* Oversize images so slanted edges don't cut content */
|
| 212 |
+
.panel img { width: 112%; height: 112%; object-fit: cover; position: absolute; top: -6%; left: -6%; object-position: center 15%; pointer-events: none; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
|
| 214 |
+
/* ROW 1: Straight top/bottom, slanted center divider */
|
| 215 |
.panel:nth-child(1) { grid-column: span 4; clip-path: polygon(0 0, 100% 0, calc(100% - var(--slant)) 100%, 0 100%); }
|
| 216 |
.panel:nth-child(2) { grid-column: span 2; clip-path: polygon(var(--slant) 0, 100% 0, 100% 100%, 0 100%); }
|
| 217 |
|
| 218 |
+
/* ROW 2: Straight top/bottom, three parallel slanted dividers */
|
| 219 |
.panel:nth-child(3) { grid-column: span 2; clip-path: polygon(0 0, calc(100% - var(--slant)) 0, 100% 100%, 0 100%); }
|
| 220 |
.panel:nth-child(4) { grid-column: span 2; clip-path: polygon(var(--slant) 0, calc(100% - var(--slant)) 0, 100% 100%, var(--slant) 100%); }
|
| 221 |
.panel:nth-child(5) { grid-column: span 2; clip-path: polygon(var(--slant) 0, 100% 0, 100% 100%, var(--slant) 100%); }
|
| 222 |
|
| 223 |
+
.panel.selected { outline: 6px solid #00d2ff; z-index: 5; filter: brightness(1.1); }
|
| 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;
|
|
|
|
| 240 |
</head>
|
| 241 |
<body>
|
| 242 |
<div id="upload-zone" class="setup-box">
|
| 243 |
+
<h1>🎬 Elite Comic Generator</h1>
|
| 244 |
+
<p>Consistent Straight & Tilted Geometry</p>
|
| 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"><br><br>
|
| 247 |
+
<button onclick="start()" style="background:#e67e22; color:white;">🚀 GENERATE</button>
|
| 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">
|
| 251 |
<div id="output"></div>
|
|
|
|
| 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("Text", 60, 60)); }
|
| 305 |
async function exportPNG() {
|
| 306 |
const pgs = document.querySelectorAll('.comic-page');
|
| 307 |
for(let pg of pgs) {
|