Google Vision API是Google Cloud提供的一项图像分析服务,能够通过机器学习技术检测图像中的对象、人脸、文本等内容,并提供多种分析功能。
pip install --upgrade google-cloud-vision
from google.cloud import vision
def detect_text(path):
"""检测图像中的文本"""
client = vision.ImageAnnotatorClient.from_service_account_file('your-service-account.json')
with open(path, 'rb') as image_file:
content = image_file.read()
image = vision.Image(content=content)
response = client.text_detection(image=image)
texts = response.text_annotations
for text in texts:
print(f'\n"{text.description}"')
vertices = [(v.x, v.y) for v in text.bounding_poly.vertices]
print(f'bounds: {vertices}')
if response.error.message:
raise Exception(f'{response.error.message}')
detect_text('path/to/your/image.jpg')
def detect_labels(path):
"""检测图像中的标签"""
client = vision.ImageAnnotatorClient.from_service_account_file('your-service-account.json')
with open(path, 'rb') as image_file:
content = image_file.read()
image = vision.Image(content=content)
response = client.label_detection(image=image)
labels = response.label_annotations
print('Labels:')
for label in labels:
print(f'{label.description} ({label.score*100:.2f}%)')
detect_labels('path/to/your/image.jpg')
def detect_faces(path):
"""检测图像中的人脸"""
client = vision.ImageAnnotatorClient.from_service_account_file('your-service-account.json')
with open(path, 'rb') as image_file:
content = image_file.read()
image = vision.Image(content=content)
response = client.face_detection(image=image)
faces = response.face_annotations
print('Faces:')
for face in faces:
print(f'Detection confidence: {face.detection_confidence}')
print(f'Joy likelihood: {face.joy_likelihood}')
print(f'Sorrow likelihood: {face.sorrow_likelihood}')
print(f'Anger likelihood: {face.anger_likelihood}')
print(f'Surprise likelihood: {face.surprise_likelihood}')
detect_faces('path/to/your/image.jpg')
def batch_annotate_images(input_uris):
"""批量处理图像"""
client = vision.ImageAnnotatorClient.from_service_account_file('your-service-account.json')
requests = []
for uri in input_uris:
request = {
'image': {'source': {'image_uri': uri}},
'features': [
{'type_': vision.Feature.Type.FACE_DETECTION},
{'type_': vision.Feature.Type.LABEL_DETECTION}
]
}
requests.append(request)
response = client.batch_annotate_images(requests)
for image_response in response.responses:
print(f'Labels: {[label.description for label in image_response.label_annotations]}')
print(f'Faces found: {len(image_response.face_annotations)}')
batch_annotate_images(['gs://your-bucket/image1.jpg', 'gs://your-bucket/image2.jpg'])
def detect_text_uri(uri):
"""从远程URI检测文本"""
client = vision.ImageAnnotatorClient.from_service_account_file('your-service-account.json')
image = vision.Image()
image.source.image_uri = uri
response = client.text_detection(image=image)
texts = response.text_annotations
for text in texts:
print(f'\n"{text.description}"')
if response.error.message:
raise Exception(f'{response.error.message}')
detect_text_uri('https://example.com/image.jpg')
问题: 收到"403 Forbidden"或类似错误
解决方案:
问题: 图像太大导致处理失败
解决方案:
问题: 收到速率限制错误
解决方案: