Update app.py
Browse files
app.py
CHANGED
|
@@ -4,10 +4,10 @@ import cv2
|
|
| 4 |
import numpy as np
|
| 5 |
import re
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
ocr = PaddleOCR(
|
| 9 |
|
| 10 |
-
#
|
| 11 |
field_patterns = {
|
| 12 |
"nik": r"\b\d{16}\b",
|
| 13 |
"nama": r"(?i)\bnama\s*[:\-]?\s*(.*)",
|
|
@@ -16,19 +16,19 @@ field_patterns = {
|
|
| 16 |
"golongan_darah": r"(?i)\bgol\.?\s*darah\s*[:\-]?\s*([A|B|AB|O]+)"
|
| 17 |
}
|
| 18 |
|
| 19 |
-
|
| 20 |
def extract_ktp_data(image):
|
|
|
|
| 21 |
image_np = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
| 22 |
-
result = ocr.ocr(image_np
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
lines = [" ".join([
|
| 26 |
-
|
| 27 |
|
| 28 |
-
# Extract
|
| 29 |
data = {}
|
| 30 |
for key, pattern in field_patterns.items():
|
| 31 |
-
match = re.search(pattern,
|
| 32 |
if match:
|
| 33 |
data[key] = match.group(1).strip() if match.groups() else match.group(0)
|
| 34 |
else:
|
|
@@ -36,16 +36,14 @@ def extract_ktp_data(image):
|
|
| 36 |
|
| 37 |
return data
|
| 38 |
|
| 39 |
-
|
| 40 |
-
# Gradio Interface
|
| 41 |
demo = gr.Interface(
|
| 42 |
fn=extract_ktp_data,
|
| 43 |
inputs=gr.Image(type="pil", source="upload", tool="editor"),
|
| 44 |
outputs="json",
|
| 45 |
title="TakeCare - KTP Extractor",
|
| 46 |
-
description="Upload or
|
| 47 |
)
|
| 48 |
|
| 49 |
-
# Run directly for Hugging Face Spaces
|
| 50 |
if __name__ == "__main__":
|
| 51 |
demo.launch()
|
|
|
|
| 4 |
import numpy as np
|
| 5 |
import re
|
| 6 |
|
| 7 |
+
# Initialize PaddleOCR with supported arguments only (CPU-only, Indonesian)
|
| 8 |
+
ocr = PaddleOCR(lang='ind', show_log=False)
|
| 9 |
|
| 10 |
+
# Field extraction patterns
|
| 11 |
field_patterns = {
|
| 12 |
"nik": r"\b\d{16}\b",
|
| 13 |
"nama": r"(?i)\bnama\s*[:\-]?\s*(.*)",
|
|
|
|
| 16 |
"golongan_darah": r"(?i)\bgol\.?\s*darah\s*[:\-]?\s*([A|B|AB|O]+)"
|
| 17 |
}
|
| 18 |
|
|
|
|
| 19 |
def extract_ktp_data(image):
|
| 20 |
+
# Convert PIL to OpenCV (RGB → BGR)
|
| 21 |
image_np = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
| 22 |
+
result = ocr.ocr(image_np)
|
| 23 |
|
| 24 |
+
# Combine detected words into lines
|
| 25 |
+
lines = [" ".join([word[1][0] for word in line]) for line in result[0]]
|
| 26 |
+
full_text = "\n".join(lines)
|
| 27 |
|
| 28 |
+
# Extract data using regex
|
| 29 |
data = {}
|
| 30 |
for key, pattern in field_patterns.items():
|
| 31 |
+
match = re.search(pattern, full_text)
|
| 32 |
if match:
|
| 33 |
data[key] = match.group(1).strip() if match.groups() else match.group(0)
|
| 34 |
else:
|
|
|
|
| 36 |
|
| 37 |
return data
|
| 38 |
|
| 39 |
+
# Gradio app (camera or upload)
|
|
|
|
| 40 |
demo = gr.Interface(
|
| 41 |
fn=extract_ktp_data,
|
| 42 |
inputs=gr.Image(type="pil", source="upload", tool="editor"),
|
| 43 |
outputs="json",
|
| 44 |
title="TakeCare - KTP Extractor",
|
| 45 |
+
description="Upload or capture an image of an Indonesian KTP to extract data like NIK, Name, Birthplace/Date, Address, and Blood Type."
|
| 46 |
)
|
| 47 |
|
|
|
|
| 48 |
if __name__ == "__main__":
|
| 49 |
demo.launch()
|