fasdfsa commited on
Commit
4afee64
·
1 Parent(s): 98307cd
Files changed (2) hide show
  1. .gitignore +2 -1
  2. convert_ocr_result.py +58 -33
.gitignore CHANGED
@@ -1,4 +1,5 @@
1
- node_modules
 
2
  world\[trim\]
3
  sqlean/
4
  llama.cpp/
 
1
+ node_modules/
2
+ data/
3
  world\[trim\]
4
  sqlean/
5
  llama.cpp/
convert_ocr_result.py CHANGED
@@ -1,6 +1,6 @@
1
 
2
 
3
- # pdfs 下的识别结果转成 ocr 的格试 image/md5.txt json/md5.json , hubggingface/project/ocr/ocrServer/data 这里
4
 
5
  # see huggingface/project/ocr/tools/flask_auto_selection.py
6
 
@@ -9,7 +9,10 @@ import cv2
9
  import json
10
  import base64
11
  import os
 
12
  import hashlib
 
 
13
 
14
  def md5(fname):
15
  hash_md5 = hashlib.md5()
@@ -20,54 +23,76 @@ def md5(fname):
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
 
1
 
2
 
3
+ # pdfs 下的识别结果转成 ocr 的格试 image/md5.txt json/md5.json , 拷贝到这里 hubggingface/project/ocr/ocrServer/data ,就不重新识别了
4
 
5
  # see huggingface/project/ocr/tools/flask_auto_selection.py
6
 
 
9
  import json
10
  import base64
11
  import os
12
+ import shutil
13
  import hashlib
14
+ import glob
15
+ import re
16
 
17
  def md5(fname):
18
  hash_md5 = hashlib.md5()
 
23
 
24
  if __name__ == '__main__':
25
 
26
+ dir_book = 'pdfs/jp/徐一平日本语句型辞典'
27
+
28
  dir = 'data'
29
  if os.path.exists(dir):
 
30
  shutil.rmtree(dir)
31
  os.makedirs(os.path.join(dir, 'img'))
32
  os.makedirs(os.path.join(dir, 'json'))
33
+ os.makedirs(os.path.join(dir, os.path.basename(dir_book)))
34
+
35
 
36
+
37
 
38
 
39
+ pths = glob.glob(dir_book + '/*.jpg', recursive=False)
40
 
41
+ pths = sorted(pths, key=lambda p:(
42
+ match := re.findall(r'.+?(\d+)\.jpg', p),
43
+ number := int(match[0]),
44
+ number # 如果第一项相等就会比较第二项
45
+ ))
46
 
47
+ for idx, pth in enumerate(pths):
 
 
 
 
 
 
48
 
49
+ m5 = md5(pth)
50
+ p_j = pth.replace('.jpg', '.json')
51
+ if not os.path.exists(p_j):
52
+ raise Exception(f'##### error: json not found. {p_j}')
53
 
54
+ imgData = np.fromfile(pth, dtype=np.uint8)
55
+ img = cv2.imdecode(imgData, cv2.IMREAD_UNCHANGED)
56
+ img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
57
+ # 把img 对象编码为jpg 格式
58
+ success, encoded_image = cv2.imencode(".jpg", img)
59
+ img_bytes = encoded_image.tobytes()
60
+ base64_str = base64.b64encode(img_bytes).decode('ascii')
61
 
62
+ img_bytes_restored = base64.b64decode(base64_str)
63
+ imgdata_restored = np.frombuffer(img_bytes_restored, dtype=np.uint8) # .reshape(img.shape)
64
+ img_restored = cv2.imdecode(imgdata_restored, cv2.IMREAD_UNCHANGED)
65
+
66
+
67
+ pth_img = os.path.join(dir, 'img/{}.txt'.format(m5))
68
+ pth_json = os.path.join(dir, 'json/{}.json'.format(m5))
69
+ pth_book = os.path.join(dir, os.path.basename(dir_book))
70
+
71
+ with open(pth_img, 'w', encoding='utf-8') as f:
72
+ f.write(base64_str)
73
+
74
+ dest = shutil.copy(p_j, pth_json)
75
+
76
+ dest = shutil.copy(pth, pth_book)
77
 
78
+ if not os.path.exists(pth_img): # 没有相应的图片,可能被删除了
79
+ # raise Exception(f'Warnnig: no image {pth_img}')
80
+ print( f'Warnnig: no image {pth_img}' )
81
+ pass
82
 
83
+ if not os.path.exists(pth_json):
84
+ # raise Exception(f'Warnnig: no image {pth_json}')
85
+ pass
 
86
 
87
+ # with open(pth_img, "r", encoding="utf-8") as fp:
88
+ # imgdata = fp.read()
89
+ # imgdata = base64.b64decode(imgdata)
90
+ # imgdata = np.frombuffer(imgdata, np.uint8)
91
+ # img = cv2.imdecode(imgdata, cv2.IMREAD_UNCHANGED)
92
 
93
+ # cv2.imshow('img', img)
94
+ # cv2.waitKey(0)
 
 
 
95
 
96
+ print( f'one task done. {idx+1} / {len(pths)}' )
 
97
 
98
+ print( 'all task done.' )