Skip to content
Open

S3 #2

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 1 addition & 23 deletions SmartCane/smartcane_app/serializers.py
Original file line number Diff line number Diff line change
@@ -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)


35 changes: 32 additions & 3 deletions SmartCane/smartcane_app/views.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -13,13 +12,17 @@
import tensorflow as tf
import os
import io
from io import BytesIO
import numpy as np
from PIL import Image
import time
import asyncio
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')

Expand All @@ -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 = []

Expand Down Expand Up @@ -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")