def sub_callback_laser_dots(results):
    global lastLastPosition, MODE, DRAW_TARGET
    claims = []
    claims.append({
        "type": "retract", "fact": [
            ["id", get_my_id_str()],
            ["id", "2"],
            ["postfix", ""],
        ]
    })
    if results and len(results) > 0:
        result = results[0]
        lastLastPosition = [int(result["x"]), int(result["y"])]
        if MODE in ["0", "1", "2", "3"]:
            ill = Illumination()
            ill.stroke(255, 0, 255, 128)
            ill.fill(255, 0, 255, 100)
            current_corner = int(MODE)
            poly = regionPoints[:current_corner] + [lastLastPosition]
            projected_poly = list(map(lambda p: project(LASER_CAMERA_ID, p[0], p[1]), poly))
            ill.polygon(projected_poly)
            SIZE = 5
            for pt in projected_poly:
                ill.ellipse(pt[0] - SIZE, pt[1] - SIZE, SIZE * 2, SIZE * 2)
            claims.append(ill.to_batch_claim(get_my_id_str(), "2", DRAW_TARGET))
    else:
        lastLastPosition = None
    batch(claims)
Beispiel #2
0
def sub_callback_graphics(results):
    global proxy_client
    if not check_and_connect_proxy_server():
        return
    logging.info("proxying message")
    claims = []
    claims.append({"type": "retract", "fact": [
        ["id", get_my_id_str()],
        ["id", "2"],
        ["postfix", ""],
    ]})
    for result in results:
        claims.append({"type": "claim", "fact": [
            ["id", get_my_id_str()],
            ["id", "2"],
            ["text", "wish"],
            ["text", "a"],
            ["text", "paper"],
            ["text", "would"],
            ["text", "be"],
            ["text", "created"],
            ["text", "in"],
            ["text", str(result["lang"])],
            ["text", "with"],
            ["text", "source"],
            ["text", "code"],
            ["text", str(result["code"])],
            ["text", "@"],
            ["integer", str(result["time"])],
        ]})
    proxy_client.send_multipart(["....BATCH{}{}".format(
        get_my_id_str(), json.dumps(claims)).encode()], zmq.NOBLOCK)
Beispiel #3
0
def show_winner():
    global PLAYER_REGIONS, WINNER
    claims = []
    claims.append({
        "type":
        "retract",
        "fact": [
            ["id", get_my_id_str()],
            ["id", "1"],
            ["postfix", ""],
        ]
    })
    if WINNER in PLAYER_REGIONS and PLAYER_REGIONS[WINNER] is not None:
        p1 = project(LASER_CAMERA_ID, PLAYER_REGIONS[WINNER]["x2"],
                     PLAYER_REGIONS[WINNER]["y2"])
        p2 = project(LASER_CAMERA_ID, PLAYER_REGIONS[WINNER]["x3"],
                     PLAYER_REGIONS[WINNER]["y3"])
        logging.error("P1 P2")
        logging.error(p1)
        logging.error(p2)
        ill = Illumination()
        ill.push()
        ill.translate(p1[0], p1[1])
        ill.rotate(
            math.atan2(p2[1] - p1[1], p2[0] - p1[0]) +
            random.uniform(-0.1, 0.1))
        ill.fontsize(40)
        ill.fontcolor(255, 255, 0)
        ill.text(0, 0, "WINNER!")
        ill.pop()
        claims.append(ill.to_batch_claim(get_my_id_str(), "1", "global"))
    batch(claims)
def render_mode():
    global MODE, is_toggleable, region_name
    RENDER_MODE_SUBSCRIPTION_ID = "99"
    claims = []
    claims.append({
        "type": "retract", "fact": [
            ["id", get_my_id_str()],
            ["id", RENDER_MODE_SUBSCRIPTION_ID],
            ["postfix", ""],
        ]
    })
    ill = Illumination()
    if MODE == "IDLE":
        ill.text(0, 0, "idle\npress down\nto define\na region")
    elif MODE in ["0", "1", "2", "3"]:
        ill.text(0, 0, "setting\ncorner\n{}/4".format(int(MODE) + 1))
    elif MODE == "is_toggleable":
        is_toggleable_text = "no"
        if is_toggleable:
            is_toggleable_text = "yes"
        ill.text(0, 0, "Should region\nbe a toggle?\ny/n -> {}\npress enter\nto accept".format(is_toggleable_text))
    elif MODE == "naming":
        region_name_text = "..."
        if region_name:
            region_name_text = region_name
        ill.text(0, 0, "region name:\n{}\npress enter\nto accept".format(region_name_text))
    claims.append(ill.to_batch_claim(get_my_id_str(), RENDER_MODE_SUBSCRIPTION_ID))
    return claims
