python图像分类_python-根据相似性对图像进行分类

我建议您使用AWS Rekognition进行操作.很简单

您可以通过3个简单的步骤来实现所需的目标:

1.上载带有元数据的图像:表示您要将具有其姓名的人的图像上载到s3,以存储其信息以供以后参考

2.为照片建立索引:这意味着向面部添加信息标签,该信息存储在dynamodb中,并通过index_faces api完成

3.比较带有索引面孔的照片:这将通过rekognition search_faces_by_image api实现

现在第1部分代码:使用元数据批量上传

import boto3

s3 = boto3.resource('s3')

# Get list of objects for indexing

images=[('image01.jpeg','Albert Einstein'),

('image02.jpeg','Candy'),

('image03.jpeg','Armstrong'),

('image04.jpeg','Ram'),

('image05.jpeg','Peter'),

('image06.jpeg','Shashank')

]

# Iterate through list to upload objects to S3

for image in images:

file = open(image[0],'rb')

object = s3.Object('rekognition-pictures','index/'+ image[0])

ret = object.put(Body=file,

Metadata={'FullName':image[1]}

)

现在第2部分代码:索引编制

from __future__ import print_function

import boto3

from decimal import Decimal

import json

import urllib

print('Loading function')

dynamodb = boto3.client('dynamodb')

s3 = boto3.client('s3')

rekognition = boto3.client('rekognition')

# --------------- Helper Functions ------------------

def index_faces(bucket, key):

response = rekognition.index_faces(

Image={"S3Object":

{"Bucket": bucket,

"Name": key}},

CollectionId="family_collection")

return response

def update_index(tableName,faceId, fullName):

response = dynamodb.put_item(

TableName=tableName,

Item={

'RekognitionId': {'S': faceId},

'FullName': {'S': fullName}

}

)

# --------------- Main handler ------------------

def lambda_handler(event, context):

# Get the object from the event

bucket = event['Records'][0]['s3']['bucket']['name']

key = urllib.unquote_plus(

event['Records'][0]['s3']['object']['key'].encode('utf8'))

try:

# Calls Amazon Rekognition IndexFaces API to detect faces in S3 object

# to index faces into specified collection

response = index_faces(bucket, key)

# Commit faceId and full name object metadata to DynamoDB

if response['ResponseMetadata']['HTTPStatusCode'] == 200:

faceId = response['FaceRecords'][0]['Face']['FaceId']

ret = s3.head_object(Bucket=bucket,Key=key)

personFullName = ret['Metadata']['fullname']

update_index('family_collection',faceId,personFullName)

# Print response to console

print(response)

return response

except Exception as e:

print(e)

print("Error processing object {} from bucket {}. ".format(key, bucket))

raise e

现在第3部分代码:比较

import boto3

import io

from PIL import Image

rekognition = boto3.client('rekognition', region_name='eu-west-1')

dynamodb = boto3.client('dynamodb', region_name='eu-west-1')

image = Image.open("group1.jpeg")

stream = io.BytesIO()

image.save(stream,format="JPEG")

image_binary = stream.getvalue()

response = rekognition.search_faces_by_image(

CollectionId='family_collection',

Image={'Bytes':image_binary}

)

for match in response['FaceMatches']:

print (match['Face']['FaceId'],match['Face']['Confidence'])

face = dynamodb.get_item(

TableName='family_collection',

Key={'RekognitionId': {'S': match['Face']['FaceId']}}

)

if 'Item' in face:

print (face['Item']['FullName']['S'])

else:

print ('no match found in person lookup')

使用上面的比较功能,您将获得照片中人脸的名称,然后您可以决定下一步要做什么,例如通过重命名照片将具有相同名称的照片存储到其他文件夹中,这将为不同文件夹中的不同人提供照片

先决条件:

创建一个名为family_collection的识别集合

aws rekognition create-collection --collection-id family_collection --region eu-west-1

创建一个名为family_collection的动态表

aws dynamodb create-table --table-name family_collection \n--attribute-definitions AttributeName=RekognitionId,AttributeType=S \n--key-schema AttributeName=RekognitionId,KeyType=HASH \n--provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1 \n--region eu-west-1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值