Skip to content

Instantly share code, notes, and snippets.

@DSamuylov
Created December 9, 2024 10:38
Show Gist options
  • Select an option

  • Save DSamuylov/c434dfd3bc08d8862a19faf59f7b1b8f to your computer and use it in GitHub Desktop.

Select an option

Save DSamuylov/c434dfd3bc08d8862a19faf59f7b1b8f to your computer and use it in GitHub Desktop.
Azure function to run OCR using Azure AI vision.
import azure.functions as func
import json
import logging
import os
import time
import requests
app = func.FunctionApp(http_auth_level=func.AuthLevel.FUNCTION)
AZURE_AI_VISION_ENDPOINT = os.getenv("AZURE_AI_VISION_ENDPOINT")
AZURE_AI_VISION_KEY = os.getenv("AZURE_AI_VISION_KEY")
@app.function_name(name="HttpTrigger1")
@app.route(route="run_ocr")
def run_ocr(req: func.HttpRequest) -> func.HttpResponse:
logging.info("Python HTTP trigger function processed a request.")
binary_data = req.get_body()
text = process_document(binary_data)
return func.HttpResponse(body=json.dumps({"text": text}), status_code=200)
def process_document(binary_data) -> str:
response = requests.post(
f"{AZURE_AI_VISION_ENDPOINT}/vision/v3.2/read/analyze",
headers={
"Content-Type": "application/pdf",
"Ocp-Apim-Subscription-Key": AZURE_AI_VISION_KEY,
},
data=binary_data,
)
operation_location = response.headers["Operation-Location"]
while True:
result_response = requests.get(
operation_location,
headers={"Ocp-Apim-Subscription-Key": AZURE_AI_VISION_KEY},
)
result = result_response.json()
status = result.get("status")
if status in ["succeeded", "failed"]:
break
time.sleep(1)
lines = []
for page in result["analyzeResult"]["readResults"]:
for line in page["lines"]:
lines.append(line["text"])
text = "\n".join(lines)
return text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment