Skip to content

Commit b3bbca7

Browse files
authored
[AutoPR cognitiveservices/data-plane/ComputerVision] [Computer Vision] Add read to CV OCR API. (#4473)
* Generated from cbcb8cc3c280c9725bca0f4e0feb4bfc57b677ec Add read to OCR API. * Packaging update of azure-cognitiveservices-vision-computervision * Generated from 51742b3e212c2e900ed5c24f7c351fa7f896d25e Mark asyncBatchAnalyze as long running operation. * Generated from b047a112a2c4cddaa54605d5de6a7bc3faab38c7 Update page property.
1 parent 798f589 commit b3bbca7

16 files changed

+447
-84
lines changed

azure-cognitiveservices-vision-computervision/MANIFEST.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
recursive-include tests *.py *.yaml
12
include *.rst
23
include azure/__init__.py
34
include azure/cognitiveservices/__init__.py

azure-cognitiveservices-vision-computervision/azure/cognitiveservices/vision/computervision/computer_vision_client.py

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,125 @@ def get_text_operation_result(
885885
return deserialized
886886
get_text_operation_result.metadata = {'url': '/textOperations/{operationId}'}
887887

888+
def batch_read_file(
889+
self, url, mode, custom_headers=None, raw=False, **operation_config):
890+
"""Use this interface to get the result of a Read operation, employing the
891+
state-of-the-art Optical Character Recognition (OCR) algorithms
892+
optimized for text-heavy documents. When you use the Read File
893+
interface, the response contains a field called "Operation-Location".
894+
The "Operation-Location" field contains the URL that you must use for
895+
your "Read Operation Result" operation to access OCR results.​.
896+
897+
:param mode: Type of text to recognize. Possible values include:
898+
'Handwritten', 'Printed'
899+
:type mode: str or
900+
~azure.cognitiveservices.vision.computervision.models.TextRecognitionMode
901+
:param url: Publicly reachable URL of an image.
902+
:type url: str
903+
:param dict custom_headers: headers that will be added to the request
904+
:param bool raw: returns the direct response alongside the
905+
deserialized response
906+
:param operation_config: :ref:`Operation configuration
907+
overrides<msrest:optionsforoperations>`.
908+
:return: None or ClientRawResponse if raw=true
909+
:rtype: None or ~msrest.pipeline.ClientRawResponse
910+
:raises:
911+
:class:`ComputerVisionErrorException<azure.cognitiveservices.vision.computervision.models.ComputerVisionErrorException>`
912+
"""
913+
image_url = models.ImageUrl(url=url)
914+
915+
# Construct URL
916+
url = self.batch_read_file.metadata['url']
917+
path_format_arguments = {
918+
'Endpoint': self._serialize.url("self.config.endpoint", self.config.endpoint, 'str', skip_quote=True)
919+
}
920+
url = self._client.format_url(url, **path_format_arguments)
921+
922+
# Construct parameters
923+
query_parameters = {}
924+
query_parameters['mode'] = self._serialize.query("mode", mode, 'TextRecognitionMode')
925+
926+
# Construct headers
927+
header_parameters = {}
928+
header_parameters['Content-Type'] = 'application/json; charset=utf-8'
929+
if custom_headers:
930+
header_parameters.update(custom_headers)
931+
932+
# Construct body
933+
body_content = self._serialize.body(image_url, 'ImageUrl')
934+
935+
# Construct and send request
936+
request = self._client.post(url, query_parameters, header_parameters, body_content)
937+
response = self._client.send(request, stream=False, **operation_config)
938+
939+
if response.status_code not in [202]:
940+
raise models.ComputerVisionErrorException(self._deserialize, response)
941+
942+
if raw:
943+
client_raw_response = ClientRawResponse(None, response)
944+
client_raw_response.add_headers({
945+
'Operation-Location': 'str',
946+
})
947+
return client_raw_response
948+
batch_read_file.metadata = {'url': '/read/core/asyncBatchAnalyze'}
949+
950+
def get_read_operation_result(
951+
self, operation_id, custom_headers=None, raw=False, **operation_config):
952+
"""This interface is used for getting OCR results of Read operation. The
953+
URL to this interface should be retrieved from "Operation-Location"
954+
field returned from Batch Read File interface.
955+
956+
:param operation_id: Id of read operation returned in the response of
957+
the "Batch Read File" interface.
958+
:type operation_id: str
959+
:param dict custom_headers: headers that will be added to the request
960+
:param bool raw: returns the direct response alongside the
961+
deserialized response
962+
:param operation_config: :ref:`Operation configuration
963+
overrides<msrest:optionsforoperations>`.
964+
:return: ReadOperationResult or ClientRawResponse if raw=true
965+
:rtype:
966+
~azure.cognitiveservices.vision.computervision.models.ReadOperationResult
967+
or ~msrest.pipeline.ClientRawResponse
968+
:raises:
969+
:class:`ComputerVisionErrorException<azure.cognitiveservices.vision.computervision.models.ComputerVisionErrorException>`
970+
"""
971+
# Construct URL
972+
url = self.get_read_operation_result.metadata['url']
973+
path_format_arguments = {
974+
'Endpoint': self._serialize.url("self.config.endpoint", self.config.endpoint, 'str', skip_quote=True),
975+
'operationId': self._serialize.url("operation_id", operation_id, 'str')
976+
}
977+
url = self._client.format_url(url, **path_format_arguments)
978+
979+
# Construct parameters
980+
query_parameters = {}
981+
982+
# Construct headers
983+
header_parameters = {}
984+
header_parameters['Accept'] = 'application/json'
985+
if custom_headers:
986+
header_parameters.update(custom_headers)
987+
988+
# Construct and send request
989+
request = self._client.get(url, query_parameters, header_parameters)
990+
response = self._client.send(request, stream=False, **operation_config)
991+
992+
if response.status_code not in [200]:
993+
raise models.ComputerVisionErrorException(self._deserialize, response)
994+
995+
deserialized = None
996+
997+
if response.status_code == 200:
998+
deserialized = self._deserialize('ReadOperationResult', response)
999+
1000+
if raw:
1001+
client_raw_response = ClientRawResponse(deserialized, response)
1002+
return client_raw_response
1003+
1004+
return deserialized
1005+
get_read_operation_result.metadata = {'url': '/read/operations/{operationId}'}
1006+
8881007
def analyze_image_in_stream(
8891008
self, image, visual_features=None, details=None, language="en", custom_headers=None, raw=False, callback=None, **operation_config):
8901009
"""This operation extracts a rich set of visual features based on the
@@ -1606,3 +1725,69 @@ def recognize_text_in_stream(
16061725
})
16071726
return client_raw_response
16081727
recognize_text_in_stream.metadata = {'url': '/recognizeText'}
1728+
1729+
def batch_read_file_in_stream(
1730+
self, image, mode, custom_headers=None, raw=False, callback=None, **operation_config):
1731+
"""Use this interface to get the result of a Read Document operation,
1732+
employing the state-of-the-art Optical Character Recognition (OCR)
1733+
algorithms optimized for text-heavy documents. When you use the Read
1734+
Document interface, the response contains a field called
1735+
"Operation-Location". The "Operation-Location" field contains the URL
1736+
that you must use for your "Get Read Result operation" to access OCR
1737+
results.​.
1738+
1739+
:param image: An image stream.
1740+
:type image: Generator
1741+
:param mode: Type of text to recognize. Possible values include:
1742+
'Handwritten', 'Printed'
1743+
:type mode: str or
1744+
~azure.cognitiveservices.vision.computervision.models.TextRecognitionMode
1745+
:param dict custom_headers: headers that will be added to the request
1746+
:param bool raw: returns the direct response alongside the
1747+
deserialized response
1748+
:param callback: When specified, will be called with each chunk of
1749+
data that is streamed. The callback should take two arguments, the
1750+
bytes of the current chunk of data and the response object. If the
1751+
data is uploading, response will be None.
1752+
:type callback: Callable[Bytes, response=None]
1753+
:param operation_config: :ref:`Operation configuration
1754+
overrides<msrest:optionsforoperations>`.
1755+
:return: None or ClientRawResponse if raw=true
1756+
:rtype: None or ~msrest.pipeline.ClientRawResponse
1757+
:raises:
1758+
:class:`ComputerVisionErrorException<azure.cognitiveservices.vision.computervision.models.ComputerVisionErrorException>`
1759+
"""
1760+
# Construct URL
1761+
url = self.batch_read_file_in_stream.metadata['url']
1762+
path_format_arguments = {
1763+
'Endpoint': self._serialize.url("self.config.endpoint", self.config.endpoint, 'str', skip_quote=True)
1764+
}
1765+
url = self._client.format_url(url, **path_format_arguments)
1766+
1767+
# Construct parameters
1768+
query_parameters = {}
1769+
query_parameters['mode'] = self._serialize.query("mode", mode, 'TextRecognitionMode')
1770+
1771+
# Construct headers
1772+
header_parameters = {}
1773+
header_parameters['Content-Type'] = 'application/octet-stream'
1774+
if custom_headers:
1775+
header_parameters.update(custom_headers)
1776+
1777+
# Construct body
1778+
body_content = self._client.stream_upload(image, callback)
1779+
1780+
# Construct and send request
1781+
request = self._client.post(url, query_parameters, header_parameters, body_content)
1782+
response = self._client.send(request, stream=False, **operation_config)
1783+
1784+
if response.status_code not in [202]:
1785+
raise models.ComputerVisionErrorException(self._deserialize, response)
1786+
1787+
if raw:
1788+
client_raw_response = ClientRawResponse(None, response)
1789+
client_raw_response.add_headers({
1790+
'Operation-Location': 'str',
1791+
})
1792+
return client_raw_response
1793+
batch_read_file_in_stream.metadata = {'url': '/read/core/asyncBatchAnalyze'}

azure-cognitiveservices-vision-computervision/azure/cognitiveservices/vision/computervision/models/__init__.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@
4545
from .celebrity_results_py3 import CelebrityResults
4646
from .word_py3 import Word
4747
from .line_py3 import Line
48-
from .recognition_result_py3 import RecognitionResult
48+
from .text_recognition_result_py3 import TextRecognitionResult
4949
from .text_operation_result_py3 import TextOperationResult
50+
from .read_operation_result_py3 import ReadOperationResult
5051
except (SyntaxError, ImportError):
5152
from .face_rectangle import FaceRectangle
5253
from .celebrities_model import CelebritiesModel
@@ -83,11 +84,14 @@
8384
from .celebrity_results import CelebrityResults
8485
from .word import Word
8586
from .line import Line
86-
from .recognition_result import RecognitionResult
87+
from .text_recognition_result import TextRecognitionResult
8788
from .text_operation_result import TextOperationResult
89+
from .read_operation_result import ReadOperationResult
8890
from .computer_vision_client_enums import (
8991
Gender,
9092
TextOperationStatusCodes,
93+
TextRecognitionResultDimensionUnit,
94+
TextRecognitionResultConfidenceClass,
9195
OcrLanguages,
9296
VisualFeatureTypes,
9397
TextRecognitionMode,
@@ -130,10 +134,13 @@
130134
'CelebrityResults',
131135
'Word',
132136
'Line',
133-
'RecognitionResult',
137+
'TextRecognitionResult',
134138
'TextOperationResult',
139+
'ReadOperationResult',
135140
'Gender',
136141
'TextOperationStatusCodes',
142+
'TextRecognitionResultDimensionUnit',
143+
'TextRecognitionResultConfidenceClass',
137144
'OcrLanguages',
138145
'VisualFeatureTypes',
139146
'TextRecognitionMode',

azure-cognitiveservices-vision-computervision/azure/cognitiveservices/vision/computervision/models/computer_vision_client_enums.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@ class TextOperationStatusCodes(str, Enum):
2626
succeeded = "Succeeded"
2727

2828

29+
class TextRecognitionResultDimensionUnit(str, Enum):
30+
31+
pixel = "pixel"
32+
inch = "inch"
33+
34+
35+
class TextRecognitionResultConfidenceClass(str, Enum):
36+
37+
high = "High"
38+
low = "Low"
39+
40+
2941
class OcrLanguages(str, Enum):
3042

3143
unk = "unk"

azure-cognitiveservices-vision-computervision/azure/cognitiveservices/vision/computervision/models/line.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313

1414

1515
class Line(Model):
16-
"""Line.
16+
"""Json object representing a recognized text line.
1717
18-
:param bounding_box:
18+
:param bounding_box: Bounding box of a recognized line.
1919
:type bounding_box: list[int]
20-
:param text:
20+
:param text: The text content of the line.
2121
:type text: str
22-
:param words:
22+
:param words: List of words in the text line.
2323
:type words:
2424
list[~azure.cognitiveservices.vision.computervision.models.Word]
2525
"""

azure-cognitiveservices-vision-computervision/azure/cognitiveservices/vision/computervision/models/line_py3.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313

1414

1515
class Line(Model):
16-
"""Line.
16+
"""Json object representing a recognized text line.
1717
18-
:param bounding_box:
18+
:param bounding_box: Bounding box of a recognized line.
1919
:type bounding_box: list[int]
20-
:param text:
20+
:param text: The text content of the line.
2121
:type text: str
22-
:param words:
22+
:param words: List of words in the text line.
2323
:type words:
2424
list[~azure.cognitiveservices.vision.computervision.models.Word]
2525
"""
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# coding=utf-8
2+
# --------------------------------------------------------------------------
3+
# Copyright (c) Microsoft Corporation. All rights reserved.
4+
# Licensed under the MIT License. See License.txt in the project root for
5+
# license information.
6+
#
7+
# Code generated by Microsoft (R) AutoRest Code Generator.
8+
# Changes may cause incorrect behavior and will be lost if the code is
9+
# regenerated.
10+
# --------------------------------------------------------------------------
11+
12+
from msrest.serialization import Model
13+
14+
15+
class ReadOperationResult(Model):
16+
"""OCR result of the read operation.
17+
18+
:param status: Status of the read operation. Possible values include: 'Not
19+
Started', 'Running', 'Failed', 'Succeeded'
20+
:type status: str or
21+
~azure.cognitiveservices.vision.computervision.models.TextOperationStatusCodes
22+
:param recognition_results: A array of text recognition result of the read
23+
operation.
24+
:type recognition_results:
25+
list[~azure.cognitiveservices.vision.computervision.models.TextRecognitionResult]
26+
"""
27+
28+
_attribute_map = {
29+
'status': {'key': 'status', 'type': 'TextOperationStatusCodes'},
30+
'recognition_results': {'key': 'recognitionResults', 'type': '[TextRecognitionResult]'},
31+
}
32+
33+
def __init__(self, **kwargs):
34+
super(ReadOperationResult, self).__init__(**kwargs)
35+
self.status = kwargs.get('status', None)
36+
self.recognition_results = kwargs.get('recognition_results', None)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# coding=utf-8
2+
# --------------------------------------------------------------------------
3+
# Copyright (c) Microsoft Corporation. All rights reserved.
4+
# Licensed under the MIT License. See License.txt in the project root for
5+
# license information.
6+
#
7+
# Code generated by Microsoft (R) AutoRest Code Generator.
8+
# Changes may cause incorrect behavior and will be lost if the code is
9+
# regenerated.
10+
# --------------------------------------------------------------------------
11+
12+
from msrest.serialization import Model
13+
14+
15+
class ReadOperationResult(Model):
16+
"""OCR result of the read operation.
17+
18+
:param status: Status of the read operation. Possible values include: 'Not
19+
Started', 'Running', 'Failed', 'Succeeded'
20+
:type status: str or
21+
~azure.cognitiveservices.vision.computervision.models.TextOperationStatusCodes
22+
:param recognition_results: A array of text recognition result of the read
23+
operation.
24+
:type recognition_results:
25+
list[~azure.cognitiveservices.vision.computervision.models.TextRecognitionResult]
26+
"""
27+
28+
_attribute_map = {
29+
'status': {'key': 'status', 'type': 'TextOperationStatusCodes'},
30+
'recognition_results': {'key': 'recognitionResults', 'type': '[TextRecognitionResult]'},
31+
}
32+
33+
def __init__(self, *, status=None, recognition_results=None, **kwargs) -> None:
34+
super(ReadOperationResult, self).__init__(**kwargs)
35+
self.status = status
36+
self.recognition_results = recognition_results

0 commit comments

Comments
 (0)