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 |
from azure.cognitiveservices.vision.computervision import ComputerVisionClient from azure.cognitiveservices.vision.computervision.models import VisualFeatureTypes from msrest.authentication import CognitiveServicesCredentials import os class colors: green = '\033[92m' blue = '\033[94m' red = '\033[31m' yellow = '\033[33m' reset = '\033[0m' # Paste your endpoint and key below cog_endpoint = "Paste_endpoint_here" cog_key = "Paste_key_here" computervision_client = ComputerVisionClient(cog_endpoint, CognitiveServicesCredentials(cog_key)) # Change the URL between the quotes below to run your own images! image_to_analyze = "https://raw.githubusercontent.com/pluralsight-cloud/AI-900-Artificial-Intelligence-Workloads-and-Considerations/main/images/image-analysis/1-computervision-couple.jpg" image_analysis = computervision_client.analyze_image(image_to_analyze,visual_features=[VisualFeatureTypes.description, VisualFeatureTypes.tags, VisualFeatureTypes.faces]) print("\n-----Image Description-----") for caption in image_analysis.description.captions: print(f"{colors.green}Confidence: {colors.reset}" + str(caption.confidence)) print(f"{colors.green}Description: {colors.reset}" + caption.text) print("----------\n") input("Press Enter to continue to image tags...\n") print("-----Image Tags-----") for tag in image_analysis.tags: print(f"{colors.green}Tag:{colors.reset} {tag.name:<15}", f" {colors.yellow}Confidence:{colors.reset} {tag.confidence:<15}") print("----------\n") input("Press Enter to continue to face detection...\n") print("-----Face Detection-----") for face in image_analysis.faces: print("Face at location {},{},{},{}".format( face.face_rectangle.left, face.face_rectangle.top, \ face.face_rectangle.left + face.face_rectangle.width, \ face.face_rectangle.top + face.face_rectangle.height)) print("----------") input("\nPress Enter to Exit...") |
结果:
-----Image Description-----
Confidence: 0.48432523012161255
Description: a man and woman sitting on a couch with a dog and a cup
Press Enter to continue to image tags...
-----Image Tags-----
Tag: clothing Confidence: 0.9980252385139465
Tag: person Confidence: 0.9966760873794556
Tag: human face Confidence: 0.9829165935516357
Tag: jeans Confidence: 0.9511970281600952
Tag: footwear Confidence: 0.9418420791625977
Tag: jacket Confidence: 0.9413744211196899
Tag: sitting Confidence: 0.9293597936630249
Tag: outdoor Confidence: 0.9179489612579346
Tag: building Confidence: 0.9165546894073486
Tag: man Confidence: 0.9083777070045471
Tag: bench Confidence: 0.8780822157859802
Tag: scarf Confidence: 0.873085618019104
Tag: coat Confidence: 0.8616373538970947
Tag: handbag Confidence: 0.856252908706665
Tag: smile Confidence: 0.8407089710235596
Tag: street Confidence: 0.820195734500885
Tag: woman Confidence: 0.780620813369751
Tag: people Confidence: 0.750357449054718
Tag: ground Confidence: 0.725933313369751
Tag: wooden Confidence: 0.5973995327949524
Press Enter to continue to face detection...
-----Face Detection-----
Face at location 304,161,458,315
文章评论