Rekognition이란 aws 객체 탐지 ai api다.
from datetime import datetime
from http import HTTPStatus
from os import access
from flask import request
from flask_jwt_extended import create_access_token, get_jwt, get_jwt_identity, jwt_required
from flask_restful import Resource
from mysql.connector.errors import Error
from config import Config
import boto3
boto3와 라이브러리를 import한다.
class ObjectDetctionResource(Resource):
def get(self):
# 1. 클라이언트로부터 데이터를 받아온다.
filename = request.args['filename']
# 2. 위의 파일은, S3에 저장되어 있어야 한다.
# rekognition 을 이용해서, object detection 한다.
client = boto3.client('rekognition', 'ap-northeast-2', aws_access_key_id = Config.ACCESS_KEY, aws_secret_access_key = Config.SECRET_ACCESS)
response = client.detect_labels(Image = {'S3Object': {'Bucket':Config.S3_BUCKET, 'Name':filename}}, MaxLabels = 10)
for label in response['Labels']:
print ("Label: " + label['Name'])
print ("Confidence: " + str(label['Confidence']))
print ("Instances:")
for instance in label['Instances']:
print (" Bounding box")
print (" Top: " + str(instance['BoundingBox']['Top']))
print (" Left: " + str(instance['BoundingBox']['Left']))
print (" Width: " + str(instance['BoundingBox']['Width']))
print (" Height: " + str(instance['BoundingBox']['Height']))
print (" Confidence: " + str(instance['Confidence']))
print()
print ("Parents:")
for parent in label['Parents']:
print (" " + parent['Name'])
print ("----------")
print ()
return {'result':'success', 'Labels':response['Labels']}, 200
클라이언트로부터 파일이름을 받고, boto3.client로 연결한다. detect_labels의 파라미터에 s3와 파일이름을 넣고 라벨 수를 조정해준 뒤 결과를 받아온다.
'REST API' 카테고리의 다른 글
boto3로 s3 파일 업로드하기 (0) | 2022.06.24 |
---|---|
Flask JWT 토큰 로그아웃 하기 (0) | 2022.06.21 |
Flask 토큰 유효기간 만료 시키는 방법 (0) | 2022.06.20 |
Flask에서 로그인한 유저만 처리할 수 있는 API에 토큰 적용하는 방법 (0) | 2022.06.20 |
Flask 회원가입/로그인 API에서, 토큰 생성해서 처리하는 방법 (0) | 2022.06.20 |