test-step6.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /**
  2. * 测试步骤6:检查格子可视化图片生成
  3. */
  4. import fs from 'fs';
  5. import path from 'path';
  6. import { fileURLToPath } from 'url';
  7. import { start as startCheckReg } from '../check-reg.js';
  8. const __filename = fileURLToPath(import.meta.url);
  9. const __dirname = path.dirname(__filename);
  10. function getProjectRoot() {
  11. return path.join(__dirname, '..', '..');
  12. }
  13. async function testStep6() {
  14. try {
  15. const projectRoot = getProjectRoot();
  16. const imagePath = path.join(
  17. projectRoot,
  18. 'static',
  19. '漫画',
  20. 'image',
  21. '鬼-巷第001卷',
  22. '第一章',
  23. '0004_鬼-巷第001卷.jpeg'
  24. );
  25. const tmpDir = path.join(
  26. projectRoot,
  27. 'static',
  28. '漫画',
  29. 'image',
  30. '鬼-巷第001卷',
  31. '第一章',
  32. 'test',
  33. 'tmp'
  34. );
  35. const textMaskPath = path.join(tmpDir, '0004_鬼-巷第001卷_text_mask.png');
  36. if (!fs.existsSync(imagePath)) {
  37. throw new Error(`图片不存在: ${imagePath}`);
  38. }
  39. if (!fs.existsSync(textMaskPath)) {
  40. throw new Error(`文字遮罩图不存在: ${textMaskPath}`);
  41. }
  42. console.log('='.repeat(60));
  43. console.log('🧪 测试步骤6:生成格子可视化图片(带绿色线框)');
  44. console.log('='.repeat(60));
  45. console.log(`📷 图片路径: ${imagePath}`);
  46. console.log(`📖 文字遮罩图: ${textMaskPath}`);
  47. console.log(`📂 输出目录: ${tmpDir}`);
  48. // 调用check-reg.js
  49. const panelResult = await startCheckReg(imagePath, tmpDir, textMaskPath, projectRoot);
  50. // 检查生成的图片
  51. const imageName = path.basename(imagePath, path.extname(imagePath));
  52. const panelVisualPath = panelResult.panelVisualPath || path.join(tmpDir, `${imageName}_panel_visual.png`);
  53. const panelMaskPath = panelResult.panelMaskPath || path.join(tmpDir, `${imageName}_panel_mask.png`);
  54. console.log('\n' + '='.repeat(60));
  55. console.log('📊 检查生成的文件');
  56. console.log('='.repeat(60));
  57. // 检查格子遮罩图
  58. if (fs.existsSync(panelMaskPath)) {
  59. const stats = fs.statSync(panelMaskPath);
  60. console.log(`✅ 格子遮罩图: ${path.basename(panelMaskPath)} (${(stats.size / 1024).toFixed(2)} KB)`);
  61. } else {
  62. console.log(`❌ 格子遮罩图不存在: ${panelMaskPath}`);
  63. }
  64. // 检查格子可视化图片(带绿色线框)
  65. if (fs.existsSync(panelVisualPath)) {
  66. const stats = fs.statSync(panelVisualPath);
  67. console.log(`✅ 格子可视化图片(带绿色线框): ${path.basename(panelVisualPath)} (${(stats.size / 1024).toFixed(2)} KB)`);
  68. console.log(` 📍 文件路径: ${panelVisualPath}`);
  69. console.log(` ✅ 图片已成功生成,包含绿色线框标记`);
  70. } else {
  71. console.log(`❌ 格子可视化图片不存在: ${panelVisualPath}`);
  72. console.log(` ⚠️ 请检查draw_panels.py脚本是否正常执行`);
  73. }
  74. // 检查panels数据
  75. if (panelResult.panels && panelResult.panels.length > 0) {
  76. console.log(`\n📊 检测到 ${panelResult.panels.length} 个格子:`);
  77. panelResult.panels.forEach((panel, index) => {
  78. console.log(` ${index + 1}. 格子 ${index + 1}: x=${panel.x}, y=${panel.y}, width=${panel.width}, height=${panel.height}`);
  79. });
  80. }
  81. console.log('\n' + '='.repeat(60));
  82. console.log('✅ 测试完成!');
  83. console.log('='.repeat(60));
  84. return {
  85. success: fs.existsSync(panelVisualPath),
  86. panelVisualPath: panelVisualPath,
  87. panelMaskPath: panelMaskPath,
  88. panels: panelResult.panels || []
  89. };
  90. } catch (error) {
  91. console.error('\n❌ 测试失败:', error.message);
  92. if (error.stack) {
  93. console.error(error.stack);
  94. }
  95. throw error;
  96. }
  97. }
  98. // 执行测试
  99. testStep6();
  100. export { testStep6 };