
* 겪고 있는 문제 상황을 최대한 자세하게 작성해주세요.
* 문제 해결을 위해 어떤 시도를 해보았는지 구체적으로 함께 알려주세요.
ㅇ오류를 해결할 수 있도록 도와주세요
# 필요한 라이브러리 가져오기
from pdf2image import convert_from_path
from azure.cognitiveservices.vision.computervision import ComputerVisionClient
from azure.cognitiveservices.vision.computervision.models import OperationStatusCodes
from msrest.authentication import CognitiveServicesCredentials
import cv2
import io
import time
import os
# Azure credentials
SUBSCRIPTION_KEY = "These keys are used to access your Azure AI services API. Do not share your keys. Store them securely– for example, using Azure Key Vault. We also recommend regenerating these keys regularly. Only one key is necessary to make an API call. When regenerating the first key, you can use the second key for continued access to the service."
ENDPOINT_URL = "https://study5.cognitiveservices.azure.com/"
SUBSCRIPTION_KEY = SUBSCRIPTION_KEY.encode('utf-8'). decode('latin-1')
computervision_client = ComputerVisionClient(ENDPOINT_URL, CognitiveServicesCredentials(SUBSCRIPTION_KEY))
def detect_text_from_image(image_path):
image_data = open(image_path, "rb").read()
sbuf = io.BytesIO(image_data)
# API 호출하여 이미지를 처리
response = computervision_client.read_in_stream(sbuf, raw=True)
operation_location = response.headers["Operation-Location"]
operation_id = operation_location.split("/")[-1]
# 대기 시간 추가
time.sleep(1)
# API 작업 완료까지 대기 (폴링 방식으로 상태 확인)
while True:
read_result = computervision_client.get_read_result(operation_id)
if read_result.status not in ['notStarted', 'running']:
break
time.sleep(1)
raw_text = ""
if read_result.status == OperationStatusCodes.succeeded:
for result in read_result.analyze_result.read_results:
for line in result.lines:
raw_text += line.text + "\n"
return raw_text
detect_text_from_image(file_path)시면, 튜터님들이 빠르게 상황을 이해할 수 있어요.
작성한 코드 및 에러 메세지
ComputerVisionOcrErrorException Traceback (most recent call last)
<ipython-input-19-08b4d9e0c5c0> in <cell line: 0>()
44 return raw_text
45
---> 46 detect_text_from_image(file_path)
1 frames
/usr/local/lib/python3.11/dist-packages/azure/cognitiveservices/vision/computervision/operations/_computer_vision_client_operations.py in read_in_stream(self, image, language, pages, model_version, reading_order, custom_headers, raw, callback, **operation_config)
1707
1708 if response.status_code not in [202]:
-> 1709 raise models.ComputerVisionOcrErrorException(self._deserialize, response)
1710
1711 if raw:
ComputerVisionOcrErrorException: Operation returned an invalid status code 'PermissionDenied'
오류 발생 시, 작성한 코드 전체와 에러 메시지를 첨부해 주세요.
Tip 1) </> 아이콘을 눌러 코드박스를 만들어 보세요.
Tip 2) Ctrl+A(맥의 경우 Command+A) 단축키로 코드를 한 번에 선택할 수 있어요!
