forked from codingforentrepreneurs/Try-Django-3.2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservices.py
29 lines (25 loc) · 827 Bytes
/
services.py
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
import requests
import os
from django.core.files import File
OCR_API_TOKEN_HEADER=os.environ.get("OCR_API_TOKEN_HEADER")
OCR_API_ENDPOINT=os.environ.get("OCR_API_ENDPOINT")
def extract_text_via_ocr_service(file_obj: File=None):
data = {}
if OCR_API_ENDPOINT is None:
return data
if OCR_API_TOKEN_HEADER is None:
return data
if file_obj is None:
return data
# get image
# send image through HTTP POST
# return dict {}
headers={
"Authorization": f"Bearer {OCR_API_TOKEN_HEADER}"
}
with file_obj.open('rb') as f:
r = requests.post(OCR_API_ENDPOINT, files={"file": f}, headers=headers)
if r.status_code in range(200, 299):
if r.headers.get("content-type") == 'application/json':
data = r.json()
return data