Update app.py
Browse files
app.py
CHANGED
|
@@ -15,26 +15,36 @@ def run_ocr(image_bytes):
|
|
| 15 |
img_np = np.array(img)
|
| 16 |
results = reader.readtext(img_np)
|
| 17 |
|
|
|
|
| 18 |
output = []
|
|
|
|
| 19 |
for box, text, conf in results:
|
|
|
|
| 20 |
output.append({
|
| 21 |
"text": str(text),
|
| 22 |
"confidence": float(conf),
|
| 23 |
"bounding_box": [[float(x), float(y)] for x, y in box]
|
| 24 |
})
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
@app.post("/ocr/buffer")
|
| 28 |
async def ocr_buffer(file: UploadFile = File(...)):
|
| 29 |
image_bytes = await file.read()
|
| 30 |
-
|
| 31 |
-
return JSONResponse({"success": True,
|
| 32 |
|
| 33 |
@app.get("/ocr/url")
|
| 34 |
async def ocr_url(image_url: str):
|
| 35 |
try:
|
| 36 |
img_data = requests.get(image_url, timeout=10).content
|
| 37 |
-
|
| 38 |
-
return JSONResponse({"success": True,
|
| 39 |
except Exception as e:
|
| 40 |
return JSONResponse({"success": False, "error": str(e)}, status_code=400)
|
|
|
|
| 15 |
img_np = np.array(img)
|
| 16 |
results = reader.readtext(img_np)
|
| 17 |
|
| 18 |
+
parsed = []
|
| 19 |
output = []
|
| 20 |
+
|
| 21 |
for box, text, conf in results:
|
| 22 |
+
parsed.append(str(text))
|
| 23 |
output.append({
|
| 24 |
"text": str(text),
|
| 25 |
"confidence": float(conf),
|
| 26 |
"bounding_box": [[float(x), float(y)] for x, y in box]
|
| 27 |
})
|
| 28 |
+
|
| 29 |
+
final_text = " ".join(parsed)
|
| 30 |
+
|
| 31 |
+
return {
|
| 32 |
+
"results": output,
|
| 33 |
+
"parsedText": parsed,
|
| 34 |
+
"finalText": final_text
|
| 35 |
+
}
|
| 36 |
|
| 37 |
@app.post("/ocr/buffer")
|
| 38 |
async def ocr_buffer(file: UploadFile = File(...)):
|
| 39 |
image_bytes = await file.read()
|
| 40 |
+
data = run_ocr(image_bytes)
|
| 41 |
+
return JSONResponse({"success": True, **data})
|
| 42 |
|
| 43 |
@app.get("/ocr/url")
|
| 44 |
async def ocr_url(image_url: str):
|
| 45 |
try:
|
| 46 |
img_data = requests.get(image_url, timeout=10).content
|
| 47 |
+
data = run_ocr(img_data)
|
| 48 |
+
return JSONResponse({"success": True, **data})
|
| 49 |
except Exception as e:
|
| 50 |
return JSONResponse({"success": False, "error": str(e)}, status_code=400)
|