当前位置:   article > 正文

Azure的AI使用-(语言检测、图像分析、图像文本识别)_azure 图片元素识别

azure 图片元素识别

1.语言检测

安装包

  1. # 语言检测
  2. %pip install azure-ai-textanalytics==5.2.0

需要用到密钥和资源的终结点,所以去Azure上创建资源,我这个是创建好的了然后点击密钥和终结者去拿到key和终结点

两个密钥选择哪个都行 

语言检测代码示例: 

  1. key = ""
  2. endpoint = ""
  3. from azure.ai.textanalytics import TextAnalyticsClient
  4. from azure.core.credentials import AzureKeyCredential
  5. def authenticate_client():
  6. ta_credential= AzureKeyCredential(key)
  7. text_analytics_client=TextAnalyticsClient(
  8. endpoint=endpoint,
  9. credential=ta_credential
  10. )
  11. return text_analytics_client
  12. client=authenticate_client();
  13. # 检测文本是哪种语言
  14. def language_detection_example():
  15. try:
  16. documents = ["Ce document est rédigé en Français."]
  17. response=client.detect_language(documents=documents,country_hint = 'us')[0]
  18. print("response",response)
  19. print("Language: ", response.primary_language.name)
  20. except Exception as err:
  21. print("Encountered exception. {}".format(err))
  22. language_detection_example()

运行结果:

  1. response {'id': '0', 'primary_language': DetectedLanguage(name=French, iso6391_name=fr, confidence_score=1.0), 'warnings': [], 'statistics': None, 'is_error': False, 'kind': 'LanguageDetection'}
  2. Language: French

2.提取关键短语

  1. # 提取关键语言
  2. def key_phrase_extraction_example(client):
  3. try:
  4. documents = ["你好啊,我叫feng,是java程序员,想学习更多的知识"]
  5. response = client.extract_key_phrases(documents = documents)[0]
  6. if not response.is_error:
  7. print("\tKey Phrases:")
  8. for phrase in response.key_phrases:
  9. print("\t\t", phrase)
  10. else:
  11. print(response.id, response.error)
  12. except Exception as err:
  13. print("Encountered exception. {}".format(err))
  14. key_phrase_extraction_example(client)

 返回:感觉对中文的提取一般不是很友好

  1. Key Phrases:
  2. feng
  3. java程
  4. 你好
  5. 想学
  6. 多的知识

换成英文 

documents = ["Dr. Smith has a very modern medical office, and she has great staff."]

 关键字提取好像就会好很多啊!

 ["Hello, my name is Feng. My hobby is singing and traveling, and I hope to make friends with you"]

确实英语就好很多。 

 

2.图像分析

安装包:

  1. # 图像分析
  2. %pip install --upgrade azure-cognitiveservices-vision-computervision
  3. # 图像处理库
  4. %pip install pillow

这是3.2版本的,这个版本可以支持返回分析中国语言

它就是给一个图片,它会分析出图片大概有什么,以及占的比例,就像是百度的识别万物一样,识别出的物品是什么以及占比。

2.1 url图片地址分析-版本3.2

咱们拿这个图片让它帮分析一下

代码示例: 

  1. # 图像分析-url版本
  2. from azure.cognitiveservices.vision.computervision import ComputerVisionClient
  3. from azure.cognitiveservices.vision.computervision.models import OperationStatusCodes
  4. from azure.cognitiveservices.vision.computervision.models import VisualFeatureTypes
  5. from msrest.authentication import CognitiveServicesCredentials
  6. import os
  7. os.environ["VISION_KEY"]=''
  8. os.environ["VISION_ENDPOINT"]=''
  9. subscription_key = os.environ["VISION_KEY"]
  10. endpoint = os.environ["VISION_ENDPOINT"]
  11. computervision_client = ComputerVisionClient(endpoint, CognitiveServicesCredentials(subscription_key))
  12. remote_image_url = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/ComputerVision/Images/landmark.jpg"
  13. tags_result_remote = computervision_client.tag_image(remote_image_url,language="zh")
  14. if (len(tags_result_remote.tags) == 0):
  15. print("No tags detected.")
  16. else:
  17. for tag in tags_result_remote.tags:
  18. print("'{}' with confidence {:.2f}%".format(tag.name, tag.confidence * 100))
  19. print()

运行结果:

 2.2 本地图片分析-版本3.2

  1. # 图像分析-本地图片
  2. from azure.cognitiveservices.vision.computervision import ComputerVisionClient
  3. from azure.cognitiveservices.vision.computervision.models import OperationStatusCodes
  4. from azure.cognitiveservices.vision.computervision.models import VisualFeatureTypes
  5. from msrest.authentication import CognitiveServicesCredentials
  6. import os
  7. os.environ["VISION_KEY"]=''
  8. os.environ["VISION_ENDPOINT"]=''
  9. subscription_key = os.environ["VISION_KEY"]
  10. endpoint = os.environ["VISION_ENDPOINT"]
  11. computervision_client = ComputerVisionClient(endpoint, CognitiveServicesCredentials(subscription_key))
  12. local_image_path = os.path.join("C:\\Users\\Uniigym3\\AppData\\Roaming\\Python\\Python38\\Scripts\\images", "11.png")
  13. local_image = open(local_image_path, "rb")
  14. tags_result_local_image = computervision_client.analyze_image_in_stream(local_image,language="zh")
  15. print(tags_result_local_image)
  16. if (len(tags_result_local_image.categories) == 0):
  17. print("No description detected.")
  18. else:
  19. for category in tags_result_local_image.categories:
  20. print("'{}' with confidence {:.2f}%".format(category.name, category.score * 100))
  21. print()

运行结果:

 2.3 url图片地址分析-版本4.0

安装包:

%pip install azure-ai-vision

咱们让它分析这个图片

  1. import os
  2. import azure.ai.vision as sdk
  3. service_options = sdk.VisionServiceOptions("",
  4. "")
  5. vision_source = sdk.VisionSource(
  6. url="https://learn.microsoft.com/azure/ai-services/computer-vision/media/quickstarts/presentation.png")
  7. analysis_options = sdk.ImageAnalysisOptions()
  8. # 可选的视觉特征
  9. analysis_options.features = (
  10. sdk.ImageAnalysisFeature.CAPTION |
  11. sdk.ImageAnalysisFeature.TEXT
  12. )
  13. analysis_options.language = "en"
  14. # 性别中立的描述文字,默认值为区分性别的描述文字。 例如,在英语中,当你选择性别中立的描述文字时,“女性”或“男性”等术语将替换为“人员”,而“男孩”或“女孩”则将替换为“儿童”。
  15. analysis_options.gender_neutral_caption = False
  16. image_analyzer = sdk.ImageAnalyzer(service_options, vision_source, analysis_options)
  17. result = image_analyzer.analyze()
  18. # 成功你就按自己选的特征进行
  19. if result.reason == sdk.ImageAnalysisResultReason.ANALYZED:
  20. if result.caption is not None:
  21. print(" Caption:")
  22. print(" '{}', Confidence {:.4f}".format(result.caption.content, result.caption.confidence))
  23. if result.text is not None:
  24. print(" Text:")
  25. for line in result.text.lines:
  26. points_string = "{" + ", ".join([str(int(point)) for point in line.bounding_polygon]) + "}"
  27. print(" Line: '{}', Bounding polygon {}".format(line.content, points_string))
  28. else:
  29. error_details = sdk.ImageAnalysisErrorDetails.from_result(result)
  30. print(" Error reason: {}".format(error_details.reason))
  31. print(" Error code: {}".format(error_details.error_code))
  32. print(" Error message: {}".format(error_details.message))

运行结果:除图片的信息展示以外还会反馈出图片的文字 

analysis_options.gender_neutral_caption = True ,性别中立的描述文字,默认值为区分性别的描述文字。 例如,在英语中,当你选择性别中立的描述文字时,“女性”或“男性”等术语将替换为“人员”,而“男孩”或“女孩”则将替换为“儿童”。

如果设置False或不加这个设置,刚才的结果就是

Caption:
   'a man pointing at a screen', Confidence 0.7768

2.4 本地图片分析-版本4.0

