فهرست منبع

AI识别新增存储S3&dynamoDB

zhangdongming 2 سال پیش
والد
کامیت
8af4678ac8
2فایلهای تغییر یافته به همراه64 افزوده شده و 1 حذف شده
  1. 63 0
      Controller/AiController.py
  2. 1 1
      Object/DynamodbObject.py

+ 63 - 0
Controller/AiController.py

@@ -25,6 +25,7 @@ from AnsjerPush.config import AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, APNS_MOD
 from AnsjerPush.config import CONFIG_INFO
 from Model.models import UidPushModel, AiService, VodHlsTag, VodHlsTagType
 from Object import MergePic
+from Object.DynamodbObject import DynamodbObject
 from Object.ETkObject import ETkObject
 from Object.ResponseObject import ResponseObject
 from Object.TokenObject import TokenObject
@@ -150,6 +151,8 @@ class AiView(View):
             logger.info('-----记录返回labels')
             logger.info('labels')
             # return response.json(0,labels)
+            # 将识别结果存到S3以及DynamoDB
+            AiView.store_image_results_to_dynamo_and_s3(file_path_list, uid, channel, n_time, labels, rekognition_res)
             if len(labels['label_list']) == 0:
                 # 需要删除图片
                 # photo.close()
@@ -527,3 +530,63 @@ class AiView(View):
             print(repr(e))
             logger.info(repr(e))
             return repr(e)
+
+    @staticmethod
+    def store_image_results_to_dynamo_and_s3(file_path_list, uid, channel, n_time, labels_data,
+                                             reko_result):
+        """
+        将图片识别结果存储到dynamoDB并且存储到S3
+        @param file_path_list: 图片名称列表
+        @param uid: 设备uid
+        @param channel: 设备通道号
+        @param n_time: 设备触发移动侦测时间戳
+        @param labels_data: 标签数据(经过reko_result结果进行计算后的数据)
+        @param reko_result: rekognition 响应结果
+        @return: 保存结果
+        """
+        logger = logging.getLogger('info')
+        try:
+            file_dict = {}
+            for i, val in enumerate(file_path_list):
+                file_dict[val] = "{uid}/{channel}/{n_time}_{i}.jpeg".format(uid=uid, channel=channel,  # 封面图
+                                                                            n_time=n_time, i=i)
+            if not reko_result:
+                logger.info('{}识别结果为空'.format(uid))
+                return False
+            if CONFIG_INFO != CONFIG_US:  # 目前只上美洲
+                return False
+            # 存美洲
+            session = Session(aws_access_key_id=AWS_ACCESS_KEY_ID[1],
+                              aws_secret_access_key=AWS_SECRET_ACCESS_KEY[1],
+                              region_name="us-west-1")
+            s3 = session.resource("s3")
+            bucket = "rekognition-pic-results"
+            # 上传到S3 rekognition-pic-results
+            for file_path, upload_path in file_dict.items():
+                logger.info('{}文件路径{}'.format(uid, file_path))
+                upload_data = open(file_path, "rb")
+                s3.Bucket(bucket).put_object(Key=upload_path, Body=upload_data)
+            # reko结果存储到dynamoDB
+            event_type = 0
+            new_bounding_box_dict = ''
+            if len(labels_data['label_list']) > 0:
+                event_type = int(labels_data['eventType'])
+                new_bounding_box_dict = json.dumps(labels_data['new_bounding_box_dict'])
+
+            table_name = 'asj_push_message'  # 表名称
+            dynamo = DynamodbObject(AWS_ACCESS_KEY_ID[1], AWS_SECRET_ACCESS_KEY[1], 'us-west-1')
+            item = {'device_uid': {'S': uid},  # 设备uid
+                    'event_time': {'N': str(n_time)},  # 设备触发时间戳,也用作S3资源对象名前缀
+                    'ai_coordinate': {'S': new_bounding_box_dict},  # ai坐标框信息
+                    'channel': {'N': str(channel)},  # 设备通道号
+                    'event_type': {'N': str(event_type)},  # 事件类型
+                    'is_pic': {'N': '3'},  # 1:图片,2:视频,3:多图
+                    'reko_result': {'S': json.dumps(reko_result)},  # reko识别结果
+                    'storage_region': {'N': '2'},  # 存储平台1:阿里云,2:AWS
+                    }
+            result = dynamo.put_item(table_name, item)
+            logger.info('{}识别后存S3与DynamoDB成功{}'.format(uid, result))
+            return True
+        except Exception as e:
+            logger.info('{}识别后存S3与DynamoDB失败:{}'.format(uid, repr(e)))
+            return False

+ 1 - 1
Object/DynamoDBObject.py → Object/DynamodbObject.py

@@ -1,6 +1,6 @@
 # -*- encoding: utf-8 -*-
 """
-@File    : DynamoDBObject.py
+@File    : DynamodbObject.py
 @Time    : 2023/8/29 10:05
 @Author  : stephen
 @Email   : zhangdongming@asj6.wecom.work