Advertisement
rileydylan581

DigitReaderMainFile

May 14th, 2020
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.35 KB | None | 0 0
  1. # cv2 is used for a lot of the image processing that needs to be done
  2. import cv2
  3. # PIL is used for a smaller ammount of necesary image processing
  4. from PIL import Image
  5. # numpy is a library used to manipulate numbers which can make the images
  6. # more friendly to the neural net
  7. import numpy as np
  8. # the neural_net is another file that contains the actuall neural network
  9. # itself.
  10. import neural_net
  11.  
  12. # Getting a connection to the camera.
  13. vid_cap = cv2.VideoCapture(1)
  14.  
  15. # creating a method to process the image
  16. def process_img(original_img):
  17.     # converting image to grayscale
  18.     processed_img = cv2.cvtColor(original_img, cv2.COLOR_BGR2GRAY)
  19.     # simplifing the image into one line (that is the digit)
  20.     processed_img = cv2.Canny(processed_img, threshold1=175, threshold2=285)
  21.     # returning the new processed image
  22.     return processed_img
  23.  
  24. # running the program forever. (While True: basically just means while
  25. # True = True do everything indented below it)
  26. while True:
  27.     # Get camera feed from the connection created earlier
  28.     has_feed, frame = vid_cap.read()
  29.  
  30.     # Crop the frame into 500x500px
  31.     image = Image.fromarray(frame).crop((575, 280, 775, 480))
  32.  
  33.     # Turning the image into a numpy array which is basically just a list of
  34.     # all the image data it is basically a list or rows, which are a list of
  35.     # pixel values. This makes it easier for the neural net to read
  36.     image_array = np.array(image)
  37.  
  38.     # resizing this image to 28x28px because the smaller the image, the
  39.     # easier it can be processed by the neural net
  40.     image_array = cv2.resize(image_array, (28, 28))
  41.  
  42.     # this puts the image throug the processing method that was created earlier
  43.     final_image_array = process_img(image_array)
  44.  
  45.     # this is the method that puts the image through the neural network
  46.     neural_net.test_image(final_image_array)
  47.  
  48.     # this ouputs a video of the camera feed with a nice box where the image
  49.     # is cropped
  50.     show_frame = cv2.rectangle(frame, (575, 280), (775, 480), (50, 245, 0), 2)
  51.     cv2.imshow("Window", show_frame)
  52.  
  53.     # this line basically says that if the q key is pressed the program will
  54.     # shut down
  55.     if cv2.waitKey(1) & 0xFF == ord('q'):
  56.         break
  57.  
  58. # this closes the connection to the camera
  59. vid_cap.release()
  60. # and destorys all of the video windows.
  61. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement