зеркало из https://github.com/Azure/azcv.git
78 строки
2.2 KiB
Python
78 строки
2.2 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright (c) Microsoft Corporation. All rights reserved.
|
|
# Licensed under the MIT License.
|
|
# Author: Graham.Williams@togaware.com
|
|
#
|
|
# A script to detect faces in an image.
|
|
#
|
|
# ml faces azcv <path>
|
|
|
|
from msrest.authentication import CognitiveServicesCredentials
|
|
from azure.cognitiveservices.vision.computervision import ComputerVisionClient
|
|
|
|
import os
|
|
import argparse
|
|
from textwrap import fill
|
|
|
|
from mlhub.pkg import azkey, is_url
|
|
from mlhub.utils import get_cmd_cwd
|
|
|
|
# ----------------------------------------------------------------------
|
|
# Parse command line arguments
|
|
# ----------------------------------------------------------------------
|
|
|
|
option_parser = argparse.ArgumentParser(add_help=False)
|
|
|
|
option_parser.add_argument(
|
|
'path',
|
|
help='path or url to image')
|
|
|
|
args = option_parser.parse_args()
|
|
|
|
# ----------------------------------------------------------------------
|
|
|
|
SERVICE = "Computer Vision"
|
|
KEY_FILE = os.path.join(os.getcwd(), "private.txt")
|
|
|
|
# Request subscription key and endpoint from user.
|
|
|
|
subscription_key, endpoint = azkey(KEY_FILE, SERVICE, verbose=False)
|
|
|
|
# Set credentials.
|
|
|
|
credentials = CognitiveServicesCredentials(subscription_key)
|
|
|
|
# Create client.
|
|
|
|
client = ComputerVisionClient(endpoint, credentials)
|
|
|
|
# ----------------------------------------------------------------------
|
|
# URL or path
|
|
# ----------------------------------------------------------------------
|
|
|
|
path = args.path
|
|
|
|
# Check the URL supplied or path exists and is an image.
|
|
|
|
# ----------------------------------------------------------------------
|
|
# Analyze
|
|
# ----------------------------------------------------------------------
|
|
|
|
image_features = ["faces"]
|
|
|
|
# Send provided image (url or path) to azure to analyse.
|
|
|
|
if is_url(path):
|
|
analysis = client.analyze_image(path, image_features)
|
|
else:
|
|
path = os.path.join(get_cmd_cwd(), path)
|
|
with open(path, 'rb') as fstream:
|
|
analysis = client.analyze_image_in_stream(fstream, image_features)
|
|
|
|
for face in analysis.faces:
|
|
print(f"{face.face_rectangle.left} {face.face_rectangle.top} " +
|
|
f"{face.face_rectangle.left + face.face_rectangle.width} " +
|
|
f"{face.face_rectangle.top + face.face_rectangle.height}," +
|
|
f"{face.gender},{face.age}")
|