app3.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import threading
  2. import time
  3. from flask import Flask, jsonify,send_from_directory
  4. from platform_wearing_detect import start_wearing_detection,init_wearing_detection
  5. from globals import inference_thread, stop_event,lock,redis_client
  6. app = Flask(__name__)
  7. #app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # 16 MB
  8. # Define the /wearing_detection endpoint
  9. @app.route('/wearing_detection', methods=['GET'])
  10. def wearing_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_wearing_detection,args=(start_events,))
  16. inference_thread.start()
  17. init_wearing_detection()
  18. #print('start_wearing_detection')
  19. # 等待所有YOLO线程开始检测,两个线程检测完毕时,才返回SUCCESS
  20. for event in start_events:
  21. event.wait()
  22. app.logger.info('start_wearing_detection')
  23. return jsonify({"status": "SUCCESS"}), 200
  24. else:
  25. app.logger.info("start_wearing_detection already running")
  26. return jsonify({"status": "ALREADY_RUNNING"}), 200
  27. @app.route('/human_postion_status', methods=['GET'])
  28. def human_postion_status():#开始登录时,检测是否需要复位,若需要,则发送复位信息,否则开始焊接检测
  29. #global inference_thread
  30. if redis_client.get("platform_wearing_human_in_postion")=='False':
  31. app.logger.info('NOT_IN_POSTION')
  32. return jsonify({"status": "NOT_IN_POSTION"}), 200
  33. else:
  34. app.logger.info('IN_POSTION')
  35. return jsonify({"status": "IN_POSTION"}), 200
  36. @app.route('/wearing_status', methods=['GET'])
  37. def wearing_status():#开始登录时,检测是否需要复位,若需要,则发送复位信息,否则开始焊接检测
  38. #global inference_thread
  39. with lock:
  40. #TODO 若出现异常再发送FAIL.
  41. redis_client.set("platform_wearing_detection_img_flag",'True')
  42. time.sleep(1)
  43. if not redis_client.exists("platform_wearing_items_nums") or not redis_client.exists("platform_wearing_detection_img"):
  44. return jsonify({"status": "NONE"}), 200##表示穿戴检测线程还未检测完
  45. wearing_items_nums = redis_client.lrange("platform_wearing_items_nums", 0, -1)
  46. wearing_items_list = ['belt', 'helmet', 'shoe']
  47. json_array = []
  48. for num, item in zip(wearing_items_nums, wearing_items_list):
  49. json_object = {"name": item, "number": num}
  50. json_array.append(json_object)
  51. app.logger.info(json_array)
  52. image=redis_client.get("platform_wearing_detection_img")
  53. app.logger.info(image)
  54. return jsonify({"status": "SUCCESS","data":json_array,"image":image}), 200
  55. @app.route('/end_wearing_exam', methods=['GET'])
  56. def end_wearing_exam():
  57. init_wearing_detection()
  58. return jsonify({"status": "SUCCESS"}), 200
  59. def stop_inference_internal():
  60. global inference_thread
  61. if inference_thread is not None and inference_thread.is_alive():
  62. stop_event.set() # 设置停止事件标志,通知推理线程停止运行
  63. inference_thread.join() # 等待推理线程结束
  64. inference_thread = None # 释放线程资源
  65. app.logger.info('detection stopped')
  66. return True
  67. else:
  68. app.logger.info('No inference stopped')
  69. return False
  70. @app.route('/stop_detection', methods=['GET'])
  71. def stop_inference():
  72. #global inference_thread
  73. if stop_inference_internal():
  74. app.logger.info('detection stopped')
  75. return jsonify({"status": "DETECTION_STOPPED"}), 200
  76. else:
  77. app.logger.info('No_detection_running')
  78. return jsonify({"status": "No_detection_running"}), 200
  79. @app.route('/images/<filename>')
  80. def get_image(filename):
  81. app.logger.info('get_image'+filename)
  82. #pdb.set_trace()
  83. return send_from_directory('static/images', filename)
  84. if __name__ == '__main__':
  85. # Start the Flask server
  86. app.run(debug=False, host='172.16.20.163', port=5003)