Пример #1
0
 def on_press(key):
     if key == StateManager.scope_key:
         StateManager.scoping = True
     elif str(key) == StateManager.beast_key:
         StateManager.toggle_beast()
         print("Beast mode = " + str(StateManager.beast_mode()))
     elif StateManager.scoping and str(key) in ReferenceManager.key_dict():
         StateManager.scope = ReferenceManager.key_dict()[str(key)]
         StateManager.clear_view()
         print("Scope = " + str(StateManager.scope))
Пример #2
0
def _test_methods(scope=ReferenceManager.Scope.x2):
    StateManager.debug = False
    StateManager.scope = scope
    labels = tuple(
        open(
            'data/labels/' + ReferenceManager.scope_string(scope) +
            '/labels.txt', 'r'))
    total_error = 0
    for label in labels:
        components = label.split('|')
        filename = components[0].strip()
        confidence = int(components[1])
        start_x, start_y = float(components[2]), float(components[3])
        end_x, end_y = float(components[4]), float(components[5])
        target = Engine.get_target(
            Screenshot(
                cv2.imread('data/samples/' +
                           ReferenceManager.scope_string(scope) + '/' +
                           filename), time.time()))
        error = 1
        # error = \
        #     min(_distance((target.x, target.y), (start_x, start_y)), _distance((target.x, target.y), (end_x, end_y)))
        image = cv2.imread('data/samples/' +
                           ReferenceManager.scope_string(scope) + '/' +
                           filename)
        if target.confidence < .5 or confidence == 0:
            if target.confidence < .5 and confidence == 0:
                error = 0
            elif target.confidence < .5:
                print("Gave up on " + filename)
                error = .5
            else:
                error = 0
        elif min(start_x, end_x) <= target.x <= max(start_x,
                                                    end_x):  # within X bounds
            if min(start_y, end_y) <= target.y <= max(
                    start_y, end_y):  # within Y bounds
                error = 0
        else:
            for i in [(0, 0), (0, 1), (1, 0), (0, -1), (-1, 0)]:
                image[target.y + i[0], target.y + i[1]] = [0, 255, 0]
            print("Missed: " + filename)
            # cv2.imshow(filename, image)
            pass
        total_error += error
        # cv2.waitKey(0)
        # cv2.destroyAllWindows()
    return total_error / len(labels)
Пример #3
0
def move(x, y):

    # Adjust for aim point
    x -= ReferenceManager.get_aim(StateManager.scope)[0]
    y -= ReferenceManager.get_aim(StateManager.scope)[1]

    # Adjust for sensitivity
    x /= StateManager.x_sensitivity
    y /= StateManager.y_sensitivity

    # Adjust for windows API:
    x = int(round(x * 65535 / win32api.GetSystemMetrics(0)))
    y = int(round(y * 65535 / win32api.GetSystemMetrics(1)))

    # Move the mouse:
    win32api.mouse_event(win32con.MOUSEEVENTF_MOVE, x, y)
Пример #4
0
def main():

    print("Running tests...")
    for scope in (ReferenceManager.Scope.x1h, ReferenceManager.Scope.x2,
                  ReferenceManager.Scope.x4v, ReferenceManager.Scope.x1t):
        print("\t\t", ReferenceManager.scope_string(scope), "=",
              _test_methods(scope))

    # Test the performance on labelled data:
    # Currently:
    #  x1h: 0.045454545454545456
    #  x2:  0.06190476190476191
    #  x4v: 0.125 (not enough data)
    #  x1t: 0.061224489795918366
    # #return

    # start the screenshotting thread (for data collection:
    StateManager.scope = ReferenceManager.Scope.x2
    screenshot_thread = BackgroundManager(float(1. / 1000),
                                          ScreenshotManager.update_view,
                                          [False])
    screenshot_thread.start()
    #
    # # Start the aiming thread:
    aim_thread = BackgroundManager(float(1. / 1000), Robot.act, [])
    aim_thread.start()
    #
    # start the hook thread:
    threading.Thread(target=InputManager.listen).start()

    print("Running...")
