ken4 commited on
Commit
da972ba
·
verified ·
1 Parent(s): 85856bb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -20
app.py CHANGED
@@ -6,48 +6,78 @@ from PIL import Image
6
  from io import BytesIO
7
  import requests
8
 
9
- app = FastAPI(title="PaddleOCR API", description="OCR via URL or File Upload")
 
 
 
 
10
 
11
- ocr = PaddleOCR(use_angle_cls=True, lang='en')
 
 
 
12
 
13
- def run_ocr(image_bytes):
14
  img = Image.open(BytesIO(image_bytes)).convert("RGB")
15
- result = ocr.ocr(np.array(img), cls=True)
 
 
16
 
17
- parsed_texts = []
18
  outputs = []
 
19
 
20
- for line in result:
21
- for box, (text, conf) in line:
 
 
 
 
22
  parsed_texts.append(text)
23
  outputs.append({
24
  "text": text,
25
- "confidence": float(conf),
26
- "bounding_box": [[float(x), float(y)] for x, y in box]
27
  })
28
 
29
- final_text = " ".join(parsed_texts)
30
-
31
  return {
32
  "results": outputs,
33
  "parsedText": parsed_texts,
34
- "finalText": final_text
35
  }
36
 
37
  @app.get("/ocr")
38
- async def ocr_url(url: str):
39
  try:
40
- img_data = requests.get(url, timeout=10).content
41
- data = run_ocr(img_data)
42
- return JSONResponse({"success": True, **data})
 
 
 
 
43
  except Exception as e:
44
- return JSONResponse({"success": False, "error": str(e)}, status_code=400)
 
 
 
 
 
 
45
 
46
  @app.post("/ocr")
47
- async def ocr_buffer(file: UploadFile = File(...)):
48
  try:
49
  image_bytes = await file.read()
50
  data = run_ocr(image_bytes)
51
- return JSONResponse({"success": True, **data})
 
 
 
52
  except Exception as e:
53
- return JSONResponse({"success": False, "error": str(e)}, status_code=400)
 
 
 
 
 
 
 
6
  from io import BytesIO
7
  import requests
8
 
9
+ app = FastAPI(
10
+ title="PaddleOCR API",
11
+ description="OCR via URL or File Upload",
12
+ version="1.0.0"
13
+ )
14
 
15
+ ocr = PaddleOCR(
16
+ use_angle_cls=True,
17
+ lang="en"
18
+ )
19
 
20
+ def run_ocr(image_bytes: bytes):
21
  img = Image.open(BytesIO(image_bytes)).convert("RGB")
22
+ img_np = np.array(img)
23
+
24
+ result = ocr.predict(img_np)
25
 
 
26
  outputs = []
27
+ parsed_texts = []
28
 
29
+ for res in result:
30
+ boxes = res.get("rec_boxes", [])
31
+ texts = res.get("rec_texts", [])
32
+ scores = res.get("rec_scores", [])
33
+
34
+ for box, text, score in zip(boxes, texts, scores):
35
  parsed_texts.append(text)
36
  outputs.append({
37
  "text": text,
38
+ "confidence": float(score),
39
+ "bounding_box": box.tolist()
40
  })
41
 
 
 
42
  return {
43
  "results": outputs,
44
  "parsedText": parsed_texts,
45
+ "finalText": " ".join(parsed_texts)
46
  }
47
 
48
  @app.get("/ocr")
49
+ async def ocr_from_url(url: str):
50
  try:
51
+ response = requests.get(url, timeout=15)
52
+ response.raise_for_status()
53
+ data = run_ocr(response.content)
54
+ return JSONResponse({
55
+ "success": True,
56
+ **data
57
+ })
58
  except Exception as e:
59
+ return JSONResponse(
60
+ status_code=400,
61
+ content={
62
+ "success": False,
63
+ "error": str(e)
64
+ }
65
+ )
66
 
67
  @app.post("/ocr")
68
+ async def ocr_from_file(file: UploadFile = File(...)):
69
  try:
70
  image_bytes = await file.read()
71
  data = run_ocr(image_bytes)
72
+ return JSONResponse({
73
+ "success": True,
74
+ **data
75
+ })
76
  except Exception as e:
77
+ return JSONResponse(
78
+ status_code=400,
79
+ content={
80
+ "success": False,
81
+ "error": str(e)
82
+ }
83
+ )