2019-10-20 03:29:07 +03:00
|
|
|
# -*- 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:
|
2019-10-20 04:47:23 +03:00
|
|
|
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}")
|