Skip to content

Commit

Permalink
Feat : #24 ai server 연결 (이미지 바이트)
Browse files Browse the repository at this point in the history
  • Loading branch information
gilyeon00 committed Sep 29, 2022
1 parent 6ebed16 commit 4849ec4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 39 deletions.
8 changes: 4 additions & 4 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,16 +147,16 @@ def post(self):
@ladder_api.route('/convert-image')
class ConvertImage(Resource):
def post(self):
origin_image = request.files['file']

originImage = request.files['file']
print('originImage request OK')

result_url = saveOriginImage(origin_image)
print("saveImage OK")
result_url = saveOriginImage(originImage)
print("save Origin Image OK")

return result_url



## Create user
@app.route('/create-user', methods=['POST'])
def create_user():
Expand Down
3 changes: 1 addition & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,4 @@ redis
requests
pillow
pybase64
bytesbufio
matplotlib
bytesbufio
49 changes: 16 additions & 33 deletions service/image_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,19 @@
import jwt as pyjwt
import redis

import base64
from PIL import Image
from flask import Flask, jsonify, request
from PIL import Image as pilImage
import json
from io import BytesIO
import matplotlib.pyplot as plt

import base64

secrets_key = 'Ladder_teamc'
jwt_redis = redis.StrictRedis(host='redis', port=6379, decode_responses=True)



app = Celery('tasks', broker=RBMQ_CONNECTION_URI)



# 사진 받아오는 함수
def saveOriginImage(file) :
user_id = 1

Expand All @@ -47,7 +44,7 @@ def saveOriginImage(file) :
# postgres image table에 업로드
origin_url = "https://ladder-s3-bucket.s3.ap-northeast-2.amazonaws.com/origin/"+origin_image_name
origin_url = origin_url.replace(" ","/")
database.add_instance(Image, user_id = user_id, origin_url = origin_url, is_deleted = False)
database.add_instance(Image, user_id = user_id, origin_url = origin_url)

# ai 셀러리 요청, 이제 요 다음부터 비동기처리
convertImage.delay(origin_url)
Expand All @@ -59,38 +56,24 @@ def convertImage(origin_url):
req = {'img' : origin_url}
print(req)

res = requests.post(AI_CONVERT_API,json=req)
result_json = res.json()
print("convertImage def : 333333333333")
# result_url type : <class 'requests.models.Response'>
json_data = requests.post(AI_CONVERT_API,json=req)
dict_data = json.loads(json_data)
img = dict_data['img']
img = base64.b64decode(img)
img = BytesIO(img)
img = pilImage.open(img)
print("====================sdsdsdsd")
print(type(img))

# image_dict = {
# "converted_image_name": file_name,
# "converted_image.png": encoded_string.decode()
# }
saveResultImage(img)

converted_image_name = result_json['converted_image_name']
converted_image = result_json['converted_image.png']
converted_image = converted_image.open(BytesIO(base64.b64decode(base64_string)))

print("===============app.task finish====================s")
print("===============app.task finish====================s")
print("===============app.task finish====================s")
print(converted_image_name)

result_url = saveResultImage(converted_image)
print(result_url)
print("===============app.task finish====================s")

return result_url



# 변환된 사진 저장
def saveResultImage(file) :
print("filename================")
# AttributeError: 'Response' object has no attribute 'filename'
# AttributeError: 'bytes' object has no attribute 'filename'

user_id =1
# 파일 이름 지정
filename = file.filename.split('.')[0]
Expand All @@ -105,7 +88,7 @@ def saveResultImage(file) :
result_url = "https://ladder-s3-bucket.s3.ap-northeast-2.amazonaws.com/result/"+result_image_name
result_url = result_url.replace(" ","/")

database.add_instance(Image, user_id = user_id, result_url = result_url, is_deleted = False)
database.add_instance(Image, user_id = user_id, result_url = result_url)

print("성공적으로 변환된 사진이 S3에 저장되었습니다.")

Expand Down

0 comments on commit 4849ec4

Please sign in to comment.