Update
Browse files
app.py
CHANGED
@@ -4,56 +4,55 @@ from PIL import Image
|
|
4 |
import os
|
5 |
import re
|
6 |
|
7 |
-
# Load OCR model
|
8 |
processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-stage1")
|
9 |
model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-stage1")
|
10 |
|
11 |
-
# Folder to store extracted records
|
12 |
PATIENT_RECORDS_DIR = "records"
|
13 |
os.makedirs(PATIENT_RECORDS_DIR, exist_ok=True)
|
14 |
|
15 |
# Extract patient name from filename
|
16 |
def extract_patient_name(file_name):
|
17 |
-
# Example: JuanDelaCruz_2025-06-13.png β JuanDelaCruz
|
18 |
match = re.match(r"([A-Za-z]+[A-Za-z]*)_.*\.(jpg|jpeg|png)$", file_name)
|
19 |
return match.group(1) if match else None
|
20 |
|
21 |
# OCR logic
|
22 |
-
def perform_ocr(
|
23 |
-
image = Image.open(
|
24 |
pixel_values = processor(images=image, return_tensors="pt").pixel_values
|
25 |
generated_ids = model.generate(pixel_values)
|
26 |
text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0].strip()
|
27 |
return text
|
28 |
|
29 |
-
# Save to patient record
|
30 |
def save_record(patient_name, ocr_text):
|
31 |
file_path = os.path.join(PATIENT_RECORDS_DIR, f"{patient_name}_records.txt")
|
32 |
with open(file_path, "a") as f:
|
33 |
f.write("\n\n===== New Lab Result =====\n")
|
34 |
f.write(ocr_text)
|
35 |
|
36 |
-
# Gradio
|
37 |
-
def process_lab_result(
|
38 |
-
file_name = os.path.basename(
|
39 |
patient_name = extract_patient_name(file_name)
|
40 |
|
41 |
if not patient_name:
|
42 |
-
return "β Cannot extract patient name from filename.
|
43 |
|
44 |
-
ocr_text = perform_ocr(
|
45 |
save_record(patient_name, ocr_text)
|
46 |
|
47 |
-
return f"β
OCR completed. Lab result saved
|
48 |
|
49 |
# Gradio interface
|
50 |
iface = gr.Interface(
|
51 |
fn=process_lab_result,
|
52 |
-
inputs=gr.File(label="Upload Lab Result
|
53 |
outputs="text",
|
54 |
-
title="π©Ί Lab Result OCR
|
55 |
-
description="Upload a lab result image named like `JuanDelaCruz_2025-06-13.
|
56 |
)
|
57 |
|
58 |
if __name__ == "__main__":
|
59 |
-
iface.launch()
|
|
|
4 |
import os
|
5 |
import re
|
6 |
|
7 |
+
# Load Hugging Face OCR model
|
8 |
processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-stage1")
|
9 |
model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-stage1")
|
10 |
|
11 |
+
# Folder to store extracted patient records
|
12 |
PATIENT_RECORDS_DIR = "records"
|
13 |
os.makedirs(PATIENT_RECORDS_DIR, exist_ok=True)
|
14 |
|
15 |
# Extract patient name from filename
|
16 |
def extract_patient_name(file_name):
|
|
|
17 |
match = re.match(r"([A-Za-z]+[A-Za-z]*)_.*\.(jpg|jpeg|png)$", file_name)
|
18 |
return match.group(1) if match else None
|
19 |
|
20 |
# OCR logic
|
21 |
+
def perform_ocr(image_path):
|
22 |
+
image = Image.open(image_path).convert("RGB")
|
23 |
pixel_values = processor(images=image, return_tensors="pt").pixel_values
|
24 |
generated_ids = model.generate(pixel_values)
|
25 |
text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0].strip()
|
26 |
return text
|
27 |
|
28 |
+
# Save to patient record
|
29 |
def save_record(patient_name, ocr_text):
|
30 |
file_path = os.path.join(PATIENT_RECORDS_DIR, f"{patient_name}_records.txt")
|
31 |
with open(file_path, "a") as f:
|
32 |
f.write("\n\n===== New Lab Result =====\n")
|
33 |
f.write(ocr_text)
|
34 |
|
35 |
+
# Main Gradio handler
|
36 |
+
def process_lab_result(image_path):
|
37 |
+
file_name = os.path.basename(image_path)
|
38 |
patient_name = extract_patient_name(file_name)
|
39 |
|
40 |
if not patient_name:
|
41 |
+
return "β Cannot extract patient name from filename. Use format: JuanDelaCruz_2025-06-13.jpg"
|
42 |
|
43 |
+
ocr_text = perform_ocr(image_path)
|
44 |
save_record(patient_name, ocr_text)
|
45 |
|
46 |
+
return f"β
OCR completed. Lab result saved for `{patient_name}`.\n\nπ Extracted Text:\n\n{ocr_text}"
|
47 |
|
48 |
# Gradio interface
|
49 |
iface = gr.Interface(
|
50 |
fn=process_lab_result,
|
51 |
+
inputs=gr.File(label="Upload Lab Result (.jpg/.png)", type="filepath"),
|
52 |
outputs="text",
|
53 |
+
title="π©Ί Lab Result OCR",
|
54 |
+
description="Upload a lab result image named like `JuanDelaCruz_2025-06-13.jpg`. The text will be extracted and saved to the patient's record."
|
55 |
)
|
56 |
|
57 |
if __name__ == "__main__":
|
58 |
+
iface.launch()
|