https://docs.aws.amazon.com/ko_kr/rekognition/latest/dg/labels-detect-labels-image.html
์ด๋ฏธ์ง์์ ๋ ์ด๋ธ ๊ฐ์ง - Amazon Rekognition
๊ธฐ๊ณ ๋ฒ์ญ์ผ๋ก ์ ๊ณต๋๋ ๋ฒ์ญ์ ๋๋ค. ์ ๊ณต๋ ๋ฒ์ญ๊ณผ ์๋ณธ ์์ด์ ๋ด์ฉ์ด ์์ถฉํ๋ ๊ฒฝ์ฐ์๋ ์์ด ๋ฒ์ ์ด ์ฐ์ ํฉ๋๋ค. ์ด๋ฏธ์ง์์ ๋ ์ด๋ธ ๊ฐ์ง ์ด DetectLabels์์ ์ ์ฌ์ฉํ์ฌ ์ด๋ฏธ์ง์์ ๋ ์ด๋ธ (
docs.aws.amazon.com
- ๋ฉ๋ด์ผ์ ์ฐธ๊ณ ํ๋ค.
์๋๋ฅผ ์ฐธ๊ณ ํ์ฌ ๋ฒํท์ ๋ง๋ ๋ค.
https://msdev-st.tistory.com/155
[AWS] S3 ๋ฒํท ๋ง๋ค๊ธฐ _ ์คํ ๋ฆฌ์ง ๋ง๋ค์ด์ ์ฌ์ฉํ๊ธฐ
S3 ๋ฒํท ๋ง๋ค๊ธฐ _ ์คํ ๋ฆฌ์ง ๋ง๋ค์ด์ ์ฌ์ฉํ๊ธฐAWS์ S3๋? Simple Storage Service์ ์ฝ์๋ก ํ์ผ ์๋ฒ์ ์ญํ ์ ํ๋ ์๋น์ค๋ค. ์ผ๋ฐ์ ์ธ ํ์ผ์๋ฒ๋ ํธ๋ํฝ์ด ์ฆ๊ฐํจ์ ๋ฐ๋ผ์ ์ฅ๋น๋ฅผ ์ฆ์คํ๋ ์
msdev-st.tistory.com
aws์ IAM์์, AmazonRekognitionFullAcess ๊ถํ์ถ๊ฐํ๊ธฐ
serverless๋ก ๋ง๋ ํด๋๋ฅผ vscode๋ก ์ฐ๋ค.
๋ฒํท ๋ง๋ค ๋ ์ฌ์ฉํ ์ด๋ฆ์ ๋ฃ์ผ๋ฉด ๋๋ค.
app.py ์์ฑ
from flask import Flask
from flask_restful import Api
from resources.image import FileUploadResource
app = Flask(__name__)
api = Api(app)
# ๊ฒฝ๋ก์ ๋ฆฌ์์ค๋ฅผ ์ฐ๊ฒฐํ๋ค.
api.add_resource( FileUploadResource , '/upload' )
if __name__ == '__main__' :
app.run()
resources ํด๋ ์์ image.py ์์ฑ
from flask_restful import Resource
from flask import request
from datetime import datetime
import boto3
from config import Config
class FileUploadResource(Resource) :
def post(self):
# 1. ํด๋ผ์ด์ธํธ๋ก๋ถํฐ ๋ฐ์ดํฐ๋ฅผ ๋ฐ์์จ๋ค.
# ํ์ผ์ request.files ์์ ์๊ณ ,
# ํ
์คํธ๋ request.form ์์ ์๋ค.
if 'photo' not in request.files :
return {"result":"fail", "error":"ํ์ผ์ ์
๋ก๋ ํ์ธ์."}, 400
if 'content' not in request.form :
return {"result":"fail", "error":"๋ด์ฉ์ ์์ฑ ํ์ธ์."}, 400
file = request.files.get('photo')
if 'image' not in file.content_type :
return {'result':'fail',
'error':'์ด๋ฏธ์ง ํ์ผ๋ง ์
๋ก๋ ๊ฐ๋ฅํฉ๋๋ค'}, 400
print(file)
content = request.form['content']
print(content)
# ํ์ผ์ s3์ ์
๋ก๋ ํด์ผ ํ๋๋ฐ,
# ๋จผ์ , ํ์ผ๋ช
์ ์ ๋ํฌ ํด์ผ ํ๋ค.
# ๋ฐ๋ผ์, ์ ๋ํฌํ ํ์ผ๋ช
์ผ๋ก ๋ฐ๊ฟ์ ์
๋ก๋ ํ๋ค.
# ํ์ฌ์๊ฐ๊ณผ ์ ์ ์์ด๋ ๋ฑ์ ์กฐํฉํด์ ๋ง๋ ๋ค.
current_time = datetime.now()
file_name = current_time.isoformat().replace(':','_') + '.' + file.content_type.split('/')[-1]
print(file_name)
# ์ ์ ๊ฐ ์
๋ก๋ํ ํ์ผ๋ช
์, ๋ด๊ฐ ๋ง๋ ํ์ผ๋ช
์ผ๋ก ๋ฐ๊พผ๋ค.
file.filename = file_name
# s3์ ํ์ผ์ ์
๋ก๋ํ๋ค.
# aws์ ์๋น์ค๋ค์ ํ์ด์ฌ์ฝ๋๋ก ์์ฑ ํ ์ ์๋
# boto3 ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ด์ฉํด์ ์ฝ๋๋ฅผ ์์ฑํ๋ค.
# ( ์ค์น๋, $ pip install boto3 )
client = boto3.client('s3' ,
aws_access_key_id = Config.AWS_ACCESS_KEY,
aws_secret_access_key = Config.AWS_SECRET_ACCESS_KEY)
# ExtraArgs = {'ACL':'public-read','ContentType':'image/jpg'} ์ค์๋ถ๋ถ
try :
client.upload_fileobj(file,
Config.S3_BUCKET,
file_name,
ExtraArgs = {'ACL':'public-read',
'ContentType':file.content_type})
except Exception as e:
return {"result":"fail", "error":str(e)}, 500
return {"result":"success", "url":Config.S3_URL + file_name}
vscode cmd์์ ์ค์นํ๋ค.
pip install boto3
rekognition.py ์์ฑ
from flask import request
from flask_restful import Resource
from datetime import datetime
import boto3
from config import Config
class ObjectDetectionResource(Resource):
def post(self):
if 'photo' not in request.files :
return {"result":"fail", "error":"์ฌ์ง์ ํ์์
๋๋ค."}, 400
file = request.files['photo']
if 'image' not in file.content_type :
return {"result":"fail", "error":"์ด๋ฏธ์งํ์ผ์ ์
๋ก๋ ํ์ธ์."}, 400
current_time = datetime.now()
file_name = current_time.isoformat().replace(':','_') + '.jpg'
file.filename = file_name
# s3์ ์
๋ก๋
# rekognition ์๋น์ค๋ฅผ ์ด์ฉํ๋ ค๋ฉด,
# ๋จผ์ s3์ ์ด๋ฏธ์ง ํ์ผ์ ์
๋ก๋ ํด๋์ผ ํ๋ค
client = boto3.client('s3',
aws_access_key_id = Config.AWS_ACCESS_KEY,
aws_secret_access_key = Config.AWS_SECRET_ACCESS_KEY)
try :
client.upload_fileobj(file,
Config.S3_BUCKET,
file_name,
ExtraArgs = {'ACL' : 'public-read',
'ContentType':'image/jpeg'})
except Exception as e:
return {"result":"fail", "error":str(e)}, 500
# ๋ฆฌ์ฝ๊ทธ๋์
์ ์ด์ฉ
label_list = self.detect_labels(file_name, Config.S3_BUCKET)
return {"result":"success", "lable": label_list}
def detect_labels(self, photo, bucket):
client = boto3.client('rekognition',
'ap-northeast-2',
aws_access_key_id = Config.AWS_ACCESS_KEY,
aws_secret_access_key = Config.AWS_SECRET_ACCESS_KEY)
response = client.detect_labels(Image={'S3Object':{'Bucket':bucket,'Name':photo}},
MaxLabels=10,
# Uncomment to use image properties and filtration settings
#Features=["GENERAL_LABELS", "IMAGE_PROPERTIES"],
#Settings={"GeneralLabels": {"LabelInclusionFilters":["Cat"]},
# "ImageProperties": {"MaxDominantColors":10}}
)
print('Detected labels for ' + photo)
print()
print(response['Labels'])
label_list = []
for label in response['Labels']:
print("Label: " + label['Name'])
label_list.append(label['Name'])
return label_list
์ฌ์ง ์ ๋ก๋ API ํฌ์คํธ๋งจ ์์ฑ
sendํ๊ณ , s3 ๋ฒํท์ ๋ค์ด๊ฐ๋ณด๋ฉด ์๋์ฒ๋ผ ์ฌ์ง์ sendํ ๋ชฉ๋ก๋ค์ด ๋ฌ๋ค.
object detection API ํฌ์คํธ๋งจ ์์ฑํ๊ณ sendํ๋ฉด
vscode์์๋ ์ด๋ ๊ฒ ๋ฌ๋ค.
.jpeg, .jpg ๋ฑ์ ํ์ ์ค๋ช ํํ์ด์ง ๋งํฌ์ด๋ค.
https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
Common MIME types - HTTP | MDN
This topic lists the most common MIME types with corresponding document types, ordered by their common extensions.
developer.mozilla.org