index.test.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. const expectData = require('./expect.json');
  2. describe('e2e test ocr model', () => {
  3. beforeAll(async () => {
  4. await page.goto(PATH);
  5. });
  6. it('ocr infer and diff test', async () => {
  7. page.on('console', msg => console.log('PAGE LOG:', msg.text()));
  8. const text = await page.evaluate(async () => {
  9. const $ocr = document.querySelector('#ocr');
  10. const ocr = paddlejs['ocr'];
  11. await ocr.init('./models/ch_PP-OCRv2_det_infer', './models/ch_PP-OCRv2_rec_infer');
  12. const res = await ocr.recognize($ocr);
  13. return res.text;
  14. });
  15. // 模型文字识别结果与预期结果diff的字符数
  16. let diffNum = 0;
  17. // 文本框字符串相似度
  18. let similarity = 0;
  19. // 预期字符diff数
  20. const expectedDiffNum = 10;
  21. // 预期文本框字符串相似度
  22. const expecteSimilarity = 0.9;
  23. // 预期文本内容
  24. const expectResult = expectData.text;
  25. expectResult && expectResult.forEach((item, index) => {
  26. const word = text[index];
  27. // 逐字符对比
  28. for(let i = 0; i < item.length; i++) {
  29. if (item[i] !== word[i]) {
  30. console.log('expect: ', item[i], ' word: ', word[i]);
  31. diffNum++;
  32. }
  33. }
  34. // 文本框字符串相似度对比
  35. const s = similar(item, word);
  36. similarity += s;
  37. });
  38. similarity = similarity / expectResult.length;
  39. expect(diffNum).toBeLessThanOrEqual(expectedDiffNum);
  40. expect(similarity).toBeGreaterThanOrEqual(expecteSimilarity);
  41. function similar(string, expect) {
  42. if (!string || !expect) {
  43. return 0;
  44. }
  45. const length = string.length > expect.length ? string.length : expect.length;
  46. const n = string.length;
  47. const m = expect.length;
  48. let data = [];
  49. const min = (a, b, c) => {
  50. return a < b ? (a < c ? a : c) : (b < c ? b : c);
  51. };
  52. let i, j, si, ej, cost;
  53. if (n === 0) return m;
  54. if (m === 0) return n;
  55. for (i = 0; i <= n; i++) {
  56. data[i] = [];
  57. [i][0] = i
  58. }
  59. for (j = 0; j <= m; j++) {
  60. data[0][j] = j;
  61. }
  62. for (i = 1; i <= n; i++) {
  63. si = string.charAt(i - 1);
  64. for (j = 1; j <= m; j++) {
  65. ej = expect.charAt(j - 1);
  66. cost = si === ej ? 0 : 1;
  67. data[i][j] = min(data[i - 1][j] + 1, data[i][j - 1] + 1, data[i - 1][j - 1] + cost);
  68. }
  69. }
  70. return (1 - data[n][m] / length);
  71. }
  72. });
  73. });