Inayatgaming commited on
Commit
6be258b
·
verified ·
1 Parent(s): e153e7b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +116 -0
app.py ADDED
@@ -0,0 +1,116 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, UploadFile, File, Query
2
+ from fastapi.responses import JSONResponse
3
+ import easyocr
4
+ import numpy as np
5
+ import cv2
6
+ import time
7
+ import requests
8
+ from io import BytesIO
9
+ from PIL import Image
10
+ import os
11
+
12
+ app = FastAPI(title="EasyOCR API")
13
+
14
+ # Use /tmp for writable storage
15
+ tmp_dir = "/tmp/EasyOCR"
16
+ model_dir = os.path.join(tmp_dir, "models")
17
+ user_net_dir = os.path.join(tmp_dir, "user_network")
18
+ os.makedirs(model_dir, exist_ok=True)
19
+ os.makedirs(user_net_dir, exist_ok=True)
20
+
21
+ # Initialize reader with custom directories
22
+ reader = easyocr.Reader(
23
+ ['en'],
24
+ gpu=False,
25
+ model_storage_directory=model_dir,
26
+ user_network_directory=user_net_dir
27
+ )
28
+
29
+ def read_image_from_url(url: str):
30
+ response = requests.get(url)
31
+ response.raise_for_status()
32
+ img = np.array(Image.open(BytesIO(response.content)).convert("RGB"))
33
+ return img
34
+
35
+ def preprocess_image(image: np.ndarray) -> np.ndarray:
36
+ # Convert to grayscale
37
+ gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
38
+
39
+ # Denoise slightly
40
+ gray = cv2.GaussianBlur(gray, (3, 3), 0)
41
+
42
+ # Adaptive threshold (makes text sharper)
43
+ thresh = cv2.adaptiveThreshold(
44
+ gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
45
+ cv2.THRESH_BINARY, 31, 2
46
+ )
47
+
48
+ return thresh
49
+
50
+ @app.get("/")
51
+ async def ocr_url(url: str = Query(None)):
52
+ start_time = time.time()
53
+
54
+ if not url:
55
+ return JSONResponse(
56
+ status_code=400,
57
+ content={"status": False, "error": "Provide ?url= for OCR."}
58
+ )
59
+
60
+ try:
61
+ image = read_image_from_url(url)
62
+
63
+ # Preprocess for better OCR
64
+ processed = preprocess_image(image)
65
+
66
+ # OCR
67
+ result = reader.readtext(processed)
68
+
69
+ # Format result (only text)
70
+ output = [r[1] for r in result]
71
+
72
+ end_time = time.time()
73
+ time_taken = round(end_time - start_time, 3)
74
+
75
+ return {
76
+ "status": True,
77
+ "time_taken": time_taken,
78
+ "results": output
79
+ }
80
+
81
+ except Exception as e:
82
+ return JSONResponse(
83
+ status_code=500,
84
+ content={"status": False, "error": str(e)}
85
+ )
86
+
87
+ @app.post("/")
88
+ async def ocr_file(file: UploadFile = File(...)):
89
+ start_time = time.time()
90
+
91
+ try:
92
+ image = np.array(Image.open(file.file).convert("RGB"))
93
+
94
+ # Preprocess for better OCR
95
+ processed = preprocess_image(image)
96
+
97
+ # OCR
98
+ result = reader.readtext(processed)
99
+
100
+ # Format result (only text)
101
+ output = [r[1] for r in result]
102
+
103
+ end_time = time.time()
104
+ time_taken = round(end_time - start_time, 3)
105
+
106
+ return {
107
+ "status": True,
108
+ "time_taken": time_taken,
109
+ "results": output
110
+ }
111
+
112
+ except Exception as e:
113
+ return JSONResponse(
114
+ status_code=500,
115
+ content={"status": False, "error": str(e)}
116
+ )