Beispiel #5
0
def sub_callback_keyboard(results):
    global is_selecting, ignore_key_press
    if ignore_key_press:
        ignore_key_press = False
        return
    claims = []
    claims.append({
        "type":
        "retract",
        "fact": [
            ["id", get_my_id_str()],
            ["id", "2"],
            ["postfix", ""],
        ]
    })
    if results:
        key = str(results[0]["key"])
        if key == "up" and is_selecting:
            is_selecting = False
            ill = Illumination()
            ill.text(0, 0, "clearing\ntoggle")
            claims.append(ill.to_batch_claim(get_my_id_str(), "2"))
        else:
            is_selecting = True
    batch(claims)
Beispiel #6
0
def sub_callback(results):
    global proxy_client
    if not check_and_connect_proxy_server():
        return
    logging.info("proxying message")
    claims = []
    claims.append({"type": "retract", "fact": [
        ["id", get_my_id_str()],
        ["id", "1"],
        ["postfix", ""],
    ]})
    for result in results:
        claims.append({"type": "claim", "fact": [
            ["id", get_my_id_str()],
            ["id", "1"],
            ["text", "tile"],
            ["text", str(result["tile"])],
            ["text", "maybe"],
            ["float", str(result["score"])],
            ["text", "seen"],
            ["text", "at"],
            ["integer", str(result["x"])],
            ["integer", str(result["y"])],
            ["text", "@"],
            ["integer", str(result["time"])],
        ]})
    proxy_client.send_multipart(["....BATCH{}{}".format(
        get_my_id_str(), json.dumps(claims)).encode()], zmq.NOBLOCK)
Beispiel #7
0
def sub_callback_graphics(results):
    global proxy_client
    if not check_and_connect_proxy_server():
        return
    logging.info("proxying message")
    claims = []
    claims.append({
        "type":
        "retract",
        "fact": [
            ["id", get_my_id_str()],
            ["id", "1"],
            ["postfix", ""],
        ]
    })
    for result in results:
        claims.append({
            "type":
            "claim",
            "fact": [
                ["id", get_my_id_str()],
                ["id", "1"],
                ["text", "draw"],
                ["text", "graphics"],
                ["text", str(result["graphics"])],
                ["text", "on"],
                ["text", "miniprojector"],
            ]
        })
    proxy_client.send_multipart(
        ["....BATCH{}{}".format(get_my_id_str(), json.dumps(claims)).encode()],
        zmq.NOBLOCK)
Beispiel #8
0
def claim_data(data):
    global last_data, CAMERA_ID
    if len(last_data) == 0 and len(data) == 0:
        if DEBUG:
            logging.info("empty data again, skipping claim")
        return
    last_data = data
    currentTimeMs = int(round(time.time() * 1000))
    claims = [
        {"type": "retract", "fact": [["id", get_my_id_str()], ["id", "1"], ["postfix", ""]]}
    ]
    for datum in data:
        x = int(sum([d[0][0] for d in datum])/len(datum))
        y = int(sum([d[0][1] for d in datum])/len(datum))
        if DEBUG:
            logging.info((x, y))
        claims.append({"type": "claim", "fact": [
            ["id", get_my_id_str()],
            ["id", "1"],
            ["text", "laser"],
            ["text", "seen"],
            ["text", "at"],
            ["integer", str(x)],
            ["integer", str(y)],
            ["text", "@"],
            ["integer", str(currentTimeMs)],
            ["text", "on"],
            ["text", "camera"],
            ["integer", CAMERA_ID],
        ]})
    batch(claims)
Beispiel #9
0
def init_claim():
    claims = [
        {"type": "retract", "fact": [["id", get_my_id_str()], ["postfix", ""]]},
        {"type": "claim", "fact": [
            ["id", get_my_id_str()],
            ["id", "0"],
            ["text", "camera"],
            ["integer", str(CAMERA_ID)],
            ["text", "has"],
            ["text", "resolution"],
            ["integer", str(CAM_WIDTH)],
            ["integer", str(CAM_HEIGHT)],
        ]},
        {"type": "claim", "fact": [
            ["id", get_my_id_str()],
            ["id", "0"],
            ["text", "camera"],
            ["integer", str(CAMERA_ID)],
            ["text", "cv"],
            ["text", "threshold"],
            ["text", "is"],
            ["integer", str(THRESHOLD)],
        ]},
        {"type": "claim", "fact": [
            ["id", get_my_id_str()],
            ["id", "0"],
            ["text", "camera"],
            ["integer", str(CAMERA_ID)],
            ["text", "frame"],
            ["text", "at"],
            ["text", "http://{}:{}/src/files/cv-frame.jpg".format(get_host_ip(), PORT)],
        ]},
    ]
    batch(claims)
