Label objects in an image
Setup the backend
If you haven't already done so, run amplify init
inside your project and then amplify add auth
(we recommend selecting the default configuration).
Run amplify add predictions
and select Identify. Then use the following answers:
? What would you like to identify? Identify Text Identify Entities❯ Identify Labels Learn More
? Would you like use the default configuration? (Use arrow keys)❯ Default Configuration Advanced Configuration
? Who should have access? Auth and Guest users
The Advanced Configuration will allow you to select moderation for unsafe content or all of the identified labels. Default uses both.
NOTE: Due to a known issue, you will need to update your amplify backend configuration with the properties below before pushing to the cloud:
amplify/backend/identify-labels-resource/parameters.json
"access": "auth | authAndGuest","type": "LABELS | UNSAFE | ALL"
Now run amplify push
which will generate your amplifyconfiguration.json
and create resources in the cloud. You can now either add this to your backend or skip and add more features to your app.
Services used: Amazon Rekognition
Working with the API
Detect labels, such if an image has a desk or a chair in it
import { Predictions } from '@aws-amplify/predictions';
Predictions.identify({ labels: { source: { file }, type: 'LABELS' }}) .then((response) => { const { labels } = response; labels.forEach((object) => { const { name, boundingBoxes } = object; }); }) .catch((err) => console.log({ err }));
Detect unsafe content in an image
import { Predictions } from '@aws-amplify/predictions';
Predictions.identify({ labels: { source: { file }, type: 'UNSAFE' }}) .then((response) => { const { unsafe } = response; // boolean }) .catch((err) => console.log({ err }));
for both labels and unsafe content
import { Predictions } from '@aws-amplify/predictions';
Predictions.identify({ labels: { source: { file }, type: 'ALL' }}) .then((response) => { const { labels } = response; const { unsafe } = response; // boolean labels.forEach((object) => { const { name, boundingBoxes } = object; }); }) .catch((err) => console.log({ err }));