|
@@ -19,6 +19,9 @@ from Object.UidTokenObject import UidTokenObject
|
|
|
from Service.CommonService import CommonService
|
|
|
from django.db.models import Q, F
|
|
|
from time import strftime
|
|
|
+from Ansjer.config import BASE_DIR
|
|
|
+from Object import MergePic
|
|
|
+import os
|
|
|
|
|
|
|
|
|
class TestServeView(View):
|
|
@@ -56,10 +59,41 @@ class TestServeView(View):
|
|
|
|
|
|
def AItest(self, userID, request, request_dict, response):
|
|
|
file = request.FILES.get('file', None)
|
|
|
- if not file:
|
|
|
- return response.json(444)
|
|
|
- maxLabels = int(request_dict.get('maxLabels', 5))
|
|
|
- minConfidence = float(request_dict.get('minConfidence', 55))
|
|
|
+ file2 = request.FILES.get('file_one', None)
|
|
|
+ file3 = request.FILES.get('file_two', None)
|
|
|
+ file4 = request.FILES.get('file_three', None)
|
|
|
+
|
|
|
+ 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]
|
|
|
+
|
|
|
+ maxLabels = int(request_dict.get('maxLabels', 10))
|
|
|
+ minConfidence = float(request_dict.get('minConfidence', 30))
|
|
|
|
|
|
try:
|
|
|
client = boto3.client(
|
|
@@ -71,7 +105,7 @@ class TestServeView(View):
|
|
|
# https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/rekognition.html#Rekognition.Client.detect_labels
|
|
|
rekognition_res = client.detect_labels(
|
|
|
Image={
|
|
|
- 'Bytes': file.read()},
|
|
|
+ 'Bytes': photo.read()},
|
|
|
MaxLabels=maxLabels,
|
|
|
MinConfidence=minConfidence)
|
|
|
print('rekognition_res: ', rekognition_res)
|
|
@@ -79,3 +113,47 @@ class TestServeView(View):
|
|
|
except Exception as e:
|
|
|
print(e)
|
|
|
return response.json(500, repr(e))
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ files1 = request.FILES.get('image1')
|
|
|
+ files2 = request.FILES.get('image2')
|
|
|
+ files3 = request.FILES.get('image3')
|
|
|
+ files4 = request.FILES.get('image4')
|
|
|
+ file_list = [files1,files2,files3,files4]
|
|
|
+ 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)
|
|
|
+ files = open(dir_path+'.jpg','rb')
|
|
|
+ # return HttpResponse(files.read())
|
|
|
+
|
|
|
+
|
|
|
+ labels = int(request_dict.get('labels',5))
|
|
|
+ minConfidence = int(request_dict.get('minConfidence',99))
|
|
|
+ if not files:
|
|
|
+ return HttpResponse('请上传图片!!!!')
|
|
|
+ client = boto3.client('rekognition', aws_access_key_id='AKIA2E67UIMD6JD6TN3J',aws_secret_access_key='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',region_name='us-east-1')
|
|
|
+ # image = open('E:/photo/a615fa40b8c476bab0f6eeb332e62a5a-1000.jpg', "rb")
|
|
|
+ response = client.detect_labels(Image={'Bytes':files.read()},MaxLabels=labels,MinConfidence=minConfidence)
|
|
|
+ # for obj in response['Labels']:
|
|
|
+ # exit(obj)
|
|
|
+ # if obj['Name'] == 'Person':
|
|
|
+ # jsonstr = json.dumps(obj)
|
|
|
+ return HttpResponse(json.dumps(response, ensure_ascii=False),
|
|
|
+ content_type="application/json,charset=utf-8")
|