From the image sent using line We have created a bot that performs gender, age prediction, and facial expression analysis.
Like this
When you send an image, the analysis result will be returned (up to 3 people can be seen at the same time)
The source code is here (I was satisfied when I tried to make it with almost solid writing for the time being I've lost the energy to refactor)
Composition like this
Since you need the url of the image when sending the image to the line bot, I got an error when I tried to reply the image with the following code
s3_url = generate_presigned_url(
ClientMethod='get_object',
Params={
'Bucket': BUCKET,
'Key': KEY
},
ExpiresIn=60,
HttpMethod='GET'
)
line_bot_api.reply_message(
reply_token=event.reply_token,
messages=ImageSendMessage(
original_content_url=s3_url,
preview_image_url=s3_url
)
)
LineBotApiError: status_code=400, request_id=xxxxx-xxx-xxx-xxx-xxxxxxxxxx, error_response=
{
"details": [
{
"message": "Length must be between 0 and 1000",
"property": "messages[0].originalContentUrl"
},
{
"message": "Length must be between 0 and 1000",
"property": "messages[0].previewImageUrl"
}
],
"message": "The request body has 2 error(s)"
}
url was too long and got angry ...
Issuing a signed url for s3 is too long to send Create a new public s3 bucket and I decided to get the url by putting the image to send in s3 with read-only
#Give access and put
client.put_object(ACL='public-read', Bucket=bucketname, Body=image, Key=key)
#url is created by string concatenation
s3_pub_url = 'https://' + bucketname + '.s3-ap-northeast-1.amazonaws.com/' + key
line_bot_api.reply_message(
reply_token=event.reply_token,
messages=ImageSendMessage(
original_content_url=s3_pub_url,
preview_image_url=s3_pub_url
)
)
I've never used s3 public access Let's look at the access authority, etc. It took a long time to play around with it.
To draw the BoundingBox on the image I got an error when trying to import a PIL module
Unable to import module 'lambda_function': cannot import name '_imaging'
It works locally, but if it is lambda, the PIL module cannot be imported and an error occurs When I asked Google teacher, it seems that Pillow's library has a part that depends on os. So, it seems that you need Pillow library installed in the environment where lambda runs (= Amazon Linux)
Reference site: https://michimani.net/post/aws-use-pillow-in-lambda/
In the above article, I created a library using docker, ~~ It seemed to be troublesome ~~, so I decided to create a library using cloud9
--Creating a cloud9 environment
--Import lambda
--Install Pillow library in the target folder
python3 -m pip install Pillow -t ./
--Download the target folder, upload it to s3 and deploy it
--Cloud9 environment deleted
I don't want to spend as much money as possible The cloud9 environment was used only to create the Pillow library
When downloaded from cloud9, it is compressed with zip, Since it is compressed for each folder, if you deploy lambda as it is, the function cannot be called and an error occurs (1 loss)
--Refactoring
--The code is almost solid.
I want to separate at least the process of accessing the DB
--Organization around AWS permissions
―― ~~ It's troublesome to think about various things ~~, lambda iam role has unnecessary privileges
I want to organize so that I do not have unnecessary privileges
--DynamoDB design
――Since I've only touched RDB until now, I don't know how to use it or its merits.
The usage has become RDB-like, so I would like to increase my knowledge and think about the design again.
Recommended Posts