Advertisement
Guest User

Untitled

a guest
Mar 21st, 2020
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.64 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3. import torch
  4. import torch.nn as nn
  5. import torch.nn.functional as F
  6. class ConvolutionalNetwork(nn.Module):
  7.     def __init__(self):
  8.         super().__init__()
  9.         #The first layer has one input channel (the grayscalle color channel). We'll use 6 output channels for feature extraction and
  10.         #a kernel size of 3 to make a 3x3 filter. The step size will be 1.
  11.         self.conv1 = nn.Conv2d(1, 6, 3, 1)
  12.         #The second layer will take our 6 input channels and deliver 16 output channels.
  13.         self.conv2 = nn.Conv2d(6, 16, 3, 1)
  14.        
  15.        
  16.         #The input size of (5x5x16) is determined by the effect of our kernels on the input image size.
  17.         #A 3x3 filter applied to a 28x28 image leaves a 1-pixel edge on all four sides.
  18.         #In one layer the size changes from 28x28 to 26x26. We could address this with zero-padding, but since an MNIST image is mostly black
  19.         #at the edges, we should be safe ignoring these pixels. We'll apply the kernel twice, and apply pooling layers twice,
  20.         #so our resulting output will be $\;(((28-2)/2)-2)/2 = 5.5\;$ which rounds down to 5 pixels per side.
  21.         self.fc1 = nn.Linear(5*5*16, 120)
  22.         self.fc2 = nn.Linear(120, 84)
  23.         self.fc3 = nn.Linear(84,10)
  24.  
  25.     def forward(self, X):
  26.         X = F.relu(self.conv1(X))
  27.         X = F.max_pool2d(X, 2, 2)
  28.         X = F.relu(self.conv2(X))
  29.         X = F.max_pool2d(X, 2, 2)
  30.        
  31.         #Flatten the data for the fully connected layers
  32.         X = X.view(-1, 5*5*16)
  33.         X = F.relu(self.fc1(X))
  34.         X = F.relu(self.fc2(X))
  35.         X = self.fc3(X)
  36.         return F.log_softmax(X, dim=1)
  37.  
  38. model2 = ConvolutionalNetwork()
  39. model2.load_state_dict(torch.load('mnist2.pt'));
  40. model2.eval()
  41. cap = cv2.VideoCapture(0)
  42.  
  43.  
  44. width = 640
  45. height = 480
  46.  
  47. while True:
  48.    
  49.     ret, frame = cap.read()
  50.     img = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
  51.    
  52.     img = img/255
  53.     img = cv2.resize(img,(28,28))
  54.     img = img.reshape(1,1,28,28)
  55.     img = torch.from_numpy(img)
  56.     with torch.no_grad():
  57.         # view(1,1,28,28) batch size of one, one channel, 28,28 pixels
  58.         label = model2(img.float()).argmax()
  59.         log_prob =  model2(img.float())
  60.         probs = torch.exp(log_prob)*100
  61.         max_probs = torch.max(probs[0])
  62.         #max_probs = torch.max(probs[0])
  63.         print("Predicted value:",label.item())
  64.         print("Probability:", max_probs.item())
  65.     cv2.putText(frame,str(label) + "   "+str(max_probs),
  66.                     (50,50),cv2.FONT_HERSHEY_COMPLEX,
  67.                     1,(0,0,255),1)
  68.     cv2.imshow('frame',frame)
  69.     print(img.shape)
  70.     if cv2.waitKey(1) & 0xFF == ord('q'):
  71.         break
  72.  
  73. cap.release()
  74. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement