Identify Image Attributes and Landmarks

Let’s learn how to detect color schemes, landmarks, and the type of image.

Detect color schemes

Microsoft Computer Vision analyzes the colors in an image to provide the following attributes:

  • The dominant foreground color.
  • The dominant background color.
  • The set of dominant colors in the image as a whole.
  • Accent color (the most vibrant color in the image, based on a combination of dominant colors and saturation). It is returned as a hexadecimal HTML color code.
  • A boolean value indicating whether an image is in black and white.

To understand this better, let’s look at an example with this image:

To detect the color scheme, we’ll call the analyse_image method with "color" as the feature.

Note: The image URL is provided at line 5. Feel free to change it and test the API on other images.

Press + to interact
# Authenticate the client
computervision_client = ComputerVisionClient("{{ENDPOINT}}", CognitiveServicesCredentials("{{SUBSCRIPTION_KEY}}"))
# Provide image URL
remote_image_url = "https://images.pexels.com/photos/1831119/pexels-photo-1831119.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260"
# Select the feature(s) you want
remote_image_features = ["color"]
# Call API with URL and features
detect_color_results_remote = computervision_client.analyze_image(remote_image_url, remote_image_features)
# Print results of color scheme
print("Getting color scheme of the remote image: ")
print("Is black and white: {}".format(detect_color_results_remote.color.is_bw_img))
print("Accent color: {}".format(detect_color_results_remote.color.accent_color))
print("Dominant background color: {}".format(detect_color_results_remote.color.dominant_color_background))
print("Dominant foreground color: {}".format(detect_color_results_remote.color.dominant_color_foreground))
print("Dominant colors: {}".format(detect_color_results_remote.color.dominant_colors))

Detect landmarks

Now let’s see how it works with the following landmark:

To analyze the image above, we’ll call the analyze_image_by_domain method and specify the model as "landmarks".

Note: The image URL is provided at line 5. Feel free to change it and test the API on other images.

Press + to interact
# Authenticate the client
computervision_client = ComputerVisionClient("{{ENDPOINT}}", CognitiveServicesCredentials("{{SUBSCRIPTION_KEY}}"))
# Provide image URL
remote_image_url = "https://images.pexels.com/photos/3879071/pexels-photo-3879071.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260"
# Call API with content type (landmarks) and URL
detect_domain_results_landmarks = computervision_client.analyze_image_by_domain("landmarks", remote_image_url)
print()
print("Landmarks in the remote image:")
if len(detect_domain_results_landmarks.result["landmarks"]) == 0:
print("No landmarks detected.")
else:
for landmark in detect_domain_results_landmarks.result["landmarks"]:
print(landmark["name"])

Detecting image types

The Computer Vision service also lets us determine the type of an image, for example, whether an image is line art or clip art, through the following measures:

  • If the image is line art, the API returns true. Otherwise, it returns false.
  • If the image is clip art, the API returns a confidence measure on a scale of 0 to 3 that the image is clip art. The specifications of these ratings are discussed in the table below.
Value Meaning
0 Non-clip-art
1 Ambiguous
2 Normal-clip-art
3 Good-clip-art

Let us see how it works with the help of this image:

Note: The image above is designed by Freepik.com

To analyze the image, we’ll use the analyze_image method and specify the feature as VisualFeatureTypes.image_type.

Note: The image URL is provided at line 5. Feel free to change it and test the API on other images.

Press + to interact
# Authenticate the client
computervision_client = ComputerVisionClient("{{ENDPOINT}}", CognitiveServicesCredentials("{{SUBSCRIPTION_KEY}}"))
# Get URL of an image with a type
remote_image_url_type = "https://image.freepik.com/free-vector/boy-student-sitting-stack-books-with-laptop-flat-icon-illustration_1284-64037.jpg"
# Select visual feature(s) you want
remote_image_features = [VisualFeatureTypes.image_type]
# Call API with URL and features
detect_type_results_remote = computervision_client.analyze_image(remote_image_url_type, remote_image_features)
# Prints type results with degree of accuracy
print("Type of remote image:")
if detect_type_results_remote.image_type.clip_art_type == 0:
print("Image is not clip art.")
elif detect_type_results_remote.image_type.line_drawing_type == 1:
print("Image is ambiguously clip art.")
elif detect_type_results_remote.image_type.line_drawing_type == 2:
print("Image is normal clip art.")
else:
print("Image is good clip art.")
if detect_type_results_remote.image_type.line_drawing_type == 0:
print("Image is not a line drawing.")
else:
print("Image is a line drawing")

Now, let’s run the same code with a line drawing. Replace the link on line 5 with this link to a picture of a crane (designed by Freepik.com). Run the code to see the results.