2
$\begingroup$

enter image description hereBelow is text output obtained after ocr image to string of medical discharge summary report.

XXXXXXXX  T D.0.A'. 20.05. 2017
13.0,? ; 20.05.2017
AGE / sax; 43 YEAR(S] / MALE
CODE: IP1’7- 14041] FHL33350709 D.0.D:22.05.2017
ROOM NO: 1309F
CONSULTANT: XXXXXXXXX [CARDIOLOGIST]
CO v CONSULTANT: XXXXXXXX [GEN . PHYSICIAN]
DIAGNOSIS : ACUTE CORONARY SYNDROME
UNSTABLE ANGINA
MILD CORONARY ARTERY DISEASE
PLAN : MEDICAL MANAGEMENT

The output I obtained is not accurate enough as the input image.I want to cleanse the text output and extract the values of the fields such as DOA,DOD,AGE/SEX,CODE,ROOM NO,CONSULTANT,DIAGNOSIS etc. For ex , i need the extract the value of the field name "consultant" as "XXXXXXXXX [CARDIOLOGIST]" as output in excel format.

Below is my attempt :

import pytesseract as pt
import re
import pandas as pd
import cv2
import numpy as np
from matplotlib import pyplot as plt

from PIL import Image

img1 = Image.open('/root/regex_try/DS_Sample.jpg')

text = pt.image_to_string(img1)

print(text)

Fieldnames_dict = {'Hospital Name':['Hospital Name', 'Hospital_Name', 
'hospital name'],
'Hospital Address':['Address', 'city', 'Hospital Address'],
'Patient Name' : ['Name', 'Patient_Name', 'Patient Name', 'patient name'],
'Address' : ['Patient Address', 'Address','Amy“'],
'Age' : ['Age', 'age'],
'Sex': ['Sex', 'sex', 'gender', 'Gender'],
'Doctor Name' : ['Doctor Name', 'doctor name', 'doctor', 'treating doctor', 
'Consultants','CONSULTANT','Consultant Name'],
'Inpatient Number' :['Inpatient Number', 'IP Number'],
'Admission Date' : ['Date of Admission', 'Admitted Date', 'D.0.A'],
'Discharge Date' : ['Date of Discharge', 'Discharged Date', 'D.0.D'],
'Diagnosis' : ['Diagnosis', 'Final Diagnosis', 'Principal/Secondary 
 Diagnosis'],
'Treatment Given' : ['Treatment Given', 'On Examination', 'Examination'],
'Follow up' : ['Follow Up', 'Follow up', 'Review & Advise', 'Condition on 
 Discharge', 'Advise on Discharge'],
'Summary' : ['Summary', 'Past Treatment Given'],
'Bed No' : ['Bed Number', 'Bed / Ward Details'],
'SS Number' : ['SS Number', 'SS No.'],
'UHID Number' : ['UHID Number', 'UHID No'],
'MR Number' : ['MR No', 'MR Number','MRNO '],
}

def preprocess_formtext(field_dict,text):
   values_dict=field_dict
   values_dict=values_dict.fromkeys(values_dict, '')
 for i in field_dict.keys(): #reaching the keys of dict
    for x in field_dict[i]: #reaching every element in tuples
        q=0 
        p=[x.upper()+r":(.*) ",x.upper()+r" : (.*) ",x.upper()+r": (.*)",x.upper()+r": (.*)"]
        for j in p:
            #print(j)
            match = re.search(j, text)
            if match:
                result = match.group(1)
                q=1
                values_dict[i]=result
                break
            else:
                result = ""
        if q==1:
            break
return values_dict

values_dict=preprocess_formtext(Fieldnames_dict,text)

formdata_df=pd.DataFrame([values_dict])                                                        
$\endgroup$
  • $\begingroup$ I think better OCR accuracy can be achieved if the scanning itself could be repeated. Put the paper 100% horizontally on the scanner. Choose the right software settings, etc. If this is infeasible, or impossible, then so be it. In any case I can't help you with this one, sorry, except for putting the -1 back to 0; done. $\endgroup$ – knb Jul 19 '17 at 7:32
  • $\begingroup$ @Shyama, what is the dpi of the scanner you are using? $\endgroup$ – Ted Taylor of Life Sep 17 '17 at 8:13
  • $\begingroup$ 96 dpi @TedTaylorofLife $\endgroup$ – Shyama Sep 19 '17 at 5:47
  • $\begingroup$ @Shyama Unfortunately, this is a hardware issue. Taking a picture is not going to work from a cell phone unless phone has ridiculous sensor and you have amazing software. I would recommend that you get hardware with dpi as high as possible, min of 300. $\endgroup$ – Ted Taylor of Life Sep 19 '17 at 9:53
3
$\begingroup$

Build a dictionary of common words that frequently appear in these documents (e.g., MEDICAL, SEX, AGE, etc). Then, for each word in the output from OCR, check whether it is similar to a word in your dictionary; if so, then replace it with the dictionary word. "Similar" might be defined as "edit distance <= 1".

For example, your sample output has the word "sax". If your dictionary contains the word "SEX", "sax" would be detected as a misspelling of "SEX" (edit distance = 1).

This post-processing will help you detect some of the common words and help you identify the fields. What it won't do is clean up errors in the OCR of codes, as those can be anything. There's probably no good way to handle that.

Also, do a Google search on how to use Tesseract. There are some best practices that seem to improve its output (e.g., convert to greyscale TIFF format, deskew text, binarize, and more).

Finally, some commercial OCR software is significantly better than Tesseract or any other free OCR. e.g., Abbyy seems to be well-regarded.

| improve this answer | |
$\endgroup$
  • $\begingroup$ Thats informative!! Thanks@D.W .Can you give me an example of the above said in code. $\endgroup$ – Shyama Jul 20 '17 at 9:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.