peng 2 жил өмнө
parent
commit
fab8030980

+ 8 - 0
AnsjerPush/config.py

@@ -130,3 +130,11 @@ APP_BUNDLE_DICT = {
     'com.ansjer.customizeda_a': 'Guardian365',
     'com.ansjer.customizedc_a': 'PatrolSecure',
 }
+
+# AI识别标签
+AI_IDENTIFICATION_TAGS_DICT = {
+    '1': 'Person',
+    '2': 'Pet',
+    '3': 'Vehicle',
+    '4': 'Package'
+}

+ 84 - 1
Controller/AiController.py

@@ -19,7 +19,7 @@ import jpush
 from boto3.session import Session
 from django.views.generic.base import View
 from pyfcm import FCMNotification
-from AnsjerPush.config import SERVER_TYPE
+from AnsjerPush.config import SERVER_TYPE, AI_IDENTIFICATION_TAGS_DICT
 from AnsjerPush.config import AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, APNS_MODE, APNS_CONFIG, BASE_DIR, \
     JPUSH_CONFIG, FCM_CONFIG
 from Model.models import UidPushModel, AiService
@@ -278,6 +278,89 @@ class AiView(View):
                     os.remove(c_path)
             os.rmdir(path)
 
+    ## 检查是否有符合条件的标签,并且返回标签坐标位置信息
+    def labelsCoords(self, user_detect_group, rekognition_res, image_size):
+        logger = logging.getLogger('info')
+        labels = rekognition_res['Labels']
+        label_name = []
+        label_list = []
+        logger.info('--------识别到的标签-------')
+        logger.info(labels)
+        all_labels_type = {
+            '1': ['Person', 'Human'],  # 人
+            '2': ['Pet', 'Dog', 'Canine', 'Animal', 'Puppy', 'Cat'],  # 动物
+            '3': ['Vehicle', 'Car', 'Transportation', 'Automobile', 'Bus'],  # 车
+            '4': ['Package', 'Carton', 'Cardboard', 'Package Delivery']  # 包裹
+        }
+        # 找出识别的所有标签
+        for label in labels:
+            label_name.append(label['Name'])
+            for Parents in label['Parents']:
+                label_name.append(Parents['Name'])
+
+        logger.info('标签名------')
+        logger.info(label_name)
+        # 删除用户没有选择的ai识别类型, 并且得出最终识别结果
+        user_detect_list = user_detect_group.split(',')
+        user_detect_list = [i.strip() for i in user_detect_list]
+        conform_label_list = []
+        conform_user_d_group = set()
+        for key, label_type_val in all_labels_type.items():
+            if key in user_detect_list:
+                for label in label_type_val:
+                    if label in label_name:
+                        conform_user_d_group.add(key)
+                        conform_label_list.append(label)
+        # 找出标签边框线位置信息
+        boundingBoxList = []
+        for label in labels:
+            if label['Name'] in conform_label_list:
+                for boundingBox in label['Instances']:
+                    boundingBoxList.append(boundingBox['BoundingBox'])
+
+        # 找出边框位置信息对应的单图位置并重新计算位置比
+        merge_image_height = image_size['height']
+        # merge_image_width = image_size['width']
+        single_height = merge_image_height // image_size['num']
+        new_bounding_box_dict = {}
+        new_bounding_box_dict['file_0'] = []
+        new_bounding_box_dict['file_1'] = []
+        new_bounding_box_dict['file_2'] = []
+        # new_bounding_box_dict['file_3'] = []
+        for k, val in enumerate(boundingBoxList):
+            boundingBoxTop = merge_image_height * val['Top']
+            # 找出当前边框属于哪张图片范围
+            boxDict = {}
+            for i in range(image_size['num']):
+                min = i * single_height  # 第n张图
+                max = (i + 1) * single_height
+                if boundingBoxTop >= min and boundingBoxTop <= max:
+                    # print("属于第{i}张图".format(i=i+1))
+                    boxDict['Width'] = val['Width']
+                    boxDict['Height'] = merge_image_height * val['Height'] / single_height
+                    boxDict['Top'] = ((merge_image_height * val['Top']) - (
+                            i * single_height)) / single_height  # 减去前i张图片的高度
+                    boxDict['Left'] = val['Left']
+                    new_bounding_box_dict["file_{i}".format(i=i)].append(boxDict)
+        # exit(new_bounding_box_list)
+        conform_user_d_group = list(conform_user_d_group)
+        if len(conform_user_d_group) > 1:
+            conform_user_d_group.sort()
+            # 集成识别标签
+            for label_key in conform_user_d_group:
+                label_list.append(AI_IDENTIFICATION_TAGS_DICT[label_key])
+            eventType = ''.join(conform_user_d_group)  # 组合类型
+        else:
+            label_list.append(AI_IDENTIFICATION_TAGS_DICT[conform_user_d_group[0]])
+            eventType = conform_user_d_group[0]
+
+        logger.info('------conform_user_d_group------ {}'.format(conform_user_d_group))
+        logger.info('------label_list------ {}'.format(label_list))
+
+        return {'eventType': eventType, 'label_list': label_list,
+                'new_bounding_box_dict': new_bounding_box_dict}
+
+
     def upload_s3(self, file_dict, dir_path):
         try:
             if SERVER_TYPE == "Ansjer.formal_settings" or SERVER_TYPE == 'Ansjer.eur_formal_settings':