Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create TomAWSrekognitioncode #9

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 144 additions & 0 deletions TomAWSrekognitioncode
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
#Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#PDX-License-Identifier: MIT-0 (For details, see https://github.com/awsdocs/amazon-rekognition-custom-labels-developer-guide/blob/master/LICENSE-SAMPLECODE.)

import boto3

def start_model(project_arn, model_arn, version_name, min_inference_units):

client=boto3.client('rekognition')

try:
# Start the model
print('Starting model: ' + model_arn)
response=client.start_project_version(ProjectVersionArn=model_arn, MinInferenceUnits=min_inference_units)
# Wait for the model to be in the running state
project_version_running_waiter = client.get_waiter('project_version_running')
project_version_running_waiter.wait(ProjectArn=project_arn, VersionNames=[version_name])

#Get the running status
describe_response=client.describe_project_versions(ProjectArn=project_arn,
VersionNames=[version_name])
for model in describe_response['ProjectVersionDescriptions']:
print("Status: " + model['Status'])
print("Message: " + model['StatusMessage'])
except Exception as e:
print(e)

print('Done...')

def main():
project_arn='arn:aws:rekognition:us-east-2:833742200021:project/Foodrecognition/1624546398379'
model_arn='arn:aws:rekognition:us-east-2:833742200021:project/Foodrecognition/version/Foodrecognition.2021-06-24T10.53.50/1624546430747'
min_inference_units=1
version_name='Foodrecognition.2021-06-24T10.53.50'
start_model(project_arn, model_arn, version_name, min_inference_units)

if __name__ == "__main__":
main()

#Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#PDX-License-Identifier: MIT-0 (For details, see https://github.com/awsdocs/amazon-rekognition-custom-labels-developer-guide/blob/master/LICENSE-SAMPLECODE.)

import boto3
import io
from PIL import Image, ImageDraw, ExifTags, ImageColor, ImageFont

def display_image(bucket,photo,response):
# Load image from S3 bucket
s3_connection = boto3.resource('s3')

s3_object = s3_connection.Object(bucket,photo)
s3_response = s3_object.get()

stream = io.BytesIO(s3_response['Body'].read())
image=Image.open(stream)

# Ready image to draw bounding boxes on it.
imgWidth, imgHeight = image.size
draw = ImageDraw.Draw(image)

# calculate and display bounding boxes for each detected custom label
print('Detected custom labels for ' + photo)
for customLabel in response['CustomLabels']:
print('Label ' + str(customLabel['Name']))
print('Confidence ' + str(customLabel['Confidence']))
if 'Geometry' in customLabel:
box = customLabel['Geometry']['BoundingBox']
left = imgWidth * box['Left']
top = imgHeight * box['Top']
width = imgWidth * box['Width']
height = imgHeight * box['Height']

fnt = ImageFont.truetype('/Library/Fonts/Arial.ttf', 50)
draw.text((left,top), customLabel['Name'], fill='#00d400', font=fnt)

print('Left: ' + '{0:.0f}'.format(left))
print('Top: ' + '{0:.0f}'.format(top))
print('Label Width: ' + "{0:.0f}".format(width))
print('Label Height: ' + "{0:.0f}".format(height))

points = (
(left,top),
(left + width, top),
(left + width, top + height),
(left , top + height),
(left, top))
draw.line(points, fill='#00d400', width=5)

image.show()

def show_custom_labels(model,bucket,photo, min_confidence):
client=boto3.client('rekognition')

#Call DetectCustomLabels
response = client.detect_custom_labels(Image={'S3Object': {'Bucket': bucket, 'Name': photo}},
MinConfidence=min_confidence,
ProjectVersionArn=model)

# For object detection use case, uncomment below code to display image.
# display_image(bucket,photo,response)

return len(response['CustomLabels'])

def main():

bucket='MY_BUCKET'
photo='MY_IMAGE_KEY'
model='arn:aws:rekognition:us-east-2:833742200021:project/Foodrecognition/version/Foodrecognition.2021-06-24T10.53.50/1624546430747'
min_confidence=95

label_count=show_custom_labels(model,bucket,photo, min_confidence)
print("Custom labels detected: " + str(label_count))


if __name__ == "__main__":
main()


#Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#PDX-License-Identifier: MIT-0 (For details, see https://github.com/awsdocs/amazon-rekognition-custom-labels-developer-guide/blob/master/LICENSE-SAMPLECODE.)

import boto3
import time


def stop_model(model_arn):

client=boto3.client('rekognition')

print('Stopping model:' + model_arn)

#Stop the model
try:
response=client.stop_project_version(ProjectVersionArn=model_arn)
status=response['Status']
print ('Status: ' + status)
except Exception as e:
print(e)

print('Done...')

def main():

model_arn='arn:aws:rekognition:us-east-2:833742200021:project/Foodrecognition/version/Foodrecognition.2021-06-24T10.53.50/1624546430747'
stop_model(model_arn)