Example #1
0
 def __init__(self) -> None:
     self.overworld_raw_data = list(
         open("data/overworld-data.bin", 'rb').read(0x300))
     self.level_1_to_6_raw_data = list(
         open("data/level-1-6-data.bin", 'rb').read(0x300))
     self.level_7_to_9_raw_data = list(
         open("data/level-7-9-data.bin", 'rb').read(0x300))
     self.level_metadata = list(
         open("data/level-metadata.bin", 'rb').read(0x9D8))
     self.overworld_caves: List[Cave] = []
     self.level_1_to_6_rooms: List[Room] = []
     self.level_7_to_9_rooms: List[Room] = []
     self.sprite_set_patch = Patch()
     self.misc_data_patch = Patch()
Example #2
0
def generate_patches(x, y, split, balanced=False):
    for i in tqdm(range(20)):
        if split == 'train':
            I = np.array(
                Image.open('DRIVE/training/images/' + format(i + 21, '02d') +
                           '_training.tif'))
            J = np.array(
                Image.open('DRIVE/training/1st_manual/' +
                           format(i + 21, '02d') + '_manual1.tif'))
            mask = np.array(
                Image.open('DRIVE/training/mask/' + format(i + 21, '02d') +
                           '_training_mask.tif'))
        elif split == 'test':
            I = np.array(
                Image.open('DRIVE/test/images/' + format(i + 1, '02d') +
                           '_test.tif'))
            J = np.array(
                Image.open('DRIVE/test/1st_manual/' + format(i + 1, '02d') +
                           '_manual1.tif'))
            mask = np.array(
                Image.open('DRIVE/test/mask/' + format(i + 1, '02d') +
                           '_test_mask.tif'))
        for j in range(20000):
            index = i * 20000 + j
            try:
                label = None
                if balanced:
                    label = int(np.random.rand() < 0.5)
                patch = Patch(I, J, mask, size=x.shape[1], label=label)
                x[index] = patch.data
                y[index, patch.label] = 1
            except ValueError:
                j -= 1
Example #3
0
def update_tags(index, entries):
    """
    Update the Git-repo tag (possibly by removing it) of patches.
    """
    for entry in entries:
        with Patch(open(entry.name, mode="r+b")) as patch:
            message = "Failed to update tag \"%s\" in patch \"%s\". This " \
                    "tag is not found."
            if entry.dest_head == git_sort.remotes[0]:
                tag_name = "Patch-mainline"
                try:
                    patch.change(tag_name, index.describe(entry.dest.index))
                except KeyError:
                    raise exc.KSNotFound(message % (
                        tag_name,
                        entry.name,
                    ))
                except git_sort.GSError as err:
                    raise exc.KSError("Failed to update tag \"%s\" in patch "
                                      "\"%s\". %s" % (
                                          tag_name,
                                          entry.name,
                                          str(err),
                                      ))
                patch.remove("Git-repo")
            else:
                tag_name = "Git-repo"
                try:
                    patch.change(tag_name, repr(entry.new_url))
                except KeyError:
                    raise exc.KSNotFound(message % (
                        tag_name,
                        entry.name,
                    ))
Example #4
0
    def create_patch(self, replace_current = False):

        patch = Patch(self.rom, self.patch_range, self.patch_size, 'increment')
        if replace_current == True:
            self.patches[len(self.patches) - 1] = patch
        else:
            self.patches.append(patch)
Example #5
0
    def _GetPatchForOverworldCaveData(self) -> Patch:
        patch = Patch()
        for cave_type in Range.VALID_CAVE_TYPES:
            cave_num = int(cave_type) - self.CAVE_TYPE_CAVE_NUM_OFFSET
            if cave_type == CaveType.ARMOS_ITEM_VIRTUAL_CAVE:
                patch.AddData(
                    self.ARMOS_ITEM_ADDRESS,
                    [self.overworld_caves[cave_num].GetItemAtPosition(2)])
                continue
            if cave_type == CaveType.COAST_ITEM_VIRTUAL_CAVE:
                patch.AddData(
                    self.COAST_ITEM_ADDRESS,
                    [self.overworld_caves[cave_num].GetItemAtPosition(2)])
                continue

            # Note that the Cave class is responsible for protecting bits 6 and 7 in its item data
            patch.AddData(
                self.OVERWORLD_DATA_START_ADDRESS +
                self._GetOverworldCaveDataIndex(
                    cave_type, 0, is_second_byte=False),
                self.overworld_caves[cave_num].GetItemData())
            patch.AddData(
                self.OVERWORLD_DATA_START_ADDRESS +
                self._GetOverworldCaveDataIndex(
                    cave_type, 0, is_second_byte=True),
                self.overworld_caves[cave_num].GetPriceData())
        return patch
Example #6
0
def computePatch(feature, potentialFeature, referenceImage) : 
    sensedImage       = potentialFeature.getImage()
    opticalCentre1    = referenceImage.getOpticalCentre()
    projectionMatrix1 = referenceImage.getProjectionMatrix()
    projectionMatrix2 = sensedImage.getProjectionMatrix()
    centre            = triangulate(feature, potentialFeature, projectionMatrix1, projectionMatrix2)
    centre            = np.array([centre[0][0], centre[0][1], centre[0][2], centre[0][3]])
    normal            = opticalCentre1 - centre
    patch = Patch(centre, normal, referenceImage)

    # gridCoordinate1 = projectPatch(referenceImage.getProjectionMatrix(), patch)
    # gridCoordinate2 = projectPatch(sensedImage.getProjectionMatrix(), patch)
    # ref = cv.imread(referenceImage.getImageName())
    # fundamentalMatrix = referenceImage.getFundamentalMatrix(potentialFeature.getImage().getImageID())
    # coordinate        = np.array([
    #     feature.getX(), 
    #     feature.getY(),
    #     1
    # ])
    # epiline   = fundamentalMatrix @ coordinate
    # img = sensedImage.computeFeatureMapSatisfyingEpiline(epiline)
    # epiline_x = (int(-epiline[2] / epiline[0]), 0)
    # epiline_y = (int((-epiline[2] - (epiline[1]*480)) / epiline[0]), 480)
    # cv.line(img, epiline_x, epiline_y, (255, 0, 0), 1)
    # cv.circle(ref, (int(feature.getX()), int(feature.getY())), 4, (0, 0, 255), -1)
    # cv.rectangle(ref, (int(gridCoordinate1[0][0][0]), int(gridCoordinate1[0][0][1])), (int(gridCoordinate1[4][4][0]), int(gridCoordinate1[4][4][1])), (0, 255, 0), -1)
    # cv.rectangle(img, (int(gridCoordinate2[0][0][0]), int(gridCoordinate2[0][0][1])), (int(gridCoordinate2[4][4][0]), int(gridCoordinate2[4][4][1])), (0, 255, 0), -1)
    # cv.imshow(f'Reference Image', ref)
    # cv.imshow(f'Sensed Image', img)
    # cv.waitKey(0)
    # cv.destroyAllWindows()

    return patch
Example #7
0
    def load(self, filename):
        """
        Loads a surface from a text file.
        The file must have the following structure:
        Each line is a control point. It contains all 3 coordinates, separated by spaces only.
        The file must contain n * 16 control points, where n is a positive integer.
        """
        self.patches = []

        control_points = np.loadtxt(filename)
        nb_of_patches = int(len(control_points) / 16)

        patches_control_points = np.split(control_points, nb_of_patches)

        for patch_nb in range(len(patches_control_points)):
            patch = Patch()
            patch_control_points = patches_control_points[patch_nb]

            for i in range(4):
                for j in range(4):
                    z = patch_control_points[4 * i + j, 2]
                    y = patch_control_points[4 * i + j, 1]
                    x = patch_control_points[4 * i + j, 0]
                    patch.control_points[i, j] = np.array([x, y, z])
            self.patches.append(patch)
Example #8
0
 def processFile(self, github_file, committed_by):
     patch = Patch(patch_text=github_file['patch'],
                   filepath=github_file['filename'],
                   repo=self.repo,
                   account=self.user,
                   committed_by=committed_by)
     patch.updateTodos()
Example #9
0
 def create_patch(self) -> Patch:
     last = self.version
     self._increment_version()
     current = self.version
     patch = Patch(last, current)
     self.patches[last] = patch
     self.current_patch = patch
     return patch
Example #10
0
def setup():
    with open("input.txt") as f:
        for line in f:
            coord = line.split(',')
            x = int(coord[0])
            y = int(coord[1].strip())

            patches.append(Patch(x, y))
            usedPoints.append([x, y])
Example #11
0
 def ResetToVanilla(self) -> None:
     self._ReadOverworldData()
     self.level_1_to_6_rooms = self._ReadDataForLevelGrid(
         self.level_1_to_6_raw_data)
     self.level_7_to_9_rooms = self._ReadDataForLevelGrid(
         self.level_7_to_9_raw_data)
     self.level_metadata = list(
         open("data/level-metadata.bin", 'rb').read(0x9D8))
     self.sprite_set_patch = Patch()
Example #12
0
 def GetPatch(self) -> Patch:
     patch = Patch()
     patch += self._GetPatchForLevelGrid(
         self.LEVEL_1_TO_6_DATA_START_ADDRESS, self.level_1_to_6_rooms)
     patch += self._GetPatchForLevelGrid(
         self.LEVEL_7_TO_9_DATA_START_ADDRESS, self.level_7_to_9_rooms)
     patch += self._GetPatchForLevelMetadata()
     patch += self._GetPatchForOverworldCaveData()
     patch += self._GetPatchForOverworldData()
     patch += self.sprite_set_patch
     return patch
Example #13
0
    def _GetPatchForLevelGrid(self, start_address: int,
                              rooms: List[Room]) -> Patch:
        patch = Patch()
        for room_num in Range.VALID_ROOM_NUMBERS:
            room_data = rooms[room_num].GetRomData()
            assert len(room_data) == self.NUM_BYTES_OF_DATA_PER_ROOM

            for table_num in range(0, self.NUM_BYTES_OF_DATA_PER_ROOM):
                patch.AddData(
                    start_address + table_num * self.LEVEL_TABLE_SIZE +
                    room_num, [room_data[table_num]])
        return patch
Example #14
0
def computePatch(feat1, feat2, ref):
    logging.info(f'IMAGE {ref.id:02d}:Constructing patch.')
    img = feat2.image
    opticalCentre = ref.opticalCentre
    projectionMatrix1 = ref.projectionMatrix
    projectionMatrix2 = img.projectionMatrix
    centre = triangulate(feat1, feat2, projectionMatrix1, projectionMatrix2)[0]
    normal = opticalCentre - centre
    normal /= norm(normal)
    patch = Patch(centre, normal, ref)

    return patch
