Update main.py
Browse files
main.py
CHANGED
|
@@ -1,9 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
@app.post("/v1/aadhaar/process")
|
| 2 |
async def process(file: UploadFile = File(...)):
|
| 3 |
try:
|
| 4 |
contents = await file.read()
|
| 5 |
img = cv2.imdecode(np.frombuffer(contents, np.uint8), cv2.IMREAD_COLOR)
|
| 6 |
|
|
|
|
|
|
|
|
|
|
| 7 |
# QR Mask
|
| 8 |
img = mask_qr(img)
|
| 9 |
|
|
@@ -13,17 +86,19 @@ async def process(file: UploadFile = File(...)):
|
|
| 13 |
|
| 14 |
img = mask_qr(img)
|
| 15 |
|
|
|
|
| 16 |
processed = enhance(img)
|
| 17 |
|
| 18 |
-
# OCR
|
| 19 |
ocr_result = ocr.ocr(processed)
|
| 20 |
|
| 21 |
-
# β
FIX
|
| 22 |
if not ocr_result:
|
| 23 |
raise HTTPException(status_code=400, detail="No text detected")
|
| 24 |
|
| 25 |
results = []
|
| 26 |
|
|
|
|
| 27 |
for line in ocr_result:
|
| 28 |
if not line:
|
| 29 |
continue
|
|
@@ -37,11 +112,12 @@ async def process(file: UploadFile = File(...)):
|
|
| 37 |
except:
|
| 38 |
continue
|
| 39 |
|
| 40 |
-
# β
|
| 41 |
extracted = {"aadhaar": None, "vid": None}
|
| 42 |
|
| 43 |
for i, (bbox, text, conf) in enumerate(results):
|
| 44 |
|
|
|
|
| 45 |
if conf < 0.6:
|
| 46 |
continue
|
| 47 |
|
|
@@ -69,6 +145,7 @@ async def process(file: UploadFile = File(...)):
|
|
| 69 |
img = mask(img, bbox, 1.0)
|
| 70 |
continue
|
| 71 |
|
|
|
|
| 72 |
_, buffer = cv2.imencode('.jpg', img)
|
| 73 |
|
| 74 |
return Response(
|
|
@@ -78,4 +155,11 @@ async def process(file: UploadFile = File(...)):
|
|
| 78 |
)
|
| 79 |
|
| 80 |
except Exception as e:
|
| 81 |
-
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
import cv2
|
| 3 |
+
import json
|
| 4 |
+
import uvicorn
|
| 5 |
+
import numpy as np
|
| 6 |
+
from fastapi import FastAPI, File, UploadFile, HTTPException
|
| 7 |
+
from fastapi.responses import Response
|
| 8 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 9 |
+
from paddleocr import PaddleOCR
|
| 10 |
+
|
| 11 |
+
# β
CREATE APP FIRST (VERY IMPORTANT)
|
| 12 |
+
app = FastAPI(title="AI Aadhaar Masking API")
|
| 13 |
+
|
| 14 |
+
app.add_middleware(
|
| 15 |
+
CORSMiddleware,
|
| 16 |
+
allow_origins=["*"],
|
| 17 |
+
allow_credentials=True,
|
| 18 |
+
allow_methods=["*"],
|
| 19 |
+
allow_headers=["*"],
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
# β
OCR INIT
|
| 23 |
+
ocr = PaddleOCR(use_angle_cls=True, lang='en')
|
| 24 |
+
|
| 25 |
+
# -------------------------------
|
| 26 |
+
# Enhance Image
|
| 27 |
+
# -------------------------------
|
| 28 |
+
def enhance(img):
|
| 29 |
+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 30 |
+
return cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8)).apply(gray)
|
| 31 |
+
|
| 32 |
+
# -------------------------------
|
| 33 |
+
# Clean Digits
|
| 34 |
+
# -------------------------------
|
| 35 |
+
def clean_digits(text):
|
| 36 |
+
text = text.upper() \
|
| 37 |
+
.replace('O','0') \
|
| 38 |
+
.replace('I','1') \
|
| 39 |
+
.replace('L','1') \
|
| 40 |
+
.replace('S','5') \
|
| 41 |
+
.replace('B','8')
|
| 42 |
+
return re.sub(r'[^0-9]', '', text)
|
| 43 |
+
|
| 44 |
+
# -------------------------------
|
| 45 |
+
# Mask Function
|
| 46 |
+
# -------------------------------
|
| 47 |
+
def mask(img, bbox, ratio):
|
| 48 |
+
p1 = tuple(map(int, bbox[0]))
|
| 49 |
+
p3 = tuple(map(int, bbox[2]))
|
| 50 |
+
width = p3[0] - p1[0]
|
| 51 |
+
mask_w = int(width * ratio)
|
| 52 |
+
cv2.rectangle(img, p1, (p1[0]+mask_w, p3[1]), (0,0,0), -1)
|
| 53 |
+
return img
|
| 54 |
+
|
| 55 |
+
# -------------------------------
|
| 56 |
+
# QR Mask
|
| 57 |
+
# -------------------------------
|
| 58 |
+
def mask_qr(img):
|
| 59 |
+
detector = cv2.QRCodeDetector()
|
| 60 |
+
data, bbox, _ = detector.detectAndDecode(img)
|
| 61 |
+
if bbox is not None:
|
| 62 |
+
bbox = bbox[0].astype(int)
|
| 63 |
+
x1, y1 = bbox[0]
|
| 64 |
+
x2, y2 = bbox[2]
|
| 65 |
+
cv2.rectangle(img, (x1,y1), (x2,y2), (0,0,0), -1)
|
| 66 |
+
return img
|
| 67 |
+
|
| 68 |
+
# -------------------------------
|
| 69 |
+
# MAIN API
|
| 70 |
+
# -------------------------------
|
| 71 |
@app.post("/v1/aadhaar/process")
|
| 72 |
async def process(file: UploadFile = File(...)):
|
| 73 |
try:
|
| 74 |
contents = await file.read()
|
| 75 |
img = cv2.imdecode(np.frombuffer(contents, np.uint8), cv2.IMREAD_COLOR)
|
| 76 |
|
| 77 |
+
if img is None:
|
| 78 |
+
raise HTTPException(status_code=400, detail="Invalid image")
|
| 79 |
+
|
| 80 |
# QR Mask
|
| 81 |
img = mask_qr(img)
|
| 82 |
|
|
|
|
| 86 |
|
| 87 |
img = mask_qr(img)
|
| 88 |
|
| 89 |
+
# Enhance
|
| 90 |
processed = enhance(img)
|
| 91 |
|
| 92 |
+
# OCR
|
| 93 |
ocr_result = ocr.ocr(processed)
|
| 94 |
|
| 95 |
+
# β
FIX: Safe check
|
| 96 |
if not ocr_result:
|
| 97 |
raise HTTPException(status_code=400, detail="No text detected")
|
| 98 |
|
| 99 |
results = []
|
| 100 |
|
| 101 |
+
# β
SAFE LOOP
|
| 102 |
for line in ocr_result:
|
| 103 |
if not line:
|
| 104 |
continue
|
|
|
|
| 112 |
except:
|
| 113 |
continue
|
| 114 |
|
| 115 |
+
# β
OUTSIDE LOOP
|
| 116 |
extracted = {"aadhaar": None, "vid": None}
|
| 117 |
|
| 118 |
for i, (bbox, text, conf) in enumerate(results):
|
| 119 |
|
| 120 |
+
# Ignore low confidence
|
| 121 |
if conf < 0.6:
|
| 122 |
continue
|
| 123 |
|
|
|
|
| 145 |
img = mask(img, bbox, 1.0)
|
| 146 |
continue
|
| 147 |
|
| 148 |
+
# Encode image
|
| 149 |
_, buffer = cv2.imencode('.jpg', img)
|
| 150 |
|
| 151 |
return Response(
|
|
|
|
| 155 |
)
|
| 156 |
|
| 157 |
except Exception as e:
|
| 158 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 159 |
+
|
| 160 |
+
|
| 161 |
+
# -------------------------------
|
| 162 |
+
# RUN
|
| 163 |
+
# -------------------------------
|
| 164 |
+
if __name__ == "__main__":
|
| 165 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|