idc_interface.py 6.6 KB

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