get /object_detection?filename=abc.jpg
오브젝트 디텍션 결과를 응답하는 API
filename의 value값은 변환해준 파일네임
aws IAM으로 가서 rekognition 권한을 추가해줘야 한다
client = boto3.client('rekognition',
'ap-northeast-2',
aws_access_key = Config.ACCESS_KEY ,
aws_secret_access_key = Config.SECRET_ACCESS)
boto를 이용해
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 ()
rekognition 활용 코드
레이블 내용들
from flask import request
from flask_jwt_extended import jwt_required
from flask_restful import Resource
from mysql.connector import Error
from flask_jwt_extended import get_jwt_identity
from mysql_connection import get_connection
from datetime import datetime
import boto3
from config import Config
class ObjectDetectionResource(Resource) :
# s3에 저장된 이미지를
# 객체 탐지하는 API
def get(slef) :
# 1. 클라이언트로부터 파일명을 받아온다
filename = request.args.get('filename')
# 2. 위의 파일은 이미 s3에 있다는 상황
# 따라서 aws의 rekognition 인공지능 서비스를
#이용해서 object detection을 한다.
# 리코그니션 서비스를 이용할수 있는지 IAM의 유저권한 확인하고 설정
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 )
print(response)
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
최종 코드
레이블 내용이 결과로 호출
'Api 개발 > flask' 카테고리의 다른 글
네이버 검색 api 활용 (0) | 2023.01.13 |
---|---|
사진과 내용을 동시에 올리는 api (0) | 2023.01.13 |
이미지 업로드 api 만들기 (0) | 2023.01.12 |
github로 lambda CI/CD하기 (0) | 2023.01.12 |
serverless 계층(layer) 설정 (0) | 2023.01.11 |