fasdfsa commited on
Commit
1dcc835
·
1 Parent(s): 33f15e0

add fileState.txt

Browse files
.gitattributes CHANGED
@@ -1,6 +1,7 @@
1
  *.7z filter=lfs diff=lfs merge=lfs -text
2
  *.pdf filter=lfs diff=lfs merge=lfs -text
3
  pdfs/**/*.json filter=lfs diff=lfs merge=lfs -text
 
4
  *.arrow filter=lfs diff=lfs merge=lfs -text
5
  *.bin filter=lfs diff=lfs merge=lfs -text
6
  *.bz2 filter=lfs diff=lfs merge=lfs -text
 
1
  *.7z filter=lfs diff=lfs merge=lfs -text
2
  *.pdf filter=lfs diff=lfs merge=lfs -text
3
  pdfs/**/*.json filter=lfs diff=lfs merge=lfs -text
4
+ pdfs/**/*.txt filter=lfs diff=lfs merge=lfs -text
5
  *.arrow filter=lfs diff=lfs merge=lfs -text
6
  *.bin filter=lfs diff=lfs merge=lfs -text
7
  *.bz2 filter=lfs diff=lfs merge=lfs -text
main.py CHANGED
@@ -213,17 +213,23 @@ def ocr_one_pdf(pth_pdf):
213
 
214
  img_b64_str = base64.b64encode(img_bts).decode('utf-8')
215
 
216
-
217
-
218
  img_name = "{:04d}.jpg".format(nth_page)
219
  pth_img = os.path.join(dir, img_name)
220
  jsn_name = "{:04d}.json".format(nth_page)
221
  pth_jsn = os.path.join(dir, jsn_name)
222
 
 
 
 
 
 
223
  if not os.path.exists(pth_jsn):
224
  jsn, error = ali_ocr(img_b64_str, alicr_config)
225
  if not jsn:
226
- raise Exception('### error: ocr fail.')
 
 
 
227
  with open(pth_jsn, 'w', encoding='utf-8') as f:
228
  f.write( jsonstring(jsn) )
229
 
@@ -231,7 +237,13 @@ def ocr_one_pdf(pth_pdf):
231
  f.write(img_bts)
232
  else:
233
  print(f'### this page ocr already: {pth_jsn}')
234
- continue
 
 
 
 
 
 
235
 
236
  wordsInfo = jsn['prism_wordsInfo']
237
  for j in range(len(wordsInfo)):
@@ -258,6 +270,8 @@ def ocr_one_pdf(pth_pdf):
258
  rd = [pos[2]['x'], pos[2]['y']]
259
  ld = [pos[3]['x'], pos[3]['y']]
260
 
 
 
261
  x1 = min( pos[0]['x'], pos[3]['x'] ) # 当前行的极左
262
  x2 = max( pos[1]['x'], pos[2]['x'] ) # 当前行的极右
263
 
@@ -270,18 +284,23 @@ def ocr_one_pdf(pth_pdf):
270
  # cv2.imshow("green", img_color)
271
  # cv2.waitKey(0)
272
 
 
 
 
273
  cv2.imwrite('./tmp.jpg', cv2.cvtColor(img_color, cv2.COLOR_RGB2BGR))
274
 
275
- if error:
276
- raise Exception(f"Error occurred: {error}")
277
- else:
278
- print("Result:", jsn)
279
- pass
280
 
281
  print( f'one page done. {nth_page} / {number_of_pages}' )
282
 
283
 
284
 
 
 
 
 
 
 
 
285
  if __name__ == '__main__':
286
 
287
  ocr_one_pdf('pdfs/jp/高木直子学日语文法不要想太多/高木直子学日语文法不要想太多.pdf')
 
213
 
214
  img_b64_str = base64.b64encode(img_bts).decode('utf-8')
215
 
 
 
216
  img_name = "{:04d}.jpg".format(nth_page)
217
  pth_img = os.path.join(dir, img_name)
218
  jsn_name = "{:04d}.json".format(nth_page)
219
  pth_jsn = os.path.join(dir, jsn_name)
220
 
221
+ label_left = f'{Path(dir).stem}/{img_name}' # for ppocrlabel
222
+ label_right = []
223
+
224
+ jsn = None
225
+
226
  if not os.path.exists(pth_jsn):
227
  jsn, error = ali_ocr(img_b64_str, alicr_config)
228
  if not jsn:
229
+ raise Exception(f'### error: ocr fail. {error}')
230
+
231
+ print(jsn)
232
+
233
  with open(pth_jsn, 'w', encoding='utf-8') as f:
234
  f.write( jsonstring(jsn) )
235
 
 
237
  f.write(img_bts)
238
  else:
239
  print(f'### this page ocr already: {pth_jsn}')
240
+
241
+ if not os.path.exists(pth_jsn):
242
+ raise Exception(f'### error: not jsn file. {pth_jsn}')
243
+
244
+ with open(pth_jsn, 'r', encoding='utf-8') as f:
245
+ s = f.read()
246
+ jsn = jsonparse(s)
247
 
248
  wordsInfo = jsn['prism_wordsInfo']
249
  for j in range(len(wordsInfo)):
 
270
  rd = [pos[2]['x'], pos[2]['y']]
271
  ld = [pos[3]['x'], pos[3]['y']]
272
 
273
+ label_right.append( { "transcription": word, "points":[ lu, ru, rd, ld ] } )
274
+
275
  x1 = min( pos[0]['x'], pos[3]['x'] ) # 当前行的极左
276
  x2 = max( pos[1]['x'], pos[2]['x'] ) # 当前行的极右
277
 
 
284
  # cv2.imshow("green", img_color)
285
  # cv2.waitKey(0)
286
 
287
+
288
+ pp_state_text += f'{label_left}\t{jsonstring(label_right)}\n'
289
+
290
  cv2.imwrite('./tmp.jpg', cv2.cvtColor(img_color, cv2.COLOR_RGB2BGR))
291
 
 
 
 
 
 
292
 
293
  print( f'one page done. {nth_page} / {number_of_pages}' )
294
 
295
 
296
 
297
+ if not os.path.exists(pth_pp_state):
298
+ # if exist, maybe ppocrlabel edited already. DO NOT rewrite it!!!
299
+ if pp_state_text:
300
+ with open(pth_pp_state, 'w', encoding='utf-8') as f:
301
+ f.write(pp_state_text)
302
+
303
+
304
  if __name__ == '__main__':
305
 
306
  ocr_one_pdf('pdfs/jp/高木直子学日语文法不要想太多/高木直子学日语文法不要想太多.pdf')
pdfs/jp/高木直子学日语文法不要想太多/fileState.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:6205695ffc690931de64422589cead0641dea13b43ba89fccf4cbe24753f4b3e
3
+ size 877529