Skip to content

Commit 4ffc8f7

Browse files
committed
Update
1 parent 4cea5c2 commit 4ffc8f7

File tree

3 files changed

+61
-58
lines changed

3 files changed

+61
-58
lines changed

examples/official/camera.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,7 @@
22
import cv2
33
import numpy as np
44
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
5+
from utils import *
196

207

218
class FrameFetcher(ImageSourceAdapter):

examples/official/orientation_detection.py

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,49 +5,7 @@
55
import numpy as np
66
import pytesseract
77
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-
8+
from utils import *
519

5210
if __name__ == '__main__':
5311
errorCode, errorMsg = LicenseManager.init_license(
@@ -86,7 +44,7 @@ def convertNormalizedImage2Mat(normalized_image):
8644
image = item.get_image_data()
8745
if image != None:
8846

89-
mat = convertNormalizedImage2Mat(image)
47+
mat = convertImageData2Mat(image)
9048
# Use Tesseract to determine the character orientation in the warped image
9149
osd_data = pytesseract.image_to_osd(
9250
mat, lang='eng', output_type=Output.DICT)

examples/official/utils.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
from dynamsoft_capture_vision_bundle import *
2+
import numpy as np
3+
4+
5+
def convertImageData2Mat(normalized_image):
6+
ba = bytearray(normalized_image.get_bytes())
7+
width = normalized_image.get_width()
8+
height = normalized_image.get_height()
9+
10+
channels = 3
11+
if normalized_image.get_image_pixel_format() == EnumImagePixelFormat.IPF_BINARY:
12+
channels = 1
13+
all = []
14+
skip = normalized_image.stride * 8 - width
15+
16+
index = 0
17+
n = 1
18+
for byte in ba:
19+
20+
byteCount = 7
21+
while byteCount >= 0:
22+
b = (byte & (1 << byteCount)) >> byteCount
23+
24+
if index < normalized_image.stride * 8 * n - skip:
25+
if b == 1:
26+
all.append(255)
27+
else:
28+
all.append(0)
29+
30+
byteCount -= 1
31+
index += 1
32+
33+
if index == normalized_image.stride * 8 * n:
34+
n += 1
35+
36+
mat = np.array(all, dtype=np.uint8).reshape(height, width, channels)
37+
return mat
38+
39+
elif normalized_image.get_image_pixel_format() == EnumImagePixelFormat.IPF_GRAYSCALED:
40+
channels = 1
41+
42+
mat = np.array(ba, dtype=np.uint8).reshape(height, width, channels)
43+
44+
return mat
45+
46+
47+
def convertMat2ImageData(mat):
48+
if len(mat.shape) == 3:
49+
height, width, channels = mat.shape
50+
pixel_format = EnumImagePixelFormat.IPF_RGB_888
51+
else:
52+
height, width = mat.shape
53+
channels = 1
54+
pixel_format = EnumImagePixelFormat.IPF_GRAYSCALED
55+
56+
stride = width * channels
57+
imagedata = ImageData(mat.tobytes(), width, height, stride, pixel_format)
58+
return imagedata

0 commit comments

Comments
 (0)