|
| 1 | +from dynamsoft_capture_vision_bundle import * |
| 2 | +import os |
| 3 | +import sys |
| 4 | +import cv2 |
| 5 | +import numpy as np |
| 6 | +import pytesseract |
| 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 | + |
| 51 | + |
| 52 | +if __name__ == '__main__': |
| 53 | + errorCode, errorMsg = LicenseManager.init_license( |
| 54 | + "DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ==") |
| 55 | + if errorCode != EnumErrorCode.EC_OK and errorCode != EnumErrorCode.EC_LICENSE_CACHE_USED: |
| 56 | + print("License initialization failed: ErrorCode:", |
| 57 | + errorCode, ", ErrorString:", errorMsg) |
| 58 | + else: |
| 59 | + cvr = CaptureVisionRouter() |
| 60 | + while (True): |
| 61 | + image_path = input( |
| 62 | + ">> Input your image full path:\n" |
| 63 | + ">> 'Enter' for sample image or 'Q'/'q' to quit\n" |
| 64 | + ).strip('\'"') |
| 65 | + |
| 66 | + if image_path.lower() == "q": |
| 67 | + sys.exit(0) |
| 68 | + |
| 69 | + if not os.path.exists(image_path): |
| 70 | + print("The image path does not exist.") |
| 71 | + continue |
| 72 | + result = cvr.capture( |
| 73 | + image_path, EnumPresetTemplate.PT_DETECT_AND_NORMALIZE_DOCUMENT.value) |
| 74 | + if result.get_error_code() != EnumErrorCode.EC_OK: |
| 75 | + print("Error:", result.get_error_code(), |
| 76 | + result.get_error_string()) |
| 77 | + normalized_images_result = result.get_normalized_images_result() |
| 78 | + if normalized_images_result is None or len(normalized_images_result.get_items()) == 0: |
| 79 | + print("No normalized documents.") |
| 80 | + else: |
| 81 | + items = normalized_images_result.get_items() |
| 82 | + print("Normalized", len(items), "documents.") |
| 83 | + for index, item in enumerate(normalized_images_result.get_items()): |
| 84 | + out_path = "normalizedResult_" + str(index) + ".png" |
| 85 | + image_manager = ImageManager() |
| 86 | + image = item.get_image_data() |
| 87 | + if image != None: |
| 88 | + |
| 89 | + mat = convertNormalizedImage2Mat(image) |
| 90 | + # Use Tesseract to determine the character orientation in the warped image |
| 91 | + osd_data = pytesseract.image_to_osd( |
| 92 | + mat, output_type=Output.DICT) |
| 93 | + rotation_angle = osd_data['rotate'] |
| 94 | + |
| 95 | + print( |
| 96 | + f"Detected Character Orientation: {rotation_angle} degrees") |
| 97 | + |
| 98 | + # Draw the detected rotation angle on the original image |
| 99 | + cv_image = cv2.imread(image_path) |
| 100 | + |
| 101 | + location = item.get_location() |
| 102 | + x1 = location.points[0].x |
| 103 | + y1 = location.points[0].y |
| 104 | + x2 = location.points[1].x |
| 105 | + y2 = location.points[1].y |
| 106 | + x3 = location.points[2].x |
| 107 | + y3 = location.points[2].y |
| 108 | + x4 = location.points[3].x |
| 109 | + y4 = location.points[3].y |
| 110 | + |
| 111 | + cv2.drawContours( |
| 112 | + cv_image, [np.intp([(x1, y1), (x2, y2), (x3, y3), (x4, y4)])], 0, (0, 255, 0), 2) |
| 113 | + cv2.putText(cv_image, f"Rotation: {rotation_angle} degrees", (10, 50), |
| 114 | + cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, cv2.LINE_AA) |
| 115 | + cv2.imshow( |
| 116 | + "Original Image with Detected Border and Rotation Angle", cv_image) |
| 117 | + cv2.imshow("Normalized Image", mat) |
| 118 | + cv2.waitKey(0) |
| 119 | + |
| 120 | + errorCode, errorMsg = image_manager.save_to_file( |
| 121 | + image, out_path) |
| 122 | + if errorCode == 0: |
| 123 | + print("Document " + str(index) + |
| 124 | + " file: " + out_path) |
| 125 | + input("Press Enter to quit...") |
0 commit comments