app6.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import re
  2. import threading
  3. import time
  4. from flask import Flask, jsonify,send_from_directory
  5. #from basket_cleaning_detect import start_basket_cleaning_detection,init_basket_cleaning_detection
  6. from equipment_cleaning_detect import start_equipment_cleaning_detection,init_equipment_cleaning_detection
  7. from globals import inference_thread, stop_event,redis_client
  8. app = Flask(__name__)
  9. # Define the /wearing_detection endpoint
  10. @app.route('/equipment_cleaning_detection', methods=['GET'])
  11. def equipment_cleaning_detection():#开启平台搭设检测
  12. global inference_thread#当全局变量需要重新赋值时,需要用global关键字声明
  13. if inference_thread is None or not inference_thread.is_alive():#防止重复开启检测服务
  14. #redis_client.set("log_in_flag",'False')
  15. stop_event.clear()
  16. start_events = []#给每个线程一个事件,让我知道某个线程是否开始检测
  17. inference_thread = threading.Thread(target=start_equipment_cleaning_detection,args=(start_events,))
  18. inference_thread.start()
  19. app.logger.info('start_equipment_cleaning_detection')
  20. init_equipment_cleaning_detection()
  21. # 等待所有YOLO线程开始检测
  22. for event in start_events:
  23. event.wait()
  24. return jsonify({"status": "SUCCESS"}), 200
  25. else:
  26. app.logger.info("reset_detection already running")
  27. return jsonify({"status": "ALREADY_RUNNING"}), 200
  28. @app.route('/equipment_cleaning_status', methods=['GET'])
  29. def equipment_cleaning_status():#获取平台搭设状态状态
  30. if not redis_client.exists('equipment_cleaning_order'):#平台搭设步骤还没有一个完成
  31. app.logger.info('equipment_cleaning_order is none')
  32. return jsonify({"status": "NONE"}), 200
  33. else:
  34. basket_cleaning_order = redis_client.lrange("equipment_cleaning_order", 0, -1)
  35. json_array = []
  36. for value in basket_cleaning_order:
  37. match = re.search(r'equipment_step_(\d+)', value)
  38. step_number = match.group(1)
  39. json_object = {"step": step_number, "image": redis_client.get(f"equipment_step_{step_number}")}
  40. json_array.append(json_object)
  41. return jsonify({"status": "SUCCESS","data":json_array}), 200
  42. @app.route('/basket_cleaning_finish', methods=['GET'])
  43. def equipment_cleaning_finish():#开始登录时,检测是否需要复位,若需要,则发送复位信息,否则开始焊接检测
  44. stop_inference_internal()
  45. app.logger.info('bequipment_cleaning_order')
  46. return jsonify({"status": "SUCCESS"}), 200
  47. def stop_inference_internal():
  48. global inference_thread
  49. if inference_thread is not None and inference_thread.is_alive():
  50. stop_event.set() # 设置停止事件标志,通知推理线程停止运行
  51. inference_thread.join() # 等待推理线程结束
  52. inference_thread = None # 释放线程资源
  53. app.logger.info('detection stopped')
  54. return True
  55. else:
  56. app.logger.info('No inference stopped')
  57. return False
  58. @app.route('/stop_detection', methods=['GET'])
  59. def stop_inference():
  60. #global inference_thread
  61. if stop_inference_internal():
  62. app.logger.info('detection stopped')
  63. return jsonify({"status": "DETECTION_STOPPED"}), 200
  64. else:
  65. app.logger.info('No_detection_running')
  66. return jsonify({"status": "No_detection_running"}), 200
  67. @app.route('/images/<filename>')
  68. def get_image(filename):
  69. app.logger.info('get_image'+filename)
  70. return send_from_directory('static/images', filename)
  71. if __name__ == '__main__':
  72. # Start the Flask server
  73. app.run(debug=False, host='172.16.20.163', port=5006)