|
2 | 2 | from dynamsoft_capture_vision_bundle import *
|
3 | 3 | import os
|
4 | 4 | import json
|
| 5 | +import cv2 |
| 6 | +import numpy as np |
5 | 7 |
|
6 | 8 | if __name__ == '__main__':
|
7 | 9 |
|
|
29 | 31 | if not os.path.exists(image_path):
|
30 | 32 | print("The image path does not exist.")
|
31 | 33 | continue
|
32 |
| - |
| 34 | + |
| 35 | + cv_image = cv2.imread(image_path) |
33 | 36 | result = cvr_instance.capture(
|
34 |
| - image_path, "ReadGS1AIBarcode") |
| 37 | + cv_image, "ReadGS1AIBarcode") |
35 | 38 | if result.get_error_code() != EnumErrorCode.EC_OK:
|
36 | 39 | print("Error:", result.get_error_code(),
|
37 | 40 | result.get_error_string())
|
38 | 41 | else:
|
39 | 42 | items = result.get_items()
|
40 | 43 | for item in items:
|
41 | 44 | if item.get_type() == EnumCapturedResultItemType.CRIT_BARCODE:
|
42 |
| - format_type = item.get_format() |
| 45 | + format_type = item.get_format_string() |
43 | 46 | text_bytes = item.get_bytes()
|
44 | 47 | text = text_bytes.decode('utf-8')
|
45 | 48 | print('Barcode text: {} '.format(text))
|
| 49 | + print('Barcode format: {} '.format(format_type)) |
46 | 50 |
|
47 | 51 | location = item.get_location()
|
48 | 52 | x1 = location.points[0].x
|
|
54 | 58 | x4 = location.points[3].x
|
55 | 59 | y4 = location.points[3].y
|
56 | 60 |
|
| 61 | + cv2.drawContours( |
| 62 | + cv_image, [np.intp([(x1, y1), (x2, y2), (x3, y3), (x4, y4)])], 0, (0, 255, 0), 2) |
| 63 | + |
| 64 | + cv2.putText(cv_image, text, (x1, y1 - 10), |
| 65 | + cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2) |
| 66 | + |
57 | 67 | elif item.get_type() == EnumCapturedResultItemType.CRIT_PARSED_RESULT:
|
58 | 68 | try:
|
59 | 69 | json_string = item.get_json_string()
|
|
64 | 74 | description = ""
|
65 | 75 | value = ""
|
66 | 76 |
|
67 |
| - # Get ChildFields |
68 | 77 | child_fields = item.get("ChildFields", [[]])[0]
|
69 | 78 | for field in child_fields:
|
70 | 79 | if field["FieldName"].endswith("AI"):
|
71 |
| - # For dynamic AIs like 310n or 392n, use RawValue instead of FieldName |
72 | 80 | ai = field.get("RawValue", ai)
|
73 | 81 | description = field.get("Value", "")
|
74 | 82 | elif field["FieldName"].endswith("Data"):
|
|
85 | 93 | except json.JSONDecodeError as e:
|
86 | 94 | print("JSON Decode Error:", e)
|
87 | 95 | continue
|
| 96 | + cv2.imshow( |
| 97 | + "Original Image with Detected Barcodes", cv_image) |
| 98 | + cv2.waitKey(0) |
| 99 | + cv2.destroyAllWindows() |
88 | 100 |
|
89 | 101 | input("Press Enter to quit...")
|
0 commit comments