sajid006

breakword1

May 7th, 2021
539
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from skimage.io import imread, imshow
  2. from skimage.filters import gaussian, threshold_otsu
  3. from skimage.feature import canny
  4. from skimage.transform import probabilistic_hough_line, rotate
  5.  
  6. #testing
  7. import numpy as np
  8. import os
  9. import cv2
  10. import math
  11. import matplotlib.pyplot as plt
  12. import imghdr
  13.  
  14. import torch
  15. from torch import nn
  16. from torch import optim
  17. import torch.nn.functional as F
  18. from torchvision import datasets, transforms, models
  19.  
  20.  
  21.  
  22. from collections import OrderedDict
  23. from PIL import Image
  24.  
  25. import pandas as pd
  26. import seaborn as sns
  27.  
  28.  
  29. def preprocess(img):
  30.    
  31.    
  32.     img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
  33.     img = cv2.inRange(img,221,255,3)
  34.     img = cv2.merge([img,img,img])
  35.     cv2.imshow("hello",img)
  36.     #cv2.waitKey(0)
  37.     return img
  38. # define the CNN architecture
  39. class Net(nn.Module):
  40.     ### TODO: choose an architecture, and complete the class
  41.    
  42.     def __init__(self):
  43.         super(Net, self).__init__()
  44.        
  45.         # convolutional layer (sees 64x64x3 image tensor)
  46.         self.conv1 = nn.Conv2d(3, 16, 3, padding=1)
  47.         # convolutional layer (sees 32x32x16 tensor)
  48.         self.conv2 = nn.Conv2d(16, 32, 3, padding=1)
  49.         # convolutional layer (sees 16x16x32 tensor)
  50.         self.conv3 = nn.Conv2d(32, 64, 3, padding=1)
  51.         # convolutional layer (sees 8x8x64 tensor)
  52.         self.conv4 = nn.Conv2d(64, 128, 3, padding=1)
  53.         self.conv5 = nn.Conv2d(128, 256, 3, padding=1)
  54.  
  55.         # max pooling layer
  56.         self.pool = nn.MaxPool2d(2, 2)
  57.         # linear layer (256 * 2 * 2 -> 512)
  58.         self.fc1 = nn.Linear(256 * 2 * 2 , 2048)
  59.         # linear layer (512 -> 50)
  60.         self.fc2 = nn.Linear(2048,512)
  61.         # dropout layer (p=0.2)
  62.         self.dropout = nn.Dropout(0.2)
  63.         self.fc3 = nn.Linear(512,50)
  64.         #self.softmax = nn.Softmax(dim=1)
  65.        
  66.      
  67.              
  68.     def forward(self, x):
  69.         # add sequence of convolutional and max pooling layers
  70.         x = self.pool(F.relu(self.conv1(x)))
  71.         x = self.pool(F.relu(self.conv2(x)))
  72.         x = self.pool(F.relu(self.conv3(x)))
  73.         x = self.pool(F.relu(self.conv4(x)))
  74.         x = self.pool(F.relu(self.conv5(x)))
  75.         # flatten image input
  76.         x = x.view(-1, 256*2*2)
  77.  
  78.         # add dropout layer
  79.         x = self.dropout(x)
  80.         # add 1st hidden layer, with relu activation function
  81.         x = F.relu(self.fc1(x))
  82.         # add dropout layer
  83.         x = self.dropout(x)
  84.         # add 2nd hidden layer, with relu activation function
  85.         x = self.fc2(x)
  86.         x = self.dropout(x)
  87.         x = self.fc3(x)
  88.         return x
  89. train_on_gpu = torch.cuda.is_available()
  90. '''
  91. if not train_on_gpu:
  92.    print('CUDA is not available.  Training on CPU ...')
  93. else:
  94.    print('CUDA is available!  Training on GPU ...')
  95. '''
  96.  
  97. classes=['অ','আ','ই', 'ঈ', 'উ','ঊ','ঋ','এ','ঐ', 'ও' ,   'ঔ','ক', 'খ',   'গ',    'ঘ',    'ঙ',    'চ',    'ছ',    'জ',    'ঝ',    'ঞ',    'ট',
  98.     'ঠ',    'ড',    'ঢ',    'ণ',    'ত',    'থ',    'দ',    'ধ',    'ন',        'প',    'ফ',    'ব',    'ভ',    'ম',    'য',    'র',        'ল',                'শ' ,   'ষ',    'স' ,   'হ' ,
  99.     'ড়','ঢ়','য়','ৎ','ং', ':', '৺']
  100.  
  101. model_scratch = Net()
  102. model_scratch.load_state_dict(torch.load('model_scratch.pt' , map_location=torch.device('cpu')))
  103.  
  104.  
  105. def process_image(image):
  106.     ''' Scales, crops, and normalizes a PIL image for a PyTorch model
  107.    
  108.    '''
  109.     img=preprocess(image)
  110.     #img = cv2.fastNlMeansDenoising(img, img, 50.0, 7, 21)
  111.    
  112.     transformation = transforms.Compose([transforms.ToPILImage(),
  113.                                          transforms.Resize([64,64]),
  114.                                       #transforms.Grayscale(num_output_channels=1),
  115.                                       transforms.ToTensor(),
  116.                                       transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
  117.                                       ])
  118.     #print("dklsfdks")
  119.     return transformation(img)
  120.  
  121. def predict(image, model,topk=2):
  122.     ''' Predict the class (or classes) of an image using a trained deep learning model.
  123.    '''
  124.     model.to('cpu')
  125.     #print("hello")
  126.     image = process_image(image)
  127.     image = image.unsqueeze_(0)
  128.     print("world")
  129.     model.eval()
  130.     with torch.no_grad():
  131.         output = model.forward(image)
  132.        
  133.     m = nn.Softmax(dim=1)
  134.     probabilities = m(output)
  135.    
  136.     topk_probabilities, topk_labels = probabilities.topk(topk)
  137.     print([topk_probabilities, topk_labels])
  138.     # convert output probabilities to predicted class
  139.     _, preds_tensor = torch.max(output, 1)
  140.     #print(probabilities)
  141.     #print(preds_tensor)
  142.     preds = np.squeeze(preds_tensor.numpy()) if not train_on_gpu else np.squeeze(preds_tensor.cpu().numpy())
  143.     print('predicted label no: '+str(preds))
  144.     if(topk_probabilities[0][0].numpy()>0.9):
  145.         return(classes[preds])
  146.     else:
  147.         return 0
  148. #img=Image.open(r'F:\Thesis Files\CMATERdb 3.1.2\BasicFinalDatabase\Test\183\bcc000027.bmp')
  149. #print(predict(img,model_scratch))
  150. #img = cv2.imread('C:/Users/Sajid/sliced92.png',cv2.IMREAD_GRAYSCALE)
  151. img=Image.open(r'C:\Users\Sajid\B00991.jpg')
  152. img=np.uint8(img)
  153.  
  154. print(predict(img,model_scratch))
  155.  
  156. #img = np.array(img)
  157. cv2.imshow("input",img)
  158.  
  159.  
  160. #cv2.waitKey(0)
  161.  
  162.  
  163. he,we,dummy=img.shape
  164. print(he)
  165. print(we)
  166. jump=math.ceil(we/10)
  167. right=we
  168. left=we
  169. bottom=0
  170. top=he
  171. letters=[]
  172. cnt=0
  173. last=1
  174. #print("dskldfjks")
  175. #imghdr.what(img)
  176. for i in range(0,105):
  177.     left=left-jump
  178.     if left<0:
  179.         left=0
  180.    
  181.     im1=img[bottom:top,left:right].copy()
  182.     print(im1.shape)
  183.     im1=np.uint8(im1)
  184.     #imghdr.what(im1)
  185.     cv2.imshow('img %d' % i,im1)
  186.     print(i)
  187.     x=predict(im1,model_scratch)
  188.     print(x)
  189.     if x==0:
  190.         if left==0:
  191.             break
  192.         else:
  193.             continue
  194.     if cnt==0:
  195.         cnt=cnt+1
  196.         last=x
  197.     elif cnt==1:
  198.         if last==x:
  199.             cnt=cnt+1
  200.         else:
  201.             last=x
  202.             cnt=1
  203.     else:
  204.         if last==x:
  205.             #letters.insert(0,x)
  206.             letters.append(x)
  207.             cnt=0
  208.             right=left
  209.         else:
  210.             last=x
  211.             cnt=1
  212.     if left==0:
  213.         break
  214.    
  215.  
  216. for i in letters:
  217.     print("yo yo")
  218.     print(i)
  219. cv2.waitKey(0)
RAW Paste Data