Skip to content

Commit c66506f

Browse files
committed
Add example code based on dynamsoft-capture-vision-bundle
1 parent 1aad3c8 commit c66506f

File tree

6 files changed

+352
-0
lines changed

6 files changed

+352
-0
lines changed

examples/official/camera.py

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
from dynamsoft_capture_vision_bundle import *
2+
import cv2
3+
import numpy as np
4+
import queue
5+
6+
7+
def convertMat2ImageData(mat):
8+
if len(mat.shape) == 3:
9+
height, width, channels = mat.shape
10+
pixel_format = EnumImagePixelFormat.IPF_RGB_888
11+
else:
12+
height, width = mat.shape
13+
channels = 1
14+
pixel_format = EnumImagePixelFormat.IPF_GRAYSCALED
15+
16+
stride = width * channels
17+
imagedata = ImageData(mat.tobytes(), width, height, stride, pixel_format)
18+
return imagedata
19+
20+
21+
class FrameFetcher(ImageSourceAdapter):
22+
def has_next_image_to_fetch(self) -> bool:
23+
return True
24+
25+
def add_frame(self, imageData):
26+
self.add_image_to_buffer(imageData)
27+
28+
29+
class MyCapturedResultReceiver(CapturedResultReceiver):
30+
def __init__(self, result_queue):
31+
super().__init__()
32+
self.result_queue = result_queue
33+
34+
def on_captured_result_received(self, captured_result):
35+
self.result_queue.put(captured_result)
36+
37+
38+
if __name__ == '__main__':
39+
errorCode, errorMsg = LicenseManager.init_license(
40+
"DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ==")
41+
if errorCode != EnumErrorCode.EC_OK and errorCode != EnumErrorCode.EC_LICENSE_CACHE_USED:
42+
print("License initialization failed: ErrorCode:",
43+
errorCode, ", ErrorString:", errorMsg)
44+
else:
45+
vc = cv2.VideoCapture(0)
46+
if not vc.isOpened():
47+
print("Error: Camera is not opened!")
48+
exit(1)
49+
50+
cvr = CaptureVisionRouter()
51+
fetcher = FrameFetcher()
52+
cvr.set_input(fetcher)
53+
54+
# Create a thread-safe queue to store captured items
55+
result_queue = queue.Queue()
56+
57+
receiver = MyCapturedResultReceiver(result_queue)
58+
cvr.add_result_receiver(receiver)
59+
60+
errorCode, errorMsg = cvr.start_capturing("Default")
61+
62+
if errorCode != EnumErrorCode.EC_OK:
63+
print("error:", errorMsg)
64+
65+
while True:
66+
ret, frame = vc.read()
67+
if not ret:
68+
print("Error: Cannot read frame!")
69+
break
70+
71+
fetcher.add_frame(convertMat2ImageData(frame))
72+
73+
# Check if there are any new captured items from the queue
74+
if not result_queue.empty():
75+
captured_result = result_queue.get_nowait()
76+
items = captured_result.get_items()
77+
for item in items:
78+
location = item.get_location()
79+
if item.get_type() == EnumCapturedResultItemType.CRIT_BARCODE:
80+
x1 = location.points[0].x
81+
y1 = location.points[0].y
82+
x2 = location.points[1].x
83+
y2 = location.points[1].y
84+
x3 = location.points[2].x
85+
y3 = location.points[2].y
86+
x4 = location.points[3].x
87+
y4 = location.points[3].y
88+
cv2.drawContours(
89+
frame, [np.intp([(x1, y1), (x2, y2), (x3, y3), (x4, y4)])], 0, (0, 255, 0), 2)
90+
elif item.get_type() == EnumCapturedResultItemType.CRIT_NORMALIZED_IMAGE:
91+
x1 = location.points[0].x
92+
y1 = location.points[0].y
93+
x2 = location.points[1].x
94+
y2 = location.points[1].y
95+
x3 = location.points[2].x
96+
y3 = location.points[2].y
97+
x4 = location.points[3].x
98+
y4 = location.points[3].y
99+
cv2.drawContours(
100+
frame, [np.intp([(x1, y1), (x2, y2), (x3, y3), (x4, y4)])], 0, (255, 0, 0), 2)
101+
102+
del location
103+
104+
if cv2.waitKey(1) & 0xFF == ord('q'):
105+
break
106+
107+
cv2.imshow('frame', frame)
108+
109+
cvr.stop_capturing()
110+
vc.release()
111+
cv2.destroyAllWindows()

examples/official/file.py

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
from dynamsoft_capture_vision_bundle import *
2+
import os
3+
import sys
4+
import cv2
5+
import numpy as np
6+
7+
8+
def convertNormalizedImage2Mat(normalized_image):
9+
ba = bytearray(normalized_image.get_bytes())
10+
width = normalized_image.get_width()
11+
height = normalized_image.get_height()
12+
13+
channels = 3
14+
if normalized_image.get_image_pixel_format() == EnumImagePixelFormat.IPF_BINARY:
15+
channels = 1
16+
all = []
17+
skip = normalized_image.stride * 8 - width
18+
19+
index = 0
20+
n = 1
21+
for byte in ba:
22+
23+
byteCount = 7
24+
while byteCount >= 0:
25+
b = (byte & (1 << byteCount)) >> byteCount
26+
27+
if index < normalized_image.stride * 8 * n - skip:
28+
if b == 1:
29+
all.append(255)
30+
else:
31+
all.append(0)
32+
33+
byteCount -= 1
34+
index += 1
35+
36+
if index == normalized_image.stride * 8 * n:
37+
n += 1
38+
39+
mat = np.array(all, dtype=np.uint8).reshape(height, width, channels)
40+
return mat
41+
42+
elif normalized_image.get_image_pixel_format() == EnumImagePixelFormat.IPF_GRAYSCALED:
43+
channels = 1
44+
45+
mat = np.array(ba, dtype=np.uint8).reshape(height, width, channels)
46+
47+
return mat
48+
49+
50+
if __name__ == '__main__':
51+
errorCode, errorMsg = LicenseManager.init_license(
52+
"DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ==")
53+
if errorCode != EnumErrorCode.EC_OK and errorCode != EnumErrorCode.EC_LICENSE_CACHE_USED:
54+
print("License initialization failed: ErrorCode:",
55+
errorCode, ", ErrorString:", errorMsg)
56+
else:
57+
cvr = CaptureVisionRouter()
58+
while (True):
59+
image_path = input(
60+
">> Input your image full path:\n"
61+
">> 'Enter' for sample image or 'Q'/'q' to quit\n"
62+
).strip('\'"')
63+
64+
if image_path.lower() == "q":
65+
sys.exit(0)
66+
67+
if not os.path.exists(image_path):
68+
print("The image path does not exist.")
69+
continue
70+
result = cvr.capture(
71+
image_path, EnumPresetTemplate.PT_DETECT_AND_NORMALIZE_DOCUMENT.value)
72+
if result.get_error_code() != EnumErrorCode.EC_OK:
73+
print("Error:", result.get_error_code(),
74+
result.get_error_string())
75+
normalized_images_result = result.get_normalized_images_result()
76+
if normalized_images_result is None or len(normalized_images_result.get_items()) == 0:
77+
print("No normalized documents.")
78+
else:
79+
items = normalized_images_result.get_items()
80+
print("Normalized", len(items), "documents.")
81+
for index, item in enumerate(normalized_images_result.get_items()):
82+
out_path = "normalizedResult_" + str(index) + ".png"
83+
image_manager = ImageManager()
84+
image = item.get_image_data()
85+
if image != None:
86+
87+
mat = convertNormalizedImage2Mat(image)
88+
89+
# Draw the detected rotation angle on the original image
90+
cv_image = cv2.imread(image_path)
91+
92+
location = item.get_location()
93+
x1 = location.points[0].x
94+
y1 = location.points[0].y
95+
x2 = location.points[1].x
96+
y2 = location.points[1].y
97+
x3 = location.points[2].x
98+
y3 = location.points[2].y
99+
x4 = location.points[3].x
100+
y4 = location.points[3].y
101+
102+
cv2.drawContours(
103+
cv_image, [np.intp([(x1, y1), (x2, y2), (x3, y3), (x4, y4)])], 0, (0, 255, 0), 2)
104+
cv2.imshow(
105+
"Original Image with Detected Border", cv_image)
106+
cv2.imshow("Normalized Image", mat)
107+
cv2.waitKey(0)
108+
109+
errorCode, errorMsg = image_manager.save_to_file(
110+
image, out_path)
111+
if errorCode == 0:
112+
print("Document " + str(index) +
113+
" file: " + out_path)
114+
input("Press Enter to quit...")
602 KB
Loading
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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...")

examples/official/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
dynamsoft-capture-vision-bundle
2+
opencv-python

examples/official/test.png

1.03 MB
Loading

0 commit comments

Comments
 (0)