зеркало из https://github.com/Azure/azcv.git
81 строка
2.2 KiB
Python
81 строка
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 = ["color"]
|
|
|
|
# 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)
|
|
|
|
print(f"{not analysis.color.is_bw_img},{analysis.color.accent_color}," +
|
|
f"{analysis.color.dominant_color_background}," +
|
|
f"{analysis.color.dominant_color_foreground},", end='')
|
|
start = ''
|
|
for c in analysis.color.dominant_colors:
|
|
print(start + c, end='')
|
|
start = ' '
|
|
print()
|