Rest API

AWS Rekognition 얼굴비교, 이미지 내 텍스트 추출하기

왕현성 2023. 2. 21. 11:13
728x90

 

얼굴 비교

 

 

 

text 감지

 

class PhotoRekognitionResource(Resource) :
    def post(self) :
        
        if 'photo' not in request.files :
            return {'error':'파일 업로드 하세요'},400

        file = request.files['photo']

        # 클라이언트가 보낸 파일의 파일명을
        # 변경시켜서 S3에 올려야 유니크하게 
        # 파일을 관리할 수 있다.

        # 파일명을 유니크하게 만드는 방법
        current_time=datetime.now()
        new_file_name=current_time.isoformat().replace(':','_') + '.jpg'

        print(new_file_name)

        # 파일명을, 유니크한 이름으로 변경한다.
        # 클라이언트에서 보낸 파일명을 대체!

        file.filename = new_file_name

        # S3에 파일을 업로드하면 된다.
        # S3에 파일 업로드하는 라이브러리가 필요
        # boto3 라이브러리를 이용해서 업로드한다.
        # 참고 : 라이브러리 설치는 pip install boto3

        client=boto3.client('s3',
                    aws_access_key_id = Config.ACCESS_KEY ,
                    aws_secret_access_key = Config.SECRET_ACCESS)
        
        try :
            client.upload_fileobj(file,Config.S3_BUCKET,new_file_name,
                                    ExtraArgs ={'ACL':'public-read','ContentType':file.content_type})
        
        except Exception as e :
            return {'error':str(e)}, 500

        # 리코그니션 서비스를 이용할 수 있는지
        # 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_text(Image={'S3Object':{'Bucket':Config.S3_BUCKET,'Name':new_file_name}})
        print(response)

        textDetections=response['TextDetections']
        print(textDetections)
        print ('Detected text\n----------')
        textList = []
        for text in textDetections:
                print ('Detected text:' + text['DetectedText'])
                textList.append(text['DetectedText'])
                print ('Confidence: ' + "{:.2f}".format(text['Confidence']) + "%")
                print ('Id: {}'.format(text['Id']))
                if 'ParentId' in text:
                    print ('Parent Id: {}'.format(text['ParentId']))
                print ('Type:' + text['Type'])
                print()

        

        # 위의 response에서 필요한 데이터만 가져와서
        # 클라이언트에게 보내준다
        # labels : [  ]


        return {'result':'success','textList':textList},200

포스트맨 실행 결과 ( 한글 )

포스트맨 실행 결과 ( 영어 )

 

위 결과를 보니 자동 언어 식별과 다국어 문서에 대한 인식율은 낮아 보입니다.