Advertisement
sajid006

Combined

May 7th, 2021
696
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.11 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.    
  215.  
  216. img = cv2.imread('C:/Users/Sajid/GTB229.bmp',cv2.IMREAD_GRAYSCALE)
  217. cv2.imshow('input image',img)
  218.  
  219. print(img.shape)
  220. img = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2)
  221. cv2.imshow('binary image',img)
  222.  
  223. angel = deskew(img)
  224. img = deskew2(img,angel)
  225. cv2.bitwise_not(img,img)
  226. cv2.imshow("Skew Corrected",img)
  227. kernel = np.zeros((5,5),np.uint8)
  228. img = cv2.fastNlMeansDenoising(img, img, 50.0, 7, 21)
  229. cv2.imshow('noiseless image1',img)
  230. #img = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
  231. #cv2.imshow('noiseless image2',img)
  232. img = cv2.erode(img,kernel,iterations = 1)
  233. cv2.imshow('thinned',img)
  234. cv2.imwrite("noyse.jpg",img)
  235. # threshold the grayscale image
  236. thresh = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
  237.  
  238. # use morphology erode to blur horizontally
  239. ho,wo=img.shape
  240. ho=math.floor(ho/5)
  241. wo=math.floor(ho/10)
  242.  
  243. kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (ho,wo))
  244. morph = cv2.morphologyEx(thresh, cv2.MORPH_DILATE, kernel)
  245.  
  246. # use morphology open to remove thin lines from dotted lines
  247. #kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 17))
  248. #morph = cv2.morphologyEx(morph, cv2.MORPH_OPEN, kernel)
  249.  
  250. # find contours
  251. cntrs = cv2.findContours(morph, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  252. cntrs = cntrs[0] if len(cntrs) == 2 else cntrs[1]
  253.  
  254.  
  255. # Draw contours excluding the topmost box
  256. result = img.copy()
  257. ara = []
  258. for c in cntrs:
  259.     box = cv2.boundingRect(c)
  260.     x,y,w,h = box
  261.     cv2.rectangle(result, (x, y), (x+w, y+h), (0, 0, 255), 2)
  262.     ara.append(img[y:y+h,x:x+w].copy())
  263.  
  264. # write result to disk
  265. print(len(ara))
  266. sz=len(ara)
  267.  
  268.    
  269. cv2.imwrite("text_above_lines_threshold.png", thresh)
  270. cv2.imwrite("text_above_lines_morph.png", morph)
  271. cv2.imwrite("text_above_lines_lines.jpg", result)
  272.  
  273. #cv2.imshow("GRAY", gray)
  274. cv2.imshow("THRESH", thresh)
  275. cv2.imshow("MORPH", morph)
  276. cv2.imshow("RESULT", result)
  277. sliced = []
  278. for i in range(0,sz):
  279.     cv2.imshow('Crop %d' % (i,), ara[sz-i-1])
  280.     cv2.imwrite("Crop%d.png" % (i,), ara[sz-i-1])
  281.     img2=ara[sz-i-1]
  282.     thresh2 = cv2.threshold(img2, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
  283.     print(img2.shape)
  284.     he,we=img2.shape
  285.     he =math.ceil(he/5.0)
  286.     we = math.floor(we/10.0)
  287.  
  288.     # use morphology erode to blur horizontally
  289.     kernel2 = cv2.getStructuringElement(cv2.MORPH_RECT, (he,we))
  290.     morph2 = cv2.morphologyEx(thresh2, cv2.MORPH_DILATE, kernel2)
  291.  
  292.     # use morphology open to remove thin lines from dotted lines
  293.     #kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 17))
  294.     #morph = cv2.morphologyEx(morph, cv2.MORPH_OPEN, kernel)
  295.  
  296.     # find contours
  297.     cntrs2 = cv2.findContours(morph2, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  298.     cntrs2 = cntrs2[0] if len(cntrs2) == 2 else cntrs2[1]
  299.  
  300.  
  301.     # Draw contours excluding the topmost box
  302.  
  303.     result2 = img2.copy()
  304.     temp=[]
  305.     for c2 in cntrs2:
  306.         box2 = cv2.boundingRect(c2)
  307.         x,y,w,h = box2
  308.         cv2.rectangle(result2, (x, y), (x+w, y+h), (0, 0, 255), 2)
  309.         temp.append(img2[y:y+h,x:x+w].copy())
  310.     cv2.imwrite("temporary%d.png" % (i), result2)
  311.     sliced.append(temp)
  312.     sz2=len(temp)
  313.     print(sz2)
  314. for i in range(0,sz):
  315.     sz2=len(sliced[sz-i-1])
  316.     for j in range(0,sz2):
  317.         cv2.imshow('sliced %d%d' % (sz-i-1,j), sliced[sz-i-1][sz2-j-1])
  318.         cv2.imwrite("sliced%d%d.png" % (sz-i-1,j), sliced[sz-i-1][sz2-j-1])
  319.         #breakword(sliced[sz-i-1][sz2-j-1])
  320.    
  321. print(predict('C:/Users/Sajid/bcc000010.bmp',model_scratch))
  322. cv2.waitKey(0)
  323.  
  324.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement