File size: 14,985 Bytes
ddd63a2
 
838e8f6
 
 
986650d
ddd63a2
838e8f6
 
 
 
 
 
 
 
 
 
ddd63a2
e219479
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ddd63a2
986650d
838e8f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e219479
 
 
 
 
 
 
838e8f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ddd63a2
986650d
838e8f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ddd63a2
838e8f6
ddd63a2
7b692ae
 
 
 
 
 
 
 
 
 
ddd63a2
 
838e8f6
7b692ae
ddd63a2
 
838e8f6
 
 
 
 
 
ddd63a2
838e8f6
 
 
 
 
ddd63a2
838e8f6
 
 
ddd63a2
838e8f6
 
 
 
 
 
 
 
 
ddd63a2
838e8f6
 
7b692ae
838e8f6
 
 
 
ddd63a2
838e8f6
 
7b692ae
838e8f6
 
 
 
 
 
 
ddd63a2
838e8f6
 
 
 
97b66ab
7b692ae
 
 
97b66ab
 
7b692ae
 
97b66ab
 
7b692ae
 
 
 
 
97b66ab
 
 
7b692ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97b66ab
7b692ae
97b66ab
 
 
7b692ae
 
 
 
97b66ab
 
 
 
7b692ae
 
 
 
97b66ab
 
 
7b692ae
 
 
 
97b66ab
 
 
7b692ae
 
 
97b66ab
 
 
7b692ae
 
 
97b66ab
 
838e8f6
ddd63a2
838e8f6
 
 
 
 
 
 
 
 
845f960
 
 
838e8f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
845f960
838e8f6
 
 
 
 
 
 
 
 
 
 
 
e219479
 
 
838e8f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
845f960
 
838e8f6
 
845f960
 
 
838e8f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ddd63a2
 
7b692ae
838e8f6
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
import gradio as gr
import numpy as np
import os
from PIL import Image
import cv2
import spaces

# Import our custom modules
from core import TextResizer
from prompt_handler import PromptHandler
from utils import (
    load_image,
    save_image,
    validate_scale_factor,
    parse_percentage_to_scale_factor,
    create_output_filename
)

# Initialize the text resizer with GPU support (English only)
text_resizer = TextResizer(languages=['en'], gpu=True)

def find_target_text_in_prompt(user_prompt, ocr_results):
    """
    从用户prompt中智能查找目标文字
    
    Args:
        user_prompt: 用户输入的指令
        ocr_results: OCR识别结果列表
        
    Returns:
        找到的目标文字,如果没找到则返回None
    """
    import re
    
    # 提取所有OCR识别的文字
    ocr_texts = [text.strip() for _, text, _ in ocr_results]
    
    # 1. 首先查找被引号包围的文字 (单引号或双引号)
    quoted_matches = re.findall(r'["\']([^"\']+)["\']', user_prompt)
    for quoted_text in quoted_matches:
        # 在OCR结果中查找完全匹配或部分匹配
        for ocr_text in ocr_texts:
            if quoted_text.lower() == ocr_text.lower():
                return ocr_text
            if quoted_text.lower() in ocr_text.lower() or ocr_text.lower() in quoted_text.lower():
                return ocr_text
    
    # 2. 如果没有引号,尝试查找prompt中包含的OCR文字
    user_prompt_lower = user_prompt.lower()
    for ocr_text in ocr_texts:
        if ocr_text.lower() in user_prompt_lower:
            return ocr_text
    
    # 3. 尝试查找部分匹配的单词
    prompt_words = re.findall(r'\b\w+\b', user_prompt_lower)
    for word in prompt_words:
        if len(word) > 2:  # 忽略太短的单词
            for ocr_text in ocr_texts:
                if word in ocr_text.lower():
                    return ocr_text
    
    return None

