|
5 | 5 | import numpy as np |
6 | 6 | import pytesseract |
7 | 7 | from pytesseract import Output |
8 | | - |
9 | | - |
10 | | -def convertNormalizedImage2Mat(normalized_image): |
11 | | - ba = bytearray(normalized_image.get_bytes()) |
12 | | - width = normalized_image.get_width() |
13 | | - height = normalized_image.get_height() |
14 | | - |
15 | | - channels = 3 |
16 | | - if normalized_image.get_image_pixel_format() == EnumImagePixelFormat.IPF_BINARY: |
17 | | - channels = 1 |
18 | | - all = [] |
19 | | - skip = normalized_image.stride * 8 - width |
20 | | - |
21 | | - index = 0 |
22 | | - n = 1 |
23 | | - for byte in ba: |
24 | | - |
25 | | - byteCount = 7 |
26 | | - while byteCount >= 0: |
27 | | - b = (byte & (1 << byteCount)) >> byteCount |
28 | | - |
29 | | - if index < normalized_image.stride * 8 * n - skip: |
30 | | - if b == 1: |
31 | | - all.append(255) |
32 | | - else: |
33 | | - all.append(0) |
34 | | - |
35 | | - byteCount -= 1 |
36 | | - index += 1 |
37 | | - |
38 | | - if index == normalized_image.stride * 8 * n: |
39 | | - n += 1 |
40 | | - |
41 | | - mat = np.array(all, dtype=np.uint8).reshape(height, width, channels) |
42 | | - return mat |
43 | | - |
44 | | - elif normalized_image.get_image_pixel_format() == EnumImagePixelFormat.IPF_GRAYSCALED: |
45 | | - channels = 1 |
46 | | - |
47 | | - mat = np.array(ba, dtype=np.uint8).reshape(height, width, channels) |
48 | | - |
49 | | - return mat |
50 | | - |
| 8 | +from utils import * |
51 | 9 |
|
52 | 10 | if __name__ == '__main__': |
53 | 11 | errorCode, errorMsg = LicenseManager.init_license( |
@@ -86,7 +44,7 @@ def convertNormalizedImage2Mat(normalized_image): |
86 | 44 | image = item.get_image_data() |
87 | 45 | if image != None: |
88 | 46 |
|
89 | | - mat = convertNormalizedImage2Mat(image) |
| 47 | + mat = convertImageData2Mat(image) |
90 | 48 | # Use Tesseract to determine the character orientation in the warped image |
91 | 49 | osd_data = pytesseract.image_to_osd( |
92 | 50 | mat, lang='eng', output_type=Output.DICT) |
|
0 commit comments