Beispiel #10
0
def sub_callback_graphics2(results):
    claims = []
    claims.append({
        "type":
        "retract",
        "fact": [
            ["id", get_my_id_str()],
            ["id", "2"],
            ["postfix", ""],
        ]
    })
    if results:
        for result in results:
            claims.append({
                "type":
                "claim",
                "fact": [
                    ["id", get_my_id_str()],
                    ["id", "2"],
                    ["text", "draw"],
                    ["text", "graphics"],
                    ["text", str(result["graphics"])],
                    ["text", "on"],
                    ["integer", str(result["displayId"])],
                ]
            })
    batch(claims)
Beispiel #11
0
def check_and_connect_proxy_server():
    global proxy_context, proxy_client, proxy_connected, PROXY_URL, last_proxy_heartbeat, health_check_delay_s
    if not proxy_connected or time.time(
    ) - last_proxy_heartbeat > health_check_delay_s:
        if not proxy_connected:
            logging.info("creating a new proxy_content")
            proxy_client = proxy_context.socket(zmq.DEALER)
            proxy_client.setsockopt(zmq.IDENTITY, get_my_id_str().encode())
            proxy_client.connect("tcp://{0}:5570".format(PROXY_URL))
            logging.info("connection established")
        else:
            logging.info("checking if proxy server is still alive")
        last_proxy_heartbeat = time.time()
        init_ping_id = str(uuid.uuid4())
        proxy_client.send_multipart(
            [".....PING{}{}".format(get_my_id_str(), init_ping_id).encode()])
        proxy_connected = False
        proxy_server_timeout_s = 2
        poll_start_time = time.time()
        while time.time() - poll_start_time < proxy_server_timeout_s:
            try:
                raw_msg = proxy_client.recv_multipart(flags=zmq.NOBLOCK)
                proxy_connected = True
                break
            except zmq.Again:
                time.sleep(0.01)
        if not proxy_connected:
            logging.info("proxy server died, message dropped")
            proxy_client.disconnect("tcp://{0}:5570".format(PROXY_URL))
            logging.info("disconnected proxy_client")
            proxy_client.close()
            logging.info("closed proxy_client")
            return False
    return True
Beispiel #12
0
def sub_callback_laser_dots(results):
    global projection_matrixes
    claims = []
    claims.append({"type": "retract", "fact": [
        ["id", get_my_id_str()],
        ["id", "1"],
        ["postfix", ""],
    ]})
    for result in results:
        pt = project(projection_matrixes.get(str(result["cameraId"])), result["x"], result["y"])
        logging.info("DOT {} {} {} {}".format(result["x"], result["y"], pt[0], pt[1]))
        if pt[0] >= 0 and pt[1] >= 0 and pt[0] < DAYS_IN_WEEK and pt[1] < WEEKS_IN_CALENDAR:
            grid_x = int(pt[0])
            grid_y = int(pt[1])
            claims.append({"type": "claim", "fact": [
                ["id", get_my_id_str()],
                ["id", "1"],
                ["text", "laser"],
                ["text", "at"],
                ["text", "calendar"],
                ["text", str(grid_x)],
                ["text", str(grid_y)],
                ["text", "@"],
                ["text", str(result["t"])],
            ]})
    batch(claims)
def highlight_regions(results, subscription_id, r, g, b):
    global projection_matrixes
    claims = []
    claims.append({
        "type":
        "retract",
        "fact": [
            ["id", get_my_id_str()],
            ["id", subscription_id],
            ["postfix", ""],
        ]
    })
    highlighted_regions = {}
    for result in results:
        if result["regionId"] in highlighted_regions:
            continue  # Region is already highlighted
        camera_homography_matrix = projection_matrixes.get(
            str(result["cameraId"]), None)
        polygon = [
            project(camera_homography_matrix, result["x1"], result["y1"]),
            project(camera_homography_matrix, result["x2"], result["y2"]),
            project(camera_homography_matrix, result["x3"], result["y3"]),
            project(camera_homography_matrix, result["x4"], result["y4"]),
        ]
        highlighted_regions[result["regionId"]] = True
        ill = Illumination()
        ill.fill(r, g, b, 150)
        ill.nostroke()
        ill.polygon(polygon)
        claims.append(
            ill.to_batch_claim(get_my_id_str(), subscription_id,
                               result["displayId"]))
    batch(claims)
Beispiel #14
0
def highlight_regions(results, subscription_id, r, g, b):
    claims = []
    claims.append({
        "type":
        "retract",
        "fact": [
            ["id", get_my_id_str()],
            ["id", subscription_id],
            ["postfix", ""],
        ]
    })
    highlighted_regions = {}
    for result in results:
        if result["regionId"] in highlighted_regions:
            continue  # Region is already highlighted
        polygon = [
            project(LASER_CAMERA_ID, result["x1"], result["y1"]),
            project(LASER_CAMERA_ID, result["x2"], result["y2"]),
            project(LASER_CAMERA_ID, result["x3"], result["y3"]),
            project(LASER_CAMERA_ID, result["x4"], result["y4"]),
        ]
        highlighted_regions[result["regionId"]] = True
        ill = Illumination()
        ill.fill(r, g, b, 50)
        ill.nostroke()
        ill.polygon(polygon)
        claims.append(
            ill.to_batch_claim(get_my_id_str(), subscription_id, "global"))
    batch(claims)
Beispiel #15
0
def sub_callback_papers(results):
    global proxy_client
    if not check_and_connect_proxy_server():
        return
    logging.info("proxying message")
    claims = []
    claims.append({
        "type":
        "retract",
        "fact": [
            ["id", get_my_id_str()],
            ["id", "1"],
            ["postfix", ""],
        ]
    })
    for result in results:
        claims.append({
            "type":
            "claim",
            "fact": [
                ["id", get_my_id_str()],
                ["id", "1"],
                ["text", "camera"],
                ["integer", str(result["cameraId"])],
                ["text", "sees"],
                ["text", "paper"],
                ["integer", str(result["id"])],
                ["text", "at"],
                ["text", "TL"],
                ["text", "("],
                ["integer", str(result["x1"])],
                ["text", ","],
                ["integer", str(result["y1"])],
                ["text", ")"],
                ["text", "TR"],
                ["text", "("],
                ["integer", str(result["x2"])],
                ["text", ","],
                ["integer", str(result["y2"])],
                ["text", ")"],
                ["text", "BR"],
                ["text", "("],
                ["integer", str(result["x3"])],
                ["text", ","],
                ["integer", str(result["y3"])],
                ["text", ")"],
                ["text", "BL"],
                ["text", "("],
                ["integer", str(result["x4"])],
                ["text", ","],
                ["integer", str(result["y4"])],
                ["text", ")"],
                ["text", "@"],
                ["integer", str(result["time"])],
            ]
        })
    proxy_client.send_multipart(
        ["....BATCH{}{}".format(get_my_id_str(), json.dumps(claims)).encode()],
        zmq.NOBLOCK)
def sub_callback_calibration_points(results):
    global projection_matrixes, camera_to_display_map
    claims = [{
        "type": "retract",
        "fact": [["id", get_my_id_str()], ["id", "0"], ["postfix", ""]]
    }]
    if results and len(results) > 0:
        for result in results:
            src = np.float32([
                [0, 0],
                [SCREEN_REGION_WIDTH_PX, 0],
                [0, SCREEN_REGION_HEIGHT_PX],
                [SCREEN_REGION_WIDTH_PX,
                 SCREEN_REGION_HEIGHT_PX]  # notice the order is not clock-wise
            ])
            camera_homography_matrix = projection_matrixes.get(
                str(result["cameraId"]), None)
            if camera_homography_matrix is None:
                continue
            dst = np.float32([
                project(camera_homography_matrix, result["rx1"],
                        result["ry1"]),
                project(camera_homography_matrix, result["rx2"],
                        result["ry2"]),
                project(camera_homography_matrix, result["rx4"],
                        result["ry4"]),
                project(camera_homography_matrix, result["rx3"],
                        result["ry3"])  # notice the order is not clock-wise
            ])
            homography_matrix = cv2.getPerspectiveTransform(src, dst)
            claims.append({
                "type":
                "claim",
                "fact": [
                    ["id", get_my_id_str()],
                    ["id", "0"],
                    ["text", str(result["regionName"])],
                    ["integer", str(result["cameraId"])],
                    ["text", "calibration"],
                    ["text", "for"],
                    [
                        "text",
                        str(camera_to_display_map[str(result["cameraId"])])
                    ],
                    ["text", "is"],
                    ["float", str(homography_matrix[0][0])],
                    ["float", str(homography_matrix[0][1])],
                    ["float", str(homography_matrix[0][2])],
                    ["float", str(homography_matrix[1][0])],
                    ["float", str(homography_matrix[1][1])],
                    ["float", str(homography_matrix[1][2])],
                    ["float", str(homography_matrix[2][0])],
                    ["float", str(homography_matrix[2][1])],
                    ["float", str(homography_matrix[2][2])],
                ]
            })
    batch(claims)
