Spaces:
Runtime error
Runtime error
Commit ·
93ee3de
1
Parent(s): d45bc8b
Add application file
Browse files- DockerFile +17 -0
- app/__pycache__/main.cpython-310.pyc +0 -0
- app/__pycache__/ocr.cpython-310.pyc +0 -0
- app/main.py +21 -0
- app/ocr.py +56 -0
- requirements.txt +4 -0
DockerFile
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Use an official Python runtime as a parent image
|
| 2 |
+
FROM python:3.10
|
| 3 |
+
|
| 4 |
+
# Set the working directory in the container
|
| 5 |
+
WORKDIR /app
|
| 6 |
+
|
| 7 |
+
# Copy the current directory contents into the container at /app
|
| 8 |
+
COPY . /app
|
| 9 |
+
|
| 10 |
+
# Install any needed packages specified in requirements.txt
|
| 11 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 12 |
+
|
| 13 |
+
# Expose the port FastAPI runs on
|
| 14 |
+
EXPOSE 8000
|
| 15 |
+
|
| 16 |
+
# Run FastAPI when the container launches
|
| 17 |
+
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
|
app/__pycache__/main.cpython-310.pyc
ADDED
|
Binary file (809 Bytes). View file
|
|
|
app/__pycache__/ocr.cpython-310.pyc
ADDED
|
Binary file (1.62 kB). View file
|
|
|
app/main.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, File, UploadFile
|
| 2 |
+
from fastapi.responses import JSONResponse
|
| 3 |
+
from app.ocr import easyocr_ocr
|
| 4 |
+
|
| 5 |
+
app = FastAPI()
|
| 6 |
+
|
| 7 |
+
@app.post("/upload/")
|
| 8 |
+
async def upload_image(file: UploadFile = File(...)):
|
| 9 |
+
try:
|
| 10 |
+
# Save the uploaded file to a temporary location
|
| 11 |
+
with open(f"/tmp/{file.filename}", "wb") as buffer:
|
| 12 |
+
buffer.write(await file.read())
|
| 13 |
+
|
| 14 |
+
# Perform OCR on the saved image
|
| 15 |
+
formatted_output = easyocr_ocr(f"/tmp/{file.filename}")
|
| 16 |
+
|
| 17 |
+
# Return the formatted output
|
| 18 |
+
return JSONResponse(content={"formatted_output": formatted_output})
|
| 19 |
+
|
| 20 |
+
except Exception as e:
|
| 21 |
+
return JSONResponse(content={"error": str(e)}, status_code=500)
|
app/ocr.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import easyocr
|
| 2 |
+
|
| 3 |
+
def reformat_ocr_result(result):
|
| 4 |
+
mapping = {
|
| 5 |
+
"NIK": "NIK : ",
|
| 6 |
+
"Nama": "Nama : ",
|
| 7 |
+
"TempatITgl Lahir": "Tempat/Tgl Lahir : ",
|
| 8 |
+
"Jenis Kelamin": "Jenis Kelamin : ",
|
| 9 |
+
"GolDarah": "Gol Darah : ",
|
| 10 |
+
"Alamat": "Alamat : ",
|
| 11 |
+
"RTIRW": "RT/RW : ",
|
| 12 |
+
"KelDesa": "Kel/Desa : ",
|
| 13 |
+
"Kecamatan": "Kecamatan : ",
|
| 14 |
+
"Agama": "Agama : ",
|
| 15 |
+
"Status Perkawinan": "Status Perkawinan : ",
|
| 16 |
+
"Pekerjaan": "Pekerjaan : ",
|
| 17 |
+
"Kewarganegaraan": "Kewarganegaraan : ",
|
| 18 |
+
"Berlaku Hingga": "Berlaku Hingga : "
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
formatted_output = []
|
| 22 |
+
current_key = None
|
| 23 |
+
current_value = []
|
| 24 |
+
|
| 25 |
+
for line in result:
|
| 26 |
+
# Extract the text from each OCR result line
|
| 27 |
+
text = line.split(' (confidence: ')[0]
|
| 28 |
+
|
| 29 |
+
# Check if the text matches any of the keys
|
| 30 |
+
if any(key in text for key in mapping):
|
| 31 |
+
# If there's a current key, finalize its value
|
| 32 |
+
if current_key:
|
| 33 |
+
formatted_output.append(f"{mapping[current_key]}{' '.join(current_value)}")
|
| 34 |
+
current_value = []
|
| 35 |
+
|
| 36 |
+
# Find the key in the text
|
| 37 |
+
for key in mapping:
|
| 38 |
+
if key in text:
|
| 39 |
+
current_key = key
|
| 40 |
+
break
|
| 41 |
+
else:
|
| 42 |
+
if current_key:
|
| 43 |
+
# Add the text to the current value
|
| 44 |
+
current_value.append(text)
|
| 45 |
+
# Finalize the last key-value pair
|
| 46 |
+
if current_key:
|
| 47 |
+
formatted_output.append(f"{mapping[current_key]}{' '.join(current_value)}")
|
| 48 |
+
print(formatted_output)
|
| 49 |
+
return formatted_output
|
| 50 |
+
|
| 51 |
+
def easyocr_ocr(image_path):
|
| 52 |
+
reader = easyocr.Reader(['en'])
|
| 53 |
+
result = reader.readtext(image_path)
|
| 54 |
+
result_list = [f"{text} (confidence: {confidence:.4f})" for (bbox, text, confidence) in result]
|
| 55 |
+
formatted_output = reformat_ocr_result(result_list)
|
| 56 |
+
return formatted_output
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
easyocr
|
| 4 |
+
pillow
|