Spaces:
Paused
Paused
| #!/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() |