idc_interface.py 6.4 KB

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