Advertisement
sajid006

Combined2

Jul 15th, 2021
859
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.98 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Mon Jan 18 19:17:12 2021
  4.  
  5. @author: Sajid
  6. """
  7.  
  8. # -*- coding: utf-8 -*-
  9. """
  10. Created on Mon Jan 18 18:33:48 2021
  11.  
  12. @author: Sajid
  13.  
  14. """
  15. from skimage.io import imread, imshow
  16. from skimage.filters import gaussian, threshold_otsu
  17. from skimage.feature import canny
  18. from skimage.transform import probabilistic_hough_line, rotate
  19.  
  20. #testing
  21. import numpy as np
  22. import os
  23. import cv2
  24. import math
  25. import matplotlib.pyplot as plt
  26.  
  27. import torch
  28. from torch import nn
  29. from torch import optim
  30. import torch.nn.functional as F
  31. from torchvision import datasets, transforms, models
  32.  
  33.  
  34.  
  35. from collections import OrderedDict
  36. from PIL import Image
  37.  
  38. import pandas as pd
  39. import seaborn as sns
  40.  
  41.  
  42. # define the CNN architecture
  43. class Net(nn.Module):
  44.     ### TODO: choose an architecture, and complete the class
  45.    
  46.     def __init__(self):
  47.         super(Net, self).__init__()
  48.        
  49.         # convolutional layer (sees 64x64x3 image tensor)
  50.         self.conv1 = nn.Conv2d(3, 16, 3, padding=1)
  51.         # convolutional layer (sees 32x32x16 tensor)
  52.         self.conv2 = nn.Conv2d(16, 32, 3, padding=1)
  53.         # convolutional layer (sees 16x16x32 tensor)
  54.         self.conv3 = nn.Conv2d(32, 64, 3, padding=1)
  55.         # convolutional layer (sees 8x8x64 tensor)
  56.         self.conv4 = nn.Conv2d(64, 128, 3, padding=1)
  57.         self.conv5 = nn.Conv2d(128, 256, 3, padding=1)
  58.  
  59.         # max pooling layer
  60.         self.pool = nn.MaxPool2d(2, 2)
  61.         # linear layer (256 * 2 * 2 -> 512)
  62.         self.fc1 = nn.Linear(256 * 2 * 2 , 2048)
  63.         # linear layer (512 -> 50)
  64.         self.fc2 = nn.Linear(2048,512)
  65.         # dropout layer (p=0.2)
  66.         self.dropout = nn.Dropout(0.2)
  67.         self.fc3 = nn.Linear(512,50)
  68.         #self.softmax = nn.Softmax(dim=1)
  69.        
  70.      
  71.              
  72.     def forward(self, x):
  73.         # add sequence of convolutional and max pooling layers
  74.         x = self.pool(F.relu(self.conv1(x)))
  75.         x = self.pool(F.relu(self.conv2(x)))
  76.         x = self.pool(F.relu(self.conv3(x)))
  77.         x = self.pool(F.relu(self.conv4(x)))
  78.         x = self.pool(F.relu(self.conv5(x)))
  79.         # flatten image input
  80.         x = x.view(-1, 256*2*2)
  81.  
  82.         # add dropout layer
  83.         x = self.dropout(x)
  84.         # add 1st hidden layer, with relu activation function
  85.         x = F.relu(self.fc1(x))
  86.         # add dropout layer
  87.         x = self.dropout(x)
  88.         # add 2nd hidden layer, with relu activation function
  89.         x = self.fc2(x)
  90.         x = self.dropout(x)
  91.         x = self.fc3(x)
  92.         return x
  93. train_on_gpu = torch.cuda.is_available()
  94. '''
  95. if not train_on_gpu:
  96.    print('CUDA is not available.  Training on CPU ...')
  97. else:
  98.    print('CUDA is available!  Training on GPU ...')
  99. '''
  100.  
  101. classes=['অ','আ','ই', 'ঈ', 'উ','ঊ','ঋ','এ','ঐ', 'ও' ,   'ঔ','ক', 'খ',   'গ',    'ঘ',    'ঙ',    'চ',    'ছ',    'জ',    'ঝ',    'ঞ',    'ট',
  102.     'ঠ',    'ড',    'ঢ',    'ণ',    'ত',    'থ',    'দ',    'ধ',    'ন',        'প',    'ফ',    'ব',    'ভ',    'ম',    'য',    'র',        'ল',                'শ' ,   'ষ',    'স' ,   'হ' ,
  103.     'ড়','ঢ়','য়','ৎ','৹', ':', '৺']
  104.  
  105. model_scratch = Net()
  106. model_scratch.load_state_dict(torch.load('model_scratch.pt' , map_location=torch.device('cpu')))
  107.  
  108.  
  109. def process_image(image):
  110.     ''' Scales, crops, and normalizes a PIL image for a PyTorch model
  111.    
  112.    '''
  113.    
  114.     img = Image.open(image)
  115.     transformation = transforms.Compose([transforms.Resize([64,64]),
  116.                                       #transforms.Grayscale(num_output_channels=1),
  117.                                       transforms.ToTensor(),
  118.                                       transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
  119.                                       ])
  120.     return transformation(img)
  121.  
  122. def predict(image_path, model):
  123.     ''' Predict the class (or classes) of an image using a trained deep learning model.
  124.    '''
  125.     model.to('cpu')
  126.     image = process_image(image_path)
  127.     image = image.unsqueeze_(0)
  128.    
  129.     model.eval()
  130.     with torch.no_grad():
  131.         output = model.forward(image)
  132.        
  133.     #probabilities = torch.exp(output)
  134.    
  135.     #topk_probabilities, topk_labels = probabilities.topk(topk)
  136.     #return([topk_probabilities, topk_labels]
  137.     # convert output probabilities to predicted class
  138.     _, preds_tensor = torch.max(output, 1)
  139.     preds = np.squeeze(preds_tensor.numpy()) if not train_on_gpu else np.squeeze(preds_tensor.cpu().numpy())
  140.     return(classes[preds])
  141.  
  142. def deskew(image):
  143.    
  144.  
  145.     #threshold to get rid of extraneous noise
  146.     thresh = threshold_otsu(image)
  147.     normalize = image > thresh
  148.  
  149.     # gaussian blur
  150.     blur = gaussian(normalize, 3)
  151.  
  152.     # canny edges in scikit-image
  153.     edges = canny(blur)
  154.  
  155.     # hough lines
  156.     hough_lines = probabilistic_hough_line(edges)
  157.  
  158.     # hough lines returns a list of points, in the form ((x1, y1), (x2, y2))
  159.     # representing line segments. the first step is to calculate the slopes of
  160.     # these lines from their paired point values
  161.     slopes = [(y2 - y1)/(x2 - x1) if (x2-x1) else 0 for (x1,y1), (x2, y2) in hough_lines]
  162.  
  163.     # it just so happens that this slope is also y where y = tan(theta), the angle
  164.     # in a circle by which the line is offset
  165.     rad_angles = [np.arctan(x) for x in slopes]
  166.  
  167.     # and we change to degrees for the rotation
  168.     deg_angles = [np.degrees(x) for x in rad_angles]
  169.  
  170.     # which of these degree values is most common?
  171.     histo = np.histogram(deg_angles, bins=180)
  172.    
  173.     # correcting for 'sideways' alignments
  174.     rotation_number = histo[1][np.argmax(histo[0])]
  175.  
  176.     if rotation_number > 45:
  177.         rotation_number = -(90-rotation_number)
  178.     elif rotation_number < -45:
  179.         rotation_number = 90 - abs(rotation_number)
  180.  
  181.     return rotation_number
  182.  
  183.  
  184.  
  185. def deskew2(img,angle):
  186.    
  187.     #load in grayscale:
  188.    
  189.    
  190.     #invert the colors of our image:
  191.     cv2.imshow('input',img)
  192.     cv2.bitwise_not(img, img)
  193.    
  194.     #compute the minimum bounding box:
  195.     non_zero_pixels = cv2.findNonZero(img)
  196.     center, wh, theta = cv2.minAreaRect(non_zero_pixels)
  197.    
  198.     root_mat = cv2.getRotationMatrix2D(center, angle, 1)
  199.     rows, cols = img.shape
  200.     rotated = cv2.warpAffine(img, root_mat, (cols, rows), flags=cv2.INTER_CUBIC)
  201.  
  202.  
  203.     #Border removing:
  204.     sizex = np.int0(wh[0])+10
  205.     sizey = np.int0(wh[1])+10
  206.     print(theta)
  207.     if theta > -45 :
  208.         temp = sizex
  209.         sizex= sizey
  210.         sizey= temp
  211.     return cv2.getRectSubPix(rotated, (sizey,sizex), center)
  212.  
  213. #def breakword(img):
  214. def detectlines(img):
  215.     kernel = np.zeros((5,5),np.uint8)
  216.     img = cv2.erode(img,kernel,iterations = 1)
  217.     cv2.imshow('thinned',img)
  218.     cv2.imwrite("noyse.jpg",img)
  219.     thresh = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
  220.  
  221.     # use morphology erode to blur horizontally
  222.     ho,wo=img.shape
  223.     ho=math.floor(ho/5)
  224.     wo=math.floor(ho/10)
  225.    
  226.     kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (ho,wo))
  227.     morph = cv2.morphologyEx(thresh, cv2.MORPH_DILATE, kernel)
  228.  
  229.     # use morphology open to remove thin lines from dotted lines
  230.     #kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 17))
  231.     #morph = cv2.morphologyEx(morph, cv2.MORPH_OPEN, kernel)
  232.  
  233.     # find contours
  234.     cntrs = cv2.findContours(morph, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  235.     cntrs = cntrs[0] if len(cntrs) == 2 else cntrs[1]
  236.  
  237.     print('yo')
  238.     print(len(cntrs))
  239.     # Draw contours excluding the topmost box
  240.     result = img.copy()
  241.     ara = []
  242.     lng=len(cntrs)
  243.     for i in range(0,lng):
  244.         c=cntrs[i]
  245.         box = cv2.boundingRect(c)
  246.         x,y,w,h = box
  247.         cv2.rectangle(result, (x, y), (x+w, y+h), (0, 0, 255), 2)
  248.         cur = img[y:y+h,x:x+w].copy()
  249.         ara.append(cur)
  250.         '''
  251.        if len(cntrs)==1:
  252.            ara.append(cur)
  253.        else:
  254.            ara1 = detectlines(cur)
  255.            for i in ara1:
  256.                ara.append(i)
  257.        '''
  258.        
  259.  
  260.     # write result to disk
  261.     print(len(ara))
  262.     sz=len(ara)
  263.  
  264.    
  265.     cv2.imwrite("text_above_lines_threshold.png", thresh)
  266.     cv2.imwrite("text_above_lines_morph.png", morph)
  267.     cv2.imwrite("text_above_lines_lines.jpg", result)
  268.  
  269.     #cv2.imshow("GRAY", gray)
  270.     cv2.imshow("THRESH", thresh)
  271.     cv2.imshow("MORPH", morph)
  272.     cv2.imshow("RESULT", result)
  273.     return ara
  274.  
  275.  
  276. img = cv2.imread(r"D:\Bangla OCR Dataset\Dataset\Dataset\1\1_3.jpg",cv2.IMREAD_GRAYSCALE)
  277. cv2.imshow('input image',img)
  278.  
  279. print(img.shape)
  280. img = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2)
  281. cv2.imshow('binary image',img)
  282.  
  283. angel = deskew(img)
  284. img = deskew2(img,angel)
  285. cv2.bitwise_not(img,img)
  286. cv2.imshow("Skew Corrected",img)
  287.  
  288. img = cv2.fastNlMeansDenoising(img, img, 50.0, 7, 21)
  289. cv2.imshow('noiseless image1',img)
  290. #img = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
  291. #cv2.imshow('noiseless image2',img)
  292.  
  293. # threshold the grayscale image
  294. ara=detectlines(img)
  295. sz=len(ara)
  296. print('yoyo')
  297. print(sz)
  298. sliced = []
  299. for i in range(0,sz):
  300.     cv2.imshow('Crop %d' % (i,), ara[sz-i-1])
  301.     cv2.imwrite("Crp%d.png" % (i,), ara[sz-i-1])
  302.     img2=ara[sz-i-1]
  303.     thresh2 = cv2.threshold(img2, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
  304.     print(img2.shape)
  305.     he,we=img2.shape
  306.     he =math.ceil(he/5.0)
  307.     we = math.floor(we/10.0)
  308.     print("ksdljjjjj")
  309.     print(ara[sz-i-1].shape)
  310.     # use morphology erode to blur horizontally
  311.     kernel2 = cv2.getStructuringElement(cv2.MORPH_RECT, (he,we))
  312.     morph2 = cv2.morphologyEx(thresh2, cv2.MORPH_DILATE, kernel2)
  313.  
  314.     # use morphology open to remove thin lines from dotted lines
  315.     #kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 17))
  316.     #morph = cv2.morphologyEx(morph, cv2.MORPH_OPEN, kernel)
  317.  
  318.     # find contours
  319.     cntrs2 = cv2.findContours(morph2, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  320.     cntrs2 = cntrs2[0] if len(cntrs2) == 2 else cntrs2[1]
  321.  
  322.  
  323.     # Draw contours excluding the topmost box
  324.  
  325.     result2 = img2.copy()
  326.     temp=[]
  327.     for c2 in cntrs2:
  328.         box2 = cv2.boundingRect(c2)
  329.         x,y,w,h = box2
  330.         cv2.rectangle(result2, (x, y), (x+w, y+h), (0, 0, 255), 2)
  331.         temp.append(img2[y:y+h,x:x+w].copy())
  332.     cv2.imwrite("temporary%d.png" % (i), result2)
  333.     sliced.append(temp)
  334.     sz2=len(temp)
  335.     print(sz2)
  336. for i in range(0,sz):
  337.     sz2=len(sliced[sz-i-1])
  338.     for j in range(0,sz2):
  339.         sliced[sz-i-1][sz2-j-1]=cv2.inRange(sliced[sz-i-1][sz2-j-1],221,255)
  340.         sliced[sz-i-1][sz2-j-1]=cv2.merge([sliced[sz-i-1][sz2-j-1],sliced[sz-i-1][sz2-j-1],sliced[sz-i-1][sz2-j-1]])
  341.         cv2.imshow('sliced6%d%d' % (sz-i-1,j), sliced[sz-i-1][sz2-j-1])
  342.         cv2.imwrite("sli9%d%d.png" % (sz-i-1,j), sliced[sz-i-1][sz2-j-1])
  343.         print('yeah')
  344.         print(sliced[sz-i-1][sz2-j-1].shape)
  345.         #breakword(sliced[sz-i-1][sz2-j-1])
  346.    
  347. #print(predict('C:/Users/Sajid/bcc000010.bmp',model_scratch))
  348. cv2.waitKey(0)
  349.  
  350.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement