def __init__(self, *args, **kwargs):
        super(TestClient, self).__init__(*args, **kwargs)

        recording_id = 871
        frame_uuid = "44d25183-cc3e-4c8b-8661-8c286e71b2dc"
        host_url = "http://webplayer.horus.nu/"

        self.grid = Grid()
        self.request_builder = ImageRequestBuilder(recording_id, frame_uuid)
        self.stitcher = ImageProvider()
        self.client = Client(host_url)
 def test_filter_wrap_left(self):
     grid = Grid()
     sections = grid.filter(h_min=-44, h_max=44, w_min=-200, w_max=-36)
     result = {
         15, 8, 9, 10, 11,
         23, 16, 17, 18, 19
     }
     count = 0
     for section in sections:
         result.remove(section.index)
         count += 1
     self.assertEqual(count, 10)
     self.assertEqual(len(result), 0)
    def test_filter(self):
        grid = Grid()
        sections = grid.filter(h_min=-44, h_max=44, w_min=-170, w_max=-1)
        count = 0
        result = {
            8, 9, 10, 11,
            16, 17, 18, 19
        }
        for section in sections:
            count += 1
            result.remove(section.index)

        self.assertEqual(count, 8)
        self.assertEqual(len(result), 0)
 def test_filter_wrap_right(self):
     grid = Grid()
     sections = grid.filter(w_min=36, w_max=250)
     count = 0
     result = {
         4, 5, 6, 7, 0, 1,
         12, 13, 14, 15, 8, 9,
         20, 21, 22, 23, 16, 17,
         28, 29, 30, 31, 24, 25,
     }
     for section in sections:
         result.remove(section.index)
         count += 1
     self.assertEqual(count, 24)
     self.assertEqual(len(result), 0)
 def test_default_constructor(self):
     grid = Grid()
     count = 0
     result = set(range(32))
     for section in grid:
         result.remove(section.index)
         count += 1
     self.assertEqual(count, 32)
     self.assertEqual(len(result), 0)
]
try:
    connection_string = " ".join(
        map("=".join, filter(lambda x: x[1] != None, db_params)))
    connection = psycopg2.connect(connection_string)
except psycopg2.OperationalError as exception:
    logging.error(f"{exception} Connecting to database")
    exit()
try:
    client = Client(args.server)
except OSError as exception:
    logging.error(f"{exception}. Connecting to server {args.server}")
    exit()

frames = Frames(connection)
grid = Grid()
image_provider = ImageProvider(grid)


def compute_angle(frame, sign_location):
    camera_model = CameraModel(frame.get_location(), frame.heading)
    return -camera_model.look_at_angle(sign_location)


for location in args.target:
    print(f"Looking for {location}")
    results = []
    cursor = frames.query(within=(location, distance),
                          recordingid=recording_id,
                          limit=1)
    frame = Frame(cursor)
class TestClient(unittest.TestCase):

    def __init__(self, *args, **kwargs):
        super(TestClient, self).__init__(*args, **kwargs)

        recording_id = 871
        frame_uuid = "44d25183-cc3e-4c8b-8661-8c286e71b2dc"
        host_url = "http://webplayer.horus.nu/"

        self.grid = Grid()
        self.request_builder = ImageRequestBuilder(recording_id, frame_uuid)
        self.stitcher = ImageProvider()
        self.client = Client(host_url)

    def request_stitched_sections(self, sections, scale, path=None):
        requests = self.client.fetch_all(
            self.request_builder.build(Mode.panoramic, scale, section) for section in sections)

        result = self.stitcher.combine(requests, scale.size, scale.size)
        if path:
            with open(path, 'wb') as f:
                f.write(result.image.getvalue())
            result.image.close()
        return result

    def test_client_request_pano(self):
        request = self.client.fetch(self.request_builder.build(Mode.panoramic, Scales.Px_1024))
        with open('./tests/data/pano.jpg', "wb") as file:
            file.write(request.result())

    def test_client_request(self):
        sections = self.grid.filter(h_min=-44, h_max=44, w_min=-170, w_max=-1)
        self.request_stitched_sections(
            sections, Scales.Px_1024, './tests/data/stitched.jpg')

    def test_client_request_pano_stitched(self):
        result = self.request_stitched_sections(
            self.grid, Scales.Px_1024, './tests/data/pano_stitched.jpg')
        self.assertEqual(result.to_pixel_coordinates((0, 0)).x, 0)
        self.assertEqual(result.fov, Rect(
            x=-180.0, y=-90.0, width=360.0, height=180.0))


    def test_client_request_single_section_stitched(self):
        result = self.request_stitched_sections(
            (self.grid[3],), Scales.Px_1024, './tests/data/single_section_stitched.jpg')
        self.assertEqual(result.to_pixel_coordinates((170.0, 0)).x, 796)
        self.assertEqual(result.fov, Rect(
            x=-45.0, y=45.0, width=45.0, height=45.0))

    def test_client_request_left_wrapped(self):
        sections = self.grid.filter(h_min=-46, h_max=1, w_min=-270, w_max=-46)
        result = self.request_stitched_sections(
            sections, Scales.Px_1024, './tests/data/stitched_lw.jpg')
        self.assertEqual(result.to_pixel_coordinates((0, 0)).x, 0)

    def test_client_request_right_wrapped(self):
        sections = self.grid.filter(h_min=0, w_min=70, w_max=246)
        result = self.request_stitched_sections(
            sections, Scales.Px_1024, './tests/data/stitched_rw.jpg')
        self.assertEqual(result.to_pixel_coordinates((70.0, 0)).x, 1592)
        self.assertEqual(result.fov, Rect(
            x=-180.0, y=0.0, width=225.0, height=90.0))

    def test_client_request_left_right_wrapped(self):
        sections = self.grid.filter(w_min=-240, w_max=246)
        result = self.request_stitched_sections(
            sections, Scales.Px_1024, './tests/data/stitched_lrw.jpg')
        self.assertEqual(result.to_pixel_coordinates((-240.0, 0)).x, 2730)
        self.assertEqual(result.fov, Rect(
            x=-180.0, y=-90.0, width=180.0, height=180.0))
mode = Mode.panoramic
scale = Scales.Px_2048

# Get the image

request_builder = ImageRequestBuilder(frame.recordingid, frame.uuid)

#(min, max) yaw in deg
directions = {
    "front": (-45, 45),
    "back": (135, 225),
    "left": (-135, -45),
    "right": (45, 135),
}

grid = Grid()
for direction in directions:
    min_yaw = directions[direction][0]
    max_yaw = directions[direction][1]
    sections = grid.filter(h_min=-44, h_max=44, w_min=min_yaw, w_max=max_yaw)
    requests = client.fetch_all(
        request_builder.build(Mode.panoramic, scale, section)
        for section in sections)
    result = image_provider.combine(requests, scale.size, scale.size)

    # Save the file
    filename = ".\\panoramic_{}_{}.jpg".format(frame.index, direction)

    with open(filename, 'wb') as f:
        f.write(result.image.getvalue())
        result.image.close()