소스 검색

头像合规检查功能

locky 2 년 전
부모
커밋
0ea51366a2
3개의 변경된 파일71개의 추가작업 그리고 5개의 파일을 삭제
  1. 25 1
      Controller/TestApi.py
  2. 14 0
      Controller/UserManger.py
  3. 32 4
      Object/ContentSecurityObject.py

+ 25 - 1
Controller/TestApi.py

@@ -20,7 +20,7 @@ from django.utils.decorators import method_decorator
 from django.views.decorators.csrf import csrf_exempt
 from django.views.generic.base import View
 
-from Ansjer.config import CONFIG_INFO
+from Ansjer.config import CONFIG_INFO, REGION_NAME, AVATAR_BUCKET, ACCESS_KEY_ID, SECRET_ACCESS_KEY
 from Ansjer.config import OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET, AWS_ACCESS_KEY_ID, \
     AWS_SECRET_ACCESS_KEY, SERVER_TYPE, AWS_SES_ACCESS_REGION
 from Ansjer.config import SERVER_DOMAIN_SSL
@@ -126,6 +126,8 @@ class testView(View):
             return self.find_device_serial(request_dict, response)
         elif operation == 'ali_text_review':  # 阿里云文本审核
             return self.ali_text_review(request_dict, response)
+        elif operation == 'ali_image_review':  # 阿里云图片审核
+            return self.ali_image_review(request_dict, response)
         # elif operation == 'funboost':  # funboost测试
         #     return self.funboost_test(request_dict, response)
         else:
@@ -915,6 +917,28 @@ class testView(View):
         review_result = ContentSecurity().text_review(service, service_parameters)
         return response.json(0, review_result)
 
+    @staticmethod
+    def ali_image_review(request_dict, response):
+        aws_s3_client = boto3.client(
+            's3',
+            region_name=REGION_NAME,
+            aws_access_key_id=ACCESS_KEY_ID,
+            aws_secret_access_key=SECRET_ACCESS_KEY,
+            config=botocore.client.Config(signature_version='s3v4'),
+        )
+        # default/default.png
+        params = {'Bucket': AVATAR_BUCKET, 'Key': '100/习大大.jpg'}
+        try:
+            image_url = aws_s3_client.generate_presigned_url('get_object', Params=params)
+            print(image_url)
+            service = 'profilePhotoCheck'
+            service_dict = {'imageUrl': image_url}
+            service_parameters = json.dumps(service_dict)
+            legal = ContentSecurity().image_review(service, service_parameters)
+            return response.json(0, legal)
+        except Exception as e:
+            return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
+
     # @staticmethod
     # @boost("task_queue_name1", qps=0.5, broker_kind=BrokerEnum.REDIS_ACK_ABLE)  # 入参包括20种,运行控制方式非常多,想得到的控制都会有。
     # def task_fun(x, y):

+ 14 - 0
Controller/UserManger.py

@@ -157,6 +157,7 @@ class perfectUserInfoView(TemplateView):
         userID = tko.userID
         if not userID:
             return response.json(309)
+
         userIconPath = ''
         if userIcon:
             # 上传头像到aws s3
@@ -168,12 +169,25 @@ class perfectUserInfoView(TemplateView):
                 config=botocore.client.Config(signature_version='s3v4'),
             )
             Key = userID + '/' + userIcon.name
+
+            #  验证头像是否合规
+            params = {'Bucket': AVATAR_BUCKET, 'Key': Key}
+            image_url = aws_s3_client.get_object(Bucket=AVATAR_BUCKET, Key=Key)
+            service = 'profilePhotoCheck'
+            service_dict = {'imageUrl': image_url}
+            service_parameters = json.dumps(service_dict)
+            legal = ContentSecurity().image_review(service, service_parameters)
+            if not legal:
+                return response.json(108)
+
             aws_s3_client.put_object(Bucket=AVATAR_BUCKET, Key=Key, Body=userIcon)
             userIconPath = userID + '/' + userIcon.name
+
         if userContent:
             dataValid = json.loads(userContent)
             if 'userID' and 'password' and 'is_superuser' in dataValid.keys():
                 return response.json(444)
+
         if not userIconPath and not userContent:
             return response.json(444)
         elif not userIconPath and userContent:

+ 32 - 4
Object/ContentSecurityObject.py

@@ -7,8 +7,10 @@ from alibabacloud_green20220302 import models as green_20220302_models
 from alibabacloud_tea_util import models as util_models
 from Ansjer.config import ALI_ACCESS_KEY_ID, ALI_ACCESS_KEY_SECRET
 
-ILLEGAL_LABEL_LIST = ['political_content', 'profanity', 'contraband', 'sexual_content', 'violence', 'negative_content',
-                      'religion', 'cyberbullying']
+TEXT_ILLEGAL_LABEL_LIST = ['political_content', 'profanity', 'contraband', 'sexual_content', 'violence',
+                           'negative_content', 'religion', 'cyberbullying']
+IMAGE_ILLEGAL_LABEL_LIST = ['pornographic_adultContent', 'sexual_suggestiveContent', 'political_politicalFigure_1',
+                            'violent_blood', 'contraband_drug']
 
 
 class ContentSecurity:
@@ -37,7 +39,7 @@ class ContentSecurity:
 
     def text_review(self, service, service_parameters):
         """
-
+        文本审核
         @param service: 审核服务类型
         @param service_parameters: 审核服务参数集
         @return: bool, True: 审核通过, False: 审核不通过
@@ -52,6 +54,32 @@ class ContentSecurity:
         labels = res.body.data.labels
         labels_list = labels.split(',')
         for label in labels_list:
-            if label in ILLEGAL_LABEL_LIST:
+            if label in TEXT_ILLEGAL_LABEL_LIST:
                 return False
             return True
+
+    def image_review(self, service, service_parameters):
+        """
+        图片审核
+        @param service: 审核服务类型
+        @param service_parameters: 审核服务参数集
+        @return: bool, True: 审核通过, False: 审核不通过
+        """
+        client = self.create_client()
+        image_moderation_request = green_20220302_models.ImageModerationRequest(service, service_parameters)
+        runtime = util_models.RuntimeOptions()
+        res = client.image_moderation_with_options(image_moderation_request, runtime)
+        print(res)
+        if res.status_code != 200:
+            return False
+        # 获取审核结果
+        result = res.body
+        if result.code != 200:
+            return False
+        result_list = result.data.result
+        # 配到风险标签且置信分高于80时返回False
+        for result in result_list:
+            for result.label in IMAGE_ILLEGAL_LABEL_LIST:
+                if result.confidence >= 80:
+                    return False
+            return True