app.py 3.9 KB

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