Rathapoom's picture
Update app.py
9395482
## OCR Gradio ##
import numpy as np
import gradio as gr
import pytesseract
import re
import cv2
def bmd_reader(input_img):
pytesseract.pytesseract.tesseract_cmd = 'tesseract'
image = cv2.resize(input_img, None, fx=2, fy=2, interpolation=cv2.INTER_CUBIC)
# Convert the image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Threshold the image to remove noise and improve OCR accuracy
threshold = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
# Set the configuration parameters for pytesseract
config = ('-l eng --oem 1 --psm 6')
# Run pytesseract on the thresholded image
text = pytesseract.image_to_string(threshold, config=config)
matches = re.finditer(r'\b[Nn]ormal\b|\b[Oo]steopenia\b|\b[Oo]steoporosis\b|\b[Ll]ow [Bb]one\b', text)
hipmatches = re.finditer(r'\b[Hh]ip\b|\b[Ff]emur\b|\b[Nn]eck\b|\b[Tt]roch\b',text)
spinematches = re.finditer(r'\b[Ss]pine\b|\b[Ll]umber\b|\bL1\b|\bL2\b|\bL3\b',text)
wristmatches = re.finditer(r'\b[Ww]rist\b|\b[Ff]orearm\b|\b[Ra]adius\b',text)
# Iterate over the matches and print the location of each match
for match in matches:
#print(f"Match '{match.group()}' found at ({match.start()}, {match.end()})")
diag = match.group()
# change low bone to osteopenia
if diag == "Low Bone" or diag == "low bone" or diag == "Low bone" or diag == "low Bone":
diag = "Osteopenia"
if not diag:
diag = "Diagnosis not found."
#return diag
h = 0
for match in hipmatches:
hip = match.group()
h = h + 1
s = 0
for match in spinematches:
spine = match.group()
s = s + 1
f = 0
for match in wristmatches:
wrist = match.group()
f = f + 1
#print(h, s, f)
if h == 0 and s == 0 and f == 0:
location = "หาตำแหน่งจากรูปภาพได้ไม่ชัดเจน"
elif h > 0:
location = "ผลการตรวจมวลกระดูกของบริเวณสะโพก"
elif s > 0:
location = "ผลการตรวจมวลกระดูกของบริเวณกระดูกสันหลัง"
elif f > 0:
location = "ผลการตรวจมวลกระดูกของบริเวณกระดูกข้อมือ"
return location, diag
#demo = gr.Interface(sepia, gr.Image(),"text")
demo = gr.Interface(
fn = bmd_reader,
inputs = "image",
outputs = [gr.Textbox(
label="บริเวณตำแหน่งที่ตรวจ",
value="กระดูกสันหลัง หรือกระดูกสะโพก",
) ,
gr.Textbox(
label="ผลอ่านจากการประมวลผลตัวอักษร",
value="ผลวินิจฉัย",
) ],
)
demo.launch()