Advertisement
sajid006

Finalhalf

Oct 9th, 2021
821
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.63 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Tue Aug 17 12:40:23 2021
  4.  
  5. @author: Sajid
  6. """
  7. from skimage.io import imread, imshow
  8. from skimage.filters import gaussian, threshold_otsu
  9. from skimage.feature import canny
  10. from skimage.transform import probabilistic_hough_line, rotate
  11.  
  12. #testing
  13. import numpy as np
  14. import os
  15. import cv2
  16. import math
  17. import matplotlib.pyplot as plt
  18.  
  19. import torch
  20. from torch import nn
  21. from torch import optim
  22. import torch.nn.functional as F
  23. from torchvision import datasets, transforms, models
  24.  
  25.  
  26.  
  27. from collections import OrderedDict
  28. from PIL import Image
  29.  
  30. import pandas as pd
  31. import seaborn as sns
  32.  
  33. import math
  34. import cv2
  35. import numpy as np
  36.  
  37. def process_image(image):
  38.     ''' Scales, crops, and normalizes a PIL image for a PyTorch model
  39.    
  40.    '''
  41.    
  42.     img = Image.open(image)
  43.     transformation = transforms.Compose([transforms.Resize([64,64]),
  44.                                       #transforms.Grayscale(num_output_channels=1),
  45.                                       transforms.ToTensor(),
  46.                                       transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
  47.                                       ])
  48.     return transformation(img)
  49.  
  50.  
  51.  
  52. def deskew(image):
  53.    
  54.  
  55.     #threshold to get rid of extraneous noise
  56.     thresh = threshold_otsu(image)
  57.     normalize = image > thresh
  58.  
  59.     # gaussian blur
  60.     blur = gaussian(normalize, 3)
  61.  
  62.     # canny edges in scikit-image
  63.     edges = canny(blur)
  64.  
  65.     # hough lines
  66.     hough_lines = probabilistic_hough_line(edges)
  67.  
  68.     # hough lines returns a list of points, in the form ((x1, y1), (x2, y2))
  69.     # representing line segments. the first step is to calculate the slopes of
  70.     # these lines from their paired point values
  71.     slopes = [(y2 - y1)/(x2 - x1) if (x2-x1) else 0 for (x1,y1), (x2, y2) in hough_lines]
  72.  
  73.     # it just so happens that this slope is also y where y = tan(theta), the angle
  74.     # in a circle by which the line is offset
  75.     rad_angles = [np.arctan(x) for x in slopes]
  76.  
  77.     # and we change to degrees for the rotation
  78.     deg_angles = [np.degrees(x) for x in rad_angles]
  79.  
  80.     # which of these degree values is most common?
  81.     histo = np.histogram(deg_angles, bins=180)
  82.    
  83.     # correcting for 'sideways' alignments
  84.     rotation_number = histo[1][np.argmax(histo[0])]
  85.  
  86.     if rotation_number > 45:
  87.         rotation_number = -(90-rotation_number)
  88.     elif rotation_number < -45:
  89.         rotation_number = 90 - abs(rotation_number)
  90.  
  91.     return rotation_number
  92.  
  93.  
  94.  
  95. def deskew2(img,angle):
  96.    
  97.     #load in grayscale:
  98.    
  99.    
  100.     #invert the colors of our image:
  101.     cv2.imshow('input',img)
  102.     cv2.bitwise_not(img, img)
  103.    
  104.     #compute the minimum bounding box:
  105.     non_zero_pixels = cv2.findNonZero(img)
  106.     center, wh, theta = cv2.minAreaRect(non_zero_pixels)
  107.    
  108.     root_mat = cv2.getRotationMatrix2D(center, angle, 1)
  109.     rows, cols = img.shape
  110.     rotated = cv2.warpAffine(img, root_mat, (cols, rows), flags=cv2.INTER_CUBIC)
  111.  
  112.  
  113.     #Border removing:
  114.     sizex = np.int0(wh[0])+10
  115.     sizey = np.int0(wh[1])+10
  116.     print(theta)
  117.     if theta > -45 :
  118.         temp = sizex
  119.         sizex= sizey
  120.         sizey= temp
  121.     return cv2.getRectSubPix(rotated, (sizey,sizex), center)
  122.  
  123. def sortit(allImages):
  124.     sz1=len(allImages)
  125.     leaders=[]
  126.     ans=[]
  127.     tmp=[]
  128.     rev=[-1]*10000
  129.     for i in range(0,sz1):
  130.         a,currImage=allImages[i];
  131.         (x,y,w,h)=a
  132.         ymin=y
  133.         ymax=y+h
  134.         sz2=len(leaders)
  135.         got=0
  136.         for j in range(0,sz2):
  137.             b,leader=leaders[j]
  138.             (xl,yl,wl,hl)=b
  139.             ylmin=yl
  140.             ylmax=yl+hl
  141.            
  142.             if (ymin<=ylmin and ymax>=ylmin) or (ymin<=ylmax and ymax>=ylmax):
  143.                 ans[rev[j]].append(allImages[i])
  144.                 got=1
  145.                 break
  146.         if got==0:
  147.             tmp=[]
  148.             tmp.append(allImages[i])
  149.             ans.append(tmp)
  150.             leaders.append(allImages[i])
  151.             rev[len(leaders)-1]=len(ans)-1
  152.  
  153.     return ans
  154.    
  155.  
  156. def wordSegment(img, kernelSize=25, sigma=11, theta=7, minArea=0):
  157.    
  158.     kernel = createKernel(kernelSize, sigma, theta)
  159.     imgFiltered = cv2.filter2D(img, -1, kernel, borderType=cv2.BORDER_REPLICATE).astype(np.uint8)
  160.     (_, imgThres) = cv2.threshold(imgFiltered, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
  161.     imgThres = 255 - imgThres
  162.  
  163.  
  164.     if cv2.__version__.startswith('3.'):
  165.         (_, components, _) = cv2.findContours(imgThres, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  166.     else:
  167.         (components, _) = cv2.findContours(imgThres, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
  168.  
  169.     res = []
  170.     it=0
  171.     for c in components:
  172.         if cv2.contourArea(c) < minArea:
  173.             continue
  174.        
  175.         currBox = cv2.boundingRect(c) # returns (x, y, w, h)
  176.         print(currBox)
  177.         (x, y, w, h) = currBox
  178.         currImg = img[y:y+h, x:x+w]
  179.         res.append((currBox, currImg))
  180.         it=it+1
  181.         #cv2.imshow('Croping %d' %(it),currImg)
  182.        
  183.  
  184.     return sortit(res)
  185.  
  186.    
  187.  
  188.  
  189.  
  190. def createKernel(kernelSize, sigma, theta):
  191.     assert kernelSize % 2
  192.     halfSize = kernelSize // 2
  193.    
  194.     kernel = np.zeros([kernelSize, kernelSize])
  195.     sigmaX = sigma
  196.     sigmaY = sigma * theta
  197.    
  198.     for i in range(kernelSize):
  199.         for j in range(kernelSize):
  200.             x = i - halfSize
  201.             y = j - halfSize
  202.            
  203.             expTerm = np.exp(-x**2 / (2 * sigmaX) - y**2 / (2 * sigmaY))
  204.             xTerm = (x**2 - sigmaX**2) / (2 * math.pi * sigmaX**5 * sigmaY)
  205.             yTerm = (y**2 - sigmaY**2) / (2 * math.pi * sigmaY**5 * sigmaX)
  206.            
  207.             kernel[i, j] = (xTerm + yTerm) * expTerm
  208.  
  209.     kernel = kernel / np.sum(kernel)
  210.     return kernel
  211.  
  212. img = cv2.imread(r"F:\Thesis Files\Bangla OCR Dataset\Dataset\Dataset\1\1_3.jpg",cv2.IMREAD_GRAYSCALE)
  213. img = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2)
  214. cv2.imshow('binary image',img)
  215.  
  216. angel = deskew(img)
  217. img = deskew2(img,angel)
  218. cv2.bitwise_not(img,img)
  219. cv2.imshow("Skew Corrected",img)
  220.  
  221. img = cv2.fastNlMeansDenoising(img, img, 50.0, 7, 21)
  222. ho,wo=img.shape
  223. area=ho*wo
  224. ara=wordSegment(img,25,11,7,area/5000)
  225. ara.reverse()
  226. cv2.imshow('input image',img)
  227. sz=len(ara)
  228. for i in range(0,sz):
  229.     ara[i]=sorted(ara[i], key=lambda entry:entry[0][0])
  230. for i in range(0,sz):
  231.     #print(ara[i].shape)
  232.     tmp=ara[i]
  233.     sz2=len(tmp)
  234.     for j in range(0,sz2):
  235.         a,b=tmp[j]
  236.         cv2.imshow('Crop %d%d' % (i,j), b)
  237. '''
  238. for i in range(0,sz):
  239.    a,b=ara[i]
  240.    cv2.imshow('Crop %d' %(i,),b)
  241.    
  242. '''
  243.  
  244.  
  245. cv2.waitKey(0)
  246.  
  247.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement