app2.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import re
  2. import threading
  3. import time
  4. from flask import Flask, jsonify,send_from_directory
  5. from welding_exam_detect import start_welding_detection,init_welding_detection
  6. from welding_reset_detect import start_reset_detection,init_rest_detection
  7. from globals import inference_thread, stop_event,lock,redis_client
  8. app = Flask(__name__)
  9. #app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # 16 MB
  10. # Define the /wearing_detection endpoint
  11. @app.route('/reset_detection', methods=['GET'])
  12. def reset_detection():#发送开启AI服务时,检测复位
  13. global inference_thread#当全局变量需要重新赋值时,需要用global关键字声明
  14. if inference_thread is None or not inference_thread.is_alive():#防止重复开启检测服务
  15. #redis_client.set("log_in_flag",'False')
  16. stop_event.clear()
  17. start_events = []#给每个线程一个事件,让我知道某个线程是否开始检测
  18. inference_thread = threading.Thread(target=start_reset_detection,args=(start_events,))
  19. inference_thread.start()
  20. app.logger.info('start_reset_detection')
  21. init_rest_detection()
  22. #init_rest()#设置复位检测图片保存标志为False
  23. #redis_client.set("log_in_flag",'True')#设置登录标志为True,进入保存图片阶段
  24. #time.sleep(8)#等待3s,等待reset_post_path列表中有数据,然后返回给前端
  25. # 等待所有YOLO线程开始检测
  26. for event in start_events:
  27. event.wait()
  28. return jsonify({"status": "SUCCESS"}), 200
  29. else:
  30. app.logger.info("reset_detection already running")
  31. return jsonify({"status": "ALREADY_RUNNING"}), 200
  32. @app.route('/reset_status', methods=['GET'])
  33. def reset_status():#获取复位检测状态
  34. if redis_client.get("welding_reset_flag")=='0':#表明不需要复位,welding_reset_flag是要复位的个数
  35. app.logger.info('reset_all is true')
  36. #此时复位的检测还在进行,需要停止复位检测
  37. stop_inference_internal()
  38. return jsonify({"status": "RESET_ALL"}), 200
  39. if redis_client.get("welding_reset_flag")>'0':#表面需要复位,并设置log_in_flag为True
  40. app.logger.info('reset_all is false')
  41. #发送需要复位的信息
  42. reset_post_path = redis_client.lrange("welding_reset_post_path", 0, -1)
  43. json_array = []
  44. for value in reset_post_path:
  45. match = re.search(r'resetStep(\d+)', value)
  46. step_number = match.group(1)
  47. json_object = {"resetStep": step_number, "image": value}
  48. json_array.append(json_object)
  49. init_rest_detection()
  50. app.logger.info(reset_post_path)
  51. return jsonify({"status": "NOT_RESET_ALL","data":json_array}), 200
  52. @app.route('/welding_detection', methods=['GET'])
  53. def welding_detection():#开始登录时,检测是否需要复位,若需要,则发送复位信息,否则开始焊接检测
  54. global inference_thread
  55. if inference_thread is None or not inference_thread.is_alive():#防止重复开启检测服务
  56. stop_event.clear()#stop_event不用global声明,因为不需要重新赋值,他只是调用了其方法,并没有重新赋值
  57. start_events = []#给每个线程一个事件,让我知道某个线程是否开始检测
  58. inference_thread = threading.Thread(target=start_welding_detection,args=(start_events,))
  59. inference_thread.start()
  60. init_welding_detection()
  61. #等待所有YOLO线程开始检测
  62. for event in start_events:
  63. event.wait()
  64. return jsonify({"status": "SUCCESS"}), 200
  65. else:
  66. app.logger.info("welding_detection already running")
  67. return jsonify({"status": "ALREADY_RUNNING"}), 200
  68. @app.route('/welding_status', methods=['GET'])
  69. def welding_status():#开始登录时,检测是否需要复位,若需要,则发送复位信息,否则开始焊接检测
  70. #global inference_thread
  71. #with lock:
  72. #TODO 若出现异常再发送FAIL.
  73. if redis_client.llen("welding_post_path") == 0:
  74. return jsonify({"status": "NONE"}), 200##表示还没有检测到任何一个焊接步骤
  75. welding_post_path = redis_client.lrange("welding_post_path", 0, -1)
  76. json_array = []
  77. for value in welding_post_path:
  78. match = re.search(r'step(\d+)', value)
  79. step_number = match.group(1)
  80. json_object = {"step": step_number, "image": value}
  81. json_array.append(json_object)
  82. #init_rest()
  83. app.logger.info(welding_post_path)
  84. return jsonify({"status": "SUCCESS","data":json_array}), 200
  85. @app.route('/end_welding_exam', methods=['GET'])#点击考试结束按钮,停止检测,并复位
  86. def end_welding_exam():
  87. stop_inference_internal()
  88. time.sleep(1)
  89. return reset_detection()
  90. # def return_post_path():
  91. # app.logger.info("List elements:", redis_client.lrange("reset_post_path", 0, -1))
  92. def stop_inference_internal():
  93. global inference_thread
  94. if inference_thread is not None and inference_thread.is_alive():
  95. stop_event.set() # 设置停止事件标志,通知推理线程停止运行
  96. inference_thread.join() # 等待推理线程结束
  97. inference_thread = None # 释放线程资源
  98. app.logger.info('detection stopped')
  99. return True
  100. else:
  101. app.logger.info('No inference stopped')
  102. return False
  103. @app.route('/stop_detection', methods=['GET'])
  104. def stop_inference():
  105. #global inference_thread
  106. if stop_inference_internal():
  107. app.logger.info('detection stopped')
  108. return jsonify({"status": "DETECTION_STOPPED"}), 200
  109. else:
  110. app.logger.info('No_detection_running')
  111. return jsonify({"status": "No_detection_running"}), 200
  112. @app.route('/images/<filename>')
  113. def get_image(filename):
  114. app.logger.info('get_image'+filename)
  115. #pdb.set_trace()
  116. return send_from_directory('static/images', filename)
  117. if __name__ == '__main__':
  118. # Start the Flask server
  119. app.run(debug=False, host='172.16.20.163', port=5002)