就只需要把上面的这个url图片的代码改成下面的图片路径代码就可以直接在本地使用了。

vision_source = sdk.VisionSource(url="https://learn.microsoft.com/azure/ai-services/computer-vision/media/quickstarts/presentation.png")
vision_source = sdk.VisionSource(filename="C:\\Users\\Uniigym3\\AppData\\Roaming\\Python\\Python38\\Scripts\\images\\test.jpg")

我们测试个百变小樱魔术卡

运行结果:

说是有卡通的小女孩,并且标签也识别出日本动漫。 

再来测试个图片:好几个国家的语言哈

 运行结果:都能轻松的识别出来

官网图片示例:多种图片https://github.com/Azure-Samples/cognitive-services-sample-data-files/tree/master/ComputerVision/Images

图像分析3.2版本git示例:https://github.com/Azure-Samples/cognitive-services-quickstart-code/blob/master/python/ComputerVision/ImageAnalysisQuickstart.py

3.图像OCR文本识别

3.1 url图像地址识别

用这个图片来测试下

  1. #OCR文本识别
  2. from azure.cognitiveservices.vision.computervision import ComputerVisionClient
  3. from azure.cognitiveservices.vision.computervision.models import OperationStatusCodes
  4. from azure.cognitiveservices.vision.computervision.models import VisualFeatureTypes
  5. from msrest.authentication import CognitiveServicesCredentials
  6. import time
  7. computervision_client = ComputerVisionClient(You endpoint,
  8. CognitiveServicesCredentials(Your key))
  9. read_image_url = "https://learn.microsoft.com/azure/ai-services/computer-vision/media/quickstarts/presentation.png"
  10. read_response = computervision_client.read(read_image_url, raw=True)
  11. read_operation_location = read_response.headers["Operation-Location"]
  12. operation_id = read_operation_location.split("/")[-1]
  13. while True:
  14. read_result = computervision_client.get_read_result(operation_id)
  15. if read_result.status not in ['notStarted', 'running']:
  16. break
  17. time.sleep(1)
  18. if read_result.status == OperationStatusCodes.succeeded:
  19. for text_result in read_result.analyze_result.read_results:
  20. for line in text_result.lines:
  21. print(line.text)
  22. print(line.bounding_box)
  23. print()

运行结果:可以看到识别到的文本

3.2 本地图像识别

用我自己手写的文字来试下,有标点符号,甚至还特别写了一个看不清的哎呀,让它识别一下

  1. #OCR文本识别-本地
  2. from azure.cognitiveservices.vision.computervision import ComputerVisionClient
  3. from azure.cognitiveservices.vision.computervision.models import OperationStatusCodes
  4. from azure.cognitiveservices.vision.computervision.models import VisualFeatureTypes
  5. from msrest.authentication import CognitiveServicesCredentials
  6. import time
  7. import os
  8. computervision_client = ComputerVisionClient(You endpoint,
  9. CognitiveServicesCredentials( Your key))
  10. local_image_path = os.path.join("C:\\Users\\Uniigym3\\AppData\\Roaming\\Python\\Python38\\Scripts\\images", "ocrTest2.jpg")
  11. local_image = open(local_image_path, "rb")
  12. read_response = computervision_client.read_in_stream(local_image, raw=True)
  13. read_operation_location = read_response.headers["Operation-Location"]
  14. operation_id = read_operation_location.split("/")[-1]
  15. while True:
  16. read_result = computervision_client.get_read_result(operation_id)
  17. if read_result.status.lower () not in ['notstarted', 'running']:
  18. break
  19. print ('Waiting for result...')
  20. print(read_result)
  21. if read_result.status == OperationStatusCodes.succeeded:
  22. for text_result in read_result.analyze_result.read_results:
  23. for line in text_result.lines:
  24. print(line.text)
  25. print(line.bounding_box)
  26. print()

运行结果:太感动了哈,它竟然识别出来了,甚至perfect的.都识别出来了 ,很有意思

 我尝试把照片倒过来,然后就识别不到那个不清楚的字了。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/40966
推荐阅读
相关标签
  

闽ICP备14008679号