@spaces.GPU
def process_image(input_image, user_prompt, use_ai_parsing=True, api_key=None):
    """
    Process image with text resizing based on user prompt
    """
    try:
        if input_image is None:
            return None, "❌ 错误: 请上传一张图片"
        
        # Convert PIL to RGB numpy array
        image_rgb = np.array(input_image.convert('RGB'))
        
        # Perform OCR
        ocr_results = text_resizer.read_text(image_rgb)
        
        if not ocr_results:
            return None, "❌ 错误: 未在图像中识别到任何文字"
        
        # Parse user prompt
        try:
            if use_ai_parsing and api_key:
                # Use OpenAI API parsing
                prompt_handler = PromptHandler(api_key=api_key)
                parsed_result = prompt_handler.parse_user_request(ocr_results, user_prompt)
                
                if not prompt_handler.validate_parsed_result(parsed_result, ocr_results):
                    raise Exception("AI解析结果验证失败")
                
                target_text = parsed_result["target_text"]
                scale_factor = validate_scale_factor(parsed_result["scale_factor"])
                status_msg = f"✅ AI解析成功: 目标文字='{target_text}', 缩放因子={scale_factor}"
                
            else:
                # Use fallback parsing
                scale_factor = parse_percentage_to_scale_factor(user_prompt)
                if scale_factor == 1.0:
                    return None, "❌ 错误: 无法从用户指令中解析出缩放信息"
                
                # Try to find target text from user prompt
                target_text = find_target_text_in_prompt(user_prompt, ocr_results)
                if not target_text:
                    # If no specific text found in prompt, ask user to specify
                    available_texts = [text.strip() for _, text, _ in ocr_results]
                    return None, f"❌ 错误: 无法确定要调整的文字。请在指令中明确指定文字,如 'enlarge \"具体文字\" by 50%'\n\n📝 可用的文字: {available_texts}"
                
                status_msg = f"✅ 备用解析: 目标文字='{target_text}', 缩放因子={scale_factor}"
                
        except Exception as e:
            return None, f"❌ 错误: 指令解析失败: {str(e)}"
        
        # Process the image
        try:
            result_image = text_resizer.resize_text(image_rgb, target_text, scale_factor)
            
            # Convert back to PIL Image
            result_pil = Image.fromarray(result_image)
            
            return result_pil, status_msg
            
        except ValueError as e:
            # Show available texts
            available_texts = [text.strip() for _, text, _ in ocr_results]
            error_msg = f"❌ 错误: {str(e)}\n\n📝 可用的文字: {available_texts}"
            return None, error_msg
            
    except Exception as e:
        return None, f"❌ 处理过程中出现错误: {str(e)}"

@spaces.GPU
def get_ocr_info(input_image):
    """
    Get OCR information from the image
    """
    if input_image is None:
        return "请先上传图片"
    
    try:
        # Convert PIL to RGB numpy array
        image_rgb = np.array(input_image.convert('RGB'))
        
        # Perform OCR
        ocr_results = text_resizer.read_text(image_rgb)
        
        if not ocr_results:
            return "未识别到任何文字"
        
        # Format results
        info = f"📝 识别到 {len(ocr_results)} 个文字区域:\n"
        info += "=" * 50 + "\n"
        for i, (bbox, text, conf) in enumerate(ocr_results):
            info += f"{i+1:2d}. '{text}' (置信度: {conf:.2f})\n"
        info += "=" * 50
        
        return info
        
    except Exception as e:
        return f"❌ OCR识别失败: {str(e)}"

# Define CSS for styling
css = """
/* Global text color fixes - high priority */
body, .gradio-container, .gradio-container * {
    color: #333 !important;
}

/* Force all text elements to have good contrast */
p, div, span, label, input, textarea, button, h1, h2, h3, h4, h5, h6 {
    color: #333 !important;
}

#col-container {
    margin: 0 auto;
    max-width: 1000px;
    color: #333 !important;
}

#input-section {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    padding: 20px;
    border-radius: 15px;
    margin-bottom: 20px;
}

#output-section {
    background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
    padding: 20px;
    border-radius: 15px;
}

.gradio-container {
    background: linear-gradient(135deg, #ffecd2 0%, #fcb69f 100%);
}

#title {
    text-align: center;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    font-size: 2.5em;
    font-weight: bold;
    margin-bottom: 20px;
}

#description {
    text-align: center;
    color: #222 !important;
    font-size: 1.1em;
    margin-bottom: 30px;
    line-height: 1.6;
}

.process-button {
    background: linear-gradient(135deg, #4CAF50 0%, #45a049 100%);
    color: white !important;
    border: none;
    padding: 15px 30px;
    font-size: 16px;
    border-radius: 10px;
    cursor: pointer;
    transition: all 0.3s ease;
}

.process-button:hover {
    transform: translateY(-2px);
    box-shadow: 0 5px 15px rgba(0,0,0,0.2);
}

/* White text for dark gradient sections */
#input-section, #input-section * {
    color: white !important;
}

#output-section, #output-section * {
    color: white !important;
}

/* Override Gradio's default text colors */
.gradio-container .gr-markdown p,
.gradio-container .gr-markdown div,
.gradio-container .gr-markdown span,
.gradio-container .gr-markdown li {
    color: #333 !important;
}

#input-section .gr-markdown p,
#input-section .gr-markdown div,
#input-section .gr-markdown span,
#input-section .gr-markdown li {
    color: white !important;
}

#output-section .gr-markdown p,
#output-section .gr-markdown div,
#output-section .gr-markdown span,
#output-section .gr-markdown li {
    color: white !important;
}

/* Force all labels and form elements to have proper contrast */
label, .gr-form label, .gr-textbox label, .gr-button, .gr-checkbox label {
    color: #333 !important;
    font-weight: 500;
}

#input-section label,
#input-section .gr-form label,
#input-section .gr-textbox label,
#input-section .gr-button,
#input-section .gr-checkbox label {
    color: white !important;
}

#output-section label,
#output-section .gr-form label,
#output-section .gr-textbox label,
#output-section .gr-button,
#output-section .gr-checkbox label {
    color: white !important;
}

/* Additional fallback for any missed text elements */
.gradio-container [class*="text"], 
.gradio-container [class*="label"], 
.gradio-container [class*="markdown"] {
    color: #333 !important;
}

#input-section [class*="text"], 
#input-section [class*="label"], 
#input-section [class*="markdown"] {
    color: white !important;
}

#output-section [class*="text"], 
#output-section [class*="label"], 
#output-section [class*="markdown"] {
    color: white !important;
}
"""

# Create the Gradio interface
with gr.Blocks(css=css, title="智能文字缩放工具") as demo:
    with gr.Column(elem_id="col-container"):
        gr.Markdown("# 🎨 智能文字缩放工具", elem_id="title")
        gr.Markdown(
            """
            🚀 **使用AI技术智能调整图片中的文字大小** 
            
            📝 支持自然语言指令,如:
            - enlarge 'Hello' by 50% - 将'Hello'放大50%
            - make the title bigger - 让标题变大
            - shrink the footer text - 缩小页脚文字
            
            🎯 **使用方法**:
            1. 上传包含文字的图片
            2. 输入文字调整指令
            3. 点击处理按钮
            4. 查看处理结果
            """,
            elem_id="description"
        )
        
        with gr.Row():
            with gr.Column(scale=1):
                with gr.Group(elem_id="input-section"):
                    gr.Markdown("### 📤 输入设置")
                    
                    # Image input
                    input_image = gr.Image(
                        label="上传图片",
                        type="pil",
                        height=300,
                        sources=["upload", "clipboard", "webcam"]
                    )
                    
                    # Prompt input
                    user_prompt = gr.Textbox(
                        label="文字调整指令",
                        placeholder="例如: enlarge 'Hello' by 50%",
                        lines=2,
                        info="支持自然语言描述,如 make XX bigger 或 enlarge XX by 50%"
                    )
                    
                    # OCR info button
                    ocr_button = gr.Button(
                        "🔍 查看图片中的文字",
                        variant="secondary",
                        size="sm"
                    )
                    
                    # Advanced settings
                    with gr.Accordion("⚙️ 高级设置", open=False):
                        use_ai_parsing = gr.Checkbox(
                            label="🤖 使用AI解析 (推荐,需要OpenAI API密钥)",
                            value=True,
                            info="使用GPT-4.1-nano模型智能理解自然语言指令"
                        )
                        
                        api_key = gr.Textbox(
                            label="🔑 OpenAI API密钥 (可选)",
                            placeholder="sk-...",
                            type="password",
                            info="仅在使用AI解析时需要"
                        )
                    
                    # Process button
                    process_button = gr.Button(
                        "🎯 开始处理",
                        variant="primary",
                        size="lg",
                        elem_classes="process-button"
                    )
            
            with gr.Column(scale=1):
                with gr.Group(elem_id="output-section"):
                    gr.Markdown("### 📤 处理结果")
                    
                    # Output image
                    output_image = gr.Image(
                        label="处理后的图片",
                        height=300,
                        show_download_button=True
                    )
                    
                    # Status message
                    status_message = gr.Textbox(
                        label="💬 状态信息",
                        lines=4,
                        max_lines=8,
                        interactive=False
                    )
                    
                    # OCR info display
                    ocr_info = gr.Textbox(
                        label="📝 OCR识别结果",
                        lines=6,
                        max_lines=10,
                        interactive=False
                    )
        
        # Examples section
        gr.Markdown("### 📚 示例用法")
        gr.Markdown(
            """
            **示例指令格式:**
            
            🔍 **指定文字 + 具体比例:**
            - enlarge 'Hello' by 50% - 将'Hello'放大50%
            - shrink 'Title' by 30% - 将'Title'缩小30%
            
            🎯 **自然语言描述:**
            - make the title bigger - 让标题变大
            - make the text smaller - 让文字变小
            - enlarge the heading - 放大标题
            
            💡 **使用提示:**
            1. 上传包含文字的图片
            2. 先点击"查看图片中的文字"了解可用文字
            3. 输入调整指令
            4. 点击"开始处理"
            """
        )
        
        # Event handlers
        process_button.click(
            fn=process_image,
            inputs=[input_image, user_prompt, use_ai_parsing, api_key],
            outputs=[output_image, status_message]
        )
        
        ocr_button.click(
            fn=get_ocr_info,
            inputs=[input_image],
            outputs=[ocr_info]
        )
        
        # Auto-run OCR when image is uploaded
        input_image.change(
            fn=get_ocr_info,
            inputs=[input_image],
            outputs=[ocr_info]
        )
        
        # Footer
        gr.Markdown(
            """
            ---
            
            🎨 **智能文字缩放工具** | 基于OCR和AI技术的智能图像文字处理
            
            📧 如有问题或建议,请联系开发者
            """
        )

if __name__ == "__main__":
    # Fixed text contrast issues - force redeploy
    demo.launch(share=True, server_name="0.0.0.0", server_port=7860)