Пример #5
0
def main():
    global refPt, cropping

    StateManager.scope = ReferenceManager.Scope.x1t
    image_path = 'data/samples/' + ReferenceManager.scope_string(
        StateManager.scope) + '/'
    label_path = 'data/labels/' + ReferenceManager.scope_string(
        StateManager.scope) + '/labels.txt'

    output = open(label_path, "w+")

    global image
    c = 0
    for image_name in os.listdir(image_path):
        refPt = []
        print(image_path + image_name, c)
        c += 0
        image = cv2.imread(image_path + image_name)
        target = Engine.get_target(Screenshot(image, time.time()))
        for i in [(0, 0), (0, 1), (1, 0), (0, -1), (-1, 0)]:
            image[target.y + i[0], target.x + i[1]] = [0, 255, 0]
        clone = image.copy()
        cv2.namedWindow("image")
        cv2.setMouseCallback("image", click_and_crop)
        while True:
            cv2.imshow("image", image)
            key = cv2.waitKey(1) & 0xFF
            if key == ord("r"):
                image = clone.copy()
                refPt = []
            elif key == ord("c"):
                break

        if len(refPt) == 2:
            confidence = 1
        else:
            confidence = 0
            refPt = [[-1, -1], [-1, -1]]
        output.write(image_name + "|" + str(confidence) + "|" +
                     str(refPt[0][0]) + "|" + str(refPt[0][1]) + "|" +
                     str(refPt[1][0]) + "|" + str(refPt[1][1]) + "\n")

        cv2.destroyAllWindows()
    output.close()
Пример #6
0
def _prep_mask(step=2, scope=ReferenceManager.Scope.x1o):
    scope_string = ReferenceManager.scope_string(scope)
    print(scope_string)
    if step == 1:
        image = cv2.imread("C:\\Users\\Eric\\Desktop\\" + scope_string +
                           ".png")
        aoi = ReferenceManager.get_aoi(scope)
        image = image[aoi.y:(aoi.y + aoi.h), aoi.x:(aoi.x + aoi.w)]
        cv2.imwrite(
            "C:\\Users\\Eric\\Desktop\\code\\legendary-aim-assist\\python\\data\\masks\\"
            + scope_string + "\\mask.png", image)
    elif step == 2:
        image = cv2.imread(
            "C:\\Users\\Eric\\Desktop\\code\\legendary-aim-assist\\python\\data\\masks\\"
            + scope_string + "\\mask.png")
        grey = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        _, binary = cv2.threshold(grey, 1, 255, cv2.THRESH_BINARY)
        cv2.imwrite(
            "C:\\Users\\Eric\\Desktop\\code\\legendary-aim-assist\\python\\data\\masks\\"
            + scope_string + "\\mask.png", binary)
def update_view(save=False):
    if StateManager.aiming and StateManager.beast_mode():
        timestamp = int(round(time.time() * 1000))
        image = _gsk_screenshot(ReferenceManager.get_aoi(StateManager.scope))
        screenshot = Screenshot(image, timestamp)
        # screenshot = Screenshot(cv2.imread('data/samples/x1h/1552066529854.png'), timestamp)
        # cv2.imshow('t', screenshot.image)
        # cv2.waitKey(0)
        # cv2.destroyAllWindows()
        if save:
            cv2.imwrite(StateManager.screenshot_path + str(timestamp) + ".png",
                        screenshot.image)
        StateManager.update_view(screenshot)
Пример #8
0
cv2.createTrackbar(sh, wnd, 0, 255, set_sah)
cv2.createTrackbar(vl, wnd, 0, 255, set_val)
cv2.createTrackbar(vh, wnd, 0, 255, set_vah)

# begin our 'infinite' while loop
while (1):
    frame = cv2.imread('out/1551644032232.png')

    # it is common to apply a blur to the frame
    # frame = cv2.GaussianBlur(frame, (5, 5), 0)

    # convert from a BGR stream to an HSV stream
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    hsv = cv2.bitwise_and(hsv,
                          hsv,
                          mask=ReferenceManager.get_mask(
                              ReferenceManager.Scope.x2))

    # create a mask for that range
    mask = cv2.inRange(hsv, np.array([hul, sal, val]),
                       np.array([huh, sah, vah]))

    res = cv2.bitwise_and(frame, frame, mask=mask)

    c = cv2.Canny(res, 100, 200)
    small_mask = cv2.erode(mask, np.ones((2, 2), np.uint8), iterations=2)
    small_c = cv2.bitwise_and(c, c, mask=small_mask)
    small_c = cv2.dilate(small_c, np.ones((3, 3), np.uint8), iterations=3)

    cv2.imshow('mask', mask)
    cv2.imshow('small_mask', small_mask)
    cv2.imshow('final', small_c)