Beispiel #17
0
def run_cv():
    global url, blob_images, debug_image
    claims = []
    claims.append({"type": "retract", "fact": [["id", get_my_id_str()], ["postfix", ""]]})
    if url is not None:
        try:
            pil_image = Image.open(requests.get(url, stream=True).raw)
            # run CV
            image = np.array(pil_image) 
            # Convert RGB to BGR 
            image = image[:, :, ::-1].copy() 
            image_grey = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
            ret, threshold_image = cv2.threshold(image_grey, THRESHOLD, 255, cv2.THRESH_BINARY)
            with lock:
                debug_image = threshold_image
            im2, raw_contours, hierarchy = cv2.findContours(threshold_image, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
            contours = []
            print("n raw contours: {}".format(len(raw_contours)))
            for (i, c) in enumerate(raw_contours):
                if hierarchy[0,i,3] == -1 and cv2.contourArea(c) > MIN_CONTOUR_AREA:
                    contours.append(c)
            contours = sorted(contours, key=lambda ctr: cv2.boundingRect(ctr)[0])
            blob_images = []
            chosen_contours = []
            for (i, c) in enumerate(contours[:MAX_N_IMAGES]):
                chosen_contours.append(c)
                box = cv2.minAreaRect(c)
                box = cv2.boxPoints(box)
                box = order_points(box)  # Order the points to (tl, tr, bl, br) to match the perspective transform dst
                contour_perspective = np.array(box, dtype="float32")
                dst = np.array([
                        [0, 0],
                        [CONTOUR_PERSPECTIVE_IMAGE_WIDTH - 1, 0],
                        [CONTOUR_PERSPECTIVE_IMAGE_WIDTH - 1, CONTOUR_PERSPECTIVE_IMAGE_HEIGHT - 1],
                        [0, CONTOUR_PERSPECTIVE_IMAGE_HEIGHT - 1]], dtype = "float32")
                contour_perspective_matrix = cv2.getPerspectiveTransform(contour_perspective, dst)
                contour_perspective_image = cv2.warpPerspective(image, contour_perspective_matrix, (CONTOUR_PERSPECTIVE_IMAGE_WIDTH, CONTOUR_PERSPECTIVE_IMAGE_HEIGHT))
                with lock:
                    blob_images.append(contour_perspective_image)
            # claim
            for i in range(len(blob_images)):
                claims.append({"type": "claim", "fact": [
                    ["id", get_my_id_str()],
                    ["id", "0"],
                    ["text", "camera"],
                    ["text", "sees"],
                    ["text", "subframe"],
                    ["text", str(i)],
                    ["text", "http://{}:{}/src/files/cv-frame-{}.jpg".format(get_host_ip(), PORT, i)],
                    ["text", "@"],
                    ["integer", str(int(time.time()*1000))],
                ]})
        except Exception as e:
            logging.error(traceback.format_exc())
    batch(claims)
def sub_callback_papers(results):
    papers = list(
        map(
            lambda p: ({
                "id":
                p["id"],
                "seenByCamera":
                p["cameraId"],
                "corners": [{
                    "x": p["x1"],
                    "y": p["y1"]
                }, {
                    "x": p["x2"],
                    "y": p["y2"]
                }, {
                    "x": p["x3"],
                    "y": p["y3"]
                }, {
                    "x": p["x4"],
                    "y": p["y4"]
                }]
            }), results))
    logging.info("--")

    WISKER_LENGTH = 150
    claims = []
    claims.append({
        "type": "retract",
        "fact": [
            ["id", get_my_id_str()],
            ["postfix", ""],
        ]
    })
    for paper in papers:
        other_paper = get_paper_you_point_at(papers, paper["id"],
                                             WISKER_LENGTH)
        logging.info("{} pointing at {}".format(paper["id"], other_paper))
        if other_paper is not None:
            claims.append({
                "type":
                "claim",
                "fact": [
                    ["id", get_my_id_str()],
                    ["id", "1"],
                    ["text", "paper"],
                    ["integer", str(paper["id"])],
                    ["text", "is"],
                    ["text", "pointing"],
                    ["text", "at"],
                    ["text", "paper"],
                    ["integer", str(other_paper)],
                ]
            })
    batch(claims)
Beispiel #19
0
def handle_key_event(e):
    claims = []
    claims.append({
        "type": "retract",
        "fact": [
            ["id", get_my_id_str()],
            ["postfix", ""],
        ]
    })
    ctrl_held = keyboard.is_pressed('ctrl')
    shift_held = keyboard.is_pressed('shift')
    if e.event_type == 'down':
        if e.name != 'unknown':
            if ctrl_held:
                if shift_held and e.name.isalpha():
                    claims.append(add_key(None, 'C-{}'.format(e.name.upper())))
                elif shift_held and e.name in shift_map:
                    claims.append(
                        add_key(None, 'C-{}'.format(shift_map[e.name])))
                elif e.name == '−':
                    claims.append(add_key(None, 'C--'.format(e.name)))
                else:
                    claims.append(add_key(None, 'C-{}'.format(e.name)))
            elif e.name in special_keys:
                claims.append(add_key(None, e.name))
            elif shift_held and e.name.isalpha():
                claims.append(add_key(e.name.upper(), None))
            elif shift_held and e.name in shift_map:
                claims.append(add_key(shift_map[e.name], None))
            elif e.name == '−':
                claims.append(add_key('-', None))
            else:
                claims.append(add_key(e.name, None))
    keys_to_claim_when_pressed = ["up", "right", "down", "left", "space"]
    for key_name in keys_to_claim_when_pressed:
        if keyboard.is_pressed(key_name):
            claims.append({
                "type":
                "claim",
                "fact": [
                    ["id", get_my_id_str()],
                    ["id", "0"],
                    ["text", "keyboard"],
                    ["text", get_my_id_str()],
                    ["text", "has"],
                    ["text", "pressed"],
                    ["text", "key"],
                    ["text", key_name],
                ]
            })
    batch(claims)
def add_key(key, special_key):
    global last_server_health_check, health_check_delay_s
    timestamp = int(time.time() * 1000.0)
    claims = []
    claims.append({
        "type": "retract",
        "fact": [
            ["id", get_my_id_str()],
            ["postfix", ""],
        ]
    })
    if special_key:
        logging.info("ADDING SPECIAL KEY {}".format(special_key))
        claims.append({
            "type":
            "claim",
            "fact": [
                ["id", get_my_id_str()],
                ["id", "0"],
                ["text", "keyboard"],
                ["text", get_my_id_str()],
                ["text", "typed"],
                ["text", "special"],
                ["text", "key"],
                ["text", str(special_key)],
                ["text", "@"],
                ["integer", str(timestamp)],
            ]
        })
    else:
        logging.info("ADDING KEY {}".format(key))
        claims.append({
            "type":
            "claim",
            "fact": [
                ["id", get_my_id_str()],
                ["id", "0"],
                ["text", "keyboard"],
                ["text", get_my_id_str()],
                ["text", "typed"],
                ["text", "key"],
                ["text", str(key)],
                ["text", "@"],
                ["integer", str(timestamp)],
            ]
        })
    batch(claims)
    if time.time() - last_server_health_check > health_check_delay_s:
        last_server_health_check = time.time()
        check_server_connection()
def prehook_init():
    global lock, cached_image
    claims = [
        {"type": "retract", "fact": [["id", get_my_id_str()], ["postfix", ""]]},
        {"type": "claim", "fact": [
            ["id", get_my_id_str()],
            ["id", "0"],
            ["text", "camera"],
            ["integer", str(CAMERA_ID)],
            ["text", "frame"],
            ["text", "at"],
            ["text", "http://{}:{}/src/files/{}".format(get_host_ip(), PORT, BOOT_FILENAME)],
        ]},
    ]
    frame = cv2.imread(os.path.join(os.path.dirname(os.path.dirname(__file__)), 'files/{}'.format(BOOT_FILENAME)))
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    aruco_dict = aruco.Dictionary_get(aruco.DICT_6X6_1000)
    arucoParameters = aruco.DetectorParameters_create()
    corners, ids, rejectedImgPoints = aruco.detectMarkers(
        gray, aruco_dict, parameters=arucoParameters)
    currentTimeMs = int(round(time.time() * 1000))
    if ids is not None and len(ids) > 0:
        for i, _tag_corners in enumerate(corners):
            tag_corners = _tag_corners[0]
            claims.append({"type": "claim", "fact": [
                ["id", get_my_id_str()],
                ["id", "1"],
                ["text", "camera"],
                ["integer", str(CAMERA_ID)],
                ["text", "sees"],
                ["text", "aruco"],
                ["integer", str(ids[i][0])],
                ["text", "at"],
                ["integer", str(int(tag_corners[0][0]))],
                ["integer", str(int(tag_corners[0][1]))],
                ["integer", str(int(tag_corners[1][0]))],
                ["integer", str(int(tag_corners[1][1]))],
                ["integer", str(int(tag_corners[2][0]))],
                ["integer", str(int(tag_corners[2][1]))],
                ["integer", str(int(tag_corners[3][0]))],
                ["integer", str(int(tag_corners[3][1]))],
                ["text", "@"],
                ["integer", str(currentTimeMs)]
            ]})
    debugFrame = aruco.drawDetectedMarkers(frame, corners)
    with lock:
        is_success, buffer = cv2.imencode(".jpg", debugFrame)
        cached_image = BytesIO(buffer)
    batch(claims)
def sub_callback_laser_dots(results):
    claims = []
    claims.append({
        "type":
        "retract",
        "fact": [
            ["id", get_my_id_str()],
            ["id", "1"],
            ["postfix", ""],
        ]
    })
    for result in results:
        polygon = [[int(result["x1"]), int(result["y1"])],
                   [int(result["x2"]), int(result["y2"])],
                   [int(result["x3"]), int(result["y3"])],
                   [int(result["x4"]), int(result["y4"])]]
        inside = point_inside_polygon(int(result["x"]), int(result["y"]),
                                      polygon)
        if inside:
            logging.info("laser {} {} inside region {}".format(
                result["x"], result["y"], result["regionId"]))
            claims.append({
                "type":
                "claim",
                "fact": [
                    ["id", get_my_id_str()],
                    ["id", "1"],
                    ["text", "laser"],
                    ["text", "in"],
                    ["text", "region"],
                    ["text", str(result["regionId"])],
                ]
            })
            claims.append({
                "type":
                "claim",
                "fact": [
                    ["id", get_my_id_str()],
                    ["id", "1"],
                    ["text", "laser"],
                    ["text", "in"],
                    ["text", "region"],
                    ["text", str(result["regionId"])],
                    ["text", "@"],
                    ["text", str(result["t"])],
                ]
            })
    batch(claims)
Beispiel #23
0
def show_scores():
    global PLAYER_REGIONS, SCORES
    logging.error("SHOWING SCORES")
    logging.error(SCORES)
    logging.error(PLAYER_REGIONS)
    claims = []
    claims.append({
        "type":
        "retract",
        "fact": [
            ["id", get_my_id_str()],
            ["id", "1"],
            ["postfix", ""],
        ]
    })
    for player_id in SCORES:
        if player_id in PLAYER_REGIONS and PLAYER_REGIONS[
                player_id] is not None:
            r = 255
            g = 0
            b = 0
            if player_id == "2":
                r = 0
                g = 0
                b = 255
            score_percentage = (1.0 * SCORES[player_id]) / (1.0 * MAX_SCORE)
            claims.append(
                draw_score_on_region(PLAYER_REGIONS[player_id],
                                     score_percentage, r, g, b, "1"))
            PLAYER_REGIONS[player_id]
    batch(claims)
Beispiel #24
0
def add_key(key, special_key):
    logging.info("{} - {}".format(key, special_key))
    timestamp = int(time.time() * 1000.0)
    claims = []
    claims.append({
        "type": "retract",
        "fact": [
            ["id", get_my_id_str()],
            ["postfix", ""],
        ]
    })
    if special_key:
        logging.info("ADDING SPECIAL KEY {}".format(special_key))
        claims.append({
            "type":
            "claim",
            "fact": [
                ["id", get_my_id_str()],
                ["id", "0"],
                ["text", "keyboard"],
                ["text", get_my_id_str()],
                ["text", "typed"],
                ["text", "special"],
                ["text", "key"],
                ["text", str(special_key)],
                ["text", "@"],
                ["integer", str(timestamp)],
            ]
        })
    else:
        logging.info("ADDING KEY {}".format(key))
        claims.append({
            "type":
            "claim",
            "fact": [
                ["id", get_my_id_str()],
                ["id", "0"],
                ["text", "keyboard"],
                ["text", get_my_id_str()],
                ["text", "typed"],
                ["text", "key"],
                ["text", str(key)],
                ["text", "@"],
                ["integer", str(timestamp)],
            ]
        })
    batch(claims)
Beispiel #25
0
 def claimProjectorCalibration(self):
     batch_claims = [{
         "type":
         "retract",
         "fact": [["id", get_my_id_str()], ["id", "0"], ["postfix", ""]]
     }]
     batch_claims.append({
         "type":
         "claim",
         "fact": [
             ["id", get_my_id_str()],
             ["id", "0"],
             ["text", "camera"],
             ["integer", "1"],
             ["text", "has"],
             ["text", "projector"],
             ["text", "calibration"],
             ["text", "TL"],
             ["text", "("],
             ["integer", str(self.projector_calibration[0][0])],
             ["text", ","],
             ["integer", str(self.projector_calibration[0][1])],
             ["text", ")"],
             ["text", "TR"],
             ["text", "("],
             ["integer", str(self.projector_calibration[1][0])],
             ["text", ","],
             ["integer", str(self.projector_calibration[1][1])],
             ["text", ")"],
             ["text", "BR"],
             ["text", "("],
             ["integer", str(self.projector_calibration[2][0])],
             ["text", ","],
             ["integer", str(self.projector_calibration[2][1])],
             ["text", ")"],
             ["text", "BL"],
             ["text", "("],
             ["integer", str(self.projector_calibration[3][0])],
             ["text", ","],
             ["integer", str(self.projector_calibration[3][1])],
             ["text", ")"],
             ["text", "@"],
             ["integer", str(int(round(time.time() * 1000)))],
         ]
     })
     batch(batch_claims)
def sub_callback_calibration_points(results):
    claims = [{
        "type": "retract",
        "fact": [["id", get_my_id_str()], ["id", "1"], ["postfix", ""]]
    }]
    if results and len(results) > 0:
        for result in results:
            src = np.float32([
                [result["rx1"], result["ry1"]],
                [result["rx2"], result["ry2"]],
                [result["rx4"], result["ry4"]],
                [result["rx3"],
                 result["ry3"]]  # notice the order is not clock-wise
            ])
            dst = np.float32([
                [result["sx1"], result["sy1"]],
                [result["sx2"], result["sy2"]],
                [result["sx4"], result["sy4"]],
                [result["sx3"],
                 result["sy3"]]  # notice the order is not clock-wise
            ])
            homography_matrix = cv2.getPerspectiveTransform(src, dst)
            claims.append({
                "type":
                "claim",
                "fact": [
                    ["id", get_my_id_str()],
                    ["id", "1"],
                    ["text", "camera"],
                    ["integer", str(result["cam"])],
                    ["text", "calibration"],
                    ["text", "for"],
                    ["text", str(result["display"])],
                    ["text", "is"],
                    ["float", str(homography_matrix[0][0])],
                    ["float", str(homography_matrix[0][1])],
                    ["float", str(homography_matrix[0][2])],
                    ["float", str(homography_matrix[1][0])],
                    ["float", str(homography_matrix[1][1])],
                    ["float", str(homography_matrix[1][2])],
                    ["float", str(homography_matrix[2][0])],
                    ["float", str(homography_matrix[2][1])],
                    ["float", str(homography_matrix[2][2])],
                ]
            })
    batch(claims)
def claim_connected_devices(connected_ble_devices):
    claims = [{
        "type": "retract",
        "fact": [["id", get_my_id_str()], ["id", "0"], ["postfix", ""]]
    }]
    for k in sorted(connected_ble_devices.keys()):
        claims.append({
            "type":
            "claim",
            "fact": [
                ["id", get_my_id_str()],
                ["id", "0"],
                ["text", "connected"],
                ["text", "to"],
                ["text", "device"],
                ["text", str(k)],
            ]
        })
    batch(claims)
def claim_camera_resolution():
    claims = [{
        "type": "retract",
        "fact": [["id", get_my_id_str()], ["postfix", ""]]
    }, {
        "type":
        "claim",
        "fact": [
            ["id", get_my_id_str()],
            ["id", "0"],
            ["text", "camera"],
            ["text", str(CAMERA_ID)],
            ["text", "has"],
            ["text", "resolution"],
            ["integer", str(CAM_WIDTH)],
            ["integer", str(CAM_HEIGHT)],
        ]
    }]
    batch(claims)
def claim_data(data):
    currentTimeMs = int(round(time.time() * 1000))
    claims = [{
        "type": "retract",
        "fact": [["id", get_my_id_str()], ["id", "0"], ["postfix", ""]]
    }]
    for (i, datum) in enumerate(data):
        retval, buffer = cv2.imencode('.jpg', datum)
        jpg_as_text = base64.b64encode(buffer)
        claims.append({
            "type":
            "claim",
            "fact": [["id", get_my_id_str()], ["id", "0"], ["text", "camera"],
                     ["text", "sees"], ["text", "subframe"],
                     ["integer", str(i)], ["text",
                                           str(jpg_as_text, "utf-8")],
                     ["text", "@"], ["integer", str(currentTimeMs)]]
        })
    batch(claims)
Beispiel #30
0
def room_claim(addr, batch_update_from_ble_device):
    print("TODO claim: {} {}".format(addr, batch_update_from_ble_device))
    batch([{
        "type":
        "claim",
        "fact": [
            ["id", get_my_id_str()],
            ["id", device.addr],
            ["text", batch_update_from_ble_device],
        ]
    }])