Browse Source

更新AI开关接口

lang 3 years ago
parent
commit
b31a3b0775
1 changed files with 84 additions and 7 deletions
  1. 84 7
      Controller/AiController.py

+ 84 - 7
Controller/AiController.py

@@ -78,8 +78,8 @@ class AiView(View):
         response = ResponseObject()
         if operation is None:
             return response.json(444, 'error path')
-        elif operation == 'dopaypalcallback':  # paypal支付回调
-            return self.do_pay_by_paypal_callback(request_dict, response)
+        elif operation == 'identification':  # ai识别
+            return self.do_ai_identification(request_dict, response)
         else:
             token = request_dict.get('token', None)
             # 设备主键uid
@@ -229,11 +229,12 @@ class AiView(View):
                 # utko.generate(data={'uid': uid})
                 etkObj = ETkObject(etk='')
                 etk = etkObj.encrypt(uid)
-                detectUrl = "{DETECT_PUSH_DOMAIN}AiService/push?etk={etk}&endTime={endTime}". \
-                    format(etk=etk, DETECT_PUSH_DOMAIN=SERVER_DOMAIN_SSL, endTime=endTime)
-                aiIdentificationUrl = "{DETECT_PUSH_DOMAIN}AiService/identification?etk={etk}&endTime={endTime}". \
-                    format(etk=etk, DETECT_PUSH_DOMAIN=SERVER_DOMAIN_SSL, endTime=endTime)
-                return response.json(0, {'detectUrl': detectUrl,'aiIdentificationUrl':aiIdentificationUrl})
+
+                #只返回一个接口就行
+                # detectUrl = "{DETECT_PUSH_DOMAIN}AiService/push?etk={etk}&endTime={endTime}". \
+                #     format(etk=etk, DETECT_PUSH_DOMAIN=SERVER_DOMAIN_SSL, endTime=endTime)
+                aiIdentificationUrl = "{DETECT_PUSH_DOMAIN}AiService/identification".format(DETECT_PUSH_DOMAIN=SERVER_DOMAIN_SSL)
+                return response.json(0, {'aiIdentificationUrl':aiIdentificationUrl, 'endTime':endTime, 'etk':etk})
         else:
             return response.json(14)
 
@@ -325,3 +326,79 @@ class AiView(View):
                                      use_status=use_status, detect_group='1')
             return response.json(0, {"orderID": orderID})
         return response.json(10, 'generate_order_false')
+
+
+    def do_ai_identification(self, request_dict):
+        etk = request_dict.get('etk', None)
+        if etk:
+            eto = ETkObject(etk)
+            uid = eto.uid
+            file_post_one = request_dict.get('file_one', None)
+            file_post = file_post_one.replace(' ', '+')
+            # file_decode = base64.b64decode(file_post)
+
+            file_post_two = request_dict.get('file_two', None)
+            file_post_two = file_post_two.replace(' ', '+')
+            # file_decode_two = base64.b64decode(file_post_two)
+
+            now_time = int(time.time())
+            dir_path = os.path.join(BASE_DIR, 'static/ai/')
+            if not os.path.exists(dir_path):
+                os.makedirs(dir_path)
+
+            file_path_one = dir_path + 'one' + str(now_time) + '.jpeg'
+            file_path_two = dir_path + 'two' + str(now_time) + '.jpeg'
+
+            file_list = ['one', 'two']
+
+            for index in file_list:
+                file_path = dir_path + index + str(now_time) + '.jpeg'
+                with open(file_path, 'wb') as f:
+                    # file_byte = file_post.encode('utf-8')
+                    f.write(file_decode)
+
+            return HttpResponse("seccess")
+            file2 = request.FILES.get('file_one', None)
+            file3 = request.FILES.get('file_two', None)
+            file4 = request.FILES.get('file_three', None)
+
+            print('--------------------------file')
+            print(file)
+
+            print('===========================post_file')
+            print(file_post)
+
+            post_file_list = [file, file2, file3, file4]
+            file_list = []
+            for index in range(len(post_file_list)):
+                if post_file_list[index]:
+                    file_list.append(post_file_list[index])
+
+            del post_file_list
+            if len(file_list) > 1:
+                merge = []
+                now_time = int(time.time())
+                dir_path = os.path.join(BASE_DIR, 'static/', str(now_time))
+                if not os.path.exists(dir_path):
+                    os.makedirs(dir_path)
+                for item in file_list:
+                    if hasattr(item, 'name'):
+                        file_path = os.path.join(dir_path, item.name)
+                        with open(file_path, 'wb') as f:
+                            for c in item.chunks():
+                                f.write(c)
+                                merge.append(file_path)
+
+                image_size = 500  # 每张小图片的大小
+                image_colnum = 2  # 合并成一张图后,一行有几个小图
+                MergePic.merge_images(dir_path, image_size, image_colnum)
+                # return HttpResponse(dir_path + '.jpg')
+                photo = open(dir_path + '.jpg', 'rb')
+            else:
+                photo = file_list[0]
+
+
+        else:
+            return HttpResponse("fail")
+
+