Advertisement
sajid006

breakword

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