|
@@ -16,11 +16,19 @@ __all__ = [
|
|
|
|
|
|
__StrBase = "qwertyuioplkjhgfdsazxcvbnm1234567890ZXCVBNMLKJHGFDSAQWERTYUIOP"
|
|
|
__StrBaseLen = len(__StrBase) - 1
|
|
|
-__MAX_IMAGE_SIZE = 3500 * 2000
|
|
|
+__MaxImageSize = 3500 * 2000
|
|
|
+__BorderWidth = 5
|
|
|
__AcceptExtNames = ["jpg", "jpeg", "bmp", "png", "rgb", "tif", "tiff", "gif"]
|
|
|
Engine = HuiMvOCR(Args())
|
|
|
|
|
|
|
|
|
+def __isWhiteBack(img: "np.ndarray") -> "bool":
|
|
|
+ row = np.concatenate([img[:__BorderWidth], img[-__BorderWidth:]], axis=0).reshape((__BorderWidth * 2, -1))
|
|
|
+ col = np.concatenate([img[:, :__BorderWidth], img[:, -__BorderWidth:]], axis=1).reshape((__BorderWidth * 2, -1))
|
|
|
+ full = np.concatenate([row, col], axis=1)
|
|
|
+ return np.average(full) > 127
|
|
|
+
|
|
|
+
|
|
|
def Response(message: "str" = None, data=None):
|
|
|
if message is None:
|
|
|
return jsonify(success=True, message="操作成功", data=data)
|
|
@@ -54,7 +62,7 @@ def read_img(content: "str") -> "np.ndarray":
|
|
|
return cv2.imdecode(np.frombuffer(content, np.uint8), 1) # noqa
|
|
|
|
|
|
|
|
|
-def compress_img(img: "np.ndarray", tar_size: "int" = __MAX_IMAGE_SIZE) -> "np.ndarray":
|
|
|
+def compress_img(img: "np.ndarray", tar_size: "int" = __MaxImageSize) -> "np.ndarray":
|
|
|
cur_size = img.shape[0] * img.shape[1]
|
|
|
if cur_size <= tar_size:
|
|
|
return img
|
|
@@ -70,7 +78,10 @@ def compress_img(img: "np.ndarray", tar_size: "int" = __MAX_IMAGE_SIZE) -> "np.n
|
|
|
|
|
|
def crop_img(image: "np.ndarray") -> "np.ndarray":
|
|
|
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # noqa 将图像转换为灰度图像
|
|
|
- _, threshold = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY) # noqa 换为二值图像 => save: [150,255]
|
|
|
+ thresh_type = cv2.THRESH_BINARY # noqa
|
|
|
+ if __isWhiteBack(gray):
|
|
|
+ thresh_type = cv2.THRESH_BINARY_INV # noqa
|
|
|
+ _, threshold = cv2.threshold(gray, 155, 255, thresh_type) # noqa 换为二值图像 => save: [150,255]
|
|
|
contours, _ = cv2.findContours(threshold, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # noqa 查找轮廓
|
|
|
if not contours:
|
|
|
return image
|