Spaces:
Sleeping
Sleeping
QMonitor Admin
commited on
Commit
·
962157e
1
Parent(s):
723c64a
Simplify Gradio interface for better API compatibility
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import pytesseract
|
| 3 |
-
from PIL import Image
|
| 4 |
import cv2
|
| 5 |
import numpy as np
|
| 6 |
import tempfile
|
|
@@ -78,45 +78,6 @@ def detect_checkboxes(image, orig_image):
|
|
| 78 |
|
| 79 |
return jenis_pengamatan
|
| 80 |
|
| 81 |
-
# Fungsi utama untuk OCR
|
| 82 |
-
def perform_ocr(image):
|
| 83 |
-
# Simpan gambar ke file temporari
|
| 84 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix='.jpg') as temp:
|
| 85 |
-
image_path = temp.name
|
| 86 |
-
img_pil = Image.fromarray(image)
|
| 87 |
-
img_pil.save(image_path)
|
| 88 |
-
|
| 89 |
-
# Preprocess gambar untuk OCR
|
| 90 |
-
preprocessed = preprocess_image(img_pil)
|
| 91 |
-
cv2.imwrite(image_path + '_processed.jpg', preprocessed)
|
| 92 |
-
|
| 93 |
-
# Lakukan OCR pada gambar yang telah diproses
|
| 94 |
-
text = pytesseract.image_to_string(Image.open(image_path + '_processed.jpg'), lang='ind')
|
| 95 |
-
|
| 96 |
-
# Hapus file temporari
|
| 97 |
-
os.unlink(image_path)
|
| 98 |
-
os.unlink(image_path + '_processed.jpg')
|
| 99 |
-
|
| 100 |
-
# Lakukan juga deteksi checkbox
|
| 101 |
-
jenis_pengamatan = detect_checkboxes(img_pil, image)
|
| 102 |
-
|
| 103 |
-
# Parse teks hasil OCR menjadi data terstruktur
|
| 104 |
-
data = parse_form_text(text)
|
| 105 |
-
|
| 106 |
-
# Tambahkan hasil deteksi checkbox ke data
|
| 107 |
-
data['jenis_pengamatan'] = []
|
| 108 |
-
for jenis, checked in jenis_pengamatan.items():
|
| 109 |
-
if checked:
|
| 110 |
-
data['jenis_pengamatan'].append(jenis)
|
| 111 |
-
|
| 112 |
-
# Gabungkan menjadi string
|
| 113 |
-
if data['jenis_pengamatan']:
|
| 114 |
-
data['jenis_pengamatan'] = ', '.join(data['jenis_pengamatan'])
|
| 115 |
-
else:
|
| 116 |
-
data['jenis_pengamatan'] = ''
|
| 117 |
-
|
| 118 |
-
return data
|
| 119 |
-
|
| 120 |
# Fungsi untuk memparse teks dari form LSB
|
| 121 |
def parse_form_text(text):
|
| 122 |
lines = text.split('\n')
|
|
@@ -168,66 +129,83 @@ def parse_form_text(text):
|
|
| 168 |
|
| 169 |
return data
|
| 170 |
|
| 171 |
-
#
|
| 172 |
-
def
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
if
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
return result
|
| 184 |
-
|
| 185 |
-
# Interface web dengan Gradio
|
| 186 |
-
with gr.Blocks() as demo:
|
| 187 |
-
gr.Markdown("# LSB Form OCR")
|
| 188 |
-
gr.Markdown("Upload gambar formulir LSB untuk ekstraksi data otomatis")
|
| 189 |
-
|
| 190 |
-
with gr.Row():
|
| 191 |
-
with gr.Column():
|
| 192 |
-
input_image = gr.Image(type="pil", label="Upload Gambar Formulir LSB")
|
| 193 |
-
submit_btn = gr.Button("Proses OCR")
|
| 194 |
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
uraian_output = gr.Textbox(label="Uraian Pengamatan")
|
| 202 |
-
tindakan_output = gr.Textbox(label="Tindakan Intervensi")
|
| 203 |
-
|
| 204 |
-
def process_image(img):
|
| 205 |
-
if img is None:
|
| 206 |
-
return ["No image uploaded"] * 7
|
| 207 |
|
| 208 |
-
|
|
|
|
|
|
|
| 209 |
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
inputs=[input_image],
|
| 223 |
-
outputs=[nama_output, posisi_output, lokasi_output, tanggal_output,
|
| 224 |
-
jenis_output, uraian_output, tindakan_output]
|
| 225 |
-
)
|
| 226 |
|
| 227 |
-
#
|
| 228 |
-
|
| 229 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 230 |
|
| 231 |
-
#
|
| 232 |
-
demo.
|
| 233 |
-
demo.launch(share=True) # Mengaktifkan share=True untuk membuat API endpoint publik
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pytesseract
|
| 3 |
+
from PIL import Image
|
| 4 |
import cv2
|
| 5 |
import numpy as np
|
| 6 |
import tempfile
|
|
|
|
| 78 |
|
| 79 |
return jenis_pengamatan
|
| 80 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
# Fungsi untuk memparse teks dari form LSB
|
| 82 |
def parse_form_text(text):
|
| 83 |
lines = text.split('\n')
|
|
|
|
| 129 |
|
| 130 |
return data
|
| 131 |
|
| 132 |
+
# Fungsi utama untuk OCR
|
| 133 |
+
def perform_ocr(image):
|
| 134 |
+
try:
|
| 135 |
+
# Jika image adalah PIL.Image, konversi ke numpy array
|
| 136 |
+
if not isinstance(image, np.ndarray):
|
| 137 |
+
image = np.array(image)
|
| 138 |
+
|
| 139 |
+
# Simpan gambar ke file temporari
|
| 140 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.jpg') as temp:
|
| 141 |
+
image_path = temp.name
|
| 142 |
+
img_pil = Image.fromarray(image)
|
| 143 |
+
img_pil.save(image_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
|
| 145 |
+
# Preprocess gambar untuk OCR
|
| 146 |
+
preprocessed = preprocess_image(img_pil)
|
| 147 |
+
cv2.imwrite(image_path + '_processed.jpg', preprocessed)
|
| 148 |
+
|
| 149 |
+
# Lakukan OCR pada gambar yang telah diproses
|
| 150 |
+
text = pytesseract.image_to_string(Image.open(image_path + '_processed.jpg'), lang='ind')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
|
| 152 |
+
# Hapus file temporari
|
| 153 |
+
os.unlink(image_path)
|
| 154 |
+
os.unlink(image_path + '_processed.jpg')
|
| 155 |
|
| 156 |
+
# Lakukan juga deteksi checkbox
|
| 157 |
+
jenis_pengamatan = detect_checkboxes(img_pil, image)
|
| 158 |
+
|
| 159 |
+
# Parse teks hasil OCR menjadi data terstruktur
|
| 160 |
+
data = parse_form_text(text)
|
| 161 |
+
|
| 162 |
+
# Tambahkan hasil deteksi checkbox ke data
|
| 163 |
+
data['jenis_pengamatan'] = []
|
| 164 |
+
for jenis, checked in jenis_pengamatan.items():
|
| 165 |
+
if checked:
|
| 166 |
+
data['jenis_pengamatan'].append(jenis)
|
| 167 |
+
|
| 168 |
+
# Gabungkan menjadi string
|
| 169 |
+
if data['jenis_pengamatan']:
|
| 170 |
+
data['jenis_pengamatan'] = ', '.join(data['jenis_pengamatan'])
|
| 171 |
+
else:
|
| 172 |
+
data['jenis_pengamatan'] = ''
|
| 173 |
+
|
| 174 |
+
return data
|
| 175 |
+
except Exception as e:
|
| 176 |
+
print(f"Error in OCR: {e}")
|
| 177 |
+
return {
|
| 178 |
+
'error': str(e),
|
| 179 |
+
'nama_pelapor': '',
|
| 180 |
+
'posisi_jabatan': '',
|
| 181 |
+
'lokasi_kejadian': '',
|
| 182 |
+
'tanggal_waktu': '',
|
| 183 |
+
'uraian_pengamatan': '',
|
| 184 |
+
'tindakan_intervensi': '',
|
| 185 |
+
'jenis_pengamatan': ''
|
| 186 |
+
}
|
| 187 |
+
|
| 188 |
+
# Buat interface Gradio sederhana
|
| 189 |
+
def process_image(img):
|
| 190 |
+
if img is None:
|
| 191 |
+
return "No image uploaded"
|
| 192 |
|
| 193 |
+
# Proses OCR
|
| 194 |
+
result = perform_ocr(img)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
|
| 196 |
+
# Return hasil sebagai string untuk ditampilkan
|
| 197 |
+
return str(result)
|
| 198 |
+
|
| 199 |
+
# Buat interface yang sangat sederhana untuk API
|
| 200 |
+
demo = gr.Interface(
|
| 201 |
+
fn=process_image,
|
| 202 |
+
inputs=gr.Image(type="pil"),
|
| 203 |
+
outputs="text",
|
| 204 |
+
title="LSB Form OCR API",
|
| 205 |
+
description="Upload gambar formulir LSB untuk ekstraksi data otomatis",
|
| 206 |
+
examples=[],
|
| 207 |
+
cache_examples=False,
|
| 208 |
+
)
|
| 209 |
|
| 210 |
+
# Launch dengan API enabled
|
| 211 |
+
demo.launch(share=True)
|
|
|