<jupyter_start><jupyter_text><jupyter_code>!apt install tesseract-ocr
!apt install libtesseract-dev
!pip install pytesseract
#!pip install colab-env -qU


try:
 from PIL import Image
except ImportError:
 import Image
import cv2
import pytesseract



#/usr/share/tesseract-ocr/4.00/tessdata
#!cp -av '/usr/share/tesseract-ocr/4.00/tessdata' '/content/drive/MyDrive/data/ocr/tessdata'
import pytesseract
tessdata_dir_config = r'--tessdata-dir "/content/drive/MyDrive/data/ocr/tessdata/"'
print(pytesseract.get_languages(config=tessdata_dir_config))
print(pytesseract.get_languages())
im=Image.open('/content/drive/MyDrive/data/ocr/NirnaySagar_page-84_000003.jpg')
tessdata_dir_config = r'--tessdata-dir "/content/drive/MyDrive/data/ocr/tessdata/"'
print(pytesseract.image_to_string(im, lang='san', config='--oem 1 --psm 7 --tessdata-dir "/content/drive/MyDrive/data/ocr/tessdata/"'))
im

from difflib import SequenceMatcher
def similar(a, b):
    return SequenceMatcher(None, a, b).ratio()

similar('स्ततो','स्तितो')


!pip install colab-env -qU
import colab_env
!echo $GOOGLE_APPLICATION_CREDENTIALS
#calculating CER
import sys

def levenshtein(u, v):
    prev = None
    curr = [0] + [*range(1, len(v) + 1)]
    # Operations: (SUB, DEL, INS)
    prev_ops = None
    curr_ops = [(0, 0, i) for i in range(len(v) + 1)]
    for x in range(1, len(u) + 1):
        prev, curr = curr, [x] + ([None] * len(v))
        prev_ops, curr_ops = curr_ops, [(0, x, 0)] + ([None] * len(v))
        for y in range(1, len(v) + 1):
            delcost = prev[y] + 1
            addcost = curr[y - 1] + 1
            subcost = prev[y - 1] + int(u[x - 1] != v[y - 1])
            curr[y] = min(subcost, delcost, addcost)
            if curr[y] == subcost:
                (n_s, n_d, n_i) = prev_ops[y - 1]
                curr_ops[y] = (n_s + int(u[x - 1] != v[y - 1]), n_d, n_i)
            elif curr[y] == delcost:
                (n_s, n_d, n_i) = prev_ops[y]
                curr_ops[y] = (n_s, n_d + 1, n_i)
            else:
                (n_s, n_d, n_i) = curr_ops[y - 1]
                curr_ops[y] = (n_s, n_d, n_i + 1)
    return curr[len(v)], curr_ops[len(v)]


def calculateCER(ref,hyp):
  wer_s, wer_i, wer_d, wer_n = 0, 0, 0, 0
  cer_s, cer_i, cer_d, cer_n = 0, 0, 0, 0
  sen_err = 0
  for n in range(len(ref)):
    # update CER statistics
    _, (s, i, d) = levenshtein(ref[n], hyp[n])
    cer_s += s
    cer_i += i
    cer_d += d
    cer_n += len(ref[n])
     
    # update WER statistics
    '''_, (s, i, d) = levenshtein(ref[n].split(), hyp[n].split())
    wer_s += s
    wer_i += i
    wer_d += d
    wer_n += len(ref[n].split())'''
    
    # update SER statistics
    if s + i + d > 0:
      sen_err += 1

  if cer_n > 0:
    print("CER S:"+str(cer_s)+" CER I:"+str(cer_i)+" CER D:"+str(cer_d)+" CER_N:"+str(cer_n))
    return (100.0 * (cer_s + cer_i + cer_d)) / cer_n
  else:
    return 0

print(calculateCER(['तोत'],['तोस्']))
[(0, 0, i) for i in range(10)]
import csv
import os
try:
 from PIL import Image
except ImportError:
 import Image
import cv2
import pytesseract
from difflib import SequenceMatcher



csvreader=csv.reader(open('/content/drive/MyDrive/data/ocr/Train.txt'),delimiter='\t')
filenames=[]
sanword=[]
groundtruth=""
predictedwords=""
groundfile=open('/content/drive/MyDrive/data/ocr/groundtruth.txt','w')
predictedfile=open('/content/drive/MyDrive/data/ocr/predictedcontent.txt','w')

for row in csvreader:
  filenames.append(row[0].replace('images/',''))
  sanword.append(row[1])

print(filenames)

for imgfile in os.listdir('/content/drive/MyDrive/data/ocr/images_subset/'):
  
  if(imgfile in filenames):
    itemnum=filenames.index(imgfile)
    im=Image.open('/content/drive/MyDrive/data/ocr/images_subset/'+imgfile)
    ocredword=pytesseract.image_to_string(im, lang='san', config='--oem 1 --psm 7 --tessdata-dir "/content/drive/MyDrive/data/ocr/tessdata/"')
    actualword=sanword[itemnum]
    groundtruth+=actualword
    predictedwords+=ocredword.strip()
    wordration=SequenceMatcher(None, ocredword, actualword).ratio()
    print('file name: '+imgfile)
    print('Actual word:'+actualword)
    print('tesseract detected word:'+ocredword.strip())
    print('word ratio:'+ str(wordration))
    print('CER: '+str(calculateCER([actualword],[ocredword])))
    print('---------------------------------------------------')
    
   # input('press any key to continue')
    im.show()
    
  
groundfile.write(groundtruth)
predictedfile.write(predictedwords)
groundfile.close()
predictedfile.close()


    


#!pip install --upgrade pip

#!pip install --upgrade google-api-python-client

# Running Vision API
import base64
IMAGE="gs://cloud-training-demos/vision/sign2.jpg"
vservice = build('vision', 'v1', developerKey=APIKEY)
request = vservice.images().annotate(body={
        'requests': [{
                'image': {
                    'source': {
                        'gcs_image_uri': IMAGE
                    }
                },
                'features': [{
                    'type': 'TEXT_DETECTION',
                    'maxResults': 3,
                }]
            }],
        })
responses = request.execute(num_retries=3)
print(responses['responses'][0]['textAnnotations'][0]['description'])

#all vision related code
!pip install --upgrade google-api-python-client
!pip install --upgrade google-cloud-vision

!export GOOGLE_APPLICATION_CREDENTIALS="/content/drive/MyDrive/data/ocr/sanskritocr-302909-00f90da33203.json"


#import getpass

#APIKEY= getpass.getpass('00f90da332035e5b5e315811c7da4358d63b6604')


def detect_text(path):
    """Detects text in the file."""
    from google.cloud import vision
    import io
    client = vision.ImageAnnotatorClient()

    with io.open(path, 'rb') as image_file:
        content = image_file.read()

    image = vision.Image(content=content)

    response = client.text_detection(image=image)
    texts = response.text_annotations
    print('Texts:')

    for text in texts:
        print('\n"{}"'.format(text.description))

        vertices = (['({},{})'.format(vertex.x, vertex.y)
                    for vertex in text.bounding_poly.vertices])

        print('bounds: {}'.format(','.join(vertices)))

    if response.error.message:
        raise Exception(
            '{}\nFor more info on error messages, check: '
            'https://cloud.google.com/apis/design/errors'.format(
                response.error.message))

print('Google Vision API detected word:')
    detect_text('/content/drive/MyDrive/data/ocr/images_subset/'+imgfile)
    
#pdf to ocrd pdf
#install required files
!pip install pdf2image
#pdf to ocrd pdf
from pd2image import<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16194331983805668, 'fraction_numerical': 0.01814364972259709, 'mean_word_length': 2.977340488968396, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '534588', 'license': 'MIT', 'n_tokens_mistral': 2558, 'n_tokens_neox': 2416, 'n_words': 491, 'path': 'tesseract_ocr.ipynb', 'repo_name': 'venkatapathy/ocrcodes', 'size': '6669'}