diff --git a/SmartCane/smartcane_app/serializers.py b/SmartCane/smartcane_app/serializers.py index fb72593..c7dec7f 100644 --- a/SmartCane/smartcane_app/serializers.py +++ b/SmartCane/smartcane_app/serializers.py @@ -1,24 +1,2 @@ -from rest_framework import serializers -#from django.core.serializers import serialize - -class DirectionSerializer(serializers.Serializer): - result = serializers.CharField() - - class Meta: - fields = ('result') - - -class Image(object): - def __init__(self,): - self.image = image - -class ImageSerializer(serializers.Serializer): - image = serializers.FileField() - - class Meta: - fields = ('image') - - # def create(self, validated_data): - # return Image(**validated_data) - + \ No newline at end of file diff --git a/SmartCane/smartcane_app/views.py b/SmartCane/smartcane_app/views.py index f2cd1a9..9213e9f 100644 --- a/SmartCane/smartcane_app/views.py +++ b/SmartCane/smartcane_app/views.py @@ -1,7 +1,6 @@ from django.shortcuts import render from rest_framework import views from rest_framework.response import Response -from .serializers import DirectionSerializer, ImageSerializer from rest_framework.decorators import api_view from rest_framework.views import APIView from django.http.response import JsonResponse @@ -13,6 +12,7 @@ import tensorflow as tf import os import io +from io import BytesIO import numpy as np from PIL import Image import time @@ -20,6 +20,9 @@ import json from django.http import JsonResponse from collections import ChainMap +from django.conf import settings +import boto3 +import uuid gpus = tf.config.experimental.list_physical_devices('GPU') @@ -38,7 +41,7 @@ file_name = os.path.dirname(__file__) + '/pspunet_weight.h5' model = keras.models.load_model(file_name) - +print(tf.__version__) #result = {} result_list = [] @@ -138,12 +141,38 @@ async def caution_async_process(): @api_view(['GET','POST']) def direction(request): if request.method == "POST": - bytes = request.FILES['file'].file.getvalue() + get_file = request.FILES['file'] + bytes = get_file.file.getvalue() image = Image.open(io.BytesIO(bytes)).convert("RGB") result_list.clear() predict(image) newdict = {} newdict["result"]=result_list + + access_key = settings.AWS_ACCESS_KEY_ID + access_secret_key = settings.AWS_SECRET_ACCESS_KEY + bucket_name = settings.AWS_STORAGE_BUCKET_NAME + + s3_client = boto3.client( + 's3', + aws_access_key_id = access_key, + aws_secret_access_key = access_secret_key + ) + im = Image.open(get_file) + im = im.resize((480, 272)) + buffer = BytesIO() + im.save(buffer, "JPEG") + buffer.seek(0) + + url_generator = str(uuid.uuid4()) + s3_client.upload_fileobj( + buffer, + "jgback", + url_generator, + ExtraArgs = { + "ContentType": 'image/jpeg' + } + ) return JsonResponse(newdict) else: return JsonResponse("GET Method")