Example #15
0
    def evaluate_grid_masked(self, x0, y0, mask, fx, fy, derivs=False):
        '''
        mask: np array of booleans (NOT Patch object!)
        '''
        from mix import c_gauss_2d_masked

        h, w = mask.shape
        result = np.zeros((h, w), np.float32)
        xderiv = yderiv = None
        if derivs:
            xderiv = np.zeros_like(result)
            yderiv = np.zeros_like(result)

        # print 'gauss_2d_masked:', int(x0), int(y0), int(w), int (h), float(fx), float(fy),
        # print '  ', self.amp.astype(np.float32),
        # print '  ', self.mean.astype(np.float32),
        # print '  ', self.var.astype(np.float32),
        # print '  ', 'res', (result.shape, result.dtype),
        # if xderiv is not None:
        #     print '  ', 'xd', (xderiv.shape, xderiv.dtype),
        # if yderiv is not None:
        #     print '  ', 'yd', (yderiv.shape, yderiv.dtype),
        # print '  ', 'mask', mask.shape, mask.dtype

        rtn = c_gauss_2d_masked(int(x0), int(y0), int(w), int(h), float(fx),
                                float(fy), self.amp.astype(np.float32),
                                self.mean.astype(np.float32),
                                self.var.astype(np.float32), result, xderiv,
                                yderiv, mask)

        # print 'gauss_2d_masked returned.'

        assert (rtn == 0)
        if derivs:
            return (Patch(x0, y0,
                          result), Patch(x0, y0,
                                         xderiv), Patch(x0, y0, yderiv))
        return Patch(x0, y0, result)
Example #16
0
 def evaluate_grid_dstn(self, x0, x1, y0, y1, cx, cy):
     '''
     [x0,x1): (int) X values to evaluate
     [y0,y1): (int) Y values to evaluate
     (cx,cy): (float) pixel center of the MoG
     '''
     from mix import c_gauss_2d_grid
     assert(self.D == 2)
     result = np.zeros((y1-y0, x1-x0))
     rtn = c_gauss_2d_grid(x0, x1, y0, y1, cx, cy,
                           self.amp, self.mean,self.var, result)
     if rtn == -1:
         raise RuntimeError('c_gauss_2d_grid failed')
     return Patch(x0, y0, result)
Example #17
0
def find_commit(commit, series, mode="rb"):
    """
    Caller must chdir to where the entries in series can be found.
    """
    for name in filter_series(series):
        patch = Patch(open(name, mode="rb"))
        found = False
        if commit in [firstword(value)
                      for value in patch.get("Git-commit")
                      if value]:
            found = True
            yield name, patch
        patch.writeback()
        if found:
            return
    raise exc.KSNotFound()
Example #18
0
def generate_patches(scaled_imgs, constants, all_patches):
    """Generate patches for all scaled images."""
    patch_size = constants.PATCH_SIZE
    step = 1 if all_patches else 2
    patches = []
    for k, sc in enumerate(scaled_imgs):
        img_patches = []
        for i in range(0, sc.shape[0] - patch_size, step):
            for j in range(0, sc.shape[1] - patch_size, step):
                raw_patch = sc[i:i + patch_size, j:j + patch_size, :]
                patch = Patch(
                    raw_patch=raw_patch,
                    patch_size=patch_size,
                )
                patch.store(sc, [i, j])
                img_patches.append(patch)
        patches.append(img_patches)
    return patches
Example #19
0
def generate_patches(scaled_imgs, constants):
    patch_size = constants.PATCH_SIZE
    std_dev_threshold = constants.STD_DEV_THRESHOLD
    patches = []
    for k, sc in enumerate(scaled_imgs):
        img_patches = []
        for i in range(sc.shape[0] - patch_size):
            for j in range(sc.shape[1] - patch_size):
                raw_patch = sc[i:i + patch_size, j:j + patch_size, :]
                patch = Patch(
                    raw_patch=raw_patch,
                    patch_size=patch_size,
                )
                if patch.std_dev > std_dev_threshold:
                    patch.store(sc, [i, j])
                    img_patches.append(patch)
        patches.append(img_patches)
    return patches
Example #20
0
def find_commit(commit, series, mode="rb"):
    """
    commit: unabbreviated git commit id
    series: list of lines from series.conf
    mode: mode to open the patch files in, should be "rb" or "r+b"

    Caller must chdir to where the entries in series can be found.

    Returns patch.Patch instances
    """
    for name in filter_series(series):
        patch = Patch(open(name, mode=mode))
        found = False
        if commit in [
                firstword(value) for value in patch.get("Git-commit") if value
        ]:
            found = True
            yield name, patch
        patch.writeback()
        if found:
            return
    raise exc.KSNotFound()
Example #21
0
    def randomize(self,
                  patch_length,
                  patch_width,
                  min_height=0,
                  max_height=0.2):
        """
        Creates a random Bézier surface. The resulting surface will be C0 only.
        patch_length is the number of patches along the x axis that will be generated.
        patch_width is the number of patches along the y axis that will be generated.
        """
        self.patches = []
        for w, l in product(range(patch_width), range(patch_length)):
            # Patches are created and stored in a 1D array.
            # The patch at coordinate (x, y) can be retrieved as self.patches[x + y * patch_length]
            # However this structure is true ONLY in this generation fuction, as in general, if
            # we import another surface, it may not follow this particular pattern.
            # It is however useful in this generation, as it allows us to easily correct random
            # patches to make them c0, without checking each control points 1 by 1.
            patch = Patch()
            patch.randomize(min_x=l,
                            max_x=l + 1,
                            min_y=w,
                            max_y=w + 1,
                            min_z=min_height,
                            max_z=max_height)
            self.patches.append(patch)

        # make the connections continuous
        for l, w in product(range(1, patch_length), range(patch_width)):
            previous_patch = self.patches[(l - 1) + w * patch_length]
            current_patch = self.patches[l + w * patch_length]
            for i in range(4):
                current_patch[0, i] = previous_patch[3, i]

        for l, w in product(range(patch_length), range(1, patch_width)):
            previous_patch = self.patches[l + (w - 1) * patch_length]
            current_patch = self.patches[l + w * patch_length]
            for i in range(4):
                current_patch[i, 0] = previous_patch[i, 3]
Example #22
0
def get_patches_from_slide(slide, tile_size=1024, overlap=0, limit_bounds=False):
    """ 
    Splits an OpenSlide object into nonoverlapping patches

    Args:
        slide: OpenSlide object
        tile_size: Width and height of a single tile
        overlap: Number of extra pixels to add to each interior edge of a tile
        limit_bounds: If True, renders only non-empty slide region
    Returns:
        Array of patches
    """

    tiles = DeepZoomGenerator(slide, tile_size=tile_size, overlap=overlap, 
                limit_bounds=limit_bounds) 

    level = len(tiles.level_tiles) - 1
    x_tiles, y_tiles = tiles.level_tiles[level] #Note: Highest level == Highest resolution
    x, y = 0, 0
    count, batch_count = 0, 0
    patches = []
    coordinate_list = []
    tiled_dims = (y_tiles, x_tiles)

    while y < y_tiles:
        while x < x_tiles:
            new_patch_img = np.array(tiles.get_tile(level, (x,y)), dtype=np.uint8)
            new_patch_coords = tiles.get_tile_coordinates(level, (x,y))
            if np.shape(new_patch_img) == (tile_size, tile_size, 3):
                new_patch = Patch(new_patch_img, new_patch_coords)
                patches.append(new_patch)
                patch_coordinates = (y,x)
                coordinate_list.append(patch_coordinates)
                count += 1
            x += 1
        y += 1
        x = 0
    return (patches, coordinate_list, tiled_dims) 
Example #23
0
 def expand(self):
     it = 1
     queue = self.patches.copy()
     while len(queue) > 0:
         p = queue.pop(0)
         for cell in p.cells:
             C = []
             for i in range(-1, 2):
                 for j in range(-1, 2):
                     if i == 0 and j == 0:
                         continue
                     idx = (cell.y + i * 2, cell.x + j * 2)
                     if idx in self.cells[cell.ref_idx]:
                         C.append(self.cells[cell.ref_idx][idx])
             
             # cp = p.cam_center + p.k * p.d
             # p_depth = (self.extrinsics[p.ref_idx] @ np.array([*cp, 1]))[-1]
             C_filtered = []
             for c in C:
                 neccessary = True
                 if len(c.q) > 0:
                     for pp in c.q:
                         # cpp = pp.cam_center + pp.k * pp.d
                         # pp_depth = (self.extrinsics[pp.ref_idx] @ np.array([*cpp, 1]))[-1]
                         # rho1 = 2 * self.intrinsic_inv[0, 0] * (p_depth + pp_depth) / 2
                         # if np.abs(np.dot(cp - cpp, sph2norm(p.theta, p.phi))) + np.abs(np.dot(cp - cpp, sph2norm(pp.theta, pp.phi))) < 2 * rho1:
                         if self.is_neighbor(p, pp):
                             neccessary = False
                             break
                     
                 if neccessary and len(c.q_star) > 0:
                     for pp in c.q_star:
                         if self.gp(pp, pp.ref_idx, pp.v_star) < 0.3:
                             neccessary = False
                             break
                         
                 if neccessary:
                     C_filtered.append(c)
             
             C = C_filtered
             # print(len(C))
             for c in C:
                 pprime = Patch(patch=p)
                 
                 # set new pc
                 n = sph2norm(p.theta, p.phi)
                 p_center = p.cam_center + p.k * p.d
                 plane = -np.dot(n, p_center)
                 pt = np.array([c.x + 1, c.y + 1, 1])
                 pt_world = np.linalg.pinv(self.extrinsics[pprime.ref_idx]) @ self.intrinsic_inv @ pt
                 pt_world = pt_world[:3] / pt_world[-1]
                 cam2pt = pt_world - p.cam_center
                 pprime_center = -(plane + np.dot(n, p.cam_center)) / np.dot(n, cam2pt) * cam2pt + p.cam_center
                 # print(p_center)
                 # print(pprime_center)
                 pprime.d = pprime_center - p.cam_center
                 pprime.k = np.linalg.norm(pprime.d)
                 pprime.d /= pprime.k
                 
                 # import pdb; pdb.set_trace()
                 
                 proj_ref = pprime.project_onto_img(self.intrinsic, self.extrinsics[pprime.ref_idx])
                 v_star = []
                 for cand in pprime.v:
                     proj_cand = pprime.project_onto_img(self.intrinsic, self.extrinsics[cand])
                     zncc = calc_zncc(self.grays[pprime.ref_idx], self.grays[cand], proj_ref, proj_cand)
                     if 1. - zncc < 0.6:
                         v_star.append(cand)
                 pprime.v_star = v_star
                 if len(pprime.v_star) < 2:
                     continue
                 
                 def gp(x):
                     backup = pprime.k, pprime.theta, pprime.phi
                     pprime.k, pprime.theta, pprime.phi = x
                     res = self.gp(pprime, pprime.ref_idx, pprime.v_star)
                     pprime.k, pprime.theta, pprime.phi = backup
                     return res
                 
                 def gp_grad(x):
                     backup = pprime.k, pprime.theta, pprime.phi
                     pprime.k, pprime.theta, pprime.phi = x
                     res = self.gp_grad(pprime, pprime.ref_idx, pprime.v_star)
                     pprime.k, pprime.theta, pprime.phi = backup
                     return res
                 
                 # print(patch.k, patch.theta, patch.phi)
                 best_x = optimize.fmin_cg(gp, np.array([pprime.k, pprime.theta, pprime.phi]), fprime=gp_grad, disp=False)
                 pprime.k, pprime.theta, pprime.phi = best_x
                 
                 # determine depth test threshold
                 n = sph2norm(pprime.theta, pprime.phi)
                 pprime_center = pprime.cam_center + pprime.k * pprime.d
                 plane = -np.dot(n, pprime_center)
                 pt = np.array([c.x + 3, c.y + 1, 1])  # offset two pixels
                 pt_world = np.linalg.pinv(self.extrinsics[pprime.ref_idx]) @ self.intrinsic_inv @ pt
                 pt_world = pt_world[:3] / pt_world[-1]
                 cam2pt = pt_world - pprime.cam_center
                 delta = np.linalg.norm(-(plane + np.dot(n, pprime.cam_center)) / np.dot(n, cam2pt) * cam2pt + pprime.cam_center - pprime_center)
                 pprime_depth = (self.extrinsics[pprime.ref_idx] @ np.array([*pprime_center, 1]))[-1]
                 
                 for cand in range(len(self.grays)):
                     if cand == pprime.ref_idx:
                         continue
                     proj = self.intrinsic @ self.extrinsics[cand] @ np.array([*(pprime.cam_center + pprime.k * pprime.d), 1]) 
                     proj = (proj[:2] / proj[-1]).astype(np.int)
                     if (proj[1], proj[0]) in self.cells[cand]:
                         if len(self.cells[cand][(proj[1], proj[0])].q) > 0:
                             valid = True
                             for pp in self.cells[cand][(proj[1], proj[0])].q:
                                 pp = self.cells[cand][(proj[1], proj[0])].q[0]
                                 pp_proj = self.extrinsics[pp.ref_idx] @ np.array([*(pp.cam_center + pp.k * pp.d), 1])
                                 depth = pp_proj[-1]
                                 if pprime_depth > depth + delta:
                                     valid = False
                                     break
                             if valid:
                                 pprime.v.append(cand)
                 
                 pprime.v = list(set(pprime.v))
                 v_star = []
                 for cand in pprime.v:
                     proj_cand = pprime.project_onto_img(self.intrinsic, self.extrinsics[cand])
                     zncc = calc_zncc(self.grays[pprime.ref_idx], self.grays[cand], proj_ref, proj_cand)
                     if 1. - zncc < 0.3:
                         v_star.append(cand)
                 pprime.v_star = list(set(v_star))
                 
                 if len(pprime.v_star) < 2:
                     continue
                 
                 queue.append(pprime)
                 self.patches.append(pprime)
                 for idx in pprime.v:
                     proj = self.intrinsic @ self.extrinsics[idx] @ np.array([*pprime_center, 1]) 
                     proj = (proj[:2] / proj[-1]).astype(np.int)
                     if (proj[1], proj[0]) in self.cells[idx]:
                         self.cells[idx][(proj[1], proj[0])].q.append(pprime)
                     
                 for idx in pprime.v_star:
                     proj = self.intrinsic @ self.extrinsics[idx] @ np.array([*pprime_center, 1]) 
                     proj = (proj[:2] / proj[-1]).astype(np.int)
                     if (proj[1], proj[0]) in self.cells[idx]:
                         self.cells[idx][(proj[1], proj[0])].q_star.append(pprime)
                         pprime.cells.append(self.cells[idx][(proj[1], proj[0])])
         
                 print('processed one: {}, current queue length: {}'.format(len(self.patches), len(queue)))
                 
                 colors = np.zeros((len(self.patches), 3))
                 vis = self.rgbs[0].copy()
                 for i, patch in enumerate(self.patches):
                     pts = patch.project_onto_img(self.intrinsic, self.extrinsics[0])
                     pt = pts[pts.shape[0] // 2].astype(np.int)
                     if pt[0] >= 0 and pt[1] >= 0 and pt[0] < self.grays[0].shape[1] and pt[1] < self.grays[0].shape[0]:
                         vis = cv2.circle(vis, (int(pt[0]), int(pt[1])), 1, color=(255, 0, 0), thickness=-1)
                         colors[i] = self.rgbs[0][pt[1], pt[0]]
                 cv2.imshow('patch', cv2.resize(vis[..., ::-1], (0, 0), fx=4, fy=4))
                 cv2.waitKey(10)
Example #24
0
        os.environ["GIT_DIR"] = repo_path
    repo = pygit2.Repository(repo_path)
    try:
        commit = repo.revparse_single(args.rev)
    except ValueError:
        print("Error: \"%s\" is not a valid revision." % (args.rev, ),
              file=sys.stderr)
        sys.exit(1)
    except KeyError:
        print("Error: revision \"%s\" not found in \"%s\"." %
              (args.rev, repo_path),
              file=sys.stderr)
        sys.exit(1)

    if args.followup:
        with Patch(io.BytesIO(commit.message.encode())) as patch:
            try:
                fixes = series_conf.firstword(patch.get("Fixes")[0])
            except IndexError:
                print("Error: no \"Fixes\" tag found in commit \"%s\"." %
                      (str(commit.id)[:12]),
                      file=sys.stderr)
                sys.exit(1)
        fixes = str(repo.revparse_single(fixes).id)

        series = open("series")
        cwd = os.getcwd()
        os.chdir("patches")
        try:
            with series_conf.find_commit(fixes, series) as (
                    name,
Example #25
0
    def matching(self):
        feature_points_mask = [np.ones((self.feature_points[i].shape[0]), dtype=np.bool) for i in range(len(self.grays))]
        for i in range(len(self.grays) - 1):
            for camera_point in tqdm(self.camera_feature_points[i][feature_points_mask[i]]):
                corrs = []
                for j in range(len(self.grays)):
                    if i == j:
                        continue
                    epiline = self.intrinsic_inv.T @ self.Fs[(i, j)] @ camera_point
                    epiline /= np.linalg.norm(epiline[:2])
                    dist2epiline = np.abs(np.sum(self.feature_points[j][feature_points_mask[j]] * epiline[None], -1))
                    close_pts = self.camera_feature_points[j][feature_points_mask[j]][dist2epiline < 3]
                    
                    for pt in close_pts:
                        c = np.asarray(opt_triangulate([self.extrinsics[i], self.extrinsics[j]], [[camera_point[0], camera_point[1], pt[0], pt[1]]])[0])
                        c = c[:3]
                        corrs.append((j, pt, c, np.linalg.norm(c - self.camera_centers[i])))
                

                    # if len(close_pts) > 10:
                    #     img = self.rgbs[j].copy()
                    #     img_ref = self.rgbs[i].copy()
                    #     cv2.circle(img_ref, (self.feature_points[i][k][0], self.feature_points[i][k][1]), 3, color=(255, 0, 0), thickness=-1)
                    #     for point in close_pts:
                    #         img = cv2.circle(img, (int(point[0]), int(point[1])), 1, color=(255, 0, 0), thickness=-1)
                        
                    #     cv2.imshow('img', img[:, :, ::-1])
                    #     cv2.imshow('img_ref', img_ref[:, :, ::-1])
                    #     cv2.waitKey()
                        
                corrs.sort(key=lambda x: x[3])
                for (j, pt, c, dist) in corrs:
                    patch = Patch(c, self.intrinsic_inv, self.extrinsics[i], i)
                    
                    # first round
                    proj_i = patch.project_onto_img(self.intrinsic, self.extrinsics[i])

                    c = patch.cam_center + patch.k * patch.d
                    co = c[None] - np.stack(self.camera_centers)
                    co /= np.linalg.norm(co, axis=-1, keepdims=True)
                    cos = co @ sph2norm(patch.theta, patch.phi)
                    vp = list(np.where(cos > 0.5)[0])
                    
                    v_star = []
                    for cand in vp:
                        proj_cand = patch.project_onto_img(self.intrinsic, self.extrinsics[cand])
                        zncc = calc_zncc(self.grays[i], self.grays[cand], proj_i, proj_cand)
                        
                        if 1. - zncc < 0.6:
                            v_star.append(cand)
                    
                    if len(v_star) < 2:
                        continue
                    
                    def gp(x):
                        backup = patch.k, patch.theta, patch.phi
                        patch.k, patch.theta, patch.phi = x
                        res = self.gp(patch, patch.ref_idx, v_star)
                        patch.k, patch.theta, patch.phi = backup
                        return res
                    
                    def gp_grad(x):
                        backup = patch.k, patch.theta, patch.phi
                        patch.k, patch.theta, patch.phi = x
                        res = self.gp_grad(patch, patch.ref_idx, v_star)
                        patch.k, patch.theta, patch.phi = backup
                        return res
                    
                    # print(patch.k, patch.theta, patch.phi)
                    best_x = optimize.fmin_cg(gp, np.array([patch.k, patch.theta, patch.phi]), fprime=gp_grad, disp=False)
                    patch.k, patch.theta, patch.phi = best_x
                    # print(patch.k, patch.theta, patch.phi)
                    
                    # conjugate gradient descent my implementation
                    # last_grad = None
                    # for it in range(100): 
                    #     if it == 0:
                    #         last_grad = gp_grad()
                    #         p = -last_grad
                    #         last_p = p
                    #     else:
                    #         beta = np.dot(last_grad, last_grad) / np.dot(last_last_grad, last_last_grad)
                    #         p = -last_grad + beta * last_p
                            
                    #     gp_before = gp()
                    #     grad_before = gp_grad()
                        
                    #     alpha = 1.
                    #     k, theta, phi = patch.k, patch.theta, patch.phi
                    #     patch.k = float(k + alpha * p[0])
                    #     patch.theta = float(theta + alpha * p[1])
                    #     patch.phi = float(phi + alpha * p[2])
                    #     patch.refresh()
                    #     while gp() > gp_before + 1 / 2 * alpha * np.dot(p, grad_before):
                    #         alpha *= 0.5
                    #         patch.k = float(k + alpha * p[0])
                    #         patch.theta = float(theta + alpha * p[1])
                    #         patch.phi = float(phi + alpha * p[2])
                    #         patch.refresh()
                        
                    #     last_p = p
                    #     last_last_grad = last_grad
                    #     last_grad = gp_grad()
                    #     if np.any(np.isnan(last_grad)):
                    #         import pdb; pdb.set_trace()
                    #     if np.linalg.norm(last_grad) < 0.1:
                    #         break
                    
                    
                        
                    # second round
                    proj_i = patch.project_onto_img(self.intrinsic, self.extrinsics[i])
                    
                    c = patch.cam_center + patch.k * patch.d
                    co = c[None] - np.stack(self.camera_centers)
                    co /= np.linalg.norm(co, axis=-1, keepdims=True)
                    cos = co @ sph2norm(patch.theta, patch.phi)
                    vp = list(np.where(cos > 0.5)[0])
                    
                    v_star = []
                    for cand in vp:
                        proj_cand = patch.project_onto_img(self.intrinsic, self.extrinsics[cand])
                        zncc = calc_zncc(self.grays[i], self.grays[cand], proj_i, proj_cand)
                        if 1. - zncc < 0.3:
                            v_star.append(cand)
                    
                    if len(v_star) < 2:
                        continue
                    
                    patch.v = vp
                    patch.v_star = v_star
                    
                    to_remove = []
                    for idx in vp:
                        proj = self.intrinsic @ self.extrinsics[idx] @ np.array([*c, 1]) 
                        proj = (proj[:2] / proj[-1]).astype(np.int)
                        if (proj[1], proj[0]) in self.cells[idx]:
                            self.cells[idx][(proj[1], proj[0])].q.append(patch)
                        
                    for idx in v_star:
                        proj = self.intrinsic @ self.extrinsics[idx] @ np.array([*c, 1]) 
                        proj = (proj[:2] / proj[-1]).astype(np.int)
                        if (proj[1], proj[0]) in self.cells[idx]:
                            self.cells[idx][(proj[1], proj[0])].q_star.append(patch)
                            patch.cells.append(self.cells[idx][(proj[1], proj[0])])
                            for pt_idx in self.cells[idx][(proj[1], proj[0])].feature_points_idx:
                                to_remove.append((idx, pt_idx))
                        
                    for (idx, pt_idx) in to_remove:
                        feature_points_mask[idx][pt_idx] = False
                        
                    self.patches.append(patch)
        
        colors = np.zeros((len(self.patches), 3))
        vis = self.rgbs[0].copy()
        for i, patch in enumerate(self.patches):
            pts = patch.project_onto_img(self.intrinsic, self.extrinsics[0])
            pt = pts[pts.shape[0] // 2].astype(np.int)
            if pt[0] >= 0 and pt[1] >= 0 and pt[0] < self.grays[0].shape[1] and pt[1] < self.grays[0].shape[0]:
                vis = cv2.circle(vis, (int(pt[0]), int(pt[1])), 1, color=(255, 0, 0), thickness=-1)
                colors[i] = self.rgbs[0][pt[1], pt[0]]
        cv2.imshow('patch', cv2.resize(vis[..., ::-1], (0, 0), fx=4, fy=4))
        draw_patches(self.patches, colors)
Example #26
0
def main():
    args = parse_command_line()

    root_dir = os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), '..'))

    config.config_init(args.version, args.revision, args.version_name,
                       args.sdk_root_path, args.sdk_version,
                       root_dir)

    sdk = Sdk(config.ARTIFACTS_ROOT, config.SDK_VERSION)

    # Assumes that source root dir is .. since build.py is ran in directory build_scripts
    patch_dir = os.path.join(root_dir, 'patches')

    try:
        if args.clean:
            clean.clean_all(sdk)

        if args.create_patch_filename:
            sdk.prepare_sdk()
            old_path = sdk.path

            tempdir_path = None

            try:
                tempdir_path = tempfile.mkdtemp(prefix='nordicsemi')
                sdk.path = os.path.join(tempdir_path, "orig_sdk")
                sdk.prepare_sdk()
                p = Patch(patch_dir=patch_dir,
                          apply_patch_root_dir=sdk.path,
                          strip=0  # The old patches has 4 more components
                          )
                p.create_patch(tempdir_path, old_path, os.path.join(patch_dir, args.create_patch_filename))
            finally:
                sdk.path = old_path
                shutil.rmtree(tempdir_path)

        if args.dependencies:
            sdk.prepare_sdk()
            patch_tag_file = posixpath.join(sdk.path, ".patched")

            if os.path.exists(patch_tag_file):
                logger.info("Patches are already applied to this SDK, skipping patching")
            else:
                open(patch_tag_file, 'w').close()
                p = Patch(patch_dir=patch_dir,
                          apply_patch_root_dir=sdk.path,
                          strip=0  # The old patches has 4 more components
                          )
                p.apply_patches(dry_run=False)

        if args.build:
            serialization_dll.build()

        if args.test:
            error_code = tests.do_testing()

            if error_code != 0:
                return error_code

        if args.package:
            package_release()

        if args.examples:
            examples.build_examples()

    except Exception, ex:
        logger.exception(ex)
        return -1
Example #27
0
    def from_patch(self, index, name, current_head, move_upstream):
        """
        This is where we decide a patch line's fate in the sorted series.conf
        The following factors determine how a patch is sorted:
        * commit found in index
        * patch's series.conf current_head is indexed (ie. the local repo
          fetches from that remote)
        * patch appears to have moved downstream/didn't move/upstream
        * patch's tag is good ("Git-repo:" == current_head.url)
        * patches may be moved upstream between subsystem sections
        """
        self.name = name
        if not os.path.exists(name):
            raise exc.KSError("Could not find patch \"%s\"" % (name, ))

        with Patch(open(name, mode="rb")) as patch:
            commit_tags = patch.get("Git-commit")
            repo_tags = patch.get("Git-repo")

        if not commit_tags:
            self.dest_head = git_sort.oot
            return

        class BadTag(Exception):
            pass

        def get_commit(value):
            if not value:
                raise BadTag(value)
            tag = series_conf.firstword(value)
            if not self.commit_match.match(tag):
                raise BadTag(tag)
            return tag

        try:
            self.revs = [get_commit(value) for value in commit_tags]
        except BadTag as e:
            raise exc.KSError("Git-commit tag \"%s\" in patch \"%s\" is not a "
                              "valid revision." % (
                                  e.args[0],
                                  name,
                              ))
        rev = self.revs[0]

        if len(repo_tags) > 1:
            raise exc.KSError("Multiple Git-repo tags found. Patch \"%s\" is "
                              "tagged improperly." % (name, ))
        elif repo_tags:
            repo = git_sort.RepoURL(repo_tags[0])
        elif commit_tags:
            repo = git_sort.remotes[0].repo_url
        self.new_url = None

        try:
            ic = index.lookup(rev)
        except git_sort.GSKeyError:  # commit not found
            if current_head not in index.repo_heads:  # repo not indexed
                if repo == current_head.repo_url:  # good tag
                    self.dest_head = current_head
                else:  # bad tag
                    raise exc.KSError(
                        "There is a problem with patch \"%s\". "
                        "The Git-repo tag is incorrect or the patch is in the "
                        "wrong section of series.conf and (the Git-commit tag "
                        "is incorrect or the relevant remote is outdated or "
                        "not available locally) or an entry for this "
                        "repository is missing from \"remotes\". In the last "
                        "case, please edit \"remotes\" in "
                        "\"scripts/git_sort/git_sort.py\" and commit the "
                        "result. Manual intervention is required." % (name, ))
            else:  # repo is indexed
                if repo == current_head.repo_url:  # good tag
                    raise exc.KSError(
                        "There is a problem with patch \"%s\". "
                        "Commit \"%s\" not found in git-sort index. "
                        "The remote fetching from \"%s\" needs to be fetched "
                        "or the Git-commit tag is incorrect or the patch is "
                        "in the wrong section of series.conf. Manual "
                        "intervention is required." % (
                            name,
                            rev,
                            current_head.repo_url,
                        ))
                else:  # bad tag
                    raise exc.KSError(
                        "There is a problem with patch \"%s\". "
                        "The Git-repo tag is incorrect or the patch is in the "
                        "wrong section of series.conf. Manual intervention is "
                        "required." % (name, ))
        else:  # commit found
            msg_bad_tag = "There is a problem with patch \"%s\". " \
                    "The Git-repo tag is incorrect or the patch is in " \
                    "the wrong section of series.conf. Manual " \
                    "intervention is required." % (name,)
            if current_head not in index.repo_heads:  # repo not indexed
                if ic.head > current_head:  # patch moved downstream
                    if repo == current_head.repo_url:  # good tag
                        self.dest_head = current_head
                    else:  # bad tag
                        raise exc.KSError(msg_bad_tag)
                elif ic.head == current_head:  # patch didn't move
                    raise exc.KSException(
                        "Head \"%s\" is not available locally but commit "
                        "\"%s\" found in patch \"%s\" was found in that head."
                        % (
                            ic.head,
                            rev,
                            name,
                        ))
                elif ic.head < current_head:  # patch moved upstream
                    if move_upstream:  # move patches between subsystem sections
                        self.dest_head = ic.head
                        self.dest = ic
                        if repo != ic.head.repo_url:  # bad tag
                            self.new_url = ic.head.repo_url
                    else:  # do not move patches between subsystem sections
                        if repo == current_head.repo_url:  # good tag
                            self.dest_head = current_head
                        else:  # bad tag
                            raise exc.KSError(msg_bad_tag)
            else:  # repo is indexed
                if ic.head > current_head:  # patch moved downstream
                    if repo == current_head.repo_url:  # good tag
                        raise exc.KSError(
                            "There is a problem with patch \"%s\". "
                            "The patch is in the wrong section of series.conf "
                            "or the remote fetching from \"%s\" needs to be "
                            "fetched or the relative order of \"%s\" and "
                            "\"%s\" in \"remotes\" is incorrect. Manual "
                            "intervention is required." % (
                                name,
                                current_head.repo_url,
                                ic.head,
                                current_head,
                            ))
                    else:  # bad tag
                        raise exc.KSError(
                            "There is a problem with patch \"%s\". "
                            "The patch is in the wrong section of series.conf "
                            "or the remote fetching from \"%s\" needs to be "
                            "fetched. Manual intervention is required." % (
                                name,
                                current_head.repo_url,
                            ))
                elif ic.head == current_head:  # patch didn't move
                    self.dest_head = ic.head
                    self.dest = ic
                    if repo != ic.head.repo_url:  # bad tag
                        self.new_url = ic.head.repo_url
                elif ic.head < current_head:  # patch moved upstream
                    if move_upstream:  # move patches between subsystem sections
                        self.dest_head = ic.head
                        self.dest = ic
                        if repo != ic.head.repo_url:  # bad tag
                            self.new_url = ic.head.repo_url
                    else:  # do not move patches between subsystem sections
                        if repo == current_head.repo_url:  # good tag
                            self.dest_head = current_head
                            self.dest = ic
                        else:  # bad tag
                            raise exc.KSError(msg_bad_tag)
Example #28
0
 def __init__(self, text_speed: str, phrase: str) -> None:
     self.patch = Patch()
     self.text_speed = text_speed
     self.phrase = phrase
Example #29
0
 def _GetPatchForOverworldData(self) -> Patch:
     patch = Patch()
     for screen_num in Range.VALID_ROOM_NUMBERS:
         addr = 0x80 + int(screen_num)
         patch.AddData(0x18410 + addr, [self.overworld_raw_data[addr]])
     return patch
Example #30
0
 def _GetPatchForLevelMetadata(self) -> Patch:
     patch = Patch()
     patch.AddData(self.LEVEL_METADATA_ADDRESS, self.level_metadata)
     return patch