idc_interface.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. import base64
  2. import copy
  3. import json
  4. import os
  5. import time
  6. import sys
  7. import traceback
  8. from glob import glob
  9. os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
  10. sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/../")
  11. from config.max_compute_config import MAX_COMPUTE
  12. import tensorflow as tf
  13. if not MAX_COMPUTE:
  14. # tensorflow 内存设置
  15. try:
  16. gpus = tf.config.list_physical_devices('GPU')
  17. if len(gpus) > 0:
  18. tf.config.experimental.set_virtual_device_configuration(
  19. gpus[0],
  20. [tf.config.experimental.VirtualDeviceConfiguration(memory_limit=1024)])
  21. except:
  22. traceback.print_exc()
  23. # pass
  24. # gpus = tf.config.list_physical_devices('GPU')
  25. # for gpu in gpus: # 如果使用多块GPU时
  26. # tf.config.experimental.set_memory_growth(gpu, True)
  27. os.environ['CUDA_CACHE_MAXSIZE'] = str(2147483648)
  28. os.environ['CUDA_CACHE_DISABLE'] = str(0)
  29. gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.05)
  30. sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))
  31. from format_convert import _global
  32. import cv2
  33. import numpy as np
  34. from PIL import Image
  35. from idc.model import direction_model
  36. from format_convert.utils import log, get_md5_from_bytes, request_post, np2pil, bytes2np, pil2np, pil_resize, np2bytes
  37. import tensorflow as tf
  38. from flask import Flask, request
  39. from idc.pre_process import get_text_region, get_best_predict_size2
  40. tf.compat.v1.disable_eager_execution()
  41. sess = tf.compat.v1.Session(graph=tf.Graph())
  42. # image_shape = (192, 192)
  43. image_shape = (640, 640)
  44. def adjust_direction(image_np, model, if_return_angle=False):
  45. # 4个方向
  46. cls_num = 4
  47. # 构建数据
  48. origin_image = copy.deepcopy(image_np)
  49. # image_np = pil_resize(image_np, image_shape[0], image_shape[1])
  50. # 获取合适的文字区域
  51. image_np = get_text_region(image_np, image_shape)
  52. # cv2.imshow("get_text_region", image_np)
  53. # cv2.waitKey(0)
  54. # print(type(image_np))
  55. if type(image_np) != np.ndarray:
  56. return None
  57. if len(image_np.shape) < 3:
  58. image_np = np.expand_dims(image_np, axis=-1)
  59. if image_np.shape[0] != image_shape[0] or image_np.shape[1] != image_shape[1]:
  60. image_np = pil_resize(image_np, image_shape[0], image_shape[1])
  61. image_np = np.expand_dims(image_np[:, :, 0], axis=-1)
  62. X = np.expand_dims(np.array(image_np), 0)
  63. # 预测
  64. with sess.as_default():
  65. with sess.graph.as_default():
  66. pred = model.predict(X, batch_size=1)
  67. pred = pred.astype(np.float64)
  68. pred = np.argmax(pred[0])
  69. # 根据分类计算角度
  70. angle = int(360 - pred*int((360/cls_num)))
  71. if if_return_angle:
  72. return angle
  73. else:
  74. if angle not in [0, 360]:
  75. # 根据角度旋转
  76. image_pil = Image.fromarray(origin_image)
  77. image_rotate = np.array(image_pil.rotate(angle, expand=1))
  78. else:
  79. image_rotate = origin_image
  80. return image_rotate
  81. def idc(data, model):
  82. log("into idc_interface idc")
  83. try:
  84. # start_time = time.time()
  85. img_data = base64.b64decode(data)
  86. img_np = bytes2np(img_data)
  87. angle = adjust_direction(img_np, model, if_return_angle=True)
  88. if angle is None:
  89. angle = 0
  90. # print(time.time()-start_time)
  91. log("idc angle " + str(angle))
  92. return {"angle": angle}
  93. except TimeoutError:
  94. return {"angle": [-5]}
  95. except:
  96. traceback.print_exc()
  97. return {"angle": [-1]}
  98. # 接口配置
  99. app = Flask(__name__)
  100. @app.route('/idc', methods=['POST'])
  101. def _idc():
  102. _global._init()
  103. _global.update({"port": globals().get("port")})
  104. start_time = time.time()
  105. log("into idc_interface _idc")
  106. try:
  107. if not request.form:
  108. log("idc no data!")
  109. return json.dumps({"angle": str([-9])})
  110. data = request.form.get("data")
  111. log("idc_interface get data time" + str(time.time()-start_time))
  112. _md5 = request.form.get("md5")
  113. _global.update({"md5": _md5})
  114. idc_model = globals().get("global_idc_model")
  115. if idc_model is None:
  116. print("=========== init idc model ===========")
  117. idc_model = IdcModels().get_model()
  118. globals().update({"global_idc_model": idc_model})
  119. angle = idc(data, idc_model).get("angle")
  120. return json.dumps({"angle": angle})
  121. except TimeoutError:
  122. return json.dumps({"angle": str([-5])})
  123. except:
  124. traceback.print_exc()
  125. return json.dumps({"angle": str([-1])})
  126. finally:
  127. log("idc interface finish time " + str(time.time()-start_time))
  128. class IdcModels:
  129. def __init__(self):
  130. # python文件所在目录
  131. _dir = os.path.abspath(os.path.dirname(__file__))
  132. # detect
  133. model_path = _dir + "/models/e484-f10.96.h5"
  134. with sess.as_default():
  135. with sess.graph.as_default():
  136. self.model = direction_model(input_shape=(image_shape[0], image_shape[1], 1),
  137. output_shape=4)
  138. self.model.load_weights(model_path)
  139. def get_model(self):
  140. return self.model
  141. def test_idc_model(from_remote=False):
  142. idc_model = IdcModels().get_model()
  143. paths = glob("C:/Users/Administrator/Desktop/test_image/error43.png")
  144. # file_path = "C:/Users/Administrator/Desktop/test_image/error10.jpg"
  145. for file_path in paths:
  146. img_np = cv2.imread(file_path)
  147. # img_np = pil_resize(img_np, 640, 640)
  148. h, w = get_best_predict_size2(img_np, threshold=1080)
  149. img_np = pil_resize(img_np, h, w)
  150. # print(img_np.shape)
  151. file_bytes = np2bytes(img_np)
  152. file_base64 = base64.b64encode(file_bytes)
  153. _md5 = get_md5_from_bytes(file_bytes)[0]
  154. _global._init()
  155. _global.update({"port": 15010, "md5": _md5})
  156. if from_remote:
  157. file_json = {"data": file_base64, "md5": _md5}
  158. # _url = "http://192.168.2.102:17000/ocr"
  159. _url = "http://127.0.0.1:17000/ocr"
  160. print(json.loads(request_post(_url, file_json)))
  161. else:
  162. result = idc(file_base64, idc_model)
  163. # print(result)
  164. if type(result.get("angle")) == list:
  165. print(result)
  166. else:
  167. angle = result.get("angle")
  168. img = Image.fromarray(img_np)
  169. img = np.array(img.rotate(angle, expand=1))
  170. print("angle", angle)
  171. print(img.shape)
  172. cv2.namedWindow('img', cv2.WINDOW_NORMAL | cv2.WINDOW_KEEPRATIO)
  173. cv2.imshow("img", img)
  174. cv2.waitKey(0)
  175. # print(result)
  176. if __name__ == "__main__":
  177. test_idc_model()