Beispiel #1
0
    def __init__(self):
        self.results = get_args()
        self.name = self.results.target
        # Check if requested target exists
        if not utils.is_target(self.name):
            return
        if self.results.camera == 'pi':
            camera_provider = PICamera()
            logging.info('Using PI Camera provider')
        elif self.results.camera == 'realsense':
            logging.info('Using RealSense camera provider')
            camera_provider = RealSense()
        elif self.results.camera == 'cv':
            camera_provider = CVCamera(self.results.port)
        else:
            logging.error('Invalid camera provider, this shouldn\'t happen')
            sys.exit(1)

        self.display = Display(provider=camera_provider)
        if self.results.local:
            self.hsv_handler = Trackbars(self.name)
        else:
            self.hsv_handler = FileHSV(self.name)
        if self.results.web:
            self.web = Web(self)
            self.web.start_thread()  # Run web server
        if self.results.networktables:
            self.nt = nt_handler.NT(self.name)
        self.stop = False
Beispiel #2
0
 def loop(self):
     printed = False
     # Check if requested target exists
     if not utils.is_target(self.name, False):
         return
     logging.info(f'Starting loop with target {self.name}')
     self.stop = False
     # We dynamically load classes in order to provide a modular base
     target = import_module(f'targets.{self.name}').Target(self)
     self.display.change_exposure(target.exposure)
     # Timer for FPS counter
     timer = time.time()
     avg = 0
     while True:
         frame = self.display.get_frame()
         if frame is None:
             if not printed:
                 logging.warning('Couldn\'t read from camera')
                 printed = True
             continue
         else:
             printed = False
         # Separate frames for display purposes
         original = frame.copy()
         contour_image = frame.copy()
         # Target functions
         mask = target.create_mask(frame, self.hsv_handler.get_hsv())
         contours, hierarchy = target.find_contours(mask)
         filtered_contours = target.filter_contours(contours, hierarchy)
         # Draw contours
         target.draw_contours(filtered_contours, contour_image)
         # Find distance and angle
         angle, distance = target.measurements(contour_image,
                                               filtered_contours)
         # Show FPS
         avg = utils.calculate_fps(contour_image, time.time(), timer, avg)
         timer = time.time()
         # Display
         self.display.process_frame(contour_image, 'contour image',
                                    self.results.local)
         self.display.process_frame(utils.bitwise_and(original, mask),
                                    'mask', self.results.local)
         if self.results.networktables:
             if distance:
                 self.nt.set_item('distance', distance)
             if angle:
                 self.nt.set_item('angle', angle)
         if self.stop:
             # If stop signal was sent we call loop again to start with new name
             logging.warning('Restarting...')
             self.loop()
             break
         k = cv2.waitKey(1) & 0xFF  # large wait time to remove freezing
         if k in (27, 113):
             logging.warning('Q pressed, stopping...')
             self.display.release()
             break
Beispiel #3
0
 def change_name(self, name):
     """
     Changes the name and starts a new loop.
     :param name:
     """
     if not utils.is_target(name, neural=True):
         return
     logging.info('Changing target to {}'.format(name))
     self.name = name
     self.stop = True
Beispiel #4
0
 def change_name(self, name):
     """
     Changes the name and starts a new loop.
     :param name:
     """
     if not utils.is_target(name):
         return
     logging.info(f'Changing target to {name}')
     self.name = name
     self.hsv_handler.name = name
     self.hsv_handler.reload()
     self.stop = True
Beispiel #5
0
 def loop(self):
     printed = False
     # Check if requested target exists
     if not utils.is_target(self.name, False, True):
         return
     logging.info('Starting loop with target {}'.format(self.name))
     self.stop = False
     # We dynamically load classes in order to provide a modular base
     target = import_module('neural_targets.{}'.format(
         self.name)).Target(self)
     self.display.change_exposure(target.exposure)
     # Timer for FPS counter
     timer = time.time()
     avg = 0
     while True:
         frame = self.display.get_frame()
         if frame is None:
             if not printed:
                 logging.warning('Couldn\'t read from camera')
                 printed = True
             continue
         else:
             printed = False
         # Get bounding boxes
         boxes = target.boxes(frame)
         # Draw on frame
         target.draw(frame, boxes)
         angle, distance, bounding_box = target.measurements(frame, boxes)
         # Show FPS
         avg = utils.calculate_fps(frame, time.time(), timer, avg)
         timer = time.time()
         # Web
         self.web.frame = frame
         # Display
         self.display.process_frame(frame, 'image', self.results.local)
         if self.results.networktables:
             if distance:
                 self.nt.set_item('distance', distance)
             if angle:
                 self.nt.set_item('angle', angle)
         if self.stop:
             # If stop signal was sent we call loop again to start with new name
             logging.warning('Restarting...')
             self.loop()
             break
         k = cv2.waitKey(1) & 0xFF  # large wait time to remove freezing
         if k in (27, 113):
             logging.warning('Q pressed, stopping...')
             self.display.release()
             self.session.close()
             break
Beispiel #6
0
    def __init__(self):
        """
        Create all initial handlers based on parameters from get_args.

        camera_provider : CVCamera or PICamera or RealSense
            - the type of camera to be used by self.display
        """
        self.results = get_args()
        self.name = self.results.target
        # Check if requested target exists
        if not utils.is_target(self.name):
            return

        # Set the camera provider
        if self.results.camera == 'pi':
            camera_provider = PICamera()
            logging.info('Using PI Camera provider')
        elif self.results.camera == 'realsense':
            logging.info('Using RealSense camera provider')
            camera_provider = RealSense()
        elif self.results.camera == 'cv':
            camera_provider = CVCamera(self.results.port)
        else:
            logging.error('Invalid camera provider, this shouldn\'t happen')
            sys.exit(1)

        # Create the display
        self.display = Display(provider=camera_provider)
        if self.results.local:
            self.hsv_handler = Trackbars(self.name)
        else:
            self.hsv_handler = FileHSV(self.name)

        # Create the web server
        if self.results.web:
            self.web = Web(self)
            self.web.start_thread()

        # Create the networktables server
        if self.results.networktables:
            self.nt = nt_handler.NT(self.name)

        self.logger = Logger(self)

        self.stop = False
Beispiel #7
0
    def change_name(self, name):
        """
        Changes the name of the target.

        Uses: Generally placed in the web handler, where it receives a string from the site. Is called before a new
        loop.
        See: update() in web.py

        :param name: The name of the new target. If it does not exist, the run is shut down.
        """
        if not utils.is_target(name):
            return
        logging.info('Changing target to {}'.format(name))
        self.name = name
        # Update the HSV variables to match the new target
        self.hsv_handler.name = name
        self.hsv_handler.reload()
        # Stop the current loop
        self.stop = True
Beispiel #8
0
    def loop(self):
        """
        Recognises the target repeatedly. Utilises all handlers initialised in __init__.

        :return: If the target doesn't exist, the run is shut down.
        """
        printed = False
        # Check if requested target exists
        if not utils.is_target(self.name, False):
            return
        logging.info('Starting loop with target {}'.format(self.name))
        self.stop = False
        # Load the target class from the 'targets' directory
        target = import_module('targets.{}'.format(self.name)).Target(self)
        time.sleep(1)
        # Change camera exposure based on the target
        self.display.change_exposure(target.exposure)
        # Timer for FPS counter
        self.timer = time.time()
        avg = 0
        while True:
            # Get initial frame
            frame = self.display.get_frame()
            # If the frame could not be read, flag it as unreadable to avoid errors
            if frame is None:
                if not printed:
                    logging.warning('Couldn\'t read from camera')
                    printed = True
                continue
            else:
                self.logger.record_latency()
                printed = False
            # Copy the initial frame for analysis and display, respectively
            original = frame.copy()
            contour_image = frame.copy()
            # Show FPS
            avg = utils.calculate_fps(contour_image, time.time(), self.timer, avg)
            self.timer = time.time()
            # Create a mask
            mask = target.create_mask(frame, self.hsv_handler.get_hsv())
            # Get all contours
            contours, hierarchy = target.find_contours(mask)
            self.is_potential_target = bool(contours)
            # Filter contours
            filtered_contours = target.filter_contours(contours, hierarchy)
            self.is_target = bool(filtered_contours)
            # Draw contours
            target.draw_contours(filtered_contours, contour_image)
            self.logger.record_contours()
            # Find distance, angle, and other measurements if stated
            angle, distance, field_angle, additional_data = target.measurements(contour_image, filtered_contours)
            if self.results.web:
                # Stream frame
                self.web.frame = contour_image
            # Display frame
            self.display.process_frame(contour_image, 'image', self.results.local)
            # Display mask
            self.display.process_frame(utils.bitwise_and(original, mask), 'mask', self.results.local)
            # Send measurements to networktables, if requested, and if measurements were returned
            if self.results.networktables:
                if distance is not None:
                    self.nt.set_item('distance', distance)
                if angle is not None:
                    self.nt.set_item('angle', angle)
                if field_angle is not None:
                    self.nt.set_item('field_angle', field_angle)
            # TODO: Send additional data
            if self.stop:
                # If stop signal was sent, call loop again to start with new name
                logging.warning('Restarting...')
                self.loop()
                break
            # Stop the code if q is pressed
            k = cv2.waitKey(1) & 0xFF  # Large wait time to remove freezing
            if k in (27, 113):
                logging.warning('Q pressed, stopping...')
                # Release the camera and close all windows
                self.display.release()
                break