File size: 21,283 Bytes
83e35a7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 |
"""
Emotion-Aware Comic Generation
Creates comics that match facial expressions with dialogue emotions
"""
import cv2
import numpy as np
import os
import json
from typing import List, Dict, Tuple, Optional
import srt
from datetime import timedelta
class FacialExpressionAnalyzer:
"""Analyze facial expressions in frames"""
def __init__(self):
# Load face detection
self.face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
self.eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_eye.xml')
self.smile_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_smile.xml')
def analyze_expression(self, image_path: str) -> Dict[str, float]:
"""Analyze facial expression in an image"""
img = cv2.imread(image_path)
if img is None:
return self._default_expression()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Detect faces
faces = self.face_cascade.detectMultiScale(gray, 1.1, 4)
if len(faces) == 0:
return self._default_expression()
# Analyze the largest face
x, y, w, h = max(faces, key=lambda f: f[2] * f[3])
face_roi = gray[y:y+h, x:x+w]
# Detect features
eyes = self.eye_cascade.detectMultiScale(face_roi, 1.1, 5)
smiles = self.smile_cascade.detectMultiScale(face_roi, 1.8, 20)
# Analyze expression based on features
expression = self._analyze_features(face_roi, eyes, smiles)
# Add intensity analysis
expression['intensity'] = self._analyze_intensity(face_roi)
return expression
def _analyze_features(self, face_roi, eyes, smiles) -> Dict[str, float]:
"""Analyze facial features to determine expression"""
expression = {
'happy': 0.0,
'sad': 0.0,
'angry': 0.0,
'surprised': 0.0,
'neutral': 0.5
}
# Smile detection
if len(smiles) > 0:
expression['happy'] = 0.7
expression['neutral'] = 0.3
# Eye analysis
if len(eyes) >= 2:
# Both eyes visible - analyze eye region
eye_region = face_roi[:face_roi.shape[0]//2, :]
eye_variance = np.var(eye_region)
if eye_variance > 1000: # Wide eyes
expression['surprised'] = 0.6
elif eye_variance < 500: # Squinted eyes
expression['angry'] = 0.4
elif len(eyes) < 2:
# Eyes not clearly visible - might be closed or squinted
expression['sad'] = 0.3
expression['angry'] = 0.3
# Normalize scores
total = sum(expression.values())
if total > 0:
expression = {k: v/total for k, v in expression.items()}
return expression
def _analyze_intensity(self, face_roi) -> float:
"""Analyze expression intensity"""
# Calculate contrast and edge density
edges = cv2.Canny(face_roi, 50, 150)
edge_density = np.sum(edges > 0) / edges.size
# Higher edge density often means more intense expression
intensity = min(edge_density * 5, 1.0)
return intensity
def _default_expression(self) -> Dict[str, float]:
"""Default expression when no face detected"""
return {
'neutral': 1.0,
'happy': 0.0,
'sad': 0.0,
'angry': 0.0,
'surprised': 0.0,
'intensity': 0.5
}
class DialogueEmotionAnalyzer:
"""Analyze emotions in dialogue text"""
def __init__(self):
# Emotion lexicons
self.emotion_words = {
'happy': {
'words': ['happy', 'joy', 'love', 'great', 'wonderful', 'amazing', 'fantastic', 'excellent', 'beautiful', 'laugh', 'smile', 'fun'],
'weight': 1.0
},
'sad': {
'words': ['sad', 'cry', 'tear', 'sorry', 'miss', 'lonely', 'depressed', 'hurt', 'pain', 'loss', 'grief'],
'weight': 1.0
},
'angry': {
'words': ['angry', 'mad', 'furious', 'hate', 'stupid', 'idiot', 'damn', 'hell', 'rage', 'annoyed'],
'weight': 1.2
},
'surprised': {
'words': ['wow', 'oh', 'what', 'really', 'seriously', 'unbelievable', 'amazing', 'shocked', 'surprised'],
'weight': 0.8
},
'fear': {
'words': ['afraid', 'scared', 'fear', 'terrified', 'nervous', 'worry', 'panic', 'help', 'danger'],
'weight': 1.0
}
}
# Punctuation patterns
self.punctuation_emotions = {
'!': {'surprised': 0.3, 'happy': 0.2, 'angry': 0.2},
'?': {'surprised': 0.4, 'confused': 0.3},
'...': {'sad': 0.3, 'thoughtful': 0.3},
'?!': {'surprised': 0.6},
'!!!': {'angry': 0.4, 'excited': 0.4}
}
def analyze_dialogue(self, text: str) -> Dict[str, float]:
"""Analyze emotion in dialogue text"""
if not text:
return {'neutral': 1.0}
text_lower = text.lower()
emotions = {'neutral': 0.2} # Base neutral score
# Word-based analysis
for emotion, data in self.emotion_words.items():
score = 0
for word in data['words']:
if word in text_lower:
score += data['weight']
if score > 0:
emotions[emotion] = score
# Punctuation analysis
for pattern, emotion_scores in self.punctuation_emotions.items():
if pattern in text:
for emotion, score in emotion_scores.items():
emotions[emotion] = emotions.get(emotion, 0) + score
# Intensity based on caps and punctuation
caps_ratio = sum(1 for c in text if c.isupper()) / len(text) if text else 0
if caps_ratio > 0.5:
emotions['intensity'] = 0.8
else:
emotions['intensity'] = 0.5
# Normalize
emotion_sum = sum(v for k, v in emotions.items() if k != 'intensity')
if emotion_sum > 0:
for k in emotions:
if k != 'intensity':
emotions[k] = emotions[k] / emotion_sum
return emotions
class StoryCondenser:
"""Condense long stories into key moments"""
def __init__(self):
self.min_panels = 10
self.max_panels = 15
def identify_key_moments(self, subtitles: List[srt.Subtitle]) -> List[int]:
"""Identify indices of key story moments"""
if len(subtitles) <= self.max_panels:
return list(range(len(subtitles)))
key_indices = []
# 1. Always include first and last (introduction and conclusion)
key_indices.extend([0, len(subtitles) - 1])
# 2. Identify turning points
turning_points = self._find_turning_points(subtitles)
key_indices.extend(turning_points)
# 3. Find emotional peaks
emotional_peaks = self._find_emotional_peaks(subtitles)
key_indices.extend(emotional_peaks)
# 4. Find action moments
action_moments = self._find_action_moments(subtitles)
key_indices.extend(action_moments)
# Remove duplicates and sort
key_indices = sorted(list(set(key_indices)))
# 5. If too many, select most important
if len(key_indices) > self.max_panels:
key_indices = self._select_most_important(subtitles, key_indices)
# 6. If too few, add transitional moments
if len(key_indices) < self.min_panels:
key_indices = self._add_transitions(subtitles, key_indices)
return sorted(key_indices)[:self.max_panels]
def _find_turning_points(self, subtitles: List[srt.Subtitle]) -> List[int]:
"""Find story turning points"""
turning_words = ['but', 'however', 'suddenly', 'then', 'meanwhile', 'later', 'finally']
indices = []
for i, sub in enumerate(subtitles):
text_lower = sub.content.lower()
if any(word in text_lower for word in turning_words):
indices.append(i)
return indices
def _find_emotional_peaks(self, subtitles: List[srt.Subtitle]) -> List[int]:
"""Find emotional peaks in dialogue"""
analyzer = DialogueEmotionAnalyzer()
emotion_scores = []
for i, sub in enumerate(subtitles):
emotions = analyzer.analyze_dialogue(sub.content)
# Calculate emotional intensity
intensity = max(v for k, v in emotions.items() if k != 'neutral')
emotion_scores.append((i, intensity))
# Sort by intensity and take top moments
emotion_scores.sort(key=lambda x: x[1], reverse=True)
return [idx for idx, score in emotion_scores[:5] if score > 0.5]
def _find_action_moments(self, subtitles: List[srt.Subtitle]) -> List[int]:
"""Find action moments"""
action_words = ['run', 'fight', 'escape', 'attack', 'save', 'help', 'stop', 'go', 'move', 'quick']
indices = []
for i, sub in enumerate(subtitles):
text_lower = sub.content.lower()
if any(word in text_lower for word in action_words):
indices.append(i)
return indices
def _select_most_important(self, subtitles: List[srt.Subtitle], indices: List[int]) -> List[int]:
"""Select most important moments from candidates"""
scored_indices = []
for idx in indices:
score = self._calculate_importance_score(subtitles[idx], idx, len(subtitles))
scored_indices.append((idx, score))
scored_indices.sort(key=lambda x: x[1], reverse=True)
return [idx for idx, score in scored_indices[:self.max_panels]]
def _calculate_importance_score(self, subtitle: srt.Subtitle, index: int, total: int) -> float:
"""Calculate importance score for a subtitle"""
score = 1.0
# Position in story (beginning and end are important)
position_ratio = index / total
if position_ratio < 0.1 or position_ratio > 0.9:
score += 0.5
elif 0.4 < position_ratio < 0.6: # Middle (potential climax)
score += 0.3
# Length (longer usually more important)
word_count = len(subtitle.content.split())
score += min(word_count * 0.1, 0.5)
# Punctuation (excitement)
if '!' in subtitle.content:
score += 0.3
if '?' in subtitle.content:
score += 0.2
return score
def _add_transitions(self, subtitles: List[srt.Subtitle], current_indices: List[int]) -> List[int]:
"""Add transitional moments between key points"""
new_indices = list(current_indices)
# Find largest gaps
gaps = []
for i in range(len(current_indices) - 1):
gap_size = current_indices[i+1] - current_indices[i]
if gap_size > 2:
gaps.append((current_indices[i], current_indices[i+1], gap_size))
# Sort by gap size
gaps.sort(key=lambda x: x[2], reverse=True)
# Add midpoints of largest gaps
for start, end, size in gaps:
if len(new_indices) >= self.min_panels:
break
midpoint = (start + end) // 2
new_indices.append(midpoint)
return sorted(new_indices)
class EmotionAwareComicGenerator:
"""Generate comics with emotion-aware panel selection"""
def __init__(self):
self.face_analyzer = FacialExpressionAnalyzer()
self.dialogue_analyzer = DialogueEmotionAnalyzer()
self.story_condenser = StoryCondenser()
def generate_emotion_comic(self, video_path: str, max_panels: int = 12) -> Dict:
"""Generate comic with emotion-matched panels"""
print("🎭 Generating Emotion-Aware Comic...")
# 1. Load subtitles and frames
subtitles = self._load_subtitles()
all_frames = self._get_all_frames()
if not subtitles or not all_frames:
print("❌ Missing subtitles or frames")
return None
# 2. Identify key story moments
print("📖 Identifying key story moments...")
key_indices = self.story_condenser.identify_key_moments(subtitles)
print(f" Found {len(key_indices)} key moments")
# 3. Match emotions for each moment
print("🎭 Matching facial expressions with dialogue...")
matched_panels = []
for idx in key_indices:
subtitle = subtitles[idx]
# Analyze dialogue emotion
text_emotions = self.dialogue_analyzer.analyze_dialogue(subtitle.content)
# Find best matching frame
best_frame = self._find_best_emotion_match(
subtitle, text_emotions, all_frames, idx, len(subtitles)
)
matched_panels.append({
'subtitle': subtitle,
'frame': best_frame['path'],
'text_emotions': text_emotions,
'face_emotions': best_frame['emotions'],
'match_score': best_frame['score'],
'index': idx
})
# 4. Create comic layout
print("📐 Creating emotion-aware layout...")
comic_data = self._create_emotion_layout(matched_panels)
# 5. Save comic
self._save_emotion_comic(comic_data)
print(f"✅ Emotion-aware comic created with {len(matched_panels)} panels!")
return comic_data
def _find_best_emotion_match(self, subtitle: srt.Subtitle, text_emotions: Dict,
frames: List[str], sub_index: int, total_subs: int) -> Dict:
"""Find frame with best emotion match"""
# Calculate approximate frame range for this subtitle
frame_ratio = sub_index / total_subs
center_frame = int(frame_ratio * len(frames))
# Search window (look at nearby frames)
search_range = 5
start = max(0, center_frame - search_range)
end = min(len(frames), center_frame + search_range + 1)
best_match = {
'path': frames[center_frame] if center_frame < len(frames) else frames[-1],
'emotions': {'neutral': 1.0},
'score': 0
}
# Find best matching frame
for i in range(start, end):
if i >= len(frames):
break
# Analyze facial expression
face_emotions = self.face_analyzer.analyze_expression(frames[i])
# Calculate match score
score = self._calculate_emotion_match_score(text_emotions, face_emotions)
if score > best_match['score']:
best_match = {
'path': frames[i],
'emotions': face_emotions,
'score': score
}
return best_match
def _calculate_emotion_match_score(self, text_emotions: Dict, face_emotions: Dict) -> float:
"""Calculate how well emotions match"""
score = 0
# Compare each emotion
emotions = set(text_emotions.keys()) | set(face_emotions.keys())
for emotion in emotions:
if emotion == 'intensity':
continue
text_score = text_emotions.get(emotion, 0)
face_score = face_emotions.get(emotion, 0)
# Higher score for matching emotions
if text_score > 0.3 and face_score > 0.3:
score += min(text_score, face_score) * 2
else:
# Penalty for mismatch
score -= abs(text_score - face_score) * 0.5
# Bonus for intensity match
text_intensity = text_emotions.get('intensity', 0.5)
face_intensity = face_emotions.get('intensity', 0.5)
if abs(text_intensity - face_intensity) < 0.3:
score += 0.5
return max(0, score)
def _create_emotion_layout(self, panels: List[Dict]) -> Dict:
"""Create layout with emotion-aware styling"""
pages = []
panels_per_page = 4
for i in range(0, len(panels), panels_per_page):
page_panels = panels[i:i+panels_per_page]
page = {
'width': 800,
'height': 600,
'panels': [],
'bubbles': []
}
positions = [
(10, 10, 380, 280),
(410, 10, 380, 280),
(10, 310, 380, 280),
(410, 310, 380, 280)
]
for j, panel_data in enumerate(page_panels):
if j >= 4:
break
x, y, w, h = positions[j]
# Determine dominant emotion
all_emotions = {**panel_data['text_emotions'], **panel_data['face_emotions']}
dominant_emotion = max(all_emotions.items(),
key=lambda x: x[1] if x[0] != 'intensity' else 0)[0]
# Add panel with emotion metadata
page['panels'].append({
'x': x, 'y': y,
'width': w, 'height': h,
'image': panel_data['frame'],
'emotion': dominant_emotion,
'match_score': panel_data['match_score']
})
# Style bubble based on emotion
bubble_style = self._get_emotion_bubble_style(dominant_emotion)
page['bubbles'].append({
'id': f'bubble_{panel_data["index"]}',
'x': x + 20,
'y': y + h - 100, # Position based on emotion
'width': 150,
'height': 70,
'text': panel_data['subtitle'].content,
'style': bubble_style
})
pages.append(page)
return {'pages': pages}
def _get_emotion_bubble_style(self, emotion: str) -> Dict:
"""Get bubble style for emotion"""
styles = {
'happy': {
'shape': 'round',
'border': '#4CAF50',
'background': '#E8F5E9',
'font': 'bold'
},
'sad': {
'shape': 'droopy',
'border': '#2196F3',
'background': '#E3F2FD',
'font': 'italic'
},
'angry': {
'shape': 'jagged',
'border': '#F44336',
'background': '#FFEBEE',
'font': 'bold',
'size': 'large'
},
'surprised': {
'shape': 'burst',
'border': '#FF9800',
'background': '#FFF3E0',
'font': 'bold'
},
'neutral': {
'shape': 'round',
'border': '#333',
'background': '#FFF',
'font': 'normal'
}
}
return styles.get(emotion, styles['neutral'])
def _load_subtitles(self) -> List[srt.Subtitle]:
"""Load subtitles"""
if os.path.exists('test1.srt'):
with open('test1.srt', 'r') as f:
return list(srt.parse(f.read()))
return []
def _get_all_frames(self) -> List[str]:
"""Get all available frames"""
frames_dir = 'frames'
if os.path.exists(frames_dir):
frames = [os.path.join(frames_dir, f) for f in sorted(os.listdir(frames_dir))
if f.endswith('.png')]
return frames
return []
def _save_emotion_comic(self, comic_data: Dict):
"""Save emotion-aware comic"""
os.makedirs('output', exist_ok=True)
# Save JSON
with open('output/emotion_comic.json', 'w') as f:
json.dump(comic_data, f, indent=2)
print("✅ Saved emotion-aware comic to output/emotion_comic.json")
# Test function
def create_emotion_comic(video_path='video/sample.mp4'):
"""Create an emotion-aware comic"""
generator = EmotionAwareComicGenerator()
return generator.generate_emotion_comic(video_path)
if __name__ == "__main__":
create_emotion_comic() |