This repository has been archived by the owner on Oct 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
color.py
80 lines (56 loc) · 2.23 KB
/
color.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# -*- 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()