fasdfsa commited on
Commit
40d6141
·
1 Parent(s): 84b147c

show img ok

Browse files
Files changed (1) hide show
  1. jp_ocr.py +67 -0
jp_ocr.py CHANGED
@@ -5,15 +5,82 @@
5
  # 第一次 aliocr 识别中文 main.py
6
  # 第二次用 ppocr 识别日文 jp_ocr.py
7
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
 
10
  if __name__ == '__main__':
11
 
12
  import glob
 
13
  from pathlib import Path
14
  pth_jsons = glob.glob('pdfs/jp/李致雨N3词汇详解/*.json', recursive=True)
15
  pth_zh_jsons = [ str( Path(pth).resolve() ) for pth in pth_jsons if '_jp' not in pth ]
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  pass
18
 
19
 
 
5
  # 第一次 aliocr 识别中文 main.py
6
  # 第二次用 ppocr 识别日文 jp_ocr.py
7
 
8
+ import json, cv2
9
+
10
+ def save_json(filename, dics):
11
+ with open(filename, 'w', encoding='utf-8') as fp:
12
+ json.dump(dics, fp, indent=4, ensure_ascii=False)
13
+ fp.close()
14
+
15
+ def load_json(filename):
16
+ with open(filename, encoding='utf-8') as fp:
17
+ js = json.load(fp)
18
+ fp.close()
19
+ return js
20
 
21
 
22
  if __name__ == '__main__':
23
 
24
  import glob
25
+ import numpy as np
26
  from pathlib import Path
27
  pth_jsons = glob.glob('pdfs/jp/李致雨N3词汇详解/*.json', recursive=True)
28
  pth_zh_jsons = [ str( Path(pth).resolve() ) for pth in pth_jsons if '_jp' not in pth ]
29
 
30
+ array = []
31
+ for idx, pth in enumerate(pth_zh_jsons):
32
+ js = load_json(pth)
33
+ array.append( { "pth":pth, "js":js, "idx":idx } )
34
+ break
35
+
36
+ for item in array:
37
+ pth = item['pth']
38
+ pth_img = pth.replace('.json', '.jpg')
39
+ jsn = item['js']
40
+ imgData = np.fromfile(pth_img, dtype=np.uint8)
41
+ img_color = cv2.imdecode(imgData, -1)
42
+ if 'prism_wordsInfo' in jsn:
43
+ wordsInfo = jsn['prism_wordsInfo']
44
+ else:
45
+ wordsInfo = []
46
+ for j in range(len(wordsInfo)):
47
+ jo = wordsInfo[j]
48
+ word = jo["word"]
49
+
50
+ # prism-wordsInfo 里的 angle 文字块的角度,这个角度只影响width和height,当角度为-90、90、-270、270,width和height的值需要自行互换
51
+ angle = jo['angle']
52
+
53
+ word_x = jo['x']
54
+ word_y = jo['y']
55
+ word_width = jo['width']
56
+ word_height = jo['height']
57
+
58
+ if abs(angle) == 90 or abs(angle) == 270:
59
+ word_width = jo['height']
60
+ word_height = jo['width']
61
+
62
+ pos = jo['pos']
63
+
64
+ # 四个角的位置 左上、右上、右下、左下 顺时针方向
65
+ lu = [pos[0]['x'], pos[0]['y']]
66
+ ru = [pos[1]['x'], pos[1]['y']]
67
+ rd = [pos[2]['x'], pos[2]['y']]
68
+ ld = [pos[3]['x'], pos[3]['y']]
69
+
70
+
71
+ x1 = min( pos[0]['x'], pos[3]['x'] ) # 当前行的极左
72
+ x2 = max( pos[1]['x'], pos[2]['x'] ) # 当前行的极右
73
+
74
+ y1 = min( pos[0]['y'], pos[1]['y'] ) # 当前行的极上
75
+ y2 = max( pos[2]['y'], pos[3]['y'] ) # 当前行的极下
76
+
77
+ # img_color = cv2.rectangle(img_color, (x1, y1), (x2, y2), (0, 255, 0), 2) # 矩形的左上角, 矩形的右下角
78
+ img_color = cv2.rectangle(img_color, (lu[0], lu[1]), (rd[0], rd[1]), (0, 255, 0), 2) # 矩形的左上角, 矩形的右下角
79
+
80
+ cv2.imshow("green", img_color)
81
+ cv2.waitKey(0)
82
+
83
+
84
  pass
85
 
86