|
| 1 | +import cv2 |
| 2 | +from dynamsoft_capture_vision_bundle import * |
| 3 | +import numpy as np |
| 4 | +capture = cv2.VideoCapture(0) |
| 5 | + |
| 6 | +if not capture.isOpened(): |
| 7 | + print("Cannot open camera") |
| 8 | + exit() |
| 9 | + |
| 10 | +error_code, error_message = LicenseManager.init_license( |
| 11 | + "DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ==") |
| 12 | +if error_code != EnumErrorCode.EC_OK and error_code != EnumErrorCode.EC_LICENSE_CACHE_USED: |
| 13 | + print("License initialization failed: ErrorCode:", |
| 14 | + error_code, ", ErrorString:", error_message) |
| 15 | +else: |
| 16 | + cvr_instance = CaptureVisionRouter() |
| 17 | + index = 0 |
| 18 | + while True: |
| 19 | + ret, frame = capture.read() |
| 20 | + |
| 21 | + if cv2.waitKey(1) == ord('q'): |
| 22 | + break |
| 23 | + |
| 24 | + result = cvr_instance.capture( |
| 25 | + frame, EnumPresetTemplate.PT_READ_BARCODES.value) |
| 26 | + |
| 27 | + if result.get_error_code() != EnumErrorCode.EC_OK: |
| 28 | + print("Error:", result.get_error_code(), |
| 29 | + result.get_error_string()) |
| 30 | + else: |
| 31 | + items = result.get_items() |
| 32 | + print('Found {} barcodes.'.format(len(items))) |
| 33 | + for item in items: |
| 34 | + format_type = item.get_format_string() |
| 35 | + text = item.get_text() |
| 36 | + print("Barcode Format:", format_type) |
| 37 | + print("Barcode Text:", text) |
| 38 | + |
| 39 | + location = item.get_location() |
| 40 | + x1 = location.points[0].x |
| 41 | + y1 = location.points[0].y |
| 42 | + x2 = location.points[1].x |
| 43 | + y2 = location.points[1].y |
| 44 | + x3 = location.points[2].x |
| 45 | + y3 = location.points[2].y |
| 46 | + x4 = location.points[3].x |
| 47 | + y4 = location.points[3].y |
| 48 | + print("Location Points:") |
| 49 | + print("({}, {})".format(x1, y1)) |
| 50 | + print("({}, {})".format(x2, y2)) |
| 51 | + print("({}, {})".format(x3, y3)) |
| 52 | + print("({}, {})".format(x4, y4)) |
| 53 | + print("-------------------------------------------------") |
| 54 | + |
| 55 | + pts = np.array([(x1, y1), (x2, y2), (x3, y3), (x4, y4)], np.int32) |
| 56 | + pts = pts.reshape((-1, 1, 2)) |
| 57 | + cv2.drawContours(frame, [pts], 0, (0, 255, 0), 2) |
| 58 | + |
| 59 | + cv2.putText(frame, text, (x1, y1 - 10), |
| 60 | + cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2) |
| 61 | + |
| 62 | + cv2.imshow( |
| 63 | + "Original Image with Detected Barcodes", frame) |
| 64 | + |
| 65 | + cv2.destroyAllWindows() |
0 commit comments