Snabbstart: Visuellt innehåll klient bibliotek för pythonQuickstart: Computer Vision client library for Python

Via tjänsten Visuellt innehåll har utvecklare tillgång till avancerade algoritmer för bearbetning av bilder och returnering av information.The Computer Vision service provides developers with access to advanced algorithms for processing images and returning information. Algoritmer för visuellt innehåll kan analysera innehållet i en bild på olika sätt beroende på vilka visuella egenskaper som du är intresserad av.Computer Vision algorithms analyze the content of an image in different ways, depending on the visual features you're interested in.

Använd Visuellt innehåll klient bibliotek för python för att:Use the Computer Vision client library for Python to:

  • Analysera en bild för taggar, text beskrivning, ansikten, vuxen innehåll med mera.Analyze an image for tags, text description, faces, adult content, and more.
  • Identifiera skriven text skriven och handskriven text med API: t batch Read.Recognize printed and handwritten text with the Batch Read API.

Anteckning

I scenarierna i den här snabb starten används URL: er för fjärravbildningen.The scenarios in this quickstart use remote image URLs. Exempel kod som utför samma åtgärder på lokala avbildningar finns i koden på GitHub.For sample code that does the same operations on local images, see the code on GitHub.

Referens dokumentation | bibliotek käll kods | paket (PiPy) | exempelReference documentation | Library source code | Package (PiPy) | Samples

FörutsättningarPrerequisites

KonfigureraSetting up

Skapa en Visuellt innehåll Azure-resursCreate a Computer Vision Azure resource

Azure-Cognitive Services representeras av Azure-resurser som du prenumererar på.Azure Cognitive Services are represented by Azure resources that you subscribe to. Skapa en resurs för Visuellt innehåll med hjälp av Azure Portal eller Azure CLI på den lokala datorn.Create a resource for Computer Vision using the Azure portal or Azure CLI on your local machine. Du kan också:You can also:

När du har fått en nyckel från din utvärderings prenumeration eller resurs skapar du miljövariabler för nyckel-och slut punkts COMPUTER_VISION_ENDPOINT-URL: en, med namnet COMPUTER_VISION_SUBSCRIPTION_KEY respektive.After you get a key from your trial subscription or resource, create environment variables for the key and endpoint URL, named COMPUTER_VISION_SUBSCRIPTION_KEY and COMPUTER_VISION_ENDPOINT, respectively.

Skapa ett nytt Python-programCreate a new Python application

Skapa ett nytt python-—skriptQuickStart-File.py, till exempel.Create a new Python script—quickstart-file.py, for example. Öppna den sedan i önskat redigerings program eller IDE och importera följande bibliotek.Then open it in your preferred editor or IDE and import the following libraries.

from azure.cognitiveservices.vision.computervision import ComputerVisionClient
from azure.cognitiveservices.vision.computervision.models import TextOperationStatusCodes
from azure.cognitiveservices.vision.computervision.models import TextRecognitionMode
from azure.cognitiveservices.vision.computervision.models import VisualFeatureTypes
from msrest.authentication import CognitiveServicesCredentials

import os
import sys
import time

Skapa sedan variabler för resursens Azure-slutpunkt och nyckel.Then, create variables for your resource's Azure endpoint and key.

# Add your Computer Vision subscription key to your environment variables.
if 'COMPUTER_VISION_SUBSCRIPTION_KEY' in os.environ:
    subscription_key = os.environ['COMPUTER_VISION_SUBSCRIPTION_KEY']
else:
    print("\nSet the COMPUTER_VISION_SUBSCRIPTION_KEY environment variable.\n**Restart your shell or IDE for changes to take effect.**")
    sys.exit()
# Add your Computer Vision endpoint to your environment variables.
if 'COMPUTER_VISION_ENDPOINT' in os.environ:
    endpoint = os.environ['COMPUTER_VISION_ENDPOINT']
else:
    print("\nSet the COMPUTER_VISION_ENDPOINT environment variable.\n**Restart your shell or IDE for changes to take effect.**")
    sys.exit()

Anteckning

Om du har skapat miljövariabeln efter att du har startat programmet måste du stänga och öppna redigerings programmet, IDE eller gränssnittet som kör det för att få åtkomst till variabeln.If you created the environment variable after you launched the application, you will need to close and reopen the editor, IDE, or shell running it to access the variable.

Installera klient biblioteketInstall the client library

Du kan installera klient biblioteket med:You can install the client library with:

pip install --upgrade azure-cognitiveservices-vision-computervision

Objekt modellObject model

Följande klasser och gränssnitt hanterar några av de viktigaste funktionerna i Visuellt innehåll python SDK.The following classes and interfaces handle some of the major features of the Computer Vision Python SDK.

NameName BeskrivningDescription
ComputerVisionClientOperationsMixinComputerVisionClientOperationsMixin Den här klassen hanterar alla avbildnings åtgärder direkt, till exempel bild analys, text identifiering och generering av miniatyrer.This class directly handles all of the image operations, such as image analysis, text detection, and thumbnail generation.
ComputerVisionClientComputerVisionClient Den här klassen krävs för alla Visuellt innehåll-funktioner.This class is needed for all Computer Vision functionality. Du instansierar det med din prenumerations information och använder den för att skapa instanser av andra klasser.You instantiate it with your subscription information, and you use it to produce instances of other classes. Den implementerar ComputerVisionClientOperationsMixin.It implements ComputerVisionClientOperationsMixin.
VisualFeatureTypesVisualFeatureTypes Den här uppräkningen definierar de olika typerna av bild analys som kan göras i en standard analys åtgärd.This enum defines the different types of image analysis that can be done in a standard Analyze operation. Du anger en uppsättning VisualFeatureTypes -värden beroende på dina behov.You specify a set of VisualFeatureTypes values depending on your needs.

Kod exempelCode examples

De här kodfragmenten visar hur du utför följande uppgifter med Visuellt innehåll klient biblioteket för python:These code snippets show you how to do the following tasks with the Computer Vision client library for Python:

Autentisera klientenAuthenticate the client

Anteckning

Den här snabb starten förutsätter att du har skapat en miljö variabel för din COMPUTER_VISION_SUBSCRIPTION_KEYvisuellt innehåll nyckel, med namnet.This quickstart assumes you've created an environment variable for your Computer Vision key, named COMPUTER_VISION_SUBSCRIPTION_KEY.

Instansiera en klient med din slut punkt och nyckel.Instantiate a client with your endpoint and key. Skapa ett CognitiveServicesCredentials -objekt med din nyckel och Använd den med slut punkten för att skapa ett ComputerVisionClient -objekt.Create a CognitiveServicesCredentials object with your key, and use it with your endpoint to create a ComputerVisionClient object.

computervision_client = ComputerVisionClient(endpoint, CognitiveServicesCredentials(subscription_key))

Analysera en bildAnalyze an image

Spara en referens till URL: en för en bild som du vill analysera.Save a reference to the URL of an image you want to analyze.

remote_image_url = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/ComputerVision/Images/landmark.jpg"

Beskrivning av Hämta avbildningGet image description

Följande kod hämtar listan över genererade under texter för avbildningen.The following code gets the list of generated captions for the image. Se Beskriv avbildningar för mer information.See Describe images for more details.

'''
Describe an image - remote
This example describes the contents of an image with the confidence score.
'''
print("===== Describe an image - remote =====")
# Call API
description_results = computervision_client.describe_image(remote_image_url )

# Get the captions (descriptions) from the response, with confidence level
print("Description of remote image: ")
if (len(description_results.captions) == 0):
    print("No description detected.")
else:
    for caption in description_results.captions:
        print("'{}' with confidence {:.2f}%".format(caption.text, caption.confidence * 100))

Hämta bild kategoriGet image category

Följande kod hämtar den identifierade kategorin för avbildningen.The following code gets the detected category of the image. Se kategorisera bilder för mer information.See Categorize images for more details.

'''
Categorize an image - remote
This example extracts (general) categories from a remote image with a confidence score.
'''
print("===== Categorize an image - remote =====")
# Select the visual feature(s) you want.
remote_image_features = ["categories"]
# Call API with URL and features
categorize_results_remote = computervision_client.analyze_image(remote_image_url , remote_image_features)

# Print results with confidence score
print("Categories from remote image: ")
if (len(categorize_results_remote.categories) == 0):
    print("No categories detected.")
else:
    for category in categorize_results_remote.categories:
        print("'{}' with confidence {:.2f}%".format(category.name, category.score * 100))

Hämta bild etiketterGet image tags

Följande kod hämtar en uppsättning identifierade Taggar i avbildningen.The following code gets the set of detected tags in the image. Se innehålls etiketter för mer information.See Content tags for more details.

'''
Tag an image - remote
This example returns a tag (key word) for each thing in the image.
'''
print("===== Tag an image - remote =====")
# Call API with remote image
tags_result_remote = computervision_client.tag_image(remote_image_url )

# Print results with confidence score
print("Tags in the remote image: ")
if (len(tags_result_remote.tags) == 0):
    print("No tags detected.")
else:
    for tag in tags_result_remote.tags:
        print("'{}' with confidence {:.2f}%".format(tag.name, tag.confidence * 100))

Identifiera objektDetect objects

Följande kod identifierar vanliga objekt i avbildningen och skriver ut dem till-konsolen.The following code detects common objects in the image and prints them to the console. Mer information finns i objekt identifiering .See Object detection for more details.

'''
Detect objects - remote
This example detects different kinds of objects with bounding boxes in a remote image.
'''
print("===== Detect objects - remote =====")
# Get URL image with different objects
remote_image_url_objects = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/ComputerVision/Images/objects.jpg"
# Call API with URL
detect_objects_results_remote = computervision_client.detect_objects(remote_image_url_objects)

# Print detected objects results with bounding boxes
print("Detecting objects in remote image:")
if len(detect_objects_results_remote.objects) == 0:
    print("No objects detected.")
else:
    for object in detect_objects_results_remote.objects:
        print("object at location {}, {}, {}, {}".format( \
        object.rectangle.x, object.rectangle.x + object.rectangle.w, \
        object.rectangle.y, object.rectangle.y + object.rectangle.h))

Identifiera varumärkenDetect brands

Följande kod identifierar företags märken och logo typer i bilden och skriver ut dem till-konsolen.The following code detects corporate brands and logos in the image and prints them to the console. Mer information finns i varumärkes identifiering .See Brand detection for more details.

'''
Detect brands - remote
This example detects common brands like logos and puts a bounding box around them.
'''
print("===== Detect brands - remote =====")
# Get a URL with a brand logo
remote_image_url = "https://docs.microsoft.com/en-us/azure/cognitive-services/computer-vision/images/gray-shirt-logo.jpg"
# Select the visual feature(s) you want
remote_image_features = ["brands"]
# Call API with URL and features
detect_brands_results_remote = computervision_client.analyze_image(remote_image_url, remote_image_features)

print("Detecting brands in remote image: ")
if len(detect_brands_results_remote.brands) == 0:
    print("No brands detected.")
else:
    for brand in detect_brands_results_remote.brands:
        print("'{}' brand detected with confidence {:.1f}% at location {}, {}, {}, {}".format( \
        brand.name, brand.confidence * 100, brand.rectangle.x, brand.rectangle.x + brand.rectangle.w, \
        brand.rectangle.y, brand.rectangle.y + brand.rectangle.h))

Identifiera ansiktenDetect faces

Följande kod returnerar identifierade ansikten i bilden med deras Rectangle-koordinater och välj ansikts attribut.The following code returns the detected faces in the image with their rectangle coordinates and select face attributes. Mer information finns i ansikts igenkänning .See Face detection for more details.

'''
Detect faces - remote
This example detects faces in a remote image, gets their gender and age, 
and marks them with a bounding box.
'''
print("===== Detect faces - remote =====")
# Get an image with faces
remote_image_url_faces = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/ComputerVision/Images/faces.jpg"
# Select the visual feature(s) you want.
remote_image_features = ["faces"]
# Call the API with remote URL and features
detect_faces_results_remote = computervision_client.analyze_image(remote_image_url_faces, remote_image_features)

# Print the results with gender, age, and bounding box
print("Faces in the remote image: ")
if (len(detect_faces_results_remote.faces) == 0):
    print("No faces detected.")
else:
    for face in detect_faces_results_remote.faces:
        print("'{}' of age {} at location {}, {}, {}, {}".format(face.gender, face.age, \
        face.face_rectangle.left, face.face_rectangle.top, \
        face.face_rectangle.left + face.face_rectangle.width, \
        face.face_rectangle.top + face.face_rectangle.height))

Identifiera vuxna, vågat eller fullständig innehållDetect adult, racy, or gory content

Följande kod skriver ut den identifierade förekomsten av olämpligt innehåll i bilden.The following code prints the detected presence of adult content in the image. Mer information finns i vuxen, vågat, fullständig-innehåll .See Adult, racy, gory content for more details.

'''
Detect adult or racy content - remote
This example detects adult or racy content in a remote image, then prints the adult/racy score.
The score is ranged 0.0 - 1.0 with smaller numbers indicating negative results.
'''
print("===== Detect adult or racy content - remote =====")
# Select the visual feature(s) you want
remote_image_features = ["adult"]
# Call API with URL and features
detect_adult_results_remote = computervision_client.analyze_image(remote_image_url, remote_image_features)

# Print results with adult/racy score
print("Analyzing remote image for adult or racy content ... ")
print("Is adult content: {} with confidence {:.2f}".format(detect_adult_results_remote.adult.is_adult_content, detect_adult_results_remote.adult.adult_score * 100))
print("Has racy content: {} with confidence {:.2f}".format(detect_adult_results_remote.adult.is_racy_content, detect_adult_results_remote.adult.racy_score * 100))

Hämta färg schema för bildGet image color scheme

Följande kod skriver ut de identifierade färgattributen i bilden, till exempel dominerande färger och dekor färg.The following code prints the detected color attributes in the image, like the dominant colors and accent color. Se färg scheman för mer information.See Color schemes for more details.

'''
Detect color - remote
This example detects the different aspects of its color scheme in a remote image.
'''
print("===== Detect color - remote =====")
# 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))

Hämta domänbaserat innehållGet domain-specific content

Visuellt innehåll kan använda specialiserad modell för att utföra ytterligare analyser av avbildningar.Computer Vision can use specialized model to do further analysis on images. Se domänbaserat innehåll för mer information.See Domain-specific content for more details.

Följande kod tolkar data om identifierade kändisar i avbildningen.The following code parses data about detected celebrities in the image.

'''
Detect domain-specific content - remote
This example detects celebrites and landmarks in remote images.
'''
print("===== Detect domain-specific content - remote =====")
# URL of one or more celebrities
remote_image_url_celebs = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/ComputerVision/Images/faces.jpg"
# Call API with content type (celebrities) and URL
detect_domain_results_celebs_remote = computervision_client.analyze_image_by_domain("celebrities", remote_image_url_celebs)

# Print detection results with name
print("Celebrities in the remote image:")
if len(detect_domain_results_celebs_remote.result["celebrities"]) == 0:
    print("No celebrities detected.")
else:
    for celeb in detect_domain_results_celebs_remote.result["celebrities"]:
        print(celeb["name"])

Följande kod tolkar data om identifierade landmärken i bilden.The following code parses data about detected landmarks in the image.

# 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"])

Hämta avbildnings typenGet the image type

Följande kod skriver ut information om typen av bild—oavsett om den är en ClipArt-eller linje ritning.The following code prints information about the type of image—whether it is clip art or line drawing.

'''
Detect image types - remote
This example detects an image's type (clip art/line drawing).
'''
print("===== Detect image types - remote =====")
# Get URL of an image with a type
remote_image_url_type = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/ComputerVision/Images/type-image.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")

Skriv ut och handskriven textRead printed and handwritten text

Visuellt innehåll kan läsa synlig text i en bild och konvertera den till en tecken ström.Computer Vision can read visible text in an image and convert it to a character stream. Du gör detta i två delar.You do this in two parts.

Anropa Read APICall the Read API

Använd först följande kod för att anropa batch_read_file -metoden för den aktuella avbildningen.First, use the following code to call the batch_read_file method for the given image. Detta returnerar ett åtgärds-ID och startar en asynkron process för att läsa innehållet i avbildningen.This returns an operation ID and starts an asynchronous process to read the content of the image.

'''
Recognize printed text - remote
This example will extract printed text in an image, then print results, line by line.
This API call can also recognize handwriting (not shown).
'''
print("===== Detect printed text - remote =====")
# Get an image with printed text
remote_image_printed_text_url = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/ComputerVision/Images/printed_text.jpg"

# Call API with URL and raw response (allows you to get the operation location)
recognize_printed_results = computervision_client.batch_read_file(remote_image_printed_text_url,  raw=True)

Hämta Läs resultatGet Read results

Hämta sedan det åtgärds-ID som returnerades från batch_read_file -anropet och Använd det för att fråga tjänsten efter åtgärds resultat.Next, get the operation ID returned from the batch_read_file call, and use it to query the service for operation results. Följande kod kontrollerar åtgärden vid en sekunds intervall tills resultatet returneras.The following code checks the operation at one-second intervals until the results are returned. Den skriver sedan ut de extraherade text data till-konsolen.It then prints the extracted text data to the console.

# Get the operation location (URL with an ID at the end) from the response
operation_location_remote = recognize_printed_results.headers["Operation-Location"]
# Grab the ID from the URL
operation_id = operation_location_remote.split("/")[-1]

# Call the "GET" API and wait for it to retrieve the results 
while True:
    get_printed_text_results = computervision_client.get_read_operation_result(operation_id)
    if get_printed_text_results.status not in ['NotStarted', 'Running']:
        break
    time.sleep(1)

# Print the detected text, line by line
if get_printed_text_results.status == TextOperationStatusCodes.succeeded:
    for text_result in get_printed_text_results.recognition_results:
        for line in text_result.lines:
            print(line.text)
            print(line.bounding_box)
print()

Köra programmetRun the application

Kör programmet med python kommandot på snabb starts filen.Run the application with the python command on your quickstart file.

python quickstart-file.py

Rensa resurserClean up resources

Om du vill rensa och ta bort en Cognitive Services prenumeration kan du ta bort resursen eller resurs gruppen.If you want to clean up and remove a Cognitive Services subscription, you can delete the resource or resource group. Om du tar bort resurs gruppen raderas även andra resurser som är kopplade till den.Deleting the resource group also deletes any other resources associated with it.

Nästa stegNext steps

I den här snabb starten har du lärt dig hur du använder Visuellt innehåll-biblioteket för python för att utföra grundläggande uppgifter.In this quickstart, you learned how to use the Computer Vision library for Python to do basis tasks. Sedan kan du utforska referens dokumentationen och läsa mer om biblioteket.Next, explore the reference documentation to learn more about the library.