import streamlit as st import os from PIL import Image import torch from manga_translator.translator import MangaTextDetector, process_manga_pages import tempfile import cv2 import io # Initialize session state if 'processed_results' not in st.session_state: st.session_state.processed_results = {} if 'temp_dir' not in st.session_state: st.session_state.temp_dir = tempfile.mkdtemp() # Set page config for wider layout and title st.set_page_config( page_title="Manga Translator", page_icon="🎯", layout="wide", initial_sidebar_state="auto" ) # Custom CSS for centering and background st.markdown(""" """, unsafe_allow_html=True) st.title("🦀 MangaFlow: AI Manga Translator") st.write("Upload a manga page to translate Japanese text to English. Powered by YOLO, Manga OCR, and DeepL.") st.warning("**Note:** Translation accuracy may vary due to the complexity of Japanese text and manga-specific expressions. We're continuously working to improve the system!") with st.expander("📋 Guidelines for Best Results", expanded=False): st.markdown("1. **Image Requirements:**") st.markdown(""" - Clear, high-resolution manga page - Japanese text should be clearly visible - Text bubbles should not be cropped - Supported formats: PNG, JPG, JPEG """) st.markdown("2. **For Best Results:**") st.markdown(""" - Avoid pages with handwritten text - Ensure text bubbles are not overlapping - Image should be properly oriented - Avoid heavily compressed images """) st.markdown("3. **Privacy & Copyright:**") st.markdown(""" - Only upload content you have rights to use - We don't store any uploaded images - All processing is done in real-time """) with st.expander("🔍 How It Works", expanded=False): st.markdown(""" 1. **Text Detection:** Custom YOLO model detects text bubbles 2. **OCR Processing:** Extracts Japanese text 3. **Translation:** Converts to English using DeepL API 4. **Text Insertion:** Places translated text back into the image """) def process_image(uploaded_file): """Process image and store results in session state.""" if uploaded_file.name not in st.session_state.processed_results: try: detector = MangaTextDetector('best.pt') # Save temporary file temp_path = os.path.join(st.session_state.temp_dir, uploaded_file.name) with open(temp_path, "wb") as f: f.write(uploaded_file.getbuffer()) # Process image image, detections, result_image, processed_regions, translated_image = detector.process_image(temp_path) # Store all results in session state st.session_state.processed_results[uploaded_file.name] = { 'image': image, 'detections': detections, 'result_image': result_image, 'processed_regions': processed_regions, 'translated_image': translated_image } return True except Exception as e: st.error(f"❌ Error: {str(e)}") return False return True uploaded_files = st.file_uploader( "Choose manga pages", type=['png', 'jpg', 'jpeg'], accept_multiple_files=True, help="Drag and drop your manga images here. Supported formats: PNG, JPG, JPEG" ) if uploaded_files: with tempfile.TemporaryDirectory() as temp_dir: for uploaded_file in uploaded_files: file_path = os.path.join(temp_dir, uploaded_file.name) with open(file_path, "wb") as f: f.write(uploaded_file.getbuffer()) detector = MangaTextDetector('best.pt') for uploaded_file in uploaded_files: st.subheader(f"Processing: {uploaded_file.name}") col1, col2 = st.columns(2) image_path = os.path.join(temp_dir, uploaded_file.name) original_image = Image.open(image_path) with col1: st.markdown("**Original Image**") st.image(original_image, use_column_width=True) try: image, detections, result_image, processed_regions, translated_image = detector.process_image(image_path) with col2: if translated_image is not None: st.markdown("**Translated Image**") st.image(translated_image, use_column_width=True) else: st.error("No text was detected in this image.") if processed_regions and processed_regions['text_regions']: with st.expander("View Detected Text and Translations"): for i, region in enumerate(processed_regions['text_regions'], 1): st.markdown(f"**Region {i}**") cols = st.columns(2) with cols[0]: st.markdown("Original Text:") st.code(region['text']) with cols[1]: st.markdown("Translation:") st.code(region['translation']) st.markdown("---") except Exception as e: st.error(f"Error processing {uploaded_file.name}: {str(e)}") continue st.markdown("---") else: st.info("👆 Upload a manga page to get started!") st.markdown("""