Spaces:
Sleeping
Sleeping
Update ocr_utils.py
Browse files- ocr_utils.py +14 -7
ocr_utils.py
CHANGED
|
@@ -1,12 +1,19 @@
|
|
| 1 |
import requests
|
| 2 |
|
| 3 |
def extract_text_from_image(file_path, api_key):
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
result = r.json()
|
| 8 |
try:
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
return ""
|
|
|
|
| 1 |
import requests
|
| 2 |
|
| 3 |
def extract_text_from_image(file_path, api_key):
|
| 4 |
+
"""
|
| 5 |
+
Extract text from image using OCR.space API
|
| 6 |
+
"""
|
|
|
|
| 7 |
try:
|
| 8 |
+
url = "https://api.ocr.space/parse/image"
|
| 9 |
+
with open(file_path, "rb") as f:
|
| 10 |
+
r = requests.post(
|
| 11 |
+
url,
|
| 12 |
+
files={"file": f},
|
| 13 |
+
data={"apikey": api_key, "language": "eng"}
|
| 14 |
+
)
|
| 15 |
+
result = r.json()
|
| 16 |
+
return result['ParsedResults'][0]['ParsedText']
|
| 17 |
+
except Exception as e:
|
| 18 |
+
print(f"OCR extraction error: {e}")
|
| 19 |
return ""
|