Thantham commited on
Commit
9c5e6eb
1 Parent(s): fc1164a
Files changed (2) hide show
  1. Dockerfile +26 -0
  2. app.py +39 -0
Dockerfile ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM ubuntu:jammy
2
+ #FROM alpine:latest
3
+
4
+ # Install dependencies
5
+ RUN apt-get update && apt-get install -y \
6
+ tesseract-ocr \
7
+ tesseract-ocr-eng \
8
+ tesseract-ocr-tha \
9
+ python3 \
10
+ python3-pip
11
+
12
+ # Install FastAPI and Uvicorn (an ASGI server)
13
+ RUN pip3 install fastapi uvicorn pillow pytesseract python-multipart PyMuPDF
14
+
15
+ # Set working directory
16
+ WORKDIR /app
17
+
18
+ # Copy your application code
19
+ COPY . .
20
+
21
+
22
+ # Expose port for the service
23
+ EXPOSE 5000
24
+
25
+ # Start your application with Uvicorn
26
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
app.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, File, UploadFile
2
+ from PIL import Image
3
+ import pytesseract
4
+ import io
5
+ import fitz
6
+
7
+ app = FastAPI()
8
+
9
+ @app.get("/test")
10
+ async def test_api():
11
+ return {"message": "API is working ..."}
12
+
13
+ @app.post("/ocr_image")
14
+ async def perform_ocr_image(image: UploadFile = File(...)):
15
+ image_data = await image.read()
16
+
17
+ image = Image.open(io.BytesIO(image_data))
18
+ text = pytesseract.image_to_string(image, lang='tha+eng')
19
+ return {"text": text}
20
+
21
+
22
+ @app.post("/ocr_pdf")
23
+ async def perform_ocr_pdf(image: UploadFile = File(...)):
24
+ image_data = await image.read()
25
+
26
+ doc = fitz.open(stream=image_data, filetype="pdf")
27
+
28
+ page = doc[0] # Access the desired page (zero-indexed)
29
+
30
+ # Generate a high-quality image of the page
31
+ zoom = 2 # Adjust for better OCR resolution if needed
32
+ mat = fitz.Matrix(zoom, zoom) # Zoom matrix
33
+ pix = page.get_pixmap(matrix=mat)
34
+ img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
35
+
36
+ # Analyze text orientation with Pytesseract
37
+ #text_orientation = pytesseract.image_to_osd(img)
38
+ text = pytesseract.image_to_string(img, lang='tha+eng')
39
+ return {"text": text}