How to improve OCR (Scanning) results?" Below 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 &amp; 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])
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 &lt;= 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.