Пример #9
0
def get_target(screenshot):

    scope = StateManager.scope
    raw = screenshot.image

    # Filter the image with the right mask:
    relevant_mask = ReferenceManager.get_mask(scope)
    raw = cv2.bitwise_and(raw, raw, mask=relevant_mask)

    # Convert to HSV:
    # print(screenshot)
    hsv = cv2.cvtColor(raw, cv2.COLOR_BGR2HSV)

    # Build a mask based on HSV filtering:
    mask = cv2.inRange(hsv,
                       ReferenceManager.get_hsv(scope).lower,
                       ReferenceManager.get_hsv(scope).upper)
    final_mask = cv2.erode(mask, np.ones((1, 2), np.uint8), iterations=1)
    final_mask = cv2.dilate(final_mask,
                            np.ones((2, 3), np.uint8),
                            iterations=4)

    # Apply the mask:
    hsv = cv2.bitwise_and(hsv, hsv, mask=final_mask)

    # Convert to binary:
    # binary = _convert_binary(hsv)

    # Use canny to convert to binary:
    c = cv2.Canny(hsv, 100, 200)
    small_mask = cv2.erode(mask, np.ones((2, 1), np.uint8), iterations=1)
    small_mask = cv2.erode(small_mask, np.ones((1, 2), np.uint8), iterations=1)
    small_c = cv2.bitwise_and(c, c, mask=small_mask)
    binary = cv2.dilate(small_c, np.ones((7, 7), np.uint8), iterations=2)

    # Fill holes:
    # im_floodfill = binary.copy()
    # h, w = im_floodfill.shape[:2]
    # flood_mask = np.zeros((h + 2, w + 2), np.uint8)
    # cv2.floodFill(im_floodfill, flood_mask, (0, 0), 255);
    # im_floodfill_inv = cv2.bitwise_not(im_floodfill)
    # binary = binary | im_floodfill_inv

    # Identify components:
    components = cv2.connectedComponentsWithStats(binary, 4, cv2.CV_32S)
    labels = components[1] + 1
    labels = cv2.bitwise_and(labels, labels, mask=final_mask)
    stats = components[2]
    centroids = components[3]

    # find the biggest non-background shape label:
    counts = np.unique(labels, return_counts=True)
    invalid_label_indices = []
    for i, label in enumerate(list(counts[0].tolist())):
        if label == 0:
            invalid_label_indices.append(i)
    stats = [i for j, i in enumerate(stats) if j not in invalid_label_indices]
    centroids = [
        i for j, i in enumerate(centroids) if j not in invalid_label_indices
    ]

    if len(stats) <= 0:
        print("No valid target found!")
        cv2.imwrite('out/fail-' + str(int(round(time.time() * 1000))) + '.png',
                    raw)
        return Target(-1)
    else:
        biggest_shape = np.argmax(list(map(lambda s: s[4], stats)))

        # find the bounding box of the biggest non-background shape:
        target_x = stats[biggest_shape][cv2.CC_STAT_LEFT]
        target_y = stats[biggest_shape][cv2.CC_STAT_TOP]
        target_w = stats[biggest_shape][cv2.CC_STAT_WIDTH]
        target_h = stats[biggest_shape][cv2.CC_STAT_HEIGHT]

        # estimate the head from the bounding box:
        head_x = int(
            round((stats[biggest_shape][0] +
                   int(round(target_x + (target_w / 2.)))) / 2.))
        head_y = int(round(target_y + (target_h / 4.)))

        # walk the head around until you find a shot:
        aim_x = head_x
        aim_y = head_y
        left = aim_x
        right = aim_x
        while left > 0 or right < binary.shape[1]:
            if left > 0 and binary[aim_y][left] != 0:
                aim_x = left
                break
            elif right < binary.shape[1] and binary[aim_y][right] != 0:
                aim_x = right
                break
            else:
                left -= 1
                right += 1

        # try to center the shot on the shape:
        head_left = aim_x
        head_right = aim_x
        while head_left > 0 and binary[aim_y][head_left] != 0:
            head_left -= 1
        while head_right < (binary.shape[1] -
                            1) and binary[aim_y][head_right] != 0:
            head_right += 1
        aim_x = int(round(((head_left + head_right) / 2)))

        # show the result:
        if StateManager.debug:

            tgt = raw
            for i in [(0, 0), (0, 1), (1, 0), (0, -1), (-1, 0)]:
                tgt[aim_y + i[0], head_left + i[1]] = [0, 255, 0]
            for i in [(0, 0), (0, 1), (1, 0), (0, -1), (-1, 0)]:
                tgt[aim_y + i[0], head_right + i[1]] = [0, 255, 0]
            for i in [(0, 0), (0, 1), (1, 0), (0, -1), (-1, 0)]:
                tgt[aim_y + i[0], aim_x + i[1]] = [255, 0, 0]

            cv2.imshow('post-canny filter', binary)
            cv2.imshow('lines', c)
            cv2.imshow('hsv', hsv)
            cv2.imshow('target', tgt)
            cv2.waitKey(0)
            cv2.destroyAllWindows()

        return Target(1., aim_x, aim_y, centroids[biggest_shape])