app.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import threading
  2. from flask import Flask, jsonify
  3. from compressed_oxygen_detect import init_compressed_oxygen_detection,start_compressed_oxygen_detection
  4. from globals import inference_thread, stop_event,lock,redis_client
  5. #焊接考核的穿戴
  6. app = Flask(__name__)
  7. # Define the /wearing_detection endpoint
  8. @app.route('/compressed_oxygen_detection', methods=['GET'])
  9. def compressed_oxygen_detection():
  10. global inference_thread#当全局变量需要重新赋值时,需要用global关键字声明
  11. if inference_thread is None or not inference_thread.is_alive():
  12. stop_event.clear()#stop_event不用global声明,因为不需要重新赋值,他只是调用了其方法,并没有重新赋值
  13. start_events = []#给每个线程一个事件,让我知道某个线程是否开始检测
  14. inference_thread = threading.Thread(target=start_compressed_oxygen_detection,args=(start_events,))
  15. inference_thread.start()
  16. init_compressed_oxygen_detection()
  17. # 等待所有YOLO线程开始检测,两个线程检测完毕时,才返回SUCCESS
  18. for event in start_events:
  19. event.wait()
  20. app.logger.info('start_compressed_oxygen_detection')
  21. return jsonify({"status": "SUCCESS"}), 200
  22. else:
  23. app.logger.info("start_compressed_oxygen_detection already running")
  24. return jsonify({"status": "ALREADY_RUNNING"}), 200
  25. @app.route('/compressed_oxygen_status', methods=['GET'])
  26. def compressed_oxygen_status():#开始登录时,检测是否需要复位,若需要,则发送复位信息,否则开始焊接检测
  27. #global inference_thread
  28. if not redis_client.exists('compressed_oxygen_order'):
  29. app.logger.info('compressed_oxygen_order is none')
  30. return jsonify({"status": "NONE"}), 200
  31. else:
  32. compressed_oxygen_order = redis_client.lrange("compressed_oxygen_order", 0, -1)
  33. json_array = []
  34. for step in compressed_oxygen_order:
  35. json_object = {"step": step}
  36. json_array.append(json_object)
  37. app.logger.info(json_array)
  38. return jsonify({"status": "SUCCESS","data":json_array}), 200
  39. @app.route('/end_compressed_oxygen_detection', methods=['GET'])
  40. def end_wearing_exam():
  41. init_compressed_oxygen_detection()
  42. return jsonify({"status": "SUCCESS"}), 200
  43. def stop_inference_internal():
  44. global inference_thread
  45. if inference_thread is not None and inference_thread.is_alive():
  46. stop_event.set() # 设置停止事件标志,通知推理线程停止运行
  47. inference_thread.join() # 等待推理线程结束
  48. inference_thread = None # 释放线程资源
  49. app.logger.info('detection stopped')
  50. return True
  51. else:
  52. app.logger.info('No inference stopped')
  53. return False
  54. @app.route('/stop_detection', methods=['GET'])
  55. def stop_inference():
  56. #global inference_thread
  57. if stop_inference_internal():
  58. app.logger.info('detection stopped')
  59. return jsonify({"status": "DETECTION_STOPPED"}), 200
  60. else:
  61. app.logger.info('No_detection_running')
  62. return jsonify({"status": "No_detection_running"}), 200
  63. # @app.route('/images/<filename>')
  64. # def get_image(filename):
  65. # app.logger.info('get_image'+filename)
  66. # #pdb.set_trace()
  67. # return send_from_directory('static/images', filename)
  68. if __name__ == '__main__':
  69. # Start the Flask server
  70. app.run(debug=False, host='127.0.0.1', port=5007)