Repository navigation
Expand file tree
/
Copy pathamazon_api.py
More file actions
44 lines (29 loc) · 1.66 KB
/
Copy pathamazon_api.py
File metadata and controls
44 lines (29 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import boto3
from googletrans import Translator
from dotenv import dotenv_values
import os
class amazon_api:
def call_api(self, img):
AWS_ACCESS_KEY_ID = os.environ['AWS_ACCESS_KEY_ID'] # AWSのアクセスキーID
AWS_SECRET_ACCESS_KEY = os.environ['AWS_SECRET_ACCESS_KEY'] # AWSのシークレットアクセスキー
# Source = '/Users/Minami-Yuta/就活用/サマーインターンシップ/Optim/img/yasai.jpg' # 検索対象となる画像を定義
# imageSource = open(img,'rb').read()
imageSource = img
# Amazon Rekognitionインスタンスを作成
rekognition = boto3.client('rekognition',
aws_access_key_id=AWS_ACCESS_KEY_ID,
aws_secret_access_key=AWS_SECRET_ACCESS_KEY ,
region_name='ap-northeast-1'
)
response = rekognition.detect_labels(Image={'Bytes': imageSource}) # 画像認識を実行
response_label_list = response['Labels'] # 出力ラベルを保持
return response_label_list
def output_label(self, response_label_list):
response_label_list_len = len(response_label_list)
translator = Translator() # 翻訳のためのインスタンス
label_name_ja_list = [] # 画像処理後の食材ラベルのリスト
for i in range(response_label_list_len):
label_name_en = response_label_list[i]['Name'] # ラベル名(英語)
label_name_ja = translator.translate(text=label_name_en, dest='ja', src='en').text # 英語->日本語に翻訳
label_name_ja_list.append(label_name_ja) # リストに追加
return label_name_ja_list