|
| 1 | +from dynamsoft_capture_vision_bundle import * |
| 2 | +import cv2 |
| 3 | +import numpy as np |
| 4 | +import queue |
| 5 | +from utils import * |
| 6 | + |
| 7 | + |
| 8 | +class FrameFetcher(ImageSourceAdapter): |
| 9 | + def has_next_image_to_fetch(self) -> bool: |
| 10 | + return True |
| 11 | + |
| 12 | + def add_frame(self, imageData): |
| 13 | + self.add_image_to_buffer(imageData) |
| 14 | + |
| 15 | + |
| 16 | +class MyCapturedResultReceiver(CapturedResultReceiver): |
| 17 | + def __init__(self, result_queue): |
| 18 | + super().__init__() |
| 19 | + self.result_queue = result_queue |
| 20 | + |
| 21 | + def on_captured_result_received(self, captured_result): |
| 22 | + self.result_queue.put(captured_result) |
| 23 | + |
| 24 | + |
| 25 | +if __name__ == '__main__': |
| 26 | + errorCode, errorMsg = LicenseManager.init_license( |
| 27 | + "DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ==") |
| 28 | + if errorCode != EnumErrorCode.EC_OK and errorCode != EnumErrorCode.EC_LICENSE_CACHE_USED: |
| 29 | + print("License initialization failed: ErrorCode:", |
| 30 | + errorCode, ", ErrorString:", errorMsg) |
| 31 | + else: |
| 32 | + vc = cv2.VideoCapture(0) |
| 33 | + if not vc.isOpened(): |
| 34 | + print("Error: Camera is not opened!") |
| 35 | + exit(1) |
| 36 | + |
| 37 | + cvr = CaptureVisionRouter() |
| 38 | + fetcher = FrameFetcher() |
| 39 | + cvr.set_input(fetcher) |
| 40 | + |
| 41 | + # Create a thread-safe queue to store captured items |
| 42 | + result_queue = queue.Queue() |
| 43 | + |
| 44 | + receiver = MyCapturedResultReceiver(result_queue) |
| 45 | + cvr.add_result_receiver(receiver) |
| 46 | + |
| 47 | + errorCode, errorMsg = cvr.start_capturing( |
| 48 | + EnumPresetTemplate.PT_READ_BARCODES.value) |
| 49 | + |
| 50 | + if errorCode != EnumErrorCode.EC_OK: |
| 51 | + print("error:", errorMsg) |
| 52 | + |
| 53 | + while True: |
| 54 | + ret, frame = vc.read() |
| 55 | + if not ret: |
| 56 | + print("Error: Cannot read frame!") |
| 57 | + break |
| 58 | + |
| 59 | + fetcher.add_frame(convertMat2ImageData(frame)) |
| 60 | + |
| 61 | + if not result_queue.empty(): |
| 62 | + captured_result = result_queue.get_nowait() |
| 63 | + |
| 64 | + items = captured_result.get_items() |
| 65 | + for item in items: |
| 66 | + |
| 67 | + if item.get_type() == EnumCapturedResultItemType.CRIT_BARCODE: |
| 68 | + text = item.get_text() |
| 69 | + location = item.get_location() |
| 70 | + x1 = location.points[0].x |
| 71 | + y1 = location.points[0].y |
| 72 | + x2 = location.points[1].x |
| 73 | + y2 = location.points[1].y |
| 74 | + x3 = location.points[2].x |
| 75 | + y3 = location.points[2].y |
| 76 | + x4 = location.points[3].x |
| 77 | + y4 = location.points[3].y |
| 78 | + cv2.drawContours( |
| 79 | + frame, [np.intp([(x1, y1), (x2, y2), (x3, y3), (x4, y4)])], 0, (0, 255, 0), 2) |
| 80 | + |
| 81 | + cv2.putText(frame, text, (x1, y1), |
| 82 | + cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2) |
| 83 | + |
| 84 | + del location |
| 85 | + |
| 86 | + if cv2.waitKey(1) & 0xFF == ord('q'): |
| 87 | + break |
| 88 | + |
| 89 | + cv2.imshow('frame', frame) |
| 90 | + |
| 91 | + cvr.stop_capturing() |
| 92 | + vc.release() |
| 93 | + cv2.destroyAllWindows() |
0 commit comments