test_rapidtable.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import unittest
  2. from PIL import Image
  3. from lxml import etree
  4. from magic_pdf.model.sub_modules.model_init import AtomModelSingleton
  5. from magic_pdf.model.sub_modules.table.rapidtable.rapid_table import RapidTableModel
  6. class TestppTableModel(unittest.TestCase):
  7. def test_image2html(self):
  8. img = Image.open("assets/table.jpg")
  9. atom_model_manager = AtomModelSingleton()
  10. ocr_engine = atom_model_manager.get_atom_model(
  11. atom_model_name='ocr',
  12. ocr_show_log=False,
  13. det_db_box_thresh=0.5,
  14. det_db_unclip_ratio=1.6,
  15. lang='ch'
  16. )
  17. table_model = RapidTableModel(ocr_engine, 'slanet_plus')
  18. html_code, table_cell_bboxes, logic_points, elapse = table_model.predict(img)
  19. # 验证生成的 HTML 是否符合预期
  20. parser = etree.HTMLParser()
  21. tree = etree.fromstring(html_code, parser)
  22. # 检查 HTML 结构
  23. assert tree.find('.//table') is not None, "HTML should contain a <table> element"
  24. assert tree.find('.//tr') is not None, "HTML should contain a <tr> element"
  25. assert tree.find('.//td') is not None, "HTML should contain a <td> element"
  26. # 检查具体的表格内容
  27. headers = tree.xpath('//table/tr[1]/td')
  28. assert len(headers) == 5, "Thead should have 5 columns"
  29. assert headers[0].text and headers[0].text.strip() == "Methods", "First header should be 'Methods'"
  30. assert headers[1].text and headers[1].text.strip() == "R", "Second header should be 'R'"
  31. assert headers[2].text and headers[2].text.strip() == "P", "Third header should be 'P'"
  32. assert headers[3].text and headers[3].text.strip() == "F", "Fourth header should be 'F'"
  33. assert headers[4].text and headers[4].text.strip() == "FPS", "Fifth header should be 'FPS'"
  34. # 检查第一行数据
  35. first_row = tree.xpath('//table/tr[2]/td')
  36. assert len(first_row) == 5, "First row should have 5 cells"
  37. assert first_row[0].text and first_row[0].text.strip() == "SegLink[26]", "First cell should be 'SegLink[26]'"
  38. assert first_row[1].text and first_row[1].text.strip() == "70.0", "Second cell should be '70.0'"
  39. assert first_row[2].text and first_row[2].text.strip() == "86.0", "Third cell should be '86.0'"
  40. assert first_row[3].text and first_row[3].text.strip() == "77.0", "Fourth cell should be '77.0'"
  41. assert first_row[4].text and first_row[4].text.strip() == "8.9", "Fifth cell should be '8.9'"
  42. # 检查倒数第二行数据
  43. second_last_row = tree.xpath('//table/tr[position()=last()-1]/td')
  44. assert len(second_last_row) == 5, "second_last_row should have 5 cells"
  45. assert second_last_row[0].text and second_last_row[0].text.strip() == "Ours (SynText)", "First cell should be 'Ours (SynText)'"
  46. assert second_last_row[1].text and second_last_row[1].text.strip() == "80.68", "Second cell should be '80.68'"
  47. assert second_last_row[2].text and second_last_row[2].text.strip() == "85.40", "Third cell should be '85.40'"
  48. # assert second_last_row[3].text and second_last_row[3].text.strip() == "82.97", "Fourth cell should be '82.97'"
  49. # assert second_last_row[3].text and second_last_row[4].text.strip() == "12.68", "Fifth cell should be '12.68'"
  50. if __name__ == "__main__":
  51. unittest.main()