@operato/ocr 0.2.19 → 0.2.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,17 +1,9 @@
1
1
  import { css, html, LitElement, PropertyValues } from 'lit'
2
2
  import { customElement, property, query, state } from 'lit/decorators.js'
3
3
 
4
+ import { NOTHING_RESULT, ScanResult } from './type'
4
5
  import { touch2mouse } from './utils/touch-to-mouse-event'
5
6
 
6
- type ScanResult = {
7
- status_code: number
8
- key_values: {
9
- [key: string]: Array<string>
10
- }
11
- boundaries: Array<Array<{ x: number; y: number }>>
12
- texts: Array<string>
13
- }
14
-
15
7
  @customElement('ox-ocr-result-image')
16
8
  export class OcrResultImage extends LitElement {
17
9
  static styles = [
@@ -60,9 +52,9 @@ export class OcrResultImage extends LitElement {
60
52
  ]
61
53
 
62
54
  @property({ type: Array }) file: any
63
- @property({ type: Array }) result: ScanResult = RESULT
55
+ @property({ type: Array }) result: ScanResult = NOTHING_RESULT
64
56
 
65
- @state() _ratio!: { x: number; y: number }
57
+ @state() _ratio: { x: number; y: number } = { x: 1, y: 1 }
66
58
  @state() _image!: string
67
59
  @state() _resizeObserver!: ResizeObserver
68
60
  @state() _trails: Array<Array<{ x: number; y: number }>> = []
@@ -75,7 +67,7 @@ export class OcrResultImage extends LitElement {
75
67
 
76
68
  render() {
77
69
  return html`
78
- <img src=${this._image} />
70
+ <img src=${this._image} @load=${() => this.calculateRatio()} />
79
71
  <canvas overlay></canvas>
80
72
 
81
73
  ${this._text ? html` <div notice>${this._text} copied!</div> ` : html``}
@@ -105,8 +97,8 @@ export class OcrResultImage extends LitElement {
105
97
  .map((bounds): Array<{ x: number; y: number }> => {
106
98
  return bounds.map(bound => {
107
99
  return {
108
- x: bound.x * this._ratio.x,
109
- y: bound.y * this._ratio.y
100
+ x: bound.x, //* this._ratio.x,
101
+ y: bound.y // * this._ratio.y
110
102
  }
111
103
  })
112
104
  })
@@ -151,14 +143,19 @@ export class OcrResultImage extends LitElement {
151
143
 
152
144
  async calculateRatio() {
153
145
  const { naturalWidth, naturalHeight, offsetWidth, offsetHeight } = this._img
146
+ const { offsetWidth: overlayWidth, offsetHeight: overlayHeight } = this._overlay
154
147
 
155
- this._overlay.setAttribute('width', String(offsetWidth))
156
- this._overlay.setAttribute('height', String(offsetHeight))
148
+ if (!naturalWidth || !naturalHeight) {
149
+ return
150
+ }
157
151
 
158
152
  this._ratio = {
159
153
  x: offsetWidth / naturalWidth,
160
154
  y: offsetHeight / naturalHeight
161
155
  }
156
+
157
+ this._overlay.setAttribute('width', String(overlayWidth / this._ratio.x))
158
+ this._overlay.setAttribute('height', String(overlayHeight / this._ratio.y))
162
159
  }
163
160
 
164
161
  async connectedCallback() {
@@ -188,23 +185,33 @@ export class OcrResultImage extends LitElement {
188
185
  this._resizeObserver.disconnect()
189
186
  }
190
187
 
188
+ reset() {
189
+ this._selections = []
190
+ this._trails = []
191
+ this._text = ''
192
+ }
193
+
191
194
  async updated(changes: PropertyValues<this>) {
195
+ if (changes.has('file') || changes.has('result')) {
196
+ this.reset()
197
+ }
198
+
192
199
  if (changes.has('file')) {
193
200
  this._image = this.file && URL.createObjectURL(this.file)
194
201
  if (this._image) {
195
- requestAnimationFrame(() => URL.revokeObjectURL(this._image))
202
+ requestAnimationFrame(() => {
203
+ URL.revokeObjectURL(this._image)
204
+ })
205
+
206
+ await this.updateComplete
207
+ this.calculateRatio()
196
208
  }
197
209
  }
198
210
 
199
- await this.updateComplete
200
-
201
211
  this.renderOverlay()
202
212
  }
203
213
 
204
214
  findIndex(x: number, y: number): number {
205
- x /= this._ratio.x
206
- y /= this._ratio.y
207
-
208
215
  const { boundaries } = this.result
209
216
 
210
217
  return boundaries.findIndex(path => {
@@ -229,8 +236,7 @@ export class OcrResultImage extends LitElement {
229
236
 
230
237
  updateTrails(point: { x: number; y: number }, started: boolean, attached?: boolean) {
231
238
  if (started && !attached) {
232
- this._selections = []
233
- this._trails = []
239
+ this.reset()
234
240
  }
235
241
 
236
242
  if (started) {
@@ -238,6 +244,9 @@ export class OcrResultImage extends LitElement {
238
244
  }
239
245
 
240
246
  var trail = this._trails[this._trails.length - 1]
247
+ if (!trail) {
248
+ return
249
+ }
241
250
 
242
251
  trail.push(point)
243
252
 
@@ -263,7 +272,7 @@ export class OcrResultImage extends LitElement {
263
272
  }
264
273
 
265
274
  this._tracking = true
266
- this.updateTrails({ x, y }, true, shiftKey)
275
+ this.updateTrails({ x: x / this._ratio.x, y: y / this._ratio.y }, true, shiftKey)
267
276
  }
268
277
 
269
278
  onTrailEnd(e: MouseEvent) {
@@ -289,666 +298,6 @@ export class OcrResultImage extends LitElement {
289
298
  return
290
299
  }
291
300
 
292
- this.updateTrails({ x, y }, false)
301
+ this.updateTrails({ x: x / this._ratio.x, y: y / this._ratio.y }, false)
293
302
  }
294
303
  }
295
-
296
- const RESULT = {
297
- status_code: 200,
298
- key_values: {
299
- '1P': ['LNK304PG'],
300
- Q: ['259', '311', '430'],
301
- '9D': ['2139', '2139', '2135'],
302
- '1T': ['BA268F', 'BA268C', 'BC488D']
303
- },
304
- boundaries: [
305
- [
306
- {
307
- x: 217.0,
308
- y: 117.0
309
- },
310
- {
311
- x: 291.0,
312
- y: 120.0
313
- },
314
- {
315
- x: 291.0,
316
- y: 143.0
317
- },
318
- {
319
- x: 216.0,
320
- y: 140.0
321
- }
322
- ],
323
- [
324
- {
325
- x: 294.0,
326
- y: 120.0
327
- },
328
- {
329
- x: 442.0,
330
- y: 129.0
331
- },
332
- {
333
- x: 441.0,
334
- y: 151.0
335
- },
336
- {
337
- x: 292.0,
338
- y: 143.0
339
- }
340
- ],
341
- [
342
- {
343
- x: 443.0,
344
- y: 130.0
345
- },
346
- {
347
- x: 484.0,
348
- y: 130.0
349
- },
350
- {
351
- x: 484.0,
352
- y: 151.0
353
- },
354
- {
355
- x: 443.0,
356
- y: 151.0
357
- }
358
- ],
359
- [
360
- {
361
- x: 549.0,
362
- y: 134.0
363
- },
364
- {
365
- x: 642.0,
366
- y: 138.0
367
- },
368
- {
369
- x: 642.0,
370
- y: 157.0
371
- },
372
- {
373
- x: 548.0,
374
- y: 152.0
375
- }
376
- ],
377
- [
378
- {
379
- x: 255.0,
380
- y: 163.0
381
- },
382
- {
383
- x: 390.0,
384
- y: 167.0
385
- },
386
- {
387
- x: 389.0,
388
- y: 191.0
389
- },
390
- {
391
- x: 254.0,
392
- y: 187.0
393
- }
394
- ],
395
- [
396
- {
397
- x: 212.0,
398
- y: 208.0
399
- },
400
- {
401
- x: 255.0,
402
- y: 208.0
403
- },
404
- {
405
- x: 255.0,
406
- y: 235.0
407
- },
408
- {
409
- x: 212.0,
410
- y: 235.0
411
- }
412
- ],
413
- [
414
- {
415
- x: 257.0,
416
- y: 210.0
417
- },
418
- {
419
- x: 320.0,
420
- y: 210.0
421
- },
422
- {
423
- x: 320.0,
424
- y: 232.0
425
- },
426
- {
427
- x: 257.0,
428
- y: 232.0
429
- }
430
- ],
431
- [
432
- {
433
- x: 383.0,
434
- y: 214.0
435
- },
436
- {
437
- x: 480.0,
438
- y: 218.0
439
- },
440
- {
441
- x: 480.0,
442
- y: 240.0
443
- },
444
- {
445
- x: 383.0,
446
- y: 236.0
447
- }
448
- ],
449
- [
450
- {
451
- x: 215.0,
452
- y: 301.0
453
- },
454
- {
455
- x: 382.0,
456
- y: 301.0
457
- },
458
- {
459
- x: 382.0,
460
- y: 325.0
461
- },
462
- {
463
- x: 215.0,
464
- y: 325.0
465
- }
466
- ],
467
- [
468
- {
469
- x: 436.0,
470
- y: 305.0
471
- },
472
- {
473
- x: 539.0,
474
- y: 305.0
475
- },
476
- {
477
- x: 539.0,
478
- y: 327.0
479
- },
480
- {
481
- x: 436.0,
482
- y: 327.0
483
- }
484
- ],
485
- [
486
- {
487
- x: 537.0,
488
- y: 308.0
489
- },
490
- {
491
- x: 582.0,
492
- y: 308.0
493
- },
494
- {
495
- x: 582.0,
496
- y: 328.0
497
- },
498
- {
499
- x: 537.0,
500
- y: 328.0
501
- }
502
- ],
503
- [
504
- {
505
- x: 594.0,
506
- y: 309.0
507
- },
508
- {
509
- x: 624.0,
510
- y: 309.0
511
- },
512
- {
513
- x: 624.0,
514
- y: 334.0
515
- },
516
- {
517
- x: 594.0,
518
- y: 334.0
519
- }
520
- ],
521
- [
522
- {
523
- x: 624.0,
524
- y: 310.0
525
- },
526
- {
527
- x: 659.0,
528
- y: 310.0
529
- },
530
- {
531
- x: 659.0,
532
- y: 330.0
533
- },
534
- {
535
- x: 624.0,
536
- y: 330.0
537
- }
538
- ],
539
- [
540
- {
541
- x: 675.0,
542
- y: 311.0
543
- },
544
- {
545
- x: 707.0,
546
- y: 311.0
547
- },
548
- {
549
- x: 707.0,
550
- y: 330.0
551
- },
552
- {
553
- x: 675.0,
554
- y: 330.0
555
- }
556
- ],
557
- [
558
- {
559
- x: 735.0,
560
- y: 342.0
561
- },
562
- {
563
- x: 775.0,
564
- y: 342.0
565
- },
566
- {
567
- x: 775.0,
568
- y: 360.0
569
- },
570
- {
571
- x: 735.0,
572
- y: 360.0
573
- }
574
- ],
575
- [
576
- {
577
- x: 806.0,
578
- y: 341.0
579
- },
580
- {
581
- x: 829.0,
582
- y: 341.0
583
- },
584
- {
585
- x: 829.0,
586
- y: 359.0
587
- },
588
- {
589
- x: 806.0,
590
- y: 359.0
591
- }
592
- ],
593
- [
594
- {
595
- x: 217.0,
596
- y: 386.0
597
- },
598
- {
599
- x: 381.0,
600
- y: 383.0
601
- },
602
- {
603
- x: 381.0,
604
- y: 407.0
605
- },
606
- {
607
- x: 217.0,
608
- y: 410.0
609
- }
610
- ],
611
- [
612
- {
613
- x: 436.0,
614
- y: 387.0
615
- },
616
- {
617
- x: 536.0,
618
- y: 387.0
619
- },
620
- {
621
- x: 536.0,
622
- y: 410.0
623
- },
624
- {
625
- x: 436.0,
626
- y: 410.0
627
- }
628
- ],
629
- [
630
- {
631
- x: 535.0,
632
- y: 388.0
633
- },
634
- {
635
- x: 577.0,
636
- y: 388.0
637
- },
638
- {
639
- x: 577.0,
640
- y: 407.0
641
- },
642
- {
643
- x: 535.0,
644
- y: 407.0
645
- }
646
- ],
647
- [
648
- {
649
- x: 590.0,
650
- y: 387.0
651
- },
652
- {
653
- x: 617.0,
654
- y: 387.0
655
- },
656
- {
657
- x: 617.0,
658
- y: 411.0
659
- },
660
- {
661
- x: 590.0,
662
- y: 411.0
663
- }
664
- ],
665
- [
666
- {
667
- x: 618.0,
668
- y: 388.0
669
- },
670
- {
671
- x: 653.0,
672
- y: 388.0
673
- },
674
- {
675
- x: 653.0,
676
- y: 409.0
677
- },
678
- {
679
- x: 618.0,
680
- y: 409.0
681
- }
682
- ],
683
- [
684
- {
685
- x: 669.0,
686
- y: 389.0
687
- },
688
- {
689
- x: 702.0,
690
- y: 389.0
691
- },
692
- {
693
- x: 702.0,
694
- y: 408.0
695
- },
696
- {
697
- x: 669.0,
698
- y: 408.0
699
- }
700
- ],
701
- [
702
- {
703
- x: 728.0,
704
- y: 416.0
705
- },
706
- {
707
- x: 769.0,
708
- y: 416.0
709
- },
710
- {
711
- x: 769.0,
712
- y: 436.0
713
- },
714
- {
715
- x: 728.0,
716
- y: 436.0
717
- }
718
- ],
719
- [
720
- {
721
- x: 798.0,
722
- y: 416.0
723
- },
724
- {
725
- x: 822.0,
726
- y: 416.0
727
- },
728
- {
729
- x: 822.0,
730
- y: 433.0
731
- },
732
- {
733
- x: 798.0,
734
- y: 433.0
735
- }
736
- ],
737
- [
738
- {
739
- x: 219.0,
740
- y: 465.0
741
- },
742
- {
743
- x: 381.0,
744
- y: 460.0
745
- },
746
- {
747
- x: 382.0,
748
- y: 482.0
749
- },
750
- {
751
- x: 220.0,
752
- y: 487.0
753
- }
754
- ],
755
- [
756
- {
757
- x: 434.0,
758
- y: 463.0
759
- },
760
- {
761
- x: 534.0,
762
- y: 461.0
763
- },
764
- {
765
- x: 534.0,
766
- y: 481.0
767
- },
768
- {
769
- x: 435.0,
770
- y: 483.0
771
- }
772
- ],
773
- [
774
- {
775
- x: 532.0,
776
- y: 462.0
777
- },
778
- {
779
- x: 574.0,
780
- y: 462.0
781
- },
782
- {
783
- x: 574.0,
784
- y: 479.0
785
- },
786
- {
787
- x: 532.0,
788
- y: 479.0
789
- }
790
- ],
791
- [
792
- {
793
- x: 587.0,
794
- y: 461.0
795
- },
796
- {
797
- x: 614.0,
798
- y: 461.0
799
- },
800
- {
801
- x: 614.0,
802
- y: 482.0
803
- },
804
- {
805
- x: 587.0,
806
- y: 482.0
807
- }
808
- ],
809
- [
810
- {
811
- x: 616.0,
812
- y: 461.0
813
- },
814
- {
815
- x: 650.0,
816
- y: 463.0
817
- },
818
- {
819
- x: 649.0,
820
- y: 481.0
821
- },
822
- {
823
- x: 615.0,
824
- y: 479.0
825
- }
826
- ],
827
- [
828
- {
829
- x: 664.0,
830
- y: 461.0
831
- },
832
- {
833
- x: 697.0,
834
- y: 461.0
835
- },
836
- {
837
- x: 697.0,
838
- y: 479.0
839
- },
840
- {
841
- x: 664.0,
842
- y: 479.0
843
- }
844
- ],
845
- [
846
- {
847
- x: 723.0,
848
- y: 486.0
849
- },
850
- {
851
- x: 761.0,
852
- y: 486.0
853
- },
854
- {
855
- x: 761.0,
856
- y: 504.0
857
- },
858
- {
859
- x: 723.0,
860
- y: 504.0
861
- }
862
- ],
863
- [
864
- {
865
- x: 792.0,
866
- y: 485.0
867
- },
868
- {
869
- x: 814.0,
870
- y: 485.0
871
- },
872
- {
873
- x: 814.0,
874
- y: 500.0
875
- },
876
- {
877
- x: 792.0,
878
- y: 500.0
879
- }
880
- ],
881
- [
882
- {
883
- x: 586.0,
884
- y: 535.0
885
- },
886
- {
887
- x: 626.0,
888
- y: 535.0
889
- },
890
- {
891
- x: 626.0,
892
- y: 551.0
893
- },
894
- {
895
- x: 586.0,
896
- y: 551.0
897
- }
898
- ],
899
- [
900
- {
901
- x: 664.0,
902
- y: 533.0
903
- },
904
- {
905
- x: 700.0,
906
- y: 533.0
907
- },
908
- {
909
- x: 700.0,
910
- y: 548.0
911
- },
912
- {
913
- x: 664.0,
914
- y: 548.0
915
- }
916
- ]
917
- ],
918
- texts: [
919
- 'POWER',
920
- 'INTEGRATIONS',
921
- 'INC.',
922
- '15/10/2021',
923
- 'LinkSwitchTM',
924
- '(1P)',
925
- 'PROD:',
926
- 'LNK304PG',
927
- '(1T)LotNo:BA268F',
928
- '(9D)DtCode:',
929
- '2139',
930
- '(Q)',
931
- 'Qty:',
932
- '259',
933
- 'COO:',
934
- 'CN',
935
- '(1T)LotNo:BA268C',
936
- '(9D)DtCode:',
937
- '2139',
938
- '(Q)',
939
- 'Qty:',
940
- '311',
941
- 'COO:',
942
- 'CN',
943
- '(1T)LotNo:BC488D',
944
- '(9D)DtCode:',
945
- '2135',
946
- '(Q)',
947
- 'Qty:',
948
- '430',
949
- 'COO:',
950
- 'CN',
951
- 'Total',
952
- '1000'
953
- ]
954
- }