File size: 7,179 Bytes
cd5aabe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
调试上色效果差异的脚本
"""

import sys
import os
import cv2
import numpy as np

# 添加当前目录到路径
sys.path.insert(0, os.path.dirname(__file__))

from ddcolor_colorizer import DDColorColorizer
from gfpgan_restorer import GFPGANRestorer
import logging

# 设置日志
logging.basicConfig(level=logging.INFO, format='[%(levelname)s] %(message)s')

def simulate_api_processing(image_path):
    """
    模拟API接口的完整处理流程
    """
    print("\n=== 模拟API接口处理流程 ===")
    
    # 初始化组件
    print("初始化GFPGAN修复器...")
    try:
        gfpgan_restorer = GFPGANRestorer()
        if not gfpgan_restorer.is_available():
            print("❌ GFPGAN不可用")
            return None
        print("✅ GFPGAN初始化成功")
    except Exception as e:
        print(f"❌ GFPGAN初始化失败: {e}")
        return None
    
    print("初始化DDColor上色器...")
    try:
        ddcolor_colorizer = DDColorColorizer()
        if not ddcolor_colorizer.is_available():
            print("❌ DDColor不可用")
            return None
        print("✅ DDColor初始化成功")
    except Exception as e:
        print(f"❌ DDColor初始化失败: {e}")
        return None
    
    # 读取图像
    print(f"读取图像: {image_path}")
    image = cv2.imread(image_path)
    if image is None:
        print(f"❌ 无法读取图像: {image_path}")
        return None
    
    print(f"原图尺寸: {image.shape}")
    
    # 保存原图
    ddcolor_colorizer.save_debug_image(image, "api_original")
    
    # 检查原图灰度状态
    original_is_grayscale = ddcolor_colorizer.is_grayscale(image)
    print(f"原图灰度检测: {original_is_grayscale}")
    
    # 新的处理流程:先上色再修复
    # 步骤1: 上色处理
    print("\n步骤1: 上色处理...")
    if original_is_grayscale:
        print("策略: 对原图进行上色")
        colorized_image = ddcolor_colorizer.colorize_image_direct(image)
        ddcolor_colorizer.save_debug_image(colorized_image, "api_colorized")
        strategy = "先上色"
        current_image = colorized_image
    else:
        print("策略: 图像已经是彩色的,跳过上色")
        strategy = "跳过上色"
        current_image = image
    
    # 步骤2: GFPGAN修复
    print("\n步骤2: GFPGAN修复...")
    final_image = gfpgan_restorer.restore_image(current_image)
    print(f"修复后图像尺寸: {final_image.shape}")
    
    # 保存最终结果
    result_path = ddcolor_colorizer.save_debug_image(final_image, "api_final")
    
    strategy += " -> 再修复"
    
    print(f"\n✅ API模拟完成")
    print(f"   - 处理策略: {strategy}")
    print(f"   - 最终结果: {result_path}")
    
    return {
        'original': image,
        'colorized': colorized_image if original_is_grayscale else None,
        'final': final_image,
        'strategy': strategy
    }

def test_direct_colorization(image_path):
    """
    测试直接上色(类似test_ddcolor.py的方式)
    """
    print("\n=== 测试直接上色 ===")
    
    colorizer = DDColorColorizer()
    if not colorizer.is_available():
        print("❌ DDColor不可用")
        return None
    
    # 直接使用URL进行上色(和test_ddcolor.py相同)
    print("使用官方示例URL上色...")
    success, message = colorizer.test_colorization()
    
    if success:
        print(f"✅ URL上色成功: {message}")
    else:
        print(f"❌ URL上色失败: {message}")
    
    # 对本地图像进行直接上色
    print(f"对本地图像直接上色: {image_path}")
    success, message = colorizer.test_local_image(image_path)
    
    if success:
        print(f"✅ 本地图像上色成功: {message}")
    else:
        print(f"❌ 本地图像上色失败: {message}")

def compare_results():
    """
    对比分析结果
    """
    print("\n=== 结果对比分析 ===")
    
    # 列出生成的调试图像
    debug_files = []
    for f in os.listdir("."):
        if f.endswith("_debug.webp"):
            debug_files.append(f)
    
    if debug_files:
        print("生成的调试文件:")
        for f in sorted(debug_files):
            print(f"  - {f}")
        
        print("\n对比建议:")
        print("1. 比较 original_debug.webp 和 api_original_debug.webp")
        print("2. 比较 local_colorized_debug.webp 和 api_final_debug.webp")
        print("3. 检查 api_restored_debug.webp 的修复效果")
        print("4. 观察 ddcolor_test_result.webp 的官方示例效果")
    else:
        print("未找到调试文件")

def analyze_image_quality(image_path):
    """
    分析图像质量指标
    """
    print(f"\n=== 分析图像质量: {image_path} ===")
    
    if not os.path.exists(image_path):
        print(f"文件不存在: {image_path}")
        return
    
    image = cv2.imread(image_path)
    if image is None:
        print(f"无法读取图像: {image_path}")
        return
    
    # 基本信息
    h, w, c = image.shape
    print(f"尺寸: {w}x{h}, 通道数: {c}")
    
    # 亮度分析
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    mean_brightness = np.mean(gray)
    print(f"平均亮度: {mean_brightness:.2f}")
    
    # 对比度分析
    contrast = np.std(gray)
    print(f"对比度(标准差): {contrast:.2f}")
    
    # 色彩分析
    hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
    mean_saturation = np.mean(hsv[:, :, 1])
    print(f"平均饱和度: {mean_saturation:.2f}")
    
    # 锐度分析(拉普拉斯算子)
    laplacian = cv2.Laplacian(gray, cv2.CV_64F)
    sharpness = np.var(laplacian)
    print(f"锐度: {sharpness:.2f}")

def main():
    """主函数"""
    print("DDColor 上色效果调试工具")
    print("=" * 60)
    
    # 可以指定测试图像路径,或使用默认路径
    test_image_path = "/path/to/your/test/image.jpg"  # 替换为实际路径
    
    if len(sys.argv) > 1:
        test_image_path = sys.argv[1]
    
    print(f"测试图像路径: {test_image_path}")
    
    if not os.path.exists(test_image_path):
        print("⚠️  测试图像不存在,将只运行URL测试")
        
        # 只测试直接上色
        test_direct_colorization(None)
        
    else:
        # 分析原图质量
        analyze_image_quality(test_image_path)
        
        # 测试直接上色
        test_direct_colorization(test_image_path)
        
        # 模拟API处理
        api_result = simulate_api_processing(test_image_path)
        
        # 分析结果图像质量
        if os.path.exists("api_final_debug.webp"):
            print("\n--- API处理结果质量分析 ---")
            analyze_image_quality("api_final_debug.webp")
        
        if os.path.exists("local_colorized_debug.webp"):
            print("\n--- 直接上色结果质量分析 ---")
            analyze_image_quality("local_colorized_debug.webp")
    
    # 对比分析
    compare_results()
    
    print("\n调试完成!")
    print("请检查生成的调试图像来识别问题所在。")

if __name__ == "__main__":
    main()