cheesecz commited on
Commit
bbf7c17
·
verified ·
1 Parent(s): 61a4623

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -14
app.py CHANGED
@@ -4,10 +4,10 @@ import cv2
4
  import numpy as np
5
  import re
6
 
7
- # Force CPU by setting use_gpu=False and using correct parameter
8
- ocr = PaddleOCR(use_textline_orientation=True, use_gpu=False, lang='ind')
9
 
10
- # Define regex patterns for KTP
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, cls=True)
23
 
24
- # Flatten into lines
25
- lines = [" ".join([word_info[1][0] for word_info in line]) for line in result[0]]
26
- text_block = "\n".join(lines)
27
 
28
- # Extract fields
29
  data = {}
30
  for key, pattern in field_patterns.items():
31
- match = re.search(pattern, text_block)
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 take a photo of an Indonesian KTP. This tool will auto-extract NIK, Nama, TTL, Alamat, and Golongan Darah."
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()