Ejemplo n.º 1
0
    def need_refuel(self):
        debug = self.debug
        opt = self.ship_config['refuel']

        frame = get_frame()
        crop = frame[opt['need_refuel_crop_y1']:opt['need_refuel_crop_y2'],
                     opt['need_refuel_crop_x1']:opt['need_refuel_crop_x2']]

        red = crop.copy()
        red[:, :, 0] = 0
        red[:, :, 1] = 0
        lower_mask = np.array([0, 0, 252])
        upper_mask = np.array([0, 0, 255])
        gray = cv2.inRange(red, lower_mask, upper_mask)

        kernel = np.ones((4, 4), np.uint8)
        erosion = cv2.erode(gray, kernel, iterations=1)

        contours = cv2.findContours(erosion, cv2.RETR_EXTERNAL,
                                    cv2.CHAIN_APPROX_SIMPLE)[1]

        w = 0
        contour = None
        if len(contours) > 0:
            contour = max(contours, key=cv2.contourArea)
            cv2.drawContours(crop, [contour], 0, (255, 255, 0), 1)
            w = cv2.boundingRect(contour)[2]

        debug_image = None
        if debug:
            debug_image = join_images(
                crop, cv2.cvtColor(erosion, cv2.COLOR_GRAY2RGB))

        return opt['min_refuel'] > w, debug_image, w
Ejemplo n.º 2
0
    def is_jumping(self):
        debug = self.debug
        opt = self.ship_config['radar']

        frame = get_frame()
        crop = frame[opt['radar_crop_y1']:opt['radar_crop_y2'],
                     opt['radar_crop_x1']:opt['radar_crop_x2']]

        lower_mask = np.array([200, 200, 200])
        upper_mask = np.array([255, 255, 255])
        mask = cv2.inRange(crop, lower_mask, upper_mask)

        kernel = np.ones((4, 4), np.uint8)
        dilation = cv2.dilate(mask, kernel, iterations=1)

        rows = dilation.shape[0]
        circles = cv2.HoughCircles(dilation,
                                   cv2.HOUGH_GRADIENT,
                                   2,
                                   rows / 8,
                                   param1=20,
                                   param2=50,
                                   minRadius=26,
                                   maxRadius=29)

        center = None
        radius = None
        if circles is not None:
            circles = np.uint16(np.around(circles))
            for c in circles:
                circle = c[0]
                center = (circle[0], circle[1])
                radius = circle[2]

        debug_image = None
        if debug:
            debug_image = crop
            if center is not None and radius is not None:
                cv2.circle(debug_image, center, radius, (0, 0, 255), 5)
            debug_image = join_images(
                crop, cv2.cvtColor(dilation, cv2.COLOR_GRAY2RGB))

        return circles is not None and len(circles) > 0, debug_image
Ejemplo n.º 3
0
    def is_refueling(self):
        debug = self.debug
        opt = self.ship_config['refuel']

        frame = get_frame()
        crop = frame[opt['need_refuel_crop_y1']:opt['need_refuel_crop_y2'],
                     opt['need_refuel_crop_x1']:opt['need_refuel_crop_x2']]

        lower_mask = np.array([240, 240, 240])
        upper_mask = np.array([255, 255, 255])
        mask = cv2.inRange(crop, lower_mask, upper_mask)

        contours = cv2.findContours(mask, cv2.RETR_EXTERNAL,
                                    cv2.CHAIN_APPROX_SIMPLE)[1]

        debug_image = None
        if debug:
            debug_image = join_images(crop,
                                      cv2.cvtColor(mask, cv2.COLOR_GRAY2RGB))

        return len(contours) > 0, debug_image
Ejemplo n.º 4
0
    def is_fuel_scoop_active(self):
        debug = self.debug
        opt = self.ship_config['refuel']

        is_active = False

        frame = get_frame()
        crop = frame[opt['fuel_scoop_crop_y1']:opt['fuel_scoop_crop_y2'],
                     opt['fuel_scoop_crop_x1']:opt['fuel_scoop_crop_x2']]

        debug_image = None
        if debug:
            debug_image = crop.copy()

        crop[:, :, 0] = 0
        crop[:, :, 1] = 0

        lower_mask = np.array([0, 0, 252])
        upper_mask = np.array([0, 0, 255])

        gray = cv2.inRange(crop, lower_mask, upper_mask)

        kernel = np.ones((2, 2), np.uint8)
        erosion = cv2.erode(gray, kernel, iterations=1)
        kernel = np.ones((2, 2), np.uint8)
        dilation = cv2.dilate(erosion, kernel, iterations=4)

        contours = cv2.findContours(dilation, cv2.RETR_EXTERNAL,
                                    cv2.CHAIN_APPROX_SIMPLE)[1]

        if len(contours) > 0:
            is_active = True

        if debug:
            debug_image = join_images(
                debug_image, cv2.cvtColor(dilation, cv2.COLOR_GRAY2RGB))

        return is_active, debug_image
Ejemplo n.º 5
0
    def center_ship(self, back=False):
        debug = self.debug
        opt = self.ship_config['radar']

        frame = get_frame()
        frame = frame[opt['radar_crop_y1']:opt['radar_crop_y2'],
                      opt['radar_crop_x1']:opt['radar_crop_x2']]
        frame = cv2.resize(frame, (0, 0), fx=2, fy=2.12)

        debug_img = None
        if debug:
            debug_img = frame.copy()

        # get blue mask
        mask = frame.copy()
        mask[:, :, 0] = 0
        mask[:, :, 1] = 0
        lower_mask = np.array([0, 0, 252])
        upper_mask = np.array([0, 0, 255])
        mask = cv2.inRange(mask, lower_mask, upper_mask)
        mask = cv2.dilate(mask, np.ones((2, 2), np.uint8), iterations=1)
        mask = cv2.bitwise_not(mask)

        # get circle
        circles = cv2.HoughCircles(mask,
                                   cv2.HOUGH_GRADIENT,
                                   4,
                                   50,
                                   param1=100,
                                   param2=30,
                                   minRadius=54,
                                   maxRadius=54)

        center = None
        radius = None
        if circles is not None:
            circles = np.uint16(np.around(circles))
            for c in circles:
                circle = c[0]
                center = (circle[0], circle[1])
                radius = circle[2]

        if debug_img is not None:
            mask2color = cv2.cvtColor(mask, cv2.COLOR_GRAY2RGB)
            if center is not None and radius is not None:
                cv2.circle(debug_img, center, radius, (0, 255, 0), 1)
                cv2.circle(debug_img, center, 4, (0, 255, 0), -1)
                cv2.circle(mask2color, center, radius, (0, 0, 255), 1)
                cv2.circle(mask2color, center, 4, (0, 0, 255), -1)
            debug_img = join_images(debug_img, mask2color)

        cx = None
        cy = None

        error = False

        # get position
        point_position = 0
        if center is not None and radius is not None:
            frame = frame[center[1] - 60:center[1] + 60,
                          center[0] - 60:center[0] + 60]

            crop = frame.copy()
            crop[:, :, 1] = 0
            crop[:, :, 2] = 0

            if debug_img is not None:
                debug_img = join_images(debug_img, crop)

            # get front point
            lower_mask = np.array([254, 0, 0])
            upper_mask = np.array([255, 0, 0])
            mask = cv2.inRange(crop, lower_mask, upper_mask)
            if mask is not None:
                mask = cv2.resize(mask, (0, 0), fx=2, fy=2)
                thresh = cv2.threshold(mask, 127, 255, 0)[1]
                kernel = np.ones((5, 5), np.uint8)
                mask = cv2.erode(thresh.copy(), kernel, iterations=3)
                contours = cv2.findContours(mask, cv2.RETR_EXTERNAL,
                                            cv2.CHAIN_APPROX_SIMPLE)[1]
                if len(contours) > 0:
                    cnt = get_contour_by_size(contours, 10, 10, 3)
                    M = cv2.moments(cnt)
                    if M['m00'] > 0:
                        cx = int(M['m10'] / M['m00'])
                        cy = int(M['m01'] / M['m00'])
                        point_position = 1
                    else:
                        error = True
                else:
                    # get back point
                    lower_mask = np.array([240, 0, 0])
                    upper_mask = np.array([255, 0, 0])
                    mask = cv2.inRange(crop, lower_mask, upper_mask)
                    if mask is not None:
                        mask = cv2.resize(mask, (0, 0), fx=2, fy=2)
                        thresh = cv2.threshold(mask, 0, 255, 0)[1]
                        mask = cv2.dilate(thresh, kernel, iterations=4)

                        contours = cv2.findContours(mask, cv2.RETR_EXTERNAL,
                                                    cv2.CHAIN_APPROX_SIMPLE)[1]
                        if len(contours) > 0:
                            cnt = get_contour_by_size(contours, 47, 47, 2)
                            M = cv2.moments(cnt)
                            if M['m00'] > 0:
                                cx = int(M['m10'] / M['m00'])
                                cy = int(M['m01'] / M['m00'])
                                point_position = 2
                            else:
                                error = True
                        else:
                            error = True
                    else:
                        print 'ERROR: MASK IS NULL!'
                        error = True

                if debug_img is not None:
                    if cx is not None and cy is not None:
                        color = (0, 255, 0)
                        if point_position is 2:
                            color = (0, 0, 255)
                        frame = cv2.resize(frame, (0, 0), fx=2, fy=2)
                        cv2.circle(frame, (cx, cy), 2, color, 4)
                        debug_img = join_images(
                            debug_img,
                            join_images(frame,
                                        cv2.cvtColor(mask, cv2.COLOR_GRAY2RGB),
                                        True))
                    else:
                        debug_img = join_images(debug_img, crop)
            else:
                print 'ERROR: MASK IS NULL!'
                error = True
        else:
            print 'ERROR: CIRCLE NOT FOUND'
            error = True

        # move
        keys = []
        if cx is not None and cy is not None:
            if (point_position > 0):
                width, height = cv2.resize(crop, (0, 0), fx=2, fy=2).shape[:2]
                deltaX = width / 2 - cx
                deltaY = height / 2 - cy

                margin = opt['margin']

                if not back:
                    if point_position == 1:
                        if deltaX < -margin:
                            keys.append(Buttons.D)
                        elif deltaX > margin:
                            keys.append(Buttons.A)

                        if deltaY < -margin:
                            keys.append(Buttons.NP_2)
                        elif deltaY > margin:
                            keys.append(Buttons.NP_8)

                    elif point_position == 2:
                        if deltaX < -margin:
                            keys.append(Buttons.D)
                        else:
                            keys.append(Buttons.A)

                        if deltaY < -margin:
                            keys.append(Buttons.NP_2)
                        else:
                            keys.append(Buttons.NP_8)
                else:
                    if point_position == 1:
                        if deltaX < -margin:
                            keys.append(Buttons.A)
                        else:
                            keys.append(Buttons.D)

                        if deltaY < -margin:
                            keys.append(Buttons.NP_8)
                        else:
                            keys.append(Buttons.NP_2)

                    elif point_position == 2:
                        if deltaX < -margin:
                            keys.append(Buttons.A)
                        elif deltaX > margin:
                            keys.append(Buttons.D)

                        if deltaY < -margin:
                            keys.append(Buttons.NP_8)
                        elif deltaY > margin:
                            keys.append(Buttons.NP_2)

                click_keys(keys, opt['move_time'])

        return len(keys) > 0, error, debug_img
Ejemplo n.º 6
0
    def avoid(self, last_keys=None, speed=1):
        debug = self.debug
        opt = self.ship_config['avoid']

        frame = get_frame()
        crop = frame[opt['danger_zone_crop_y1']:opt['danger_zone_crop_y2'],
                     opt['danger_zone_crop_x1']:opt['danger_zone_crop_x2']]

        debug_image = None
        if debug:
            debug_image = crop

        lower_mask = np.array([60, 60, 60])
        upper_mask = np.array([255, 255, 255])
        mask = cv2.inRange(crop, lower_mask, upper_mask)

        kernel = np.ones((8, 8), np.uint8)
        erosion = cv2.erode(mask, kernel, iterations=4)
        dilation = cv2.dilate(erosion, kernel, iterations=10)

        mask = dilation

        contours = cv2.findContours(mask, cv2.RETR_EXTERNAL,
                                    cv2.CHAIN_APPROX_SIMPLE)[1]

        M = None
        deltaX = 0
        deltaY = 0
        if len(contours) > 0:
            contour = max(contours, key=cv2.contourArea)

            if debug_image is not None:
                cv2.drawContours(debug_image, [contour], 0, (255, 255, 0), 1)

            M = cv2.moments(contour)
            cX = int(M["m10"] / M["m00"])
            cY = int(M["m01"] / M["m00"])

            cv2.circle(crop, (cX, cY), 7, (0, 0, 0), -1)
            cv2.circle(crop, (opt['avoid_center_x'], opt['avoid_center_y']), 7,
                       (255, 0, 0), -1)

            deltaX = opt['avoid_center_x'] - cX
            deltaY = opt['avoid_center_y'] - cY

            if deltaX < 5 and deltaX > -5:
                deltaX = 0
            if deltaY < 5 and deltaY > -5:
                deltaY = 0

        keys = []
        if M is not None:
            if deltaX < 0 and last_keys is None:
                keys.append(Buttons.A)
            elif deltaX > 0 and last_keys is None:
                keys.append(Buttons.D)
            elif last_keys is not None and Buttons.A in last_keys:
                keys.append(Buttons.A)
            elif last_keys is not None and Buttons.D in last_keys:
                keys.append(Buttons.D)
            elif deltaX < 0 and last_keys is not None and Buttons.A not in last_keys:
                keys.append(Buttons.A)
            elif deltaX > 0 and last_keys is not None and Buttons.D not in last_keys:
                keys.append(Buttons.D)

            if deltaY > 0 and last_keys is None:
                keys.append(Buttons.NP_2)
            elif last_keys is None:
                keys.append(Buttons.NP_8)
            elif last_keys is not None and Buttons.NP_2 in last_keys:
                keys.append(Buttons.NP_2)
            elif last_keys is not None and Buttons.NP_8 in last_keys:
                keys.append(Buttons.NP_8)
            elif deltaY > 0 and last_keys is not None and Buttons.NP_2 not in last_keys:
                keys.append(Buttons.NP_2)
            elif last_keys is not None and Buttons.NP_8 not in last_keys:
                keys.append(Buttons.NP_8)

        elif last_keys is not None:
            keys = last_keys

        if len(keys) > 0:
            click_keys(keys, speed)

        if debug_image is not None:
            debug_image = cv2.resize(join_images(
                crop, cv2.cvtColor(mask, cv2.COLOR_GRAY2RGB), True), (0, 0),
                                     fx=0.5,
                                     fy=0.5)

        return M is not None, keys, debug_image