Esempio n. 1
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)
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)
Esempio n. 3
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)
Esempio n. 4
0
def draw_score_on_region(result, percentage, r, g, b, subscription_id):
    polygon = [
        project(LASER_CAMERA_ID, result["x1"], result["y1"]),
        project(LASER_CAMERA_ID, result["x2"], result["y2"]),
        project(LASER_CAMERA_ID,
                result["x2"] + (result["x3"] - result["x2"]) * percentage,
                result["y2"] + (result["y3"] - result["y2"]) * percentage),
        project(LASER_CAMERA_ID,
                result["x1"] + (result["x4"] - result["x1"]) * percentage,
                result["y1"] + (result["y4"] - result["y1"]) * percentage),
    ]
    ill = Illumination()
    ill.fill(r, g, b)
    ill.nostroke()
    ill.polygon(polygon)
    return ill.to_batch_claim(get_my_id_str(), subscription_id, "global")
def sub_callback_laser_dots(results):
    claims = []
    claims.append({
        "type":
        "retract",
        "fact": [
            ["id", get_my_id_str()],
            ["id", "1"],
            ["postfix", ""],
        ]
    })
    for result in results:
        laser_point = project(LASER_CAMERA_ID, result["x"], result["y"])
        ill = Illumination()
        ill.stroke(255, 255, 0, 100)
        ill.nofill()
        SIZE = 10
        ill.ellipse(laser_point[0] - SIZE, laser_point[1] - SIZE, SIZE * 2,
                    SIZE * 2)
        claims.append(ill.to_batch_claim(get_my_id_str(), "1", "global"))
    batch(claims)
    else:
        FRAMES = []

def check_for_new_frames():
    more_messages_to_receive = True
    while more_messages_to_receive:
        more_messages_to_receive = listen(blocking=False)

init(__file__, skipListening=True)

while True:
    start_time = time.time()
    batch_claims = [{"type": "retract", "fact": [
        ["id", get_my_id_str()],
        ["id", "0"],
        ["postfix", ""]
    ]}]
    if len(FRAMES) > 0:
        FRAME_INDEX += 1
        if FRAME_INDEX >= len(FRAMES):
            FRAME_INDEX = 0
        ill = Illumination()
        ill.image(0, 0, 100*8, 200*8, FRAMES[FRAME_INDEX])
        ill.fontcolor(255, 0, 0)
        ill.text(0, 0, "{}/{}".format(FRAME_INDEX, len(FRAMES)))
        batch_claims.append(ill.to_batch_claim(get_my_id_str(), "0"))
    batch(batch_claims)
    check_for_new_frames()
    sleep_time = max(DELAY_S_BETWEEN_FRAMES - (time.time() - start_time), 0.01)
    time.sleep(sleep_time)
Esempio n. 7
0
def sub_callback_graphics(results):
    claims = []
    claims.append({
        "type":
        "retract",
        "fact": [
            ["id", get_my_id_str()],
            ["id", "1"],
            ["postfix", ""],
        ]
    })
    if results:
        for result in results:
            camera_id = result["cam"]
            projection_matrix = [
                [1, 0, 0],
                [0, 1, 0],
                [0, 0, 1],
            ]
            if camera_id in projection_matrixes:
                projection_matrix = projection_matrixes[camera_id]
            dst = np.float32([
                project(camera_id, result["x1"], result["y1"]),
                project(camera_id, result["x2"], result["y2"]),
                project(camera_id, result["x4"], result["y4"]),
                # notice the order is not clock-wise
                project(camera_id, result["x3"], result["y3"])
            ])
            diameter = np.sqrt((result["x1"] - result["x3"])**2 +
                               (result["y1"] - result["y3"])**2) * 2
            cx = int(
                (result["x1"] + result["x2"] + result["x3"] + result["x4"]) /
                4)
            cy = int(
                (result["y1"] + result["y2"] + result["y3"] + result["y4"]) /
                4)
            angle = math.atan2(int(result["y1"] - result["y2"]),
                               int(result["x1"] - result["x2"]))
            ill = Illumination()
            ill.set_transform(
                projection_matrix[0][0],
                projection_matrix[0][1],
                projection_matrix[0][2],
                projection_matrix[1][0],
                projection_matrix[1][1],
                projection_matrix[1][2],
                projection_matrix[2][0],
                projection_matrix[2][1],
                projection_matrix[2][2],
            )
            ill.nofill()
            ill.stroke(255, 0, 0)
            ill.strokewidth(5)
            ill.ellipse(int(cx - diameter / 2), int(cy - diameter / 2),
                        diameter, int(diameter))
            ill.stroke(0, 255, 0)
            ill.line(
                cx + diameter * 0.5 * math.cos(angle),
                cy + diameter * 0.5 * math.sin(angle),
                cx + diameter * 0.5 * math.cos(angle) * 1.2,
                cy + diameter * 0.5 * math.sin(angle) * 1.2,
            )
            claims.append(
                ill.to_batch_claim(get_my_id_str(),
                                   "1",
                                   target=result["displayid"]))
            claims.append({
                "type":
                "claim",
                "fact": [
                    ["id", get_my_id_str()],
                    ["id", "1"],
                    ["text", "aruco"],
                    ["integer", str(result["tagId"])],
                    ["text", "has"],
                    ["text", "value"],
                    ["float", str(angle)],
                ]
            })
    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
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)
Esempio n. 10
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)
Esempio n. 11
0
def my_prehook():
    batch_claims = []
    batch_claims.append({"type": "retract", "fact": [
        ["id", get_my_id_str()],
        ["postfix", ""],
    ]})
    ill = Illumination()
    ill.push()
    ill.fill("red")
    ill.rect(0, 0, 50, 50)
    ill.fill(0, 0, 255)
    ill.strokewidth(5)
    ill.ellipse(0, 0, 50, 50)
    ill.fontcolor(255, 255, 0)
    ill.text(25, 25, "Hello World!")
    ill.fontcolor(100, 100, 100, 100)
    ill.text(25, 25, "          █")
    ill.nostroke()
    ill.rect(100, 10, 60, 60)
    ill.stroke("green")
    ill.nofill()
    ill.polygon([[200, 10], [300, 10], [250, 50]])
    ill.fill(255, 255, 255, 50)
    ill.polygon([[300, 10], [400, 10], [350, 50]])
    ill.pop()
    ill.push()
    ill.translate(50, 200)
    ill.line(0, 0, 100, 0)
    ill.pop()
    ill.push()
    ill.translate(200, 200)
    ill.rotate(math.pi / 4.0)
    ill.line(0, 0, 100, 0)
    ill.pop()
    ill.push()
    ill.translate(300, 200)
    ill.rotate(math.pi / 2.0)
    ill.line(0, 0, 200, 0)
    ill.pop()
    batch_claims.append(ill.to_batch_claim(get_my_id_str(), "0"))
    batch(batch_claims)
Esempio n. 12
0
def sub_callback(results):
    if not results:
        return
    start = time.time()
    result = results[0]
    frameb64 = result["frame"]
    frame_bytes = base64.b64decode(frameb64)
    npimg = np.fromstring(frame_bytes, dtype=np.uint8)
    frame = cv2.imdecode(npimg, 1)
    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)
    frame = aruco.drawDetectedMarkers(frame, corners)
    retval, buffer = cv2.imencode('.png', frame)
    png_as_text = base64.b64encode(buffer).decode('utf-8')
    batch_claims = []
    batch_claims.append({
        "type": "retract",
        "fact": [
            ["id", get_my_id_str()],
            ["postfix", ""],
        ]
    })
    ill = Illumination()
    ill.image(0, 0, 384, 216, png_as_text)
    ill.fontsize(100)
    ill.fontcolor(255, 0, 0)
    ill.text(0, 0, "{}".format(time.time()))
    batch_claims.append(ill.to_batch_claim(get_my_id_str(), "0"))
    batch(batch_claims)
    logging.error("Time to fully proccess and claim: {}".format(time.time() -
                                                                start))
    logging.error("Lag from message send: {} --- {}".format(
        time.time() - result["t"] / 1000, time.time()))
Esempio n. 13
0
def sub_callback_laser_dots(results):
    global lastLastPosition, MODE, CURRENT_CAMERA_TARGET
    claims = []
    claims.append({
        "type":
        "retract",
        "fact": [
            ["id", get_my_id_str()],
            ["id", "2"],
            ["postfix", ""],
        ]
    })
    if results and len(results) > 0:
        result = results[0]
        if MODE in ["0", "1", "2", "3"]:
            CURRENT_CAMERA_TARGET = str(result["cameraId"])
            lastLastPosition = [int(result["x"]), int(result["y"])]
            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(CURRENT_CAMERA_TARGET, p[0], p[1]),
                    poly))
            if len(poly) >= 3:
                ill.polygon(projected_poly)
            SIZE = 5
            for pt in projected_poly:
                ill.ellipse(pt[0] - SIZE, pt[1] - SIZE, SIZE * 2, SIZE * 2)
            draw_target = get_my_id_str()
            if CURRENT_CAMERA_TARGET in camera_to_display_map:
                draw_target = camera_to_display_map[CURRENT_CAMERA_TARGET]
            claims.append(ill.to_batch_claim(get_my_id_str(), "2",
                                             draw_target))
    else:
        lastLastPosition = None
    batch(claims)
Esempio n. 14
0
def sub_callback_laser_dots(results):
    claims = []
    claims.append({
        "type":
        "retract",
        "fact": [
            ["id", get_my_id_str()],
            ["id", "1"],
            ["postfix", ""],
        ]
    })
    laser_point_in_projector_space = None
    laser_point_in_dot_camera_space = None
    if results:
        logging.info("checking laser {} {} in papers".format(
            results[0]["x"], results[0]["x"]))
        laser_point_in_projector_space = project(LASER_CAMERA_ID,
                                                 results[0]["x"],
                                                 results[0]["y"])
        laser_point_in_dot_camera_space = project(
            DOTS_CAMERA_ID,
            laser_point_in_projector_space[0],
            laser_point_in_projector_space[1],
            inverse=True)
        logging.info("-- {} {} -> {} {} -> {} {}".format(
            results[0]["x"],
            results[0]["x"],
            laser_point_in_projector_space[0],
            laser_point_in_projector_space[1],
            laser_point_in_dot_camera_space[0],
            laser_point_in_dot_camera_space[1],
        ))
    highlighted_papers = {}
    for result in results:
        if result["paper"] in highlighted_papers:
            continue  # paper already highlighted
        polygon = [
            [result["x1"], result["y1"]],
            [result["x2"], result["y2"]],
            [result["x3"], result["y3"]],
            [result["x4"], result["y4"]],
        ]
        inside = point_inside_polygon(laser_point_in_dot_camera_space[0],
                                      laser_point_in_dot_camera_space[1],
                                      polygon)
        if inside:
            logging.info("paper {} is inside laser {} {}".format(
                result["paper"], result["x"], result["y"]))
            claims.append({
                "type":
                "claim",
                "fact": [
                    ["id", get_my_id_str()],
                    ["id", "1"],
                    ["text", "laser"],
                    ["text", "in"],
                    ["text", "paper"],
                    ["integer", str(result["paper"])],
                ]
            })
            highlighted_papers[result["paper"]] = True
            ill = Illumination()
            ill.fill(255, 255, 255, 100)
            ill.rect(0, 0, 1000, 1000)
            claims.append(
                ill.to_batch_claim(get_my_id_str(), "1", result["paper"]))
        # else:
        #     logging.info("paper {} is not inside laser {} {}".format(result["paper"], result["x"], result["y"]))
    batch(claims)