Advertisement
sajid006

combined3

Aug 12th, 2021
1,108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.33 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. def process_image(image):
  43.     ''' Scales, crops, and normalizes a PIL image for a PyTorch model
  44.    
  45.    '''
  46.    
  47.     img = Image.open(image)
  48.     transformation = transforms.Compose([transforms.Resize([64,64]),
  49.                                       #transforms.Grayscale(num_output_channels=1),
  50.                                       transforms.ToTensor(),
  51.                                       transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
  52.                                       ])
  53.     return transformation(img)
  54.  
  55.  
  56.  
  57. def deskew(image):
  58.    
  59.  
  60.     #threshold to get rid of extraneous noise
  61.     thresh = threshold_otsu(image)
  62.     normalize = image > thresh
  63.  
  64.     # gaussian blur
  65.     blur = gaussian(normalize, 3)
  66.  
  67.     # canny edges in scikit-image
  68.     edges = canny(blur)
  69.  
  70.     # hough lines
  71.     hough_lines = probabilistic_hough_line(edges)
  72.  
  73.     # hough lines returns a list of points, in the form ((x1, y1), (x2, y2))
  74.     # representing line segments. the first step is to calculate the slopes of
  75.     # these lines from their paired point values
  76.     slopes = [(y2 - y1)/(x2 - x1) if (x2-x1) else 0 for (x1,y1), (x2, y2) in hough_lines]
  77.  
  78.     # it just so happens that this slope is also y where y = tan(theta), the angle
  79.     # in a circle by which the line is offset
  80.     rad_angles = [np.arctan(x) for x in slopes]
  81.  
  82.     # and we change to degrees for the rotation
  83.     deg_angles = [np.degrees(x) for x in rad_angles]
  84.  
  85.     # which of these degree values is most common?
  86.     histo = np.histogram(deg_angles, bins=180)
  87.    
  88.     # correcting for 'sideways' alignments
  89.     rotation_number = histo[1][np.argmax(histo[0])]
  90.  
  91.     if rotation_number > 45:
  92.         rotation_number = -(90-rotation_number)
  93.     elif rotation_number < -45:
  94.         rotation_number = 90 - abs(rotation_number)
  95.  
  96.     return rotation_number
  97.  
  98.  
  99.  
  100. def deskew2(img,angle):
  101.    
  102.     #load in grayscale:
  103.    
  104.    
  105.     #invert the colors of our image:
  106.     cv2.imshow('input',img)
  107.     cv2.bitwise_not(img, img)
  108.    
  109.     #compute the minimum bounding box:
  110.     non_zero_pixels = cv2.findNonZero(img)
  111.     center, wh, theta = cv2.minAreaRect(non_zero_pixels)
  112.    
  113.     root_mat = cv2.getRotationMatrix2D(center, angle, 1)
  114.     rows, cols = img.shape
  115.     rotated = cv2.warpAffine(img, root_mat, (cols, rows), flags=cv2.INTER_CUBIC)
  116.  
  117.  
  118.     #Border removing:
  119.     sizex = np.int0(wh[0])+10
  120.     sizey = np.int0(wh[1])+10
  121.     print(theta)
  122.     if theta > -45 :
  123.         temp = sizex
  124.         sizex= sizey
  125.         sizey= temp
  126.     return cv2.getRectSubPix(rotated, (sizey,sizex), center)
  127.  
  128. #def breakword(img):
  129. def detectlines(img):
  130.     kernel = np.zeros((5,5),np.uint8)
  131.     img = cv2.erode(img,kernel,iterations = 1)
  132.     cv2.imshow('thinned',img)
  133.     cv2.imwrite("noyse.jpg",img)
  134.     thresh = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
  135.  
  136.     # use morphology erode to blur horizontally
  137.     ho,wo=img.shape
  138.     mark=0;
  139.     b=[]
  140.     for i in range(0,ho):
  141.         cnt=0
  142.         for j in range(0,wo):
  143.             #print(img[i][j])
  144.             if img[i][j] != 255:
  145.                 cnt=cnt+1;#black
  146.                
  147.         #print(cnt)
  148.         if(cnt<50):
  149.             if mark==0:
  150.                 mark=1
  151.                 b.append(i)
  152.         else:
  153.             if mark==1:
  154.                 mark=0
  155.        
  156.     print(b)
  157.    
  158.     ara = []
  159.     lng=len(b)
  160.     for i in range(0,lng):
  161.         if i==lng-1:
  162.             continue
  163.         c=b[i]
  164.         d=b[i+1]
  165.         #cv2.rectangle(img,(c,0),(d,wo),(0,0,255),2)
  166.         #print(c)
  167.         #print(d)
  168.         #print(wo)
  169.         #print("yo")
  170.         #box = cv2.boundingRect(img)
  171.         #x,y,w,h = box
  172.         cv2.rectangle(img, (0,c), (wo,d), (0, 0, 255), 2)
  173.         cur = img[c:d,0:wo].copy()
  174.         ara.append(cur)
  175.        
  176.        
  177.  
  178.     # write result to disk
  179.     print(len(ara))
  180.     sz=len(ara)
  181.  
  182.    
  183.    
  184.     return ara
  185.  
  186.  
  187. img = cv2.imread(r"D:\Bangla OCR Dataset\Dataset\Dataset\1\1_3.jpg",cv2.IMREAD_GRAYSCALE)
  188. cv2.imshow('input image',img)
  189.  
  190. print(img.shape)
  191. img = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2)
  192. cv2.imshow('binary image',img)
  193.  
  194. angel = deskew(img)
  195. img = deskew2(img,angel)
  196. cv2.bitwise_not(img,img)
  197. cv2.imshow("Skew Corrected",img)
  198.  
  199. img = cv2.fastNlMeansDenoising(img, img, 50.0, 7, 21)
  200. cv2.imshow('noiseless image1',img)
  201. #img = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
  202. #cv2.imshow('noiseless image2',img)
  203.  
  204. # threshold the grayscale image
  205. ara=detectlines(img)
  206. sz=len(ara)
  207. for i in range(0,sz):
  208.     #print(ara[i].shape)
  209.     cv2.imshow('Crop %d' % (i,), ara[i])
  210. cv2.waitKey(0)
  211.  
  212.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement