소스 검색

完成初步识别逻辑

dxc 7 달 전
부모
커밋
69e903b7e4
6개의 변경된 파일92개의 추가작업 그리고 41개의 파일을 삭제
  1. 23 6
      README.md
  2. 7 15
      app.py
  3. 52 7
      compressed_oxygen_detect.py
  4. 5 5
      config.py
  5. 4 7
      globals.py
  6. 1 1
      post_demo.py

+ 23 - 6
README.md

@@ -1,9 +1,26 @@
 # self_rescuer
 ## 压缩氧
+## 过程
+1. 外壳去掉
+2. 脖带戴好 
+3. 咬口具
+4. 氧气瓶开启
+5. 补气
+6. 带鼻夹
 
-1 = 外壳去掉
-2 = 脖带戴好 
-3 = 咬口具
-4 = 氧气瓶开启
-5 = 补气
-6 = 带鼻夹
+## 模型label名称
+- chemical_oxygen_self-rescuer
+- aerostat_gasbag
+- neckband
+- head
+- mouthpiece
+- valve
+- air make-up
+- hand
+- noseclip
+- compressed_oxygen_self_rescuer
+- cloth_cover
+- compression_airbags
+- starter_pin
+- activate
+- pin

+ 7 - 15
app.py

@@ -37,25 +37,17 @@ def compressed_oxygen_detection():
 @app.route('/compressed_oxygen_status', methods=['GET']) 
 def compressed_oxygen_status():#开始登录时,检测是否需要复位,若需要,则发送复位信息,否则开始焊接检测
     #global inference_thread
-    with lock:   
-        #TODO 若出现异常再发送FAIL.
-        redis_client.set("welding_wearing_detection_img_flag",'True')
-        time.sleep(1)
-        if not redis_client.exists("welding_wearing_items_nums") or not redis_client.exists("welding_wearing_detection_img"):
-            return jsonify({"status": "NONE"}), 200##表示穿戴检测线程还未检测完
-        
-        wearing_items_nums = redis_client.lrange("welding_wearing_items_nums", 0, -1)
-        wearing_items_list = ['pants', 'jacket', 'helmet', 'gloves', 'shoes']
+    with lock:           
+        compressed_oxygen_order = redis_client.lrange("compressed_oxygen_order", 0, -1)
+        # wearing_items_list = ['pants', 'jacket', 'helmet', 'gloves', 'shoes']
         json_array = []
-        for num, item in zip(wearing_items_nums, wearing_items_list):
-            json_object = {"name": item, "number": num}
+        for step in compressed_oxygen_order:
+            json_object = {"step": step}
             json_array.append(json_object)
 
         app.logger.info(json_array)
-        image=redis_client.get("welding_wearing_detection_img")
-        app.logger.info(image)
 
-        return jsonify({"status": "SUCCESS","data":json_array,"image":image}), 200
+        return jsonify({"status": "SUCCESS","data":json_array}), 200
 
                
 @app.route('/end_compressed_oxygen_detection', methods=['GET'])
@@ -97,4 +89,4 @@ def get_image(filename):
 if __name__ == '__main__':
 
     # Start the Flask server
-    app.run(debug=False, host='172.16.20.163', port=5007)
+    app.run(debug=False, host='127.0.0.1', port=5007)

+ 52 - 7
compressed_oxygen_detect.py

@@ -3,12 +3,13 @@ import cv2
 import threading
 from datetime import datetime
 from ultralytics import YOLO
-from globals import stop_event,redis_client
-from config import SAVE_IMG_PATH,POST_IMG_PATH1,VIDEO_SOURCE,MODEL_PATH
+from globals import stop_event,redis_client,steps,hand_box,head_box
+from config import VIDEO_SOURCE,MODEL_PATH
 
 
 def init_compressed_oxygen_detection():
-    pass
+    for i, step in enumerate(steps):
+        redis_client.set(f"compressed_oxygen_step_{i+1}",'False')
 
 def start_compressed_oxygen_detection(start_events):
         
@@ -19,6 +20,24 @@ def start_compressed_oxygen_detection(start_events):
     thread.start()
     thread.join()
 
+def IoU(box1, box2):
+    '''
+    计算两个矩形框的交并比
+    :param box1: list,第一个矩形框的左上角和右下角坐标
+    :param box2: list,第二个矩形框的左上角和右下角坐标
+    :return: 两个矩形框的交并比iou
+    '''
+    x1 = max(box1[0], box2[0])   # 交集左上角x
+    x2 = min(box1[2], box2[2])   # 交集右下角x
+    y1 = max(box1[1], box2[1])   # 交集左上角y
+    y2 = min(box1[3], box2[3])   # 交集右下角y
+ 
+    overlap = max(0., x2-x1) * max(0., y2-y1)
+    union = (box1[2]-box1[0]) * (box1[3]-box1[1]) \
+            + (box2[2]-box2[0]) * (box2[3]-box2[1]) \
+            - overlap
+ 
+    return overlap/union
 
 def process_video(model_path, video_source, start_event):
 
@@ -35,7 +54,7 @@ def process_video(model_path, video_source, start_event):
             if cap.get(cv2.CAP_PROP_POS_FRAMES) % 10 != 0:#跳帧检测,
                 continue
 
-
+            global step,hand_box,head_box
             results = model.predict(frame,conf=0.6,verbose=False)
 
             for r in results:
@@ -43,16 +62,42 @@ def process_video(model_path, video_source, start_event):
                 confidences = r.boxes.conf  # 提取所有检测到的置信度
                 classes = r.boxes.cls  # 提取所有检测到的类别索引
 
-                
+                head_box=[]
+                hand_box=[]
                 for i in range(len(boxes)):
                     x1, y1, x2, y2 = boxes[i].tolist()
                     confidence = confidences[i].item()
                     cls = int(classes[i].item())
                     label = model.names[cls]
                     print("label:",label)
-                #pass
-                
+
+                    if(label=='head'):head_box=[x1,y1,x2,y2]
+                    if(label=='hand'):hand_box=[x1,y1,x2,y2]
+
+                    if(label=='aerostat_gasbag'):
+                        steps[0]=True
+                        if(IoU(head_box,[x1,y1,x2,y2])>0.1):steps[2]=True
+
+                    if(label=='neckband'):
+                        if(IoU(head_box,[x1,y1,x2,y2])>0.1):steps[1]=True
+
+
+                    if(label=='valve'):
+                        if(IoU(hand_box,[x1,y1,x2,y2])>0.1):steps[2]=True
                     
+                    if(label=='air make-up'):
+                        steps[0]=True
+                        if(IoU(hand_box,[x1,y1,x2,y2])>0.1):steps[4]=True
+                        
+                    if(label=='nose clip'):
+                        if(IoU(head_box,[x1,y1,x2,y2])>0.1):steps[5]=True
+                
+
+                for i, step in enumerate(steps):
+                    if step and redis_client.get(f"compressed_oxygen_step_{i+1}")=='False':
+                        redis_client.rpush("compressed_oxygen_order", f"{i+1}")
+                        redis_client.set(f"compressed_oxygen_step_{i+1}",'True')
+
                 start_event.set()    
 
 

+ 5 - 5
config.py

@@ -1,14 +1,14 @@
-import numpy as np
+# import numpy as np
 
 
 #CLIENT_URL = 'http://172.16.20.23:8081/'
 
-SAVE_IMG_PATH = '/mnt/xcd/code/ai_test/static/images'  # 图片保存在服务器的实际位置
+# SAVE_IMG_PATH = '/mnt/xcd/code/ai_test/static/images'  # 图片保存在服务器的实际位置
 
-POST_IMG_PATH1 = 'http://172.16.20.163:5001/images'  # 通过端口映射post发送能够访问的位置 焊接考核科目1
-POST_IMG_PATH2 = 'http://172.16.20.163:5002/images' #焊接考核科目2
+# POST_IMG_PATH1 = 'http://172.16.20.163:5001/images'  # 通过端口映射post发送能够访问的位置 焊接考核科目1
+# POST_IMG_PATH2 = 'http://172.16.20.163:5002/images' #焊接考核科目2
 
-VIDEO_SOURCE = 'rtsp://admin:yaoan1234@172.16.22.233/cam/realmonitor?channel=1&subtype=0'  # 视频源路径
+VIDEO_SOURCE = 'rtsp://admin:yaoan1234@192.169.1.114/cam/realmonitor?channel=1&subtype=0'  # 视频源路径
 #焊接考核视频流
 # Define paths to RTSP streams
 

+ 4 - 7
globals.py

@@ -3,7 +3,7 @@ import threading
 import redis
 import time
 # 连接到 Redis 服务器
-redis_client = redis.StrictRedis(host='localhost', port=5050, db=0,decode_responses=True)
+redis_client = redis.StrictRedis(host='127.0.0.1', port=6379, db=0,decode_responses=True)
 
 inference_thread = None
 stop_event = threading.Event()
@@ -11,12 +11,9 @@ lock=threading.Lock()
 
 ###############焊接考核
 #为True时,表示某一步骤完成,并保存图片post
-step1=False 
-step2=False
-step3=False
-step4=False
-step5=False
-step6=False 
+steps = [False] * 6
+head_box=[]
+hand_box=[]
 # 压缩氧
 # 1 = 外壳去掉
 # 2 = 脖带戴好 

+ 1 - 1
post_demo.py

@@ -1,6 +1,6 @@
 import requests
 
-url = 'http://172.16.20.163:5007/compressed_oxygen_detection'
+url = 'http://127.0.0.1:5007/compressed_oxygen_detection'
 
 
 response = requests.get(url)