Skip to content

Instantly share code, notes, and snippets.

@jorke
Last active May 3, 2021 03:54
Show Gist options
  • Save jorke/1fbb6f41cc0d41d0a3c4859eb604882d to your computer and use it in GitHub Desktop.
Save jorke/1fbb6f41cc0d41d0a3c4859eb604882d to your computer and use it in GitHub Desktop.
rekognition-testing.ipnyb
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
import io
import boto3
from PIL import Image, ImageDraw, ExifTags, ImageColor, ImageFont
r = boto3.client("rekognition")
# ignore anything below this confidence level
confidence = 80
#filepath
testimage = "testing/glass81.jpg"
image = Image.open(testimage)
def display_bbox(image, labels):
# Ready image to draw bounding boxes on it.
imgWidth, imgHeight = image.size
draw = ImageDraw.Draw(image)
for label in labels:
print('Label ' + str(label['Name']))
print('Confidence ' + str(label['Confidence']))
if 'Instances' in label:
for instance in label['Instances']:
box = instance['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), f"{label['Name']} - {label['Confidence']}", 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()
return image
%time
fd = open(testimage, 'rb')
img = fd.read()
fd.close()
response = r.detect_labels(Image={'Bytes': img})
labels = [{ x['Name'],x['Confidence']} for i,x in enumerate(response['Labels']) if x['Confidence'] > confidence]
display_bbox(image,response['Labels'])
# start model
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...')
project_arn='arn:aws:rekognition:xxxxx'
model_arn='arn:aws:rekognition:xxxx'
min_inference_units=1
version_name='xxxx'
start_model(project_arn, model_arn, version_name, min_inference_units)
custom_labels = r.detect_custom_labels(Image={'Bytes': img},ProjectVersionArn=model_arn)
[{ x['Name'],x['Confidence']} for i,x in enumerate(custom_labels['CustomLabels']) if x['Confidence']]
display_bbox(image,custom_labels['CustomLabels'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment