Ketechi

Normbox

May 4th, 2020
797
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.74 KB | None | 0 0
  1. # For a tutorial on how to use Normbox please watch this video on YouTube: https://youtu.be/34cgrzyaOzE
  2. import os
  3. import cv2
  4. import gtuner
  5. import time
  6.  
  7.  
  8. class GCVWorker:
  9.     def __init__(self, width, height):
  10.         os.chdir(os.path.dirname(__file__))
  11.         self.gcvdata = bytearray([0x00])
  12.         self.X = width
  13.         self.Y = height
  14.         self.index = 0
  15.         self.nextIndez = True
  16.         self.nextIndex = 0
  17.         self.x1 = 1051
  18.         self.y1 = 615
  19.         self.x2 = 1076
  20.         self.y2 = 642
  21.         self.w = self.x2 - self.x1
  22.         self.h = self.y2 - self.y1
  23.         self.image = True
  24.         self.showFrozenFrame = False
  25.         self.freeze = True
  26.  
  27.     def __del__(self):
  28.         del self.gcvdata
  29.         del self.X
  30.         del self.Y
  31.         del self.x1
  32.         del self.x2
  33.         del self.y1
  34.         del self.y2
  35.         del self.w
  36.         del self.index
  37.         del self.nextIndez
  38.         del self.nextIndex
  39.         del self.image
  40.         del self.freeze
  41.         del self.showFrozenFrame
  42.  
  43.     def process(self, frame):
  44.         # The width and height value
  45.         self.w = self.x2 - self.x1
  46.         self.h = self.y2 - self.y1
  47.  
  48.         # If the user activate the frozenFrame function then it will always display the same frame
  49.         if self.showFrozenFrame:
  50.             frame = cv2.imread('frozenFrame.jpg')
  51.  
  52.         # Extract an image of the box | Button LS/L3
  53.         if gtuner.get_actual(gtuner.BUTTON_9) and self.image:
  54.             # Image extracted from the x and y coordinates
  55.             cv2.imwrite('xy.jpg', frame[self.y1:self.y1 + self.h, self.x1:self.x1 + self.w])
  56.  
  57.             # String variables that will be saved as txt file
  58.             text = "\"x1\": {}, \n\"y1\": {}, \n\"x2\": {}, \n\"y2\": {}, \n\"w\": {}, \n\"h\": {}\n".format(
  59.                 self.x1,
  60.                 self.y1,
  61.                 self.x2,
  62.                 self.y2,
  63.                 self.w,
  64.                 self.h)
  65.  
  66.             variable = "\nframe = frame[{}:{}, {}:{}]".format(self.y1, self.y1 + self.h, self.x1, self.x1 + self.w)
  67.             f = open('xy.txt', 'w+')
  68.             f.write(text)
  69.             f.write(variable)
  70.             f.close()
  71.  
  72.             # Inform the user where the files have been saved
  73.             print('A JPG and TXT file as been created on directory: {}'.format(os.path.dirname(__file__)))
  74.             self.image = False
  75.         elif gtuner.get_actual(gtuner.BUTTON_9):
  76.             pass
  77.         else:
  78.             self.image = True
  79.  
  80.         # Grab the current frame and freeze it so that the user can extract the image easier | Button RS/R3
  81.         if gtuner.get_actual(gtuner.BUTTON_6) and self.freeze:
  82.             self.freeze = False
  83.             self.showFrozenFrame = not self.showFrozenFrame
  84.  
  85.             # Display the current state of the frame
  86.             if self.showFrozenFrame:
  87.                 # Create an image of the current frame and save it to display it
  88.                 cv2.imwrite('frozenFrame.jpg', frame)
  89.                 print('Showing the frozen frame')
  90.             else:
  91.                 print('Unfreezing the frame')
  92.  
  93.         elif gtuner.get_actual(gtuner.BUTTON_6):
  94.             pass
  95.         else:
  96.             self.freeze = True
  97.  
  98.         # X1 cord
  99.         cv2.putText(frame, "X1: " + str(self.x1), (0, 40),
  100.                     cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 1,
  101.                     cv2.LINE_AA)
  102.  
  103.         # Y1 cord
  104.         cv2.putText(frame, "Y1: " + str(self.y1), (0, 120),
  105.                     cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 1,
  106.                     cv2.LINE_AA)
  107.  
  108.         # X2 cord
  109.         cv2.putText(frame, "X2: " + str(self.x2), (0, 80),
  110.                     cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 1,
  111.                     cv2.LINE_AA)
  112.  
  113.         # Y2 cord
  114.         cv2.putText(frame, "Y2: " + str(self.y2), (0, 160),
  115.                     cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 1,
  116.                     cv2.LINE_AA)
  117.  
  118.         # Width
  119.         cv2.putText(frame, "Width: " + str(self.w), (0, 200),
  120.                     cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 1,
  121.                     cv2.LINE_AA)
  122.  
  123.         # Height
  124.         cv2.putText(frame, "Height: " + str(self.h), (0, 240),
  125.                     cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 1,
  126.                     cv2.LINE_AA)
  127.  
  128.         # Changes the x and y index | Button LB/L1
  129.         if gtuner.get_actual(gtuner.BUTTON_7) and self.nextIndez:
  130.             self.index = self.index + 1
  131.             self.nextIndez = False
  132.             if self.index > 1 or self.index == 0:
  133.                 self.index = 0
  134.                 print('Now changing X1, Y1')
  135.             elif self.index == 1:
  136.                 print('Now changing X2, Y2')
  137.  
  138.         # Moves the square being drawn on the square
  139.         if gtuner.get_actual(gtuner.BUTTON_10):  # D-Pad up
  140.             if self.index == 0:
  141.                 self.y1 = self.y1 - 1
  142.             else:
  143.                 self.y2 = self.y2 - 1
  144.  
  145.         elif gtuner.get_actual(gtuner.BUTTON_11):  # D-Pad Down
  146.             if self.index == 0:
  147.                 self.y1 = self.y1 + 1
  148.             else:
  149.                 self.y2 = self.y2 + 1
  150.  
  151.         elif gtuner.get_actual(gtuner.BUTTON_13):  # D-Pad Right
  152.             if self.index == 0:
  153.                 self.x1 = self.x1 + 1
  154.             else:
  155.                 self.x2 = self.x2 + 1
  156.  
  157.         elif gtuner.get_actual(gtuner.BUTTON_12):  # D-Pad Left
  158.             if self.index == 0:
  159.                 self.x1 = self.x1 - 1
  160.             else:
  161.                 self.x2 = self.x2 - 1
  162.  
  163.         if gtuner.get_actual(gtuner.BUTTON_7) <= 1.0:
  164.             self.nextIndez = True
  165.  
  166.         # Draws a box around the specified area
  167.         cv2.rectangle(frame, (self.x1, self.y1), (self.x2, self.y2), (255, 0, 0), 1)
  168.  
  169.         return (frame, None)
Add Comment
Please, Sign In to add comment