utils.py 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974
  1. # -*- coding:utf-8 -*-
  2. import argparse
  3. import copy
  4. import hashlib
  5. import inspect
  6. import json
  7. import os
  8. import socket
  9. import subprocess
  10. import sys
  11. from io import BytesIO
  12. from subprocess import Popen
  13. import cv2
  14. import requests
  15. sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/../")
  16. import difflib
  17. import logging
  18. import mimetypes
  19. import platform
  20. import re
  21. import traceback
  22. import filetype
  23. from bs4 import BeautifulSoup
  24. import yaml
  25. from pdfminer.layout import *
  26. from format_convert import _global
  27. from functools import wraps
  28. import psutil
  29. import time
  30. import numpy as np
  31. from format_convert.judge_platform import get_platform
  32. if get_platform() == "Linux":
  33. import resource
  34. import math
  35. def judge_error_code(_list, code=[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]):
  36. """
  37. [0] : continue
  38. [-1]: 逻辑处理错误
  39. [-2]: 接口调用错误
  40. [-3]: 文件格式错误,无法打开
  41. [-4]: 各类文件调用第三方包读取超时
  42. [-5]: 整个转换过程超时
  43. [-6]: 阿里云UDF队列超时
  44. [-7]: 文件需密码,无法打开
  45. [-8]: 调用现成接口报错
  46. [-9]: 接口接收数据为空
  47. """
  48. for c in code:
  49. if _list == [c]:
  50. return True
  51. return False
  52. def add_div(text):
  53. if text == "" or text is None:
  54. return text
  55. # if get_platform() == "Windows":
  56. # print("add_div", text)
  57. if re.findall("<div>", text):
  58. return text
  59. text = "<div>" + text + "\n"
  60. text = re.sub("\n", "</div><div>", text)
  61. # text += "</div>"
  62. if text[-5:] == "<div>":
  63. # print("add_div has cut", text[-30:])
  64. text = text[:-5]
  65. return text
  66. def get_platform():
  67. sys = platform.system()
  68. return sys
  69. def get_html_p(html_path):
  70. log("into get_html_p")
  71. try:
  72. with open(html_path, "r") as ff:
  73. html_str = ff.read()
  74. soup = BeautifulSoup(html_str, 'lxml')
  75. text = ""
  76. for p in soup.find_all("p"):
  77. p_text = p.text
  78. p_text = p_text.strip()
  79. if p.string != "":
  80. text += p_text
  81. text += "\n"
  82. return text
  83. except Exception as e:
  84. log("get_html_p error!")
  85. return [-1]
  86. def string_similarity(str1, str2):
  87. # 去掉<div>和回车
  88. str1 = re.sub("<div>", "", str1)
  89. str1 = re.sub("</div>", "", str1)
  90. str1 = re.sub("\n", "", str1)
  91. str2 = re.sub("<div>", "", str2)
  92. str2 = re.sub("</div>", "", str2)
  93. str2 = re.sub("\n", "", str2)
  94. # print("********************************")
  95. # print("str1", str1)
  96. # print("********************************")
  97. # print("str2", str2)
  98. # print("********************************")
  99. score = difflib.SequenceMatcher(None, str1, str2).ratio()
  100. print("string_similarity", score)
  101. return score
  102. def get_sequential_data(text_list, bbox_list, html=False):
  103. logging.info("into get_sequential_data")
  104. try:
  105. text = ""
  106. order_list = []
  107. for i in range(len(text_list)):
  108. length_start = bbox_list[i][0][0]
  109. length_end = bbox_list[i][1][0]
  110. height_start = bbox_list[i][0][1]
  111. height_end = bbox_list[i][-1][1]
  112. # print([length_start, length_end, height_start, height_end])
  113. order_list.append([text_list[i], length_start, length_end, height_start, height_end])
  114. # text = text + infomation['text'] + "\n"
  115. if get_platform() == "Windows":
  116. print("get_sequential_data", order_list)
  117. if not order_list:
  118. if get_platform() == "Windows":
  119. print("get_sequential_data", "no order list")
  120. return ""
  121. # 根据bbox的坐标对输出排序
  122. order_list.sort(key=lambda x: (x[3], x[1], x[0]))
  123. # 根据bbox分行分列
  124. # col_list = []
  125. # height_end = int((order_list[0][4] + order_list[0][3]) / 2)
  126. # for i in range(len(order_list)):
  127. # if height_end - threshold <= order_list[i][3] <= height_end + threshold:
  128. # col_list.append(order_list[i])
  129. # else:
  130. # row_list.append(col_list)
  131. # col_list = []
  132. # height_end = int((order_list[i][4] + order_list[i][3]) / 2)
  133. # col_list.append(order_list[i])
  134. # if i == len(order_list) - 1:
  135. # row_list.append(col_list)
  136. row_list = []
  137. used_box = []
  138. threshold = 5
  139. for box in order_list:
  140. if box in used_box:
  141. continue
  142. height_center = (box[4] + box[3]) / 2
  143. row = []
  144. for box2 in order_list:
  145. if box2 in used_box:
  146. continue
  147. height_center2 = (box2[4] + box2[3]) / 2
  148. if height_center - threshold <= height_center2 <= height_center + threshold:
  149. if box2 not in row:
  150. row.append(box2)
  151. used_box.append(box2)
  152. row.sort(key=lambda x: x[0])
  153. row_list.append(row)
  154. for row in row_list:
  155. if not row:
  156. continue
  157. if len(row) <= 1:
  158. text = text + row[0][0] + "\n"
  159. else:
  160. sub_text = ""
  161. row.sort(key=lambda x: x[1])
  162. for col in row:
  163. sub_text = sub_text + col[0] + " "
  164. sub_text = sub_text + "\n"
  165. text += sub_text
  166. if html:
  167. text = "<div>" + text
  168. text = re.sub("\n", "</div>\n<div>", text)
  169. text += "</div>"
  170. # if text[-5:] == "<div>":
  171. # text = text[:-5]
  172. return text
  173. except Exception as e:
  174. logging.info("get_sequential_data error!")
  175. print("get_sequential_data", traceback.print_exc())
  176. return [-1]
  177. # def get_formatted_table(text_list, text_bbox_list, table_bbox_list, split_line):
  178. # logging.info("into get_formatted_table")
  179. # try:
  180. # # 重新定义text_bbox_list,[point, point, text]
  181. # text_bbox_list = [[text_bbox_list[i][0], text_bbox_list[i][2], text_list[i]] for i in
  182. # range(len(text_bbox_list))]
  183. # # 按纵坐标排序
  184. # text_bbox_list.sort(key=lambda x: (x[0][1], x[0][0]))
  185. # table_bbox_list.sort(key=lambda x: (x[0][1], x[0][0]))
  186. #
  187. # # print("text_bbox_list", text_bbox_list)
  188. # # print("table_bbox_list", table_bbox_list)
  189. #
  190. # # bbox位置 threshold
  191. # threshold = 5
  192. #
  193. # # 根据split_line分区,可能有个区多个表格 [(), ()]
  194. # area_text_bbox_list = []
  195. # area_table_bbox_list = []
  196. # # print("get_formatted_table, split_line", split_line)
  197. # for j in range(1, len(split_line)):
  198. # last_y = split_line[j - 1][0][1]
  199. # current_y = split_line[j][0][1]
  200. # temp_text_bbox_list = []
  201. # temp_table_bbox_list = []
  202. #
  203. # # 找出该区域下text bbox
  204. # for text_bbox in text_bbox_list:
  205. # # 计算 text bbox 中心点
  206. # text_bbox_center = ((text_bbox[1][0] + text_bbox[0][0]) / 2,
  207. # (text_bbox[1][1] + text_bbox[0][1]) / 2)
  208. # if last_y - threshold <= text_bbox_center[1] <= current_y + threshold:
  209. # temp_text_bbox_list.append(text_bbox)
  210. # area_text_bbox_list.append(temp_text_bbox_list)
  211. #
  212. # # 找出该区域下table bbox
  213. # for table_bbox in table_bbox_list:
  214. # # 计算 table bbox 中心点
  215. # table_bbox_center = ((table_bbox[1][0] + table_bbox[0][0]) / 2,
  216. # (table_bbox[1][1] + table_bbox[0][1]) / 2)
  217. # if last_y < table_bbox_center[1] < current_y:
  218. # temp_table_bbox_list.append(table_bbox)
  219. # area_table_bbox_list.append(temp_table_bbox_list)
  220. #
  221. # # for j in range(len(area_text_bbox_list)):
  222. # # print("area_text_bbox_list", j, area_text_bbox_list[j])
  223. #
  224. # # 对每个区域分别进行两个bbox匹配,生成表格
  225. # area_text_list = []
  226. # area_column_list = []
  227. # for j in range(len(area_text_bbox_list)):
  228. # # 每个区域的table bbox 和text bbox
  229. # temp_table_bbox_list = area_table_bbox_list[j]
  230. # temp_text_bbox_list = area_text_bbox_list[j]
  231. #
  232. # # 判断该区域有无表格bbox
  233. # # 若无表格,将该区域文字连接
  234. # if not temp_table_bbox_list:
  235. # # 找出该区域的所有text bbox
  236. # only_text_list = []
  237. # only_bbox_list = []
  238. # for text_bbox in temp_text_bbox_list:
  239. # only_text_list.append(text_bbox[2])
  240. # only_bbox_list.append([text_bbox[0], text_bbox[1]])
  241. # only_text = get_sequential_data(only_text_list, only_bbox_list, True)
  242. # if only_text == [-1]:
  243. # return [-1], [-1]
  244. # area_text_list.append(only_text)
  245. # area_column_list.append(0)
  246. # continue
  247. #
  248. # # 有表格
  249. # # 文本对应的表格格子
  250. # text_in_table = {}
  251. # for i in range(len(temp_text_bbox_list)):
  252. # text_bbox = temp_text_bbox_list[i]
  253. #
  254. # # 计算 text bbox 中心点
  255. # text_bbox_center = ((text_bbox[1][0] + text_bbox[0][0]) / 2,
  256. # (text_bbox[1][1] + text_bbox[0][1]) / 2)
  257. #
  258. # # 判断中心点在哪个table bbox中
  259. # for table_bbox in temp_table_bbox_list:
  260. # # 中心点在table bbox中,将text写入字典
  261. # if table_bbox[0][0] <= text_bbox_center[0] <= table_bbox[1][0] and \
  262. # table_bbox[0][1] <= text_bbox_center[1] <= table_bbox[1][1]:
  263. # if str(table_bbox) in text_in_table.keys():
  264. # text_in_table[str(table_bbox)] = text_in_table.get(str(table_bbox)) + text_bbox[2]
  265. # else:
  266. # text_in_table[str(table_bbox)] = text_bbox[2]
  267. # break
  268. #
  269. # # 如果未找到text bbox匹配的table bbox,加大threshold匹配
  270. # # elif (table_bbox[0][0] <= text_bbox_center[0]+threshold <= table_bbox[1][0] and
  271. # # table_bbox[0][1] <= text_bbox_center[1]+threshold <= table_bbox[1][1]) or \
  272. # # (table_bbox[0][0] <= text_bbox_center[0]-threshold <= table_bbox[1][0] and
  273. # # table_bbox[0][1] <= text_bbox_center[1]-threshold <= table_bbox[1][1]) or \
  274. # # (table_bbox[0][0] <= text_bbox_center[0]+threshold <= table_bbox[1][0] and
  275. # # table_bbox[0][1] <= text_bbox_center[1]-threshold <= table_bbox[1][1]) or \
  276. # # (table_bbox[0][0] <= text_bbox_center[0]-threshold <= table_bbox[1][0] and
  277. # # table_bbox[0][1] <= text_bbox_center[1]+threshold <= table_bbox[1][1]):
  278. # # if str(table_bbox) in text_in_table.keys():
  279. # # text_in_table[str(table_bbox)] = text_in_table.get(str(table_bbox)) + text_bbox[2]
  280. # # else:
  281. # # text_in_table[str(table_bbox)] = text_bbox[2]
  282. # # break
  283. #
  284. # # 对表格格子进行分行分列,并计算总计多少小列
  285. # # 放入坐标
  286. # all_col_list = []
  287. # all_row_list = []
  288. # for i in range(len(temp_table_bbox_list)):
  289. # table_bbox = temp_table_bbox_list[i]
  290. #
  291. # # 放入所有坐标x
  292. # if table_bbox[0][0] not in all_col_list:
  293. # all_col_list.append(table_bbox[0][0])
  294. # if table_bbox[1][0] not in all_col_list:
  295. # all_col_list.append(table_bbox[1][0])
  296. #
  297. # # 放入所有坐标y
  298. # if table_bbox[0][1] not in all_row_list:
  299. # all_row_list.append(table_bbox[0][1])
  300. # if table_bbox[1][1] not in all_row_list:
  301. # all_row_list.append(table_bbox[1][1])
  302. # all_col_list.sort(key=lambda x: x)
  303. # all_row_list.sort(key=lambda x: x)
  304. #
  305. # # 分行
  306. # row_list = []
  307. # rows = []
  308. # temp_table_bbox_list.sort(key=lambda x: (x[0][1], x[0][0], x[1][1], x[1][0]))
  309. # y_row = temp_table_bbox_list[0][0][1]
  310. # for i in range(len(temp_table_bbox_list)):
  311. # table_bbox = temp_table_bbox_list[i]
  312. #
  313. # if y_row - threshold <= table_bbox[0][1] <= y_row + threshold:
  314. # rows.append(table_bbox)
  315. # else:
  316. # y_row = table_bbox[0][1]
  317. # if rows:
  318. # rows.sort(key=lambda x: x[0][0])
  319. # row_list.append(rows)
  320. # rows = []
  321. # rows.append(table_bbox)
  322. # # print("*" * 30)
  323. # # print(row_list)
  324. #
  325. # if i == len(temp_table_bbox_list) - 1:
  326. # if rows:
  327. # rows.sort(key=lambda x: x[0][0])
  328. # row_list.append(rows)
  329. #
  330. # # 生成表格,包括文字和格子宽度
  331. # area_column = []
  332. # text = '<table border="1">' + "\n"
  333. # for row in row_list:
  334. # text += "<tr>" + "\n"
  335. # for col in row:
  336. # # 计算bbox y坐标之间有多少其他点,+1即为所占行数
  337. # row_span = 1
  338. # for y in all_row_list:
  339. # if col[0][1] < y < col[1][1]:
  340. # if y - col[0][1] >= 2 and col[1][1] - y >= 2:
  341. # row_span += 1
  342. #
  343. # # 计算bbox x坐标之间有多少其他点,+1即为所占列数
  344. # col_span = 1
  345. # for x in all_col_list:
  346. # if col[0][0] < x < col[1][0]:
  347. # if x - col[0][0] >= 2 and col[1][0] - x >= 2:
  348. # col_span += 1
  349. #
  350. # text += "<td colspan=" + str(col_span) + " rowspan=" + str(row_span) + ">"
  351. #
  352. # if str(col) in text_in_table.keys():
  353. # text += text_in_table.get(str(col))
  354. # else:
  355. # text += ""
  356. # text += "</td>" + "\n"
  357. # text += "</tr>" + "\n"
  358. # text += "</table>" + "\n"
  359. #
  360. # # 计算最大column
  361. # max_col_num = 0
  362. # for row in row_list:
  363. # col_num = 0
  364. # for col in row:
  365. # col_num += 1
  366. # if max_col_num < col_num:
  367. # max_col_num = col_num
  368. #
  369. # area_text_list.append(text)
  370. # area_column_list.append(max_col_num)
  371. #
  372. # text = ""
  373. # if get_platform() == "Windows":
  374. # print("get_formatted_table area_text_list", area_text_list)
  375. # for area_text in area_text_list:
  376. # text += area_text
  377. # return text, area_column_list
  378. # except Exception as e:
  379. # logging.info("get_formatted_table error!")
  380. # print("get_formatted_table", traceback.print_exc())
  381. # return [-1], [-1]
  382. def rename_inner_files(root_path):
  383. try:
  384. logging.info("into rename_inner_files")
  385. # 获取解压文件夹下所有文件+文件夹,不带根路径
  386. path_list = []
  387. for root, dirs, files in os.walk(root_path, topdown=False):
  388. for name in dirs:
  389. p = os.path.join(root, name) + os.sep
  390. if get_platform() == "Windows":
  391. root_path = slash_replace(root_path)
  392. p = slash_replace(p)
  393. p = re.sub(root_path, "", p)
  394. root_path = slash_replace(root_path, True)
  395. p = slash_replace(p, True)
  396. else:
  397. p = re.sub(root_path, "", p)
  398. path_list.append(p)
  399. for name in files:
  400. p = os.path.join(root, name)
  401. if get_platform() == "Windows":
  402. root_path = slash_replace(root_path)
  403. p = slash_replace(p)
  404. p = re.sub(root_path, "", p)
  405. root_path = slash_replace(root_path, True)
  406. p = slash_replace(p, True)
  407. else:
  408. p = re.sub(root_path, "", p)
  409. path_list.append(p)
  410. # 按路径长度排序
  411. path_list.sort(key=lambda x: len(x), reverse=True)
  412. # 循环改名
  413. for old_path in path_list:
  414. # 按路径分隔符分割
  415. ss = old_path.split(os.sep)
  416. # 判断是否文件夹
  417. is_dir = 0
  418. file_type = ""
  419. if os.path.isdir(root_path + old_path):
  420. ss = ss[:-1]
  421. is_dir = 1
  422. else:
  423. if "." in old_path:
  424. file_type = "." + old_path.split(".")[-1]
  425. else:
  426. file_type = ""
  427. # 最后一级需要用hash改名
  428. new_path = ""
  429. # new_path = re.sub(ss[-1], str(hash(ss[-1])), old_path) + file_type
  430. current_level = 0
  431. for s in ss:
  432. # 路径拼接
  433. if current_level < len(ss) - 1:
  434. new_path += s + os.sep
  435. else:
  436. new_path += str(hash(s)) + file_type
  437. current_level += 1
  438. new_ab_path = root_path + new_path
  439. old_ab_path = root_path + old_path
  440. os.rename(old_ab_path, new_ab_path)
  441. # 重新获取解压文件夹下所有文件+文件夹
  442. new_path_list = []
  443. for root, dirs, files in os.walk(root_path, topdown=False):
  444. for name in dirs:
  445. new_path_list.append(os.path.join(root, name) + os.sep)
  446. for name in files:
  447. new_path_list.append(os.path.join(root, name))
  448. return new_path_list
  449. except:
  450. traceback.print_exc()
  451. return [-1]
  452. def judge_format(path):
  453. guess1 = mimetypes.guess_type(path)
  454. _type = None
  455. if guess1[0]:
  456. _type = guess1[0]
  457. else:
  458. guess2 = filetype.guess(path)
  459. if guess2:
  460. _type = guess2.mime
  461. if _type == "application/pdf":
  462. return "pdf"
  463. if _type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
  464. return "docx"
  465. if _type == "application/x-zip-compressed" or _type == "application/zip":
  466. return "zip"
  467. if _type == "application/x-rar-compressed" or _type == "application/rar":
  468. return "rar"
  469. if _type == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
  470. return "xlsx"
  471. if _type == "application/msword":
  472. return "doc"
  473. if _type == "image/png":
  474. return "png"
  475. if _type == "image/jpeg":
  476. return "jpg"
  477. # 猜不到,返回None
  478. return None
  479. def slash_replace(_str, reverse=False):
  480. if reverse:
  481. _str = eval(repr(_str).replace('/', '\\\\'))
  482. else:
  483. _str = eval(repr(_str).replace('\\\\', '/'))
  484. return _str
  485. class LineTable:
  486. def recognize_table(self,list_textbox, list_line,sourceP_LB=True):
  487. self.list_line = list_line
  488. self.list_crosspoints = self.recognize_crosspoints(list_line)
  489. # 聚类
  490. cluster_crosspoints = []
  491. for _point in self.list_crosspoints:
  492. cluster_crosspoints.append({"lines": _point.get("lines"), "points": [_point]})
  493. while 1:
  494. _find = False
  495. new_cluster_crosspoints = []
  496. for l_point in cluster_crosspoints:
  497. _flag = False
  498. for l_n_point in new_cluster_crosspoints:
  499. line1 = l_point.get("lines")
  500. line2 = l_n_point.get("lines")
  501. if len(line1&line2) > 0:
  502. _find = True
  503. _flag = True
  504. l_n_point["lines"] = line1.union(line2)
  505. l_n_point["points"].extend(l_point["points"])
  506. if not _flag:
  507. new_cluster_crosspoints.append({"lines":l_point.get("lines"),"points":l_point.get("points")})
  508. cluster_crosspoints = new_cluster_crosspoints
  509. if not _find:
  510. break
  511. list_l_rect = []
  512. for table_crosspoint in cluster_crosspoints:
  513. list_rect = self.crosspoint2rect(table_crosspoint.get("points"))
  514. list_l_rect.append(list_rect)
  515. in_objs = set()
  516. list_tables = []
  517. for l_rect in list_l_rect:
  518. _ta = self.rect2table(list_textbox,l_rect,in_objs,sourceP_LB=sourceP_LB)
  519. if _ta:
  520. list_tables.append(_ta)
  521. # self._plot(list_line, list_textbox)
  522. return list_tables, in_objs, list_l_rect
  523. def recognize_table_by_rect(self, list_textbox, list_rect, margin=2):
  524. dump_margin = 5
  525. list_rect_tmp = []
  526. # 去重
  527. for _rect in list_rect:
  528. if (_rect.bbox[3]-_rect.bbox[1] < 10) or (abs(_rect.bbox[2]-_rect.bbox[0]) < 5):
  529. continue
  530. _find = False
  531. for _tmp in list_rect_tmp:
  532. for i in range(4):
  533. if abs(_rect.bbox[i]-_tmp.bbox[i]) < dump_margin:
  534. pass
  535. else:
  536. _find = False
  537. break
  538. if i == 3:
  539. _find = True
  540. if _find:
  541. break
  542. if not _find:
  543. list_rect_tmp.append(_rect)
  544. # print("=====",len(list_rect),len(list_rect_tmp))
  545. # print(list_rect_tmp)
  546. # from matplotlib import pyplot as plt
  547. # plt.figure()
  548. # for _rect in list_rect_tmp:
  549. # x0,y0,x1,y1 = _rect.bbox
  550. # plt.boxplot(_rect.bbox)
  551. # plt.show()
  552. cluster_rect = []
  553. for _rect in list_rect:
  554. _find = False
  555. for cr in cluster_rect:
  556. for cr_rect in cr:
  557. if abs((cr_rect.bbox[2]-cr_rect.bbox[0]+_rect.bbox[2]-_rect.bbox[0])-(max(cr_rect.bbox[2],_rect.bbox[2])-min(cr_rect.bbox[0],_rect.bbox[0])))<margin:
  558. _find = True
  559. cr.append(_rect)
  560. break
  561. elif abs((cr_rect.bbox[3]-cr_rect.bbox[1]+_rect.bbox[3]-_rect.bbox[1])-(max(cr_rect.bbox[3],_rect.bbox[3])-min(cr_rect.bbox[1],_rect.bbox[1])))<margin:
  562. _find = True
  563. cr.append(_rect)
  564. break
  565. if _find:
  566. break
  567. if not _find:
  568. cluster_rect.append([_rect])
  569. list_l_rect = cluster_rect
  570. in_objs = set()
  571. list_tables = []
  572. for l_rect in list_l_rect:
  573. _ta = self.rect2table(list_textbox,l_rect,in_objs)
  574. if _ta:
  575. list_tables.append(_ta)
  576. return list_tables,in_objs,list_l_rect
  577. def recognize_crosspoints(self, list_line,fixLine=True):
  578. list_crosspoints = []
  579. # print("lines num",len(list_line))
  580. def getMaxPoints(list_x,margin=5,reverse=False):
  581. clust_x = []
  582. for _x in list_x:
  583. _find = False
  584. for cx in clust_x:
  585. if abs(cx[0]-_x)<margin:
  586. _find = True
  587. cx.append(_x)
  588. break
  589. if not _find:
  590. clust_x.append([_x])
  591. clust_x.sort(key=lambda x:x,reverse=reverse)
  592. return clust_x[0][0],len(clust_x[0])
  593. for _i in range(len(list_line)):
  594. for _j in range(len(list_line)):
  595. line1 = list_line[_i].__dict__.get("bbox")
  596. line2 = list_line[_j].__dict__.get("bbox")
  597. exists,point = self.cross_point(line1,line2)
  598. if exists:
  599. list_crosspoints.append(point)
  600. if fixLine:
  601. #聚类
  602. cluster_crosspoints = []
  603. for _point in list_crosspoints:
  604. cluster_crosspoints.append({"lines":_point.get("lines"),"points":[_point]})
  605. while 1:
  606. _find = False
  607. new_cluster_crosspoints = []
  608. for l_point in cluster_crosspoints:
  609. _flag = False
  610. for l_n_point in new_cluster_crosspoints:
  611. line1 = l_point.get("lines")
  612. line2 = l_n_point.get("lines")
  613. if len(line1&line2)>0:
  614. _find = True
  615. _flag = True
  616. l_n_point["lines"] = line1.union(line2)
  617. l_n_point["points"].extend(l_point["points"])
  618. if not _flag:
  619. new_cluster_crosspoints.append({"lines":l_point.get("lines"),"points":l_point.get("points")})
  620. cluster_crosspoints = new_cluster_crosspoints
  621. if not _find:
  622. break
  623. list_crosspoints = []
  624. for list_cp in cluster_crosspoints:
  625. points = list_cp.get("points")
  626. l_lines = []
  627. for p in points:
  628. l_lines.extend(p.get("p_lines"))
  629. l_lines = list(set(l_lines))
  630. l_lines.sort(key=lambda x:x[0])
  631. min_x,_count = getMaxPoints([l[0] for l in l_lines],reverse=False)
  632. if _count<=2:
  633. min_x = None
  634. min_y,_count = getMaxPoints([l[1] for l in l_lines],reverse=False)
  635. if _count<2:
  636. min_y = None
  637. max_x,_count = getMaxPoints([l[2] for l in l_lines],reverse=True)
  638. if _count<=2:
  639. max_x = None
  640. max_y,_count = getMaxPoints([l[3] for l in l_lines],reverse=True)
  641. if _count<=2:
  642. max_y = None
  643. if min_x and min_y and max_x and max_y:
  644. points.sort(key=lambda x:x["point"][0])
  645. if abs(min_x-points[0]["point"][0])>30:
  646. _line = LTLine(1,(min_x,min_y),(min_x,max_y))
  647. list_line.append(_line)
  648. l_lines.append(_line.bbox)
  649. # print("add=====",_line.bbox)
  650. if abs(max_x-points[-1]["point"][0])>30:
  651. _line = LTLine(1,(max_x,min_y),(max_x,max_y))
  652. list_line.append(_line)
  653. l_lines.append(_line.bbox)
  654. # print("add=====1",_line.bbox)
  655. points.sort(key=lambda x:x["point"][1])
  656. if abs(min_y-points[0]["point"][1])>30:
  657. _line = LTLine(1,(min_x,min_y),(max_x,min_y))
  658. list_line.append(_line)
  659. l_lines.append(_line.bbox)
  660. # print("add=====2",_line.bbox)
  661. if abs(max_y-points[-1]["point"][1])>30:
  662. _line = LTLine(1,(min_x,max_y),(max_x,max_y))
  663. list_line.append(_line)
  664. l_lines.append(_line.bbox)
  665. # print("add=====2",_line.bbox)
  666. for _i in range(len(l_lines)):
  667. for _j in range(len(l_lines)):
  668. line1 = l_lines[_i]
  669. line2 = l_lines[_j]
  670. exists,point = self.cross_point(line1,line2)
  671. if exists:
  672. list_crosspoints.append(point)
  673. # from matplotlib import pyplot as plt
  674. # plt.figure()
  675. # for _line in l_lines:
  676. # x0,y0,x1,y1 = _line
  677. # plt.plot([x0,x1],[y0,y1])
  678. # for point in list_crosspoints:
  679. # plt.scatter(point.get("point")[0],point.get("point")[1])
  680. # plt.show()
  681. # print(list_crosspoints)
  682. # print("points num",len(list_crosspoints))
  683. return list_crosspoints
  684. def recognize_rect(self, _page):
  685. list_line = []
  686. for _obj in _page._objs:
  687. if isinstance(_obj, (LTLine)):
  688. list_line.append(_obj)
  689. list_crosspoints = self.recognize_crosspoints(list_line)
  690. #聚类
  691. cluster_crosspoints = []
  692. for _point in list_crosspoints:
  693. cluster_crosspoints.append({"lines":_point.get("lines"),"points":[_point]})
  694. while 1:
  695. _find = False
  696. new_cluster_crosspoints = []
  697. for l_point in cluster_crosspoints:
  698. _flag = False
  699. for l_n_point in new_cluster_crosspoints:
  700. line1 = l_point.get("lines")
  701. line2 = l_n_point.get("lines")
  702. if len(line1&line2)>0:
  703. _find = True
  704. _flag = True
  705. l_n_point["lines"] = line1.union(line2)
  706. l_n_point["points"].extend(l_point["points"])
  707. if not _flag:
  708. new_cluster_crosspoints.append({"lines":l_point.get("lines"),"points":l_point.get("points")})
  709. cluster_crosspoints = new_cluster_crosspoints
  710. if not _find:
  711. break
  712. # print(len(cluster_crosspoints))
  713. list_l_rect = []
  714. for table_crosspoint in cluster_crosspoints:
  715. list_rect = self.crosspoint2rect(table_crosspoint.get("points"))
  716. list_l_rect.append(list_rect)
  717. return list_l_rect
  718. def crosspoint2rect(self, list_crosspoint, margin=10):
  719. dict_line_points = {}
  720. for _point in list_crosspoint:
  721. lines = list(_point.get("lines"))
  722. for _line in lines:
  723. if _line not in dict_line_points:
  724. dict_line_points[_line] = {"direct":None,"points":[]}
  725. dict_line_points[_line]["points"].append(_point)
  726. # 排序
  727. for k, v in dict_line_points.items():
  728. list_x = []
  729. list_y = []
  730. for _p in v["points"]:
  731. list_x.append(_p.get("point")[0])
  732. list_y.append(_p.get("point")[1])
  733. if max(list_x)-min(list_x)>max(list_y)-min(list_y):
  734. v.get("points").sort(key=lambda x:x.get("point")[0])
  735. v["direct"] = "row"
  736. else:
  737. v.get("points").sort(key=lambda x:x.get("point")[1])
  738. v["direct"] = "column"
  739. list_rect = []
  740. for _point in list_crosspoint:
  741. if _point["buttom"]>=margin and _point["right"]>=margin:
  742. lines = list(_point.get("lines"))
  743. _line = lines[0]
  744. if dict_line_points[_line]["direct"]=="column":
  745. _line = lines[1]
  746. next_point = None
  747. for p1 in dict_line_points[_line]["points"]:
  748. if p1["buttom"]>=margin and p1["point"][0]>_point["point"][0]:
  749. next_point = p1
  750. break
  751. if not next_point:
  752. continue
  753. lines = list(next_point.get("lines"))
  754. _line = lines[0]
  755. if dict_line_points[_line]["direct"]=="row":
  756. _line = lines[1]
  757. final_point = None
  758. for p1 in dict_line_points[_line]["points"]:
  759. if p1["left"]>=margin and p1["point"][1]>next_point["point"][1]:
  760. final_point = p1
  761. break
  762. if not final_point:
  763. continue
  764. _r = LTRect(1,(_point["point"][0],_point["point"][1],final_point["point"][0],final_point["point"][1]))
  765. list_rect.append(_r)
  766. tmp_rect = []
  767. set_bbox = set()
  768. for _r in list_rect:
  769. _bbox = "%.2f-%.2f-%.2f-%.2f"%_r.bbox
  770. width = _r.bbox[2]-_r.bbox[0]
  771. height = _r.bbox[3]-_r.bbox[1]
  772. if width<=margin or height<=margin:
  773. continue
  774. if _bbox not in set_bbox:
  775. tmp_rect.append(_r)
  776. set_bbox.add(_bbox)
  777. list_rect = tmp_rect
  778. # import cv2
  779. # import numpy as np
  780. # import random
  781. # img = np.zeros(shape=(1000,1000),dtype=np.uint8)
  782. # img += 255
  783. #
  784. # color = []
  785. # for rect in list_rect:
  786. # color += 10
  787. # x0,y0,x1,y1 = rect.bbox
  788. # x0 *= 10/18
  789. # y0 *= 10/18
  790. # x1 *= 10/18
  791. # y1 *= 10/18
  792. # print(rect.bbox)
  793. # cv2.rectangle(img, (int(x0),int(y0)),(int(x1),int(y1)), (color%255, (color+10)%255, (color+20)%255), 3)
  794. # cv2.imshow("bbox", img)
  795. # cv2.waitKey(0)
  796. return list_rect
  797. def cross_point(self, line1, line2, segment=True, margin=2):
  798. point_is_exist = False
  799. x = y = 0
  800. x1, y1, x2, y2 = line1
  801. x3, y3, x4, y4 = line2
  802. if (x2 - x1) == 0:
  803. k1 = None
  804. b1 = 0
  805. else:
  806. k1 = (y2 - y1) * 1.0 / (x2 - x1) # 计算k1,由于点均为整数,需要进行浮点数转化
  807. b1 = y1 * 1.0 - x1 * k1 * 1.0 # 整型转浮点型是关键
  808. if (x4 - x3) == 0: # L2直线斜率不存在
  809. k2 = None
  810. b2 = 0
  811. else:
  812. k2 = (y4 - y3) * 1.0 / (x4 - x3) # 斜率存在
  813. b2 = y3 * 1.0 - x3 * k2 * 1.0
  814. if k1 is None:
  815. if not k2 is None:
  816. x = x1
  817. y = k2 * x1 + b2
  818. point_is_exist = True
  819. elif k2 is None:
  820. x = x3
  821. y = k1 * x3 + b1
  822. elif not k2 == k1:
  823. x = (b2 - b1) * 1.0 / (k1 - k2)
  824. y = k1 * x * 1.0 + b1 * 1.0
  825. point_is_exist = True
  826. left = 0
  827. right = 0
  828. top = 0
  829. buttom = 0
  830. if point_is_exist:
  831. if segment:
  832. if x>=(min(x1,x2)-margin) and x<=(max(x1,x2)+margin) and y>=(min(y1,y2)-margin) and y<=(max(y1,y2)+margin):
  833. if x>=(min(x3,x4)-margin) and x<=(max(x3,x4)+margin) and y>=(min(y3,y4)-margin) and y<=(max(y3,y4)+margin):
  834. point_is_exist = True
  835. left = abs(min(x1,x3)-x)
  836. right = abs(max(x2,x4)-x)
  837. top = abs(min(y1,y3)-y)
  838. buttom = abs(max(y2,y4)-y)
  839. else:
  840. point_is_exist = False
  841. else:
  842. point_is_exist = False
  843. line1_key = "%.2f-%.2f-%.2f-%.2f"%(x1, y1, x2, y2)
  844. line2_key = "%.2f-%.2f-%.2f-%.2f"%(x3, y3, x4, y4)
  845. return point_is_exist, {"point": [x, y], "left": left, "right": right,
  846. "top": top, "buttom": buttom, "lines": set([line1_key,line2_key]),"p_lines":[line1,line2]}
  847. def unionTable(self, list_table, fixspan=True, margin=2):
  848. set_x = set()
  849. set_y = set()
  850. list_cell = []
  851. for _t in list_table:
  852. for _line in _t:
  853. list_cell.extend(_line)
  854. clusters_rects = []
  855. #根据y1聚类
  856. set_id = set()
  857. list_cell_dump = []
  858. for _cell in list_cell:
  859. _id = id(_cell)
  860. if _id in set_id:
  861. continue
  862. set_id.add(_id)
  863. list_cell_dump.append(_cell)
  864. list_cell = list_cell_dump
  865. list_cell.sort(key=lambda x:x.get("bbox")[3])
  866. for _rect in list_cell:
  867. _y0 = _rect.get("bbox")[3]
  868. _find = False
  869. for l_cr in clusters_rects:
  870. if abs(l_cr[0].get("bbox")[3]-_y0)<2:
  871. _find = True
  872. l_cr.append(_rect)
  873. break
  874. if not _find:
  875. clusters_rects.append([_rect])
  876. clusters_rects.sort(key=lambda x:x[0].get("bbox")[3],reverse=True)
  877. for l_cr in clusters_rects:
  878. l_cr.sort(key=lambda x:x.get("bbox")[0])
  879. # print("=============:")
  880. # for l_r in clusters_rects:
  881. # print(len(l_r))
  882. for _line in clusters_rects:
  883. for _rect in _line:
  884. (x0,y0,x1,y1) = _rect.get("bbox")
  885. set_x.add(x0)
  886. set_x.add(x1)
  887. set_y.add(y0)
  888. set_y.add(y1)
  889. if len(set_x)==0 or len(set_y)==0:
  890. return
  891. list_x = list(set_x)
  892. list_y = list(set_y)
  893. list_x.sort(key=lambda x:x)
  894. list_y.sort(key=lambda x:x,reverse=True)
  895. _table = []
  896. line_i = 0
  897. for _line in clusters_rects:
  898. table_line = []
  899. cell_i = 0
  900. for _rect in _line:
  901. (x0,y0,x1,y1) = _rect.get("bbox")
  902. _cell = {"bbox":(x0,y0,x1,y1),"rect":_rect.get("rect"),"rowspan":self.getspan(list_y,y0,y1,margin),"columnspan":self.getspan(list_x,x0,x1,margin),"text":_rect.get("text","")}
  903. table_line.append(_cell)
  904. cell_i += 1
  905. line_i += 1
  906. _table.append(table_line)
  907. # print("=====================>>")
  908. # for _line in _table:
  909. # for _cell in _line:
  910. # print(_cell,end="\t")
  911. # print("\n")
  912. # print("=====================>>")
  913. # print(_table)
  914. if fixspan:
  915. for _line in _table:
  916. extend_line = []
  917. for c_i in range(len(_line)):
  918. _cell = _line[c_i]
  919. if _cell.get("columnspan")>1:
  920. _cospan = _cell.get("columnspan")
  921. _cell["columnspan"] = 1
  922. for i in range(1,_cospan):
  923. extend_line.append({"index":c_i+1,"cell":_cell})
  924. extend_line.sort(key=lambda x:x["index"],reverse=True)
  925. for _el in extend_line:
  926. _line.insert(_el["index"],_el["cell"])
  927. for l_i in range(len(_table)):
  928. _line = _table[l_i]
  929. for c_i in range(len(_line)):
  930. _cell = _line[c_i]
  931. if _cell.get("rowspan")>1:
  932. _rospan = _cell.get("rowspan")
  933. _cell["rowspan"] = 1
  934. for i in range(1,_rospan):
  935. _table[l_i+i].insert(c_i,_cell)
  936. table_bbox = (_table[0][0].get("bbox")[0],_table[0][0].get("bbox")[1],_table[-1][-1].get("bbox")[2],_table[-1][-1].get("bbox")[3])
  937. ta = {"bbox":table_bbox,"table":_table}
  938. return ta
  939. #获取点阵
  940. def getSpanLocation(self,_list, x0, x1, margin):
  941. list_location = []
  942. (x0,x1) = (min(x0,x1),max(x0,x1))
  943. for _x in _list:
  944. if _x>=(x0-margin) and _x<=(x1+margin):
  945. list_location.append(_x)
  946. return list_location
  947. def fixSpan(self,_table,list_x,list_y):
  948. for _line in _table:
  949. c_i = 0
  950. while c_i<len(_line):
  951. _cell = _line[c_i]
  952. if _cell.get("columnspan")>1:
  953. x0,y0,x1,y1 = _cell.get("bbox")
  954. _cospan = _cell.get("columnspan")
  955. locations = self.getSpanLocation(list_x,x0,x1,10)
  956. if len(locations)==_cospan+1:
  957. _cell["bbox"] = (x0,y0,locations[1],y1)
  958. _cell["columnspan"] = 1
  959. #len(locations)==_colspan+1
  960. for i in range(1,_cospan):
  961. n_cell = {}
  962. n_cell.update(_cell)
  963. n_cell["bbox"] = (locations[i],y0,locations[i+1],y1)
  964. c_i += 1
  965. _line.insert(c_i,n_cell)
  966. c_i += 1
  967. for l_i in range(len(_table)):
  968. _line = _table[l_i]
  969. c_i = 0
  970. while c_i<len(_line):
  971. _cell = _line[c_i]
  972. if _cell.get("rowspan")>1:
  973. x0,y0,x1,y1 = _cell.get("bbox")
  974. _rospan = _cell.get("rowspan")
  975. locations = self.getSpanLocation(list_y,y0,y1,10)
  976. if len(locations)==_rospan+1:
  977. _cell["bbox"] = (x0,y0,x1,locations[1])
  978. _cell["rowspan"] = 1
  979. for i in range(1,_rospan):
  980. n_cell = {}
  981. n_cell.update(_cell)
  982. if l_i+i<=len(_table)-1:
  983. # print(len(_table),l_i+i)
  984. n_cell["bbox"] = (x0,locations[i],x1,locations[i+1])
  985. _table[l_i+i].insert(c_i,n_cell)
  986. c_i += 1
  987. def fixRect(self,_table,list_x,list_y,list_textbox,in_objs,sourceP_LB,margin):
  988. self.fixSpan(_table,list_x,list_y)
  989. # for line_i in range(len(_table)):
  990. # for cell_i in range(len(_table[line_i])):
  991. # _cell = _table[line_i][cell_i]
  992. # print(line_i,cell_i,_cell["bbox"],_cell["text"])
  993. for _line in _table:
  994. extend_line = []
  995. for c_i in range(len(_line)):
  996. c_cell = _line[c_i]
  997. #first cell missing
  998. if c_i==0 and c_cell["bbox"][0]!=list_x[0]:
  999. _bbox = (list_x[0],c_cell["bbox"][1], c_cell["bbox"][0],c_cell["bbox"][3])
  1000. _cell = {"bbox": _bbox,
  1001. "rect": LTRect(1,_bbox),
  1002. "rowspan": self.getspan(list_y,_bbox[1], _bbox[3], margin),
  1003. "columnspan": self.getspan(list_x, _bbox[0], _bbox[2], margin),
  1004. "text": ""}
  1005. extend_line.append({"index":c_i,"cell":_cell})
  1006. #cell in the median missing
  1007. if c_i<len(_line)-1:
  1008. n_cell = _line[c_i+1]
  1009. _bbox = c_cell["bbox"]
  1010. n_bbox = n_cell["bbox"]
  1011. if _bbox[0]==n_bbox[0] and _bbox[2]==n_bbox[2]:
  1012. continue
  1013. else:
  1014. if abs(_bbox[2]-n_bbox[0])>margin:
  1015. _bbox = (_bbox[2],_bbox[1], n_bbox[0],_bbox[3])
  1016. _cell = {"bbox": _bbox,
  1017. "rect": LTRect(1,_bbox),
  1018. "rowspan": self.getspan(list_y,_bbox[1], _bbox[3], margin),
  1019. "columnspan": self.getspan(list_x, _bbox[0], _bbox[2], margin),
  1020. "text": ""}
  1021. extend_line.append({"index":c_i+1,"cell":_cell})
  1022. #last cell missing
  1023. if c_i==len(_line)-1:
  1024. if abs(c_cell["bbox"][2]-list_x[-1])>margin:
  1025. _bbox = (c_cell["bbox"][2],c_cell["bbox"][1], list_x[-1],c_cell["bbox"][3])
  1026. _cell = {"bbox": _bbox,
  1027. "rect": LTRect(1,_bbox),
  1028. "rowspan": self.getspan(list_y,_bbox[1], _bbox[3], margin),
  1029. "columnspan": self.getspan(list_x, _bbox[0], _bbox[2], margin),
  1030. "text": ""}
  1031. extend_line.append({"index":c_i+1,"cell":_cell})
  1032. extend_line.sort(key=lambda x: x["index"],reverse=True)
  1033. for _tmp in extend_line:
  1034. _line.insert(_tmp["index"],_tmp["cell"])
  1035. list_textbox.sort(key=lambda x: x.bbox[0])
  1036. list_textbox.sort(key=lambda x: x.bbox[3], reverse=sourceP_LB)
  1037. for textbox in list_textbox:
  1038. if textbox in in_objs:
  1039. continue
  1040. x0, y0, x1, y1 = textbox.bbox
  1041. _text = textbox.get_text()
  1042. _find = False
  1043. for table_line in _table:
  1044. for _cell in table_line:
  1045. if self.inbox(textbox.bbox, _cell["bbox"], textbox.get_text()):
  1046. _cell["text"] += _text
  1047. in_objs.add(textbox)
  1048. _find = True
  1049. break
  1050. if _find:
  1051. break
  1052. def rect2table(self, list_textbox, list_rect, in_objs, margin=5, fixspan=False,sourceP_LB=True,fixRect=True):
  1053. def getIOU(bbox0,bbox1):
  1054. width = max(bbox0[2],bbox1[2])-min(bbox0[0],bbox1[0])-(bbox0[2]-bbox0[0]+bbox1[2]-bbox1[0])
  1055. height = max(bbox0[3],bbox1[3])-min(bbox0[1],bbox1[1])-(bbox0[3]-bbox0[1]+bbox1[3]-bbox1[1])
  1056. if width<0 and height<0:
  1057. return abs(width*height/min(abs((bbox0[2]-bbox0[0])*(bbox0[3]-bbox0[1])),abs((bbox1[2]-bbox1[0])*(bbox1[3]-bbox1[1]))))
  1058. return 0
  1059. _table = []
  1060. set_x = set()
  1061. set_y = set()
  1062. clusters_rects = []
  1063. # 根据y1聚类
  1064. if sourceP_LB:
  1065. list_rect.sort(key=lambda x:x.bbox[3])
  1066. for _rect in list_rect:
  1067. _y0 = _rect.bbox[3]
  1068. _find = False
  1069. for l_cr in clusters_rects:
  1070. if abs(l_cr[0].bbox[3]-_y0)<margin:
  1071. _find = True
  1072. l_cr.append(_rect)
  1073. break
  1074. if not _find:
  1075. clusters_rects.append([_rect])
  1076. else:
  1077. list_rect.sort(key=lambda x:x.bbox[1])
  1078. for _rect in list_rect:
  1079. _y0 = _rect.bbox[1]
  1080. _find = False
  1081. for l_cr in clusters_rects:
  1082. if abs(l_cr[0].bbox[1]-_y0)<margin:
  1083. _find = True
  1084. l_cr.append(_rect)
  1085. break
  1086. if not _find:
  1087. clusters_rects.append([_rect])
  1088. # print("textbox:===================")
  1089. # for _textbox in list_textbox:
  1090. # print(_textbox.get_text())
  1091. # print("textbox:======>>>>>>>>>>>>>")
  1092. # for c in clusters_rects:
  1093. # print("+"*30)
  1094. # for cc in c:
  1095. # print("rect", cc.)
  1096. # cul spans
  1097. for _line in clusters_rects:
  1098. for _rect in _line:
  1099. (x0,y0,x1,y1) = _rect.bbox
  1100. set_x.add(x0)
  1101. set_x.add(x1)
  1102. set_y.add(y0)
  1103. set_y.add(y1)
  1104. if len(set_x)==0 or len(set_y)==0:
  1105. return
  1106. if len(list_rect)<=1:
  1107. return
  1108. list_x = list(set_x)
  1109. list_y = list(set_y)
  1110. list_x.sort(key=lambda x:x)
  1111. list_y.sort(key=lambda x:x,reverse=sourceP_LB)
  1112. # print("clusters_rects", len(clusters_rects))
  1113. if sourceP_LB:
  1114. clusters_rects.sort(key=lambda x:x[0].bbox[3],reverse=sourceP_LB)
  1115. else:
  1116. clusters_rects.sort(key=lambda x:x[0].bbox[1],reverse=sourceP_LB)
  1117. for l_cr in clusters_rects:
  1118. l_cr.sort(key=lambda x:x.bbox[0])
  1119. pop_x = []
  1120. for i in range(len(list_x)-1):
  1121. _i = len(list_x)-i-1
  1122. l_i = _i-1
  1123. if abs(list_x[_i]-list_x[l_i])<5:
  1124. pop_x.append(_i)
  1125. pop_x.sort(key=lambda x:x,reverse=True)
  1126. for _x in pop_x:
  1127. list_x.pop(_x)
  1128. #
  1129. pop_x = []
  1130. for i in range(len(list_y)-1):
  1131. _i = len(list_y)-i-1
  1132. l_i = _i-1
  1133. if abs(list_y[_i]-list_y[l_i])<5:
  1134. pop_x.append(_i)
  1135. pop_x.sort(key=lambda x:x,reverse=True)
  1136. for _x in pop_x:
  1137. list_y.pop(_x)
  1138. # print(list_x)
  1139. # print(list_y)
  1140. line_i = 0
  1141. for _line in clusters_rects:
  1142. table_line = []
  1143. cell_i = 0
  1144. for _rect in _line:
  1145. (x0, y0, x1, y1) = _rect.bbox
  1146. _cell = {"bbox": (x0, y0, x1, y1),
  1147. "rect": _rect,
  1148. "rowspan": self.getspan(list_y, y0, y1, margin),
  1149. "columnspan": self.getspan(list_x, x0, x1, margin),
  1150. "text": ""}
  1151. cell_i += 1
  1152. table_line.append(_cell)
  1153. line_i += 1
  1154. _table.append(table_line)
  1155. # print("table===========================>")
  1156. # for _line in _table:
  1157. # for _cell in _line:
  1158. # print("||%d%d"%(_cell["rowspan"],_cell["columnspan"]),end="\t")
  1159. # print()
  1160. # print("table===========================>")
  1161. list_textbox.sort(key=lambda x:x.bbox[0])
  1162. list_textbox.sort(key=lambda x:x.bbox[3],reverse=sourceP_LB)
  1163. # print("list_textbox", list_textbox)
  1164. #find the suitable cell of the textbox
  1165. list_cells = []
  1166. for table_line in _table:
  1167. for _cell in table_line:
  1168. list_cells.append({"cell":_cell,"inbox_textbox_list":[]})
  1169. for textbox in list_textbox:
  1170. list_iou = []
  1171. for _d in list_cells:
  1172. _cell = _d["cell"]
  1173. _iou = self.getIOU(textbox.bbox,_cell["bbox"])
  1174. list_iou.append(_iou)
  1175. max_iou_index = np.argmax(list_iou)
  1176. max_iou = list_iou[max_iou_index]
  1177. if max_iou>0.1:
  1178. list_cells[max_iou_index]["inbox_textbox_list"].append(textbox)
  1179. in_objs.add(textbox)
  1180. has_matched_box_list = []
  1181. for _d in list_cells:
  1182. _cell = _d["cell"]
  1183. inbox_textbox_list = _d["inbox_textbox_list"]
  1184. # 分行,根据y重合
  1185. all_match_box_list = []
  1186. if sourceP_LB:
  1187. inbox_textbox_list.sort(key=lambda x:x.bbox[1],reverse=True)
  1188. else:
  1189. inbox_textbox_list.sort(key=lambda x:x.bbox[1])
  1190. for i in range(len(inbox_textbox_list)):
  1191. match_box_list = []
  1192. box1 = inbox_textbox_list[i]
  1193. if box1 in has_matched_box_list:
  1194. continue
  1195. min_y1 = box1.bbox[1] + 1/3 * abs(box1.bbox[3]-box1.bbox[1])
  1196. max_y1 = box1.bbox[3] - 1/3 * abs(box1.bbox[3]-box1.bbox[1])
  1197. match_box_list.append([box1.get_text(), box1.bbox[0], box1.bbox[1], box1.bbox[2], box1.bbox[3],min_y1,max_y1])
  1198. has_matched_box_list.append(box1)
  1199. for j in range(i+1, len(inbox_textbox_list)):
  1200. box2 = inbox_textbox_list[j]
  1201. if box2 in has_matched_box_list:
  1202. continue
  1203. # print(min_y1, box2.bbox[1], box2.bbox[3], max_y1)
  1204. # print(min_y2, box1.bbox[3], max_y2)
  1205. if min_y1 <= box2.bbox[1] <= max_y1 or \
  1206. min_y1 <= box2.bbox[3] <= max_y1 or \
  1207. box2.bbox[1] <= min_y1 <= max_y1 <= box2.bbox[3]:
  1208. match_box_list.append([box2.get_text(), box2.bbox[0], box2.bbox[1], box2.bbox[2], box2.bbox[3],min_y1,max_y1])
  1209. has_matched_box_list.append(box2)
  1210. match_box_list.sort(key=lambda x: x[1])
  1211. all_match_box_list.append(match_box_list)
  1212. # print("match_box_list", all_match_box_list)
  1213. if sourceP_LB:
  1214. all_match_box_list.sort(key=lambda x:(x[0][2]+x[0][4])/2,reverse=True)
  1215. else:
  1216. all_match_box_list.sort(key=lambda x:(x[0][2]+x[0][4])/2)
  1217. for box_list in all_match_box_list:
  1218. for box in box_list:
  1219. _cell["text"] += box[0]
  1220. # print("------------")
  1221. # for _line in _table:
  1222. # for _cell in _line:
  1223. # print(_cell["text"])
  1224. # print("\n")
  1225. # print("------------")
  1226. self.fixRect(_table,list_x,list_y,list_textbox,in_objs,sourceP_LB,margin)
  1227. table_bbox = (_table[0][0].get("bbox")[0],
  1228. _table[0][0].get("bbox")[1],
  1229. _table[-1][-1].get("bbox")[2],
  1230. _table[-1][-1].get("bbox")[3])
  1231. # print("=======")
  1232. # for _line in _table:
  1233. # for _cell in _line:
  1234. # print(_cell["text"])
  1235. # print("\n")
  1236. # print("===========")
  1237. ta = {"bbox": table_bbox, "table": _table}
  1238. return ta
  1239. def inbox(self, bbox0, bbox_g, text=""):
  1240. # if bbox_g[0]<=bbox0[0] and bbox_g[1]<=bbox0[1] and bbox_g[2]>=bbox0[2] and bbox_g[3]>=bbox0[3]:
  1241. # return 1
  1242. # print("utils inbox", text, self.getIOU(bbox0,bbox_g), bbox0, bbox_g)
  1243. if self.getIOU(bbox0,bbox_g)>0.2:
  1244. return 1
  1245. return 0
  1246. def getIOU(self, bbox0, bbox1):
  1247. width = abs(max(bbox0[2],bbox1[2])-min(bbox0[0],bbox1[0]))-(abs(bbox0[2]-bbox0[0])+abs(bbox1[2]-bbox1[0]))
  1248. height = abs(max(bbox0[3],bbox1[3])-min(bbox0[1],bbox1[1]))-(abs(bbox0[3]-bbox0[1])+abs(bbox1[3]-bbox1[1]))
  1249. if width < 0 and height < 0:
  1250. iou = abs(width*height/min(abs((bbox0[2]-bbox0[0])*(bbox0[3]-bbox0[1])),
  1251. abs((bbox1[2]-bbox1[0])*(bbox1[3]-bbox1[1]))))
  1252. # print("getIOU", iou)
  1253. return iou
  1254. return 0
  1255. def getspan(self, _list, x0, x1, margin):
  1256. _count = 0
  1257. (x0,x1) = (min(x0,x1),max(x0,x1))
  1258. for _x in _list:
  1259. if _x>=(x0-margin) and _x<=(x1+margin):
  1260. _count += 1
  1261. return _count-1
  1262. def _plot(self, list_line, list_textbox):
  1263. from matplotlib import pyplot as plt
  1264. plt.figure()
  1265. for _line in list_line:
  1266. x0, y0, x1, y1 = _line.__dict__.get("bbox")
  1267. plt.plot([x0, x1], [y0, y1])
  1268. for _line in list_line:
  1269. x0, y0, x1, y1 = _line.bbox
  1270. plt.plot([x0, x1], [y0, y1])
  1271. # for point in list_crosspoints:
  1272. # plt.scatter(point.get("point")[0],point.get("point")[1])
  1273. for textbox in list_textbox:
  1274. x0, y0, x1, y1 = textbox.bbox
  1275. plt.plot([x0, x1], [y0, y1])
  1276. plt.show()
  1277. def get_table_html(table):
  1278. html_text = '<table border="1">'
  1279. for row in table:
  1280. html_text += "<tr>"
  1281. for col in row:
  1282. row_span = col.get("rowspan")
  1283. col_span = col.get("columnspan")
  1284. bbox_text = col.get("text")
  1285. html_text += "<td colspan=" + str(col_span) + " rowspan=" + str(row_span) + ">"
  1286. html_text += bbox_text + "</td>"
  1287. html_text += "</tr>"
  1288. html_text += "</table>"
  1289. return html_text
  1290. def sort_object(obj_list, is_reverse=False):
  1291. from format_convert.convert_tree import _Table, _Image, _Sentence, _Page
  1292. obj_list = combine_object(obj_list)
  1293. if len(obj_list) == 0:
  1294. return obj_list
  1295. if isinstance(obj_list[0], (_Table, _Sentence, _Image)):
  1296. obj_list.sort(key=lambda x: (x.y, x.x), reverse=is_reverse)
  1297. return obj_list
  1298. elif isinstance(obj_list[0], _Page):
  1299. obj_list.sort(key=lambda x: x.page_no)
  1300. return obj_list
  1301. else:
  1302. return obj_list
  1303. def combine_object(obj_list, threshold=5):
  1304. from format_convert.convert_tree import _Sentence
  1305. sentence_list = []
  1306. for obj in obj_list:
  1307. if isinstance(obj, _Sentence):
  1308. obj.content = re.sub("\s", "", obj.content)
  1309. sentence_list.append(obj)
  1310. sentence_list.sort(key=lambda x: (x.y, x.x))
  1311. for sen in sentence_list:
  1312. obj_list.remove(sen)
  1313. delete_list = []
  1314. for i in range(1, len(sentence_list)):
  1315. sen1 = sentence_list[i-1]
  1316. sen2 = sentence_list[i]
  1317. if abs(sen2.y - sen1.y) <= threshold:
  1318. if sen2.x > sen1.x:
  1319. sen2.x = sen1.x
  1320. sen2.content = sen1.content + sen2.content
  1321. else:
  1322. sen2.content = sen2.content + sen1.content
  1323. if sen2.y > sen1.y:
  1324. sen2.y = sen1.y
  1325. delete_list.append(sen1)
  1326. for sen in delete_list:
  1327. sentence_list.remove(sen)
  1328. for sen in sentence_list:
  1329. obj_list.append(sen)
  1330. return obj_list
  1331. session_ocr = requests.Session()
  1332. session_otr = requests.Session()
  1333. session_all = requests.Session()
  1334. def request_post(url, param, time_out=1000, use_zlib=False):
  1335. fails = 0
  1336. text = json.dumps([-2])
  1337. while True:
  1338. try:
  1339. if fails >= 1:
  1340. break
  1341. headers = {'content-type': 'application/json'}
  1342. # result = requests.post(url, data=param, timeout=time_out)
  1343. if param.get("model_type") == "ocr":
  1344. result = session_ocr.post(url, data=param, timeout=time_out)
  1345. elif param.get("model_type") == "otr":
  1346. result = session_otr.post(url, data=param, timeout=time_out)
  1347. else:
  1348. result = session_all.post(url, data=param, timeout=time_out)
  1349. # print('result.status_code', result.status_code)
  1350. # print('result.text', result.text)
  1351. if result.status_code == 200:
  1352. text = result.text
  1353. break
  1354. else:
  1355. print('result.status_code', result.status_code)
  1356. print('result.text', result.text)
  1357. fails += 1
  1358. continue
  1359. except socket.timeout:
  1360. fails += 1
  1361. print('timeout! fail times:', fails)
  1362. except:
  1363. fails += 1
  1364. print('fail! fail times:', fails)
  1365. traceback.print_exc()
  1366. return text
  1367. def test_gpu():
  1368. print("="*30)
  1369. import paddle
  1370. paddle.utils.run_check()
  1371. # import tensorflow as tf
  1372. # print("tf gpu", tf.config.list_physical_devices('GPU'))
  1373. print("="*30)
  1374. def my_subprocess_call(*popenargs, timeout=None):
  1375. logging.info("into my_subprocess_call")
  1376. with Popen(*popenargs, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as p:
  1377. try:
  1378. for line in p.stdout:
  1379. print("stdout", line)
  1380. for line in p.stderr:
  1381. print("stderr", line)
  1382. p.wait(timeout=timeout)
  1383. # p.communicate()
  1384. return p.pid, p.returncode
  1385. except: # Including KeyboardInterrupt, wait handled that.
  1386. p.kill()
  1387. # We don't call p.wait() again as p.__exit__ does that for us.
  1388. raise
  1389. finally:
  1390. logging.info("out my_subprocess_call")
  1391. p.kill()
  1392. def parse_yaml():
  1393. yaml_path = os.path.dirname(os.path.abspath(__file__)) + "/interface.yml"
  1394. with open(yaml_path, "r", encoding='utf-8') as f:
  1395. cfg = f.read()
  1396. params = yaml.load(cfg, Loader=yaml.SafeLoader)
  1397. return params
  1398. def get_ip_port(node_type=None, interface_type=None):
  1399. if node_type is None:
  1400. node_type_list = ["master", "slave"]
  1401. else:
  1402. node_type_list = [node_type]
  1403. if interface_type is None:
  1404. interface_type_list = ["convert", "ocr", "otr", "office", "path"]
  1405. else:
  1406. interface_type_list = [interface_type]
  1407. ip_port_dict = {}
  1408. params = parse_yaml()
  1409. # 循环 master slave
  1410. for type1 in node_type_list:
  1411. node_type = type1.upper()
  1412. ip_list = params.get(node_type).get("ip")
  1413. # 循环多个IP
  1414. for j in range(len(ip_list)):
  1415. _ip = ip_list[j]
  1416. if ip_port_dict.get(_ip):
  1417. ip_port_dict.get(_ip).update({node_type: {}})
  1418. else:
  1419. ip_port_dict.update({_ip: {node_type: {}}})
  1420. # 有IP时,循环多个参数
  1421. for type2 in interface_type_list:
  1422. python_path = None
  1423. project_path = None
  1424. gunicorn_path = None
  1425. processes = 0
  1426. port_list = []
  1427. interface_type = type2.upper()
  1428. if interface_type in ["convert".upper()]:
  1429. _port = params.get(node_type).get(interface_type).get("port")
  1430. if _port is None:
  1431. port_list = []
  1432. else:
  1433. if interface_type == "convert".upper():
  1434. processes = params.get(node_type).get(interface_type).get("processes")[j]
  1435. port_list = [str(_port[j])]*int(processes)
  1436. # port_list = [str(_port)]
  1437. elif interface_type == "path".upper():
  1438. python_path = params.get(node_type).get(interface_type).get("python")[j]
  1439. project_path = params.get(node_type).get(interface_type).get("project")[j]
  1440. gunicorn_path = params.get(node_type).get(interface_type).get("gunicorn")[j]
  1441. else:
  1442. port_start = params.get(node_type).get(interface_type).get("port_start")
  1443. port_no = params.get(node_type).get(interface_type).get("port_no")
  1444. if port_start is None or port_no is None:
  1445. port_list = []
  1446. else:
  1447. port_list = [str(x) for x in range(port_start[j], port_start[j]+port_no[j], 1)]
  1448. # if ip_list:
  1449. # for i in range(len(ip_list)):
  1450. # 参数放入dict
  1451. if port_list:
  1452. ip_port_dict.get(_ip).get(node_type).update({interface_type.lower(): port_list})
  1453. if processes:
  1454. ip_port_dict.get(_ip).get(node_type).update({interface_type.lower()+"_processes": processes})
  1455. if project_path and python_path and gunicorn_path:
  1456. ip_port_dict.get(_ip).get(node_type).update({"project_path": project_path,
  1457. "python_path": python_path,
  1458. "gunicorn_path": gunicorn_path})
  1459. # print("ip_port_dict", ip_port_dict)
  1460. return ip_port_dict
  1461. def get_intranet_ip():
  1462. try:
  1463. # Create a new socket using the given address family,
  1464. # socket type and protocol number.
  1465. s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  1466. # Connect to a remote socket at address.
  1467. # (The format of address depends on the address family.)
  1468. address = ("8.8.8.8", 80)
  1469. s.connect(address)
  1470. # Return the socket’s own address.
  1471. # This is useful to find out the port number of an IPv4/v6 socket, for instance.
  1472. # (The format of the address returned depends on the address family.)
  1473. sockname = s.getsockname()
  1474. ip = sockname[0]
  1475. port = sockname[1]
  1476. finally:
  1477. s.close()
  1478. return ip
  1479. def memory_decorator(func):
  1480. @wraps(func)
  1481. def get_memory_info(*args, **kwargs):
  1482. if get_platform() == "Windows":
  1483. return func(*args, **kwargs)
  1484. # 只有linux有resource包
  1485. # usage = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
  1486. usage = psutil.Process(os.getpid()).memory_info().rss / 1024 / 1024 / 1024
  1487. start_time = time.time()
  1488. logging.info("----- memory info start - " + func.__qualname__
  1489. + " - " + str(os.getpid())
  1490. + " - " + str(round(usage, 2)) + " GB"
  1491. + " - " + str(round(time.time()-start_time, 2)) + " sec")
  1492. result = func(*args, **kwargs)
  1493. # usage = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
  1494. usage = psutil.Process(os.getpid()).memory_info().rss / 1024 / 1024 / 1024
  1495. logging.info("----- memory info end - " + func.__qualname__
  1496. + " - " + str(os.getpid())
  1497. + " - " + str(round(usage, 2)) + " GB"
  1498. + " - " + str(round(time.time()-start_time, 2)) + " sec")
  1499. return result
  1500. return get_memory_info
  1501. def log(msg):
  1502. call_func_name = inspect.currentframe().f_back.f_code.co_name
  1503. logger = get_logger(call_func_name, {"md5": _global.get("md5"),
  1504. "port": _global.get("port")})
  1505. logger.info(msg)
  1506. # logging.info(msg)
  1507. def get_logger(_name, _dict):
  1508. extra = _dict
  1509. _format = '%(asctime)s - %(name)s - %(levelname)s - %(md5)s - %(port)s - %(message)s'
  1510. logger = logging.getLogger(_name)
  1511. create_new_flag = 1
  1512. handlers = logger.handlers
  1513. if handlers:
  1514. for h in handlers:
  1515. if h.formatter.__dict__.get("_fmt") == _format:
  1516. create_new_flag = 0
  1517. break
  1518. if create_new_flag:
  1519. formatter = logging.Formatter(_format)
  1520. handler = logging.StreamHandler()
  1521. handler.setFormatter(formatter)
  1522. logger.addHandler(handler)
  1523. logger.setLevel(logging.INFO)
  1524. logger.propagate = False
  1525. logger = logging.LoggerAdapter(logger, extra)
  1526. return logger
  1527. def set_flask_global():
  1528. # 接口轮询所需锁、参数
  1529. ip_port_flag = {}
  1530. ip_flag = []
  1531. ip_port_dict = get_ip_port()
  1532. for _k in ip_port_dict.keys():
  1533. ip_port_flag.update({_k: {"ocr": 0,
  1534. "otr": 0,
  1535. "convert": 0,
  1536. "office": 0
  1537. }})
  1538. if ip_port_dict.get(_k).get("MASTER"):
  1539. ip_flag.append([_k+"_master", 0])
  1540. if ip_port_dict.get(_k).get("SLAVE"):
  1541. ip_flag.append([_k+"_slave", 0])
  1542. _global.update({"ip_port_flag": ip_port_flag})
  1543. _global.update({"ip_port": ip_port_dict})
  1544. _global.update({"ip_flag": ip_flag})
  1545. # print(globals().get("ip_port"))
  1546. def get_md5_from_bytes(_bytes):
  1547. def generate_fp(_b):
  1548. bio = BytesIO()
  1549. bio.write(_b)
  1550. return bio
  1551. _length = 0
  1552. try:
  1553. _md5 = hashlib.md5()
  1554. ff = generate_fp(_bytes)
  1555. ff.seek(0)
  1556. while True:
  1557. data = ff.read(4096)
  1558. if not data:
  1559. break
  1560. _length += len(data)
  1561. _md5.update(data)
  1562. return _md5.hexdigest(), _length
  1563. except Exception as e:
  1564. traceback.print_exc()
  1565. return None, _length
  1566. # def to_share_memory(np_data, name=None):
  1567. # # from multiprocessing.resource_tracker import unregister
  1568. # from multiprocessing import shared_memory
  1569. # if name is None:
  1570. # sm_name = "psm_" + str(os.getpid())
  1571. # else:
  1572. # sm_name = name
  1573. # logging.info("into from_share_memory sm_name " + sm_name)
  1574. # shm = shared_memory.SharedMemory(name=sm_name, create=True, size=np_data.nbytes)
  1575. # # unregister(sm_name, 'shared_memory')
  1576. # sm_data = np.ndarray(np_data.shape, dtype=np_data.dtype, buffer=shm.buf)
  1577. # sm_data[:] = np_data[:] # Copy the original data into shared memory
  1578. #
  1579. # shm.close()
  1580. # del sm_data
  1581. # return shm
  1582. # def from_share_memory(sm_name, _shape, _dtype, if_close=True):
  1583. # from multiprocessing import shared_memory
  1584. # logging.info("into from_share_memory sm_name " + sm_name)
  1585. # shm = shared_memory.SharedMemory(name=sm_name, create=False)
  1586. # b = np.ndarray(_shape, dtype=_dtype, buffer=shm.buf)
  1587. # sm_data = copy.deepcopy(b)
  1588. # b[::] = 0
  1589. #
  1590. # if if_close:
  1591. # try:
  1592. # shm.close()
  1593. # shm.unlink()
  1594. # except Exception:
  1595. # log("file not found! " + sm_name)
  1596. # return sm_data
  1597. # def get_share_memory(sm_name):
  1598. # try:
  1599. # from multiprocessing import shared_memory
  1600. # shm = shared_memory.SharedMemory(name=sm_name, create=False)
  1601. # return shm
  1602. # except:
  1603. # return None
  1604. # def release_share_memory(shm):
  1605. # try:
  1606. # if shm is None:
  1607. # return
  1608. # shm.close()
  1609. # shm.unlink()
  1610. # log(str(shm.name) + " release successfully!")
  1611. # except FileNotFoundError:
  1612. # log(str(shm.name) + " has released!")
  1613. # except Exception as e:
  1614. # traceback.print_exc()
  1615. # def get_share_memory_list(sm_list_name, list_size=None):
  1616. # # from multiprocessing.resource_tracker import unregister
  1617. # from multiprocessing import shared_memory
  1618. # if list_size is None:
  1619. # sm_list = shared_memory.ShareableList(name=sm_list_name)
  1620. # else:
  1621. # sm_list = shared_memory.ShareableList(name=sm_list_name, sequence=["0"]+[' '*2048]*(list_size-2)+["0"])
  1622. # # unregister(sm_list_name, 'shared_memory')
  1623. # return sm_list
  1624. # def close_share_memory_list(sm_list):
  1625. # try:
  1626. # sm_list.shm.close()
  1627. # except Exception:
  1628. # traceback.print_exc()
  1629. def get_np_type(_str):
  1630. _dtype = None
  1631. if _str == 'uint8':
  1632. _dtype = np.uint8
  1633. elif _str == 'float16':
  1634. _dtype = np.float16
  1635. elif _str == 'float32':
  1636. _dtype = np.float32
  1637. logging.info("get_np_type " + _str + " " + str(_dtype))
  1638. return _dtype
  1639. def namespace_to_dict(agrs_or_dict, reverse=False):
  1640. if reverse:
  1641. agrs_or_dict = argparse.Namespace(**agrs_or_dict)
  1642. else:
  1643. agrs_or_dict = vars(agrs_or_dict)
  1644. return agrs_or_dict
  1645. def get_args_from_config(ip_port_dict, ip, arg_type, node_type=None):
  1646. if node_type is None:
  1647. node_type = ["MASTER", "SLAVE"]
  1648. else:
  1649. node_type = [node_type]
  1650. arg_list = []
  1651. for _type in node_type:
  1652. if ip_port_dict.get(ip).get(_type):
  1653. if ip_port_dict.get(ip).get(_type).get(arg_type):
  1654. arg_list.append(ip_port_dict.get(ip).get(_type).get(arg_type))
  1655. return arg_list
  1656. def remove_red_seal(image_np):
  1657. """
  1658. 去除红色印章
  1659. """
  1660. cv2.namedWindow("image_np", 0)
  1661. cv2.resizeWindow("image_np", 1000, 800)
  1662. cv2.imshow("image_np", image_np)
  1663. height, width, c = image_np.shape
  1664. window_h = int(height / 15)
  1665. image_hsv = cv2.cvtColor(image_np, cv2.COLOR_BGR2HSV)
  1666. # 遍历numpy
  1667. red_point_list = []
  1668. image_list = image_np.tolist()
  1669. hsv_dict = {}
  1670. for index_1 in range(len(image_list)):
  1671. for index_2 in range(len(image_list[index_1])):
  1672. h, s, v = image_hsv[index_1][index_2]
  1673. if (0 <= h <= 10 or 156 <= h <= 180) and 43 <= s <= 255 and 46 <= v <= 255:
  1674. key = str(image_hsv[index_1][index_2].tolist())
  1675. red_point_list.append([key, index_1, index_2])
  1676. if hsv_dict.get(key):
  1677. hsv_dict[key] += 1
  1678. else:
  1679. hsv_dict[key] = 1
  1680. # 找出相同最多的hsv值
  1681. hsv_most_key = None
  1682. hsv_most_value = 0
  1683. for hsv in hsv_dict.keys():
  1684. if hsv_dict.get(hsv) > hsv_most_value:
  1685. hsv_most_value = hsv_dict.get(hsv)
  1686. hsv_most_key = hsv
  1687. # print(hsv_dict)
  1688. # 根据hsv判断其填充为黑色还是白色
  1689. hsv_most_key = eval(hsv_most_key)
  1690. for point in red_point_list:
  1691. if abs(eval(point[0])[2] - hsv_most_key[2]) <= 70:
  1692. image_np[point[1]][point[2]][0] = 255
  1693. image_np[point[1]][point[2]][1] = 255
  1694. image_np[point[1]][point[2]][2] = 255
  1695. else:
  1696. image_np[point[1]][point[2]][0] = 0
  1697. image_np[point[1]][point[2]][1] = 0
  1698. image_np[point[1]][point[2]][2] = 0
  1699. cv2.namedWindow("remove_red_seal", 0)
  1700. cv2.resizeWindow("remove_red_seal", 1000, 800)
  1701. cv2.imshow("remove_red_seal", image_np)
  1702. # cv2.imwrite("C:/Users/Administrator/Downloads/1.png", image_np)
  1703. cv2.waitKey(0)
  1704. return image_np
  1705. if __name__ == "__main__":
  1706. # strs = r"D:\Project\temp\04384fcc9e8911ecbd2844f971944973\043876ca9e8911eca5e144f971944973_rar\1624114035529.jpeg"
  1707. # print(slash_replace(strs))
  1708. # from matplotlib import pyplot as plt
  1709. # import random
  1710. # fig = plt.figure()
  1711. # plt.xlim(100)
  1712. # plt.ylim(100)
  1713. # fig.add_subplot(111)
  1714. # x0,y0,x1,y1 = (1,2,3,4)
  1715. # plt.gca().add_patch(plt.Rectangle(xy=(x0, y0),
  1716. # width=x1-x0,
  1717. # height=y1-y0,
  1718. # edgecolor=(random.randint(0,255)/255,random.randint(0,255)/255,random.randint(0,255)/255),
  1719. # fill=False, linewidth=2))
  1720. #
  1721. # # plt.show()
  1722. # import cv2
  1723. # import numpy as np
  1724. # img = np.zeros(shape=(1800,1800),dtype=np.uint8)
  1725. # img += 255
  1726. # cv2.imshow("bbox", img)
  1727. # cv2.waitKey(0)
  1728. # print(json.dumps({"data":[1, 2]}))
  1729. # print(parse_yaml())
  1730. # print(get_ip_port())
  1731. # print(get_args_from_config(get_ip_port(), "http://127.0.0.1", "gunicorn_path"))
  1732. # print(get_intranet_ip())
  1733. _path = "C:/Users/Administrator/Downloads/3.png"
  1734. remove_red_seal(cv2.imread(_path))