File size: 2,090 Bytes
65a8134
2a07d13
 
 
 
 
df43f0a
2a07d13
 
 
df43f0a
65a8134
 
2a07d13
65a8134
2a07d13
65a8134
 
 
 
df43f0a
 
2a07d13
 
65a8134
 
 
df43f0a
65a8134
 
 
 
 
 
df43f0a
 
 
65a8134
 
2a07d13
df43f0a
65a8134
df43f0a
65a8134
 
df43f0a
2a07d13
65a8134
 
 
df43f0a
65a8134
df43f0a
 
65a8134
2a07d13
 
df43f0a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import gradio as gr
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
from PIL import Image
import os
import re

# Load Hugging Face OCR model
processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-stage1")
model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-stage1")

# Folder to store extracted patient records
PATIENT_RECORDS_DIR = "records"
os.makedirs(PATIENT_RECORDS_DIR, exist_ok=True)

# Extract patient name from filename
def extract_patient_name(file_name):
    match = re.match(r"([A-Za-z]+[A-Za-z]*)_.*\.(jpg|jpeg|png)$", file_name)
    return match.group(1) if match else None

# OCR logic
def perform_ocr(image_path):
    image = Image.open(image_path).convert("RGB")
    pixel_values = processor(images=image, return_tensors="pt").pixel_values
    generated_ids = model.generate(pixel_values)
    text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0].strip()
    return text

# Save to patient record
def save_record(patient_name, ocr_text):
    file_path = os.path.join(PATIENT_RECORDS_DIR, f"{patient_name}_records.txt")
    with open(file_path, "a") as f:
        f.write("\n\n===== New Lab Result =====\n")
        f.write(ocr_text)

# Main Gradio handler
def process_lab_result(image_path):
    file_name = os.path.basename(image_path)
    patient_name = extract_patient_name(file_name)

    if not patient_name:
        return "❌ Cannot extract patient name from filename. Use format: JuanDelaCruz_2025-06-13.jpg"

    ocr_text = perform_ocr(image_path)
    save_record(patient_name, ocr_text)

    return f"βœ… OCR completed. Lab result saved for `{patient_name}`.\n\nπŸ“„ Extracted Text:\n\n{ocr_text}"

# Gradio interface
iface = gr.Interface(
    fn=process_lab_result,
    inputs=gr.File(label="Upload Lab Result (.jpg/.png)", type="filepath"),
    outputs="text",
    title="🩺 Lab Result OCR",
    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."
)

if __name__ == "__main__":
    iface.launch()