not done yet.
Browse files- convert_ocr_result.py +56 -5
convert_ocr_result.py
CHANGED
|
@@ -8,15 +8,66 @@ import numpy as np
|
|
| 8 |
import cv2
|
| 9 |
import json
|
| 10 |
import base64
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
if __name__ == '__main__':
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
imgData = np.fromfile('pdfs/jp/徐一平日本语句型辞典/0001.jpg', dtype=np.uint8)
|
| 14 |
img = cv2.imdecode(imgData, cv2.IMREAD_UNCHANGED)
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
# img2 = cv2.imdecode(imgdata2, -1)
|
| 21 |
|
| 22 |
pass
|
|
|
|
| 8 |
import cv2
|
| 9 |
import json
|
| 10 |
import base64
|
| 11 |
+
import os
|
| 12 |
+
import hashlib
|
| 13 |
+
|
| 14 |
+
def md5(fname):
|
| 15 |
+
hash_md5 = hashlib.md5()
|
| 16 |
+
with open(fname, "rb") as f:
|
| 17 |
+
for chunk in iter(lambda: f.read(4096), b""):
|
| 18 |
+
hash_md5.update(chunk)
|
| 19 |
+
return hash_md5.hexdigest()
|
| 20 |
|
| 21 |
if __name__ == '__main__':
|
| 22 |
+
|
| 23 |
+
dir = 'data'
|
| 24 |
+
if os.path.exists(dir):
|
| 25 |
+
import shutil
|
| 26 |
+
shutil.rmtree(dir)
|
| 27 |
+
os.makedirs(os.path.join(dir, 'img'))
|
| 28 |
+
os.makedirs(os.path.join(dir, 'json'))
|
| 29 |
+
|
| 30 |
+
dir_book = 'pdfs/jp/徐一平日本语句型辞典'
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
|
| 35 |
imgData = np.fromfile('pdfs/jp/徐一平日本语句型辞典/0001.jpg', dtype=np.uint8)
|
| 36 |
img = cv2.imdecode(imgData, cv2.IMREAD_UNCHANGED)
|
| 37 |
+
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 38 |
+
# 把img 对象编码为jpg 格式
|
| 39 |
+
success, encoded_image = cv2.imencode(".jpg", img)
|
| 40 |
+
img_bytes = encoded_image.tobytes()
|
| 41 |
+
base64_str = base64.b64encode(img_bytes).decode('ascii')
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
img_bytes_restored = base64.b64decode(base64_str)
|
| 45 |
+
imgdata_restored = np.frombuffer(img_bytes_restored, dtype=np.uint8) # .reshape(img.shape)
|
| 46 |
+
img_restored = cv2.imdecode(imgdata_restored, cv2.IMREAD_UNCHANGED)
|
| 47 |
+
fp = 'data/0003.jpg'
|
| 48 |
+
|
| 49 |
+
m5 = md5(fp)
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
pth_img = os.path.join(dir, 'img/{}.txt'.format(m5))
|
| 53 |
+
pth_json = os.path.join(dir, 'json/{}.json'.format(m5))
|
| 54 |
+
|
| 55 |
+
if not os.path.exists(pth_img): # 没有相应的图片,可能被删除了
|
| 56 |
+
# raise Exception(f'Warnnig: no image {pth_img}')
|
| 57 |
+
print( f'Warnnig: no image {pth_img}' )
|
| 58 |
+
pass
|
| 59 |
+
|
| 60 |
+
if not os.path.exists(pth_json):
|
| 61 |
+
# raise Exception(f'Warnnig: no image {pth_json}')
|
| 62 |
+
pass
|
| 63 |
+
|
| 64 |
+
with open(pth_img, "r", encoding="utf-8") as fp:
|
| 65 |
+
imgdata = fp.read()
|
| 66 |
+
imgdata = base64.b64decode(imgdata)
|
| 67 |
+
imgdata = np.frombuffer(imgdata, np.uint8)
|
| 68 |
+
img = cv2.imdecode(imgdata, cv2.IMREAD_UNCHANGED)
|
| 69 |
|
| 70 |
+
cv2.imshow('img', img)
|
| 71 |
+
cv2.waitKey(0)
|
|
|
|
| 72 |
|
| 73 |
pass
|