Exemple #1
0
    def generate_xml_from_all_seq(self):
        clear_scene_file_startswith(self.seq_name)
        SceneGenerate.write_range_num_for_RT(self._cfgFile)
        self.readParameterFileList()
        for filename in self.parameter_file_list:
            tcfgfile = os.path.join(session.get_input_dir(), filename)
            prifix = os.path.splitext(filename)[0] + "_"
            SceneGenerate.terr_generate(tcfgfile, prifix)
            SceneGenerate.generate_objects_file(tcfgfile, prifix)
            SceneGenerate.forest_generate(tcfgfile, prifix)

        # if len(self.parameter_file_list) > 0:
        # SceneGenerate.generate_objects_file(self._cfgFile, self.seq_name + "_")
        # SceneGenerate.forest_generate(self._cfgFile, self.seq_name + "_")
        # log("INFO: Generating view and illuminations.")

        # 输出irridiance
        fi = open(
            os.path.join(session.get_output_dir(),
                         self.seq_name + "_" + irradiance_file), 'w')
        sp = SceneParser()
        for filename in self.parameter_file_list:
            tcfgfile = os.path.join(session.get_input_dir(), filename)
            prifix = os.path.splitext(filename)[0] + "_"
            irrstr = sp.parse(tcfgfile, prifix, prifix)
            fi.write(os.path.splitext(filename)[0] + "\n" + irrstr + "\n")
        log("INFO: view and illuminations generated.")
        fi.close()
Exemple #2
0
    def run_seq(self):
        currdir = os.path.split(os.path.realpath(__file__))[0]
        os.environ[
            'PATH'] = currdir + '/bin/rt/' + current_rt_program + os.pathsep + os.environ[
                'PATH']
        self.readParameterFileList()
        for filename in self.parameter_file_list:
            log("INFO: " + filename)
            tcfgfile = os.path.join(session.get_input_dir(), filename)
            prifix = os.path.splitext(filename)[0]
            f = open(tcfgfile, 'r')
            cfg = json.load(f)
            excuable = "lessrt"
            scene_path = session.get_scenefile_path()

            distFile = os.path.join(session.get_output_dir(), prifix)
            # parameter = " -o " + distFile
            scene_file_path = os.path.join(scene_path,
                                           prifix + "_" + main_scene_xml_file)
            # cmd = excuable + " " + os.path.join(scene_path, prifix+"_"+main_scene_xml_file) + parameter
            # os.system(cmd)
            cores = cfg["Advanced"]["number_of_cores"]
            if cfg["Advanced"]["network_sim"]:
                subprocess.call([
                    excuable, scene_file_path, "-o", distFile, "-p",
                    str(cores), "-s",
                    os.path.join(session.get_input_dir(), "server.txt")
                ])
            else:
                subprocess.call([
                    excuable, scene_file_path, "-o", distFile, "-p",
                    str(cores)
                ])
                # os.system(excuable + " " + scene_file_path + " " + "-o " + distFile + " -p " + str(cores))
                time.sleep(0.5)
            if output_format not in ("npy", "NPY"):
                if os.path.exists(distFile + ".npy"):
                    self.convert_npy_to_envi(cfg, distFile, output_format)
                if os.path.exists(distFile + "_downwelling.npy"):
                    self.convert_npy_to_envi(cfg, distFile + "_downwelling",
                                             output_format)
                if os.path.exists(distFile + "_upwelling.npy"):
                    self.convert_npy_to_envi(cfg, distFile + "_upwelling",
                                             output_format)
                if os.path.exists(distFile + "_4Components.npy"):
                    data = np.load(distFile + "_4Components.npy")
                    dshape = data.shape
                    if len(dshape) == 3:
                        if dshape[2] <= 4:
                            data = data[:, :, 0]
                        else:
                            data = data[:, :, 0:5]
                    bandlist = []
                    RasterHelper.saveToHdr_no_transform(
                        data, distFile + "_4Components", bandlist,
                        output_format)
                    os.remove(distFile + "_4Components.npy")
Exemple #3
0
 def readParameterFileList(self):
     if self.parameter_file_list is None:
         self.parameter_file_list = []
         for filename in os.listdir(session.get_input_dir()):
             if filename.startswith(self.seq_name):
                 self.parameter_file_list.append(filename)
         self.parameter_file_list = self.sort_list(self.parameter_file_list)
Exemple #4
0
    def parse_seq(self):
        clear_param_file_startswith(self.seq_name)
        # write seq info
        finfo = open(
            os.path.join(session.get_output_dir(),
                         self.seq_name + BATCH_INFO_FILE), 'w')

        params = []
        f = open(self.seqPath, 'r')

        seq = json.load(f, object_pairs_hook=OrderedDict)
        groupNames = []
        for groupName in seq:
            groupParam = dict()
            groupNames.append(groupName)
            for paramName in seq[groupName]:  # in each group
                paramValue = seq[groupName][paramName]
                if paramValue.startswith(batch_List_name):
                    arr = paramValue.split(":", 1)
                    arr = arr[1].split("/")
                    index = 0
                    for data in arr:
                        if index not in groupParam:
                            groupParam[index] = dict()
                        groupParam[index][paramName] = data
                        index += 1
            # print groupParam
            params.append(groupParam)

        # mixture between group
        indexList = []
        for i in range(0, len(params)):
            groupLen = len(params[i])
            indexList.append(range(0, groupLen))
        totalIndex = 0
        combinations = list(itertools.product(*indexList))
        for combination in combinations:
            if len(combination) > 0:
                groupIndex = 0
                seq_input_file = os.path.join(
                    session.get_input_dir(),
                    self.seq_name + "_" + str(totalIndex) + ".json")
                finfo.write(self.seq_name + "_" + str(totalIndex) + " ")
                cfg = self.read_config_file()
                totalIndex += 1
                for paramValueIndex in combination:
                    for paramName in params[groupIndex][paramValueIndex]:
                        paraVal = params[groupIndex][paramValueIndex][
                            paramName]
                        print("INFO: --", paramName)
                        self.writeKey(cfg, paramName,
                                      self.getTypedValue(paramName, paraVal))
                        finfo.write(paramName + " " + paraVal + " ")
                    groupIndex += 1
                # write batch config file
                self.writeCfgFile(cfg, seq_input_file)
                finfo.write("\n")
        finfo.close()
Exemple #5
0
            log("INFO: Sub region trees:", area_max)
            log("INFO: Start parallel computing...")
            # temp file
            folder = tempfile.mkdtemp()
            detected_trees_pos = np.memmap(os.path.join(folder, 'treedetect'), dtype=float, shape=(area_max, 5),
                                           mode='w+')
            detected_trees_pos += -1

            joblib.Parallel(n_jobs=joblib.cpu_count()-1, max_nbytes=None)(
                joblib.delayed(sub_fun)(nuclear, areas, detected_trees_pos, i,
                                        real_coordinate, offset_x, offset_y,
                                        pixel_size, obj_num, seg_size, crownDiameterList,crownHeightsList) for i in range(0, seg_num))

            if out_file == "":
                from session import session
                out_file = os.path.join(session.get_input_dir(), "instances.txt")
            if num_r == 0 and num_c == 0:
                fw = open(out_file, 'w')
            else:
                fw = open(out_file, 'a')
            for i in range(0, len(detected_trees_pos)):
                if detected_trees_pos[i][0] >= 0:
                    outstr = objList[int(detected_trees_pos[i][0])] + " " + str(detected_trees_pos[i][1]) + " " + str(
                        detected_trees_pos[i][2])+" 0 "+str(detected_trees_pos[i][3])+" "+str(detected_trees_pos[i][4])
                # if detected_trees_pos[i][0] >= 0:
                #     outstr = objList[int(detected_trees_pos[i][0])] + " " + str(detected_trees_pos[i][1]) + " " + str(
                #         detected_trees_pos[i][2]) + " 0"
                    fw.write(outstr + "\n")
            fw.close()
            del detected_trees_pos
            detected_trees_pos = np.memmap(os.path.join(folder, 'treedetect'),
                                           dtype=float,
                                           shape=(area_max, 5),
                                           mode='w+')
            detected_trees_pos += -1

            joblib.Parallel(n_jobs=joblib.cpu_count() - 1, max_nbytes=None)(
                joblib.delayed(sub_fun)
                (nuclear, areas, detected_trees_pos, i, real_coordinate,
                 offset_x, offset_y, pixel_size, obj_num, seg_size,
                 crownDiameterList, crownHeightsList)
                for i in range(0, seg_num))

            if out_file == "":
                from session import session
                out_file = os.path.join(session.get_input_dir(),
                                        "instances.txt")
            if num_r == 0 and num_c == 0:
                fw = open(out_file, 'w')
            else:
                fw = open(out_file, 'a')
            for i in range(0, len(detected_trees_pos)):
                if detected_trees_pos[i][0] >= 0:
                    outstr = objList[int(
                        detected_trees_pos[i][0])] + " " + str(
                            detected_trees_pos[i][1]) + " " + str(
                                detected_trees_pos[i][2]) + " 0 " + str(
                                    detected_trees_pos[i][3]) + " " + str(
                                        detected_trees_pos[i][4])
                    # if detected_trees_pos[i][0] >= 0:
                    #     outstr = objList[int(detected_trees_pos[i][0])] + " " + str(detected_trees_pos[i][1]) + " " + str(
Exemple #7
0
 def clear_json_file(self):
     self.readParameterFileList()
     for filename in self.parameter_file_list:
         filepath = os.path.join(session.get_input_dir(), filename)
         os.remove(filepath)
Exemple #8
0
def getTreeLocAndHeight(chm_hdr_path,
                        out_file,
                        obj_list,
                        subregion=0,
                        real_coordinate=True):
    idata_set = gdal.Open(chm_hdr_path)
    transform = idata_set.GetGeoTransform()
    if transform is None:
        log("ERROR: No geotransform found for file ", chm_hdr_path)
        return

    objList = obj_list.strip().split("*")
    obj_num = len(objList)

    pixel_size = abs(transform[1])

    band = idata_set.GetRasterBand(1)
    banddata = band.ReadAsArray(0, 0, band.XSize, band.YSize)
    width = band.XSize  # XSize是列数, YSize是行数
    height = band.YSize
    if (subregion == 0):
        subregion = max(width, height)
    num_width = int(math.ceil(width / float(subregion)))
    num_height = int(math.ceil(height / float(subregion)))
    result = []
    treeCount = 0
    for num_r in range(0, num_height):  # row
        for num_c in range(0, num_width):  #col
            log("INFO: Region: " + str(num_r) + " " + str(num_c))
            row_start = num_r * subregion
            row_end = min((num_r + 1) * subregion, height)
            col_start = num_c * subregion
            col_end = min((num_c + 1) * subregion, width)
            offset_x = num_c * subregion * pixel_size
            offset_y = num_r * subregion * pixel_size

            nuclear = banddata[row_start:row_end, col_start:col_end]
            threshed = (nuclear > 3)
            nuclear *= threshed
            bc = np.ones((3, 3))

            maxima = mahotas.morph.regmax(nuclear, Bc=bc)
            spots, n_spots = mahotas.label(maxima)

            surface = (nuclear.max() - nuclear)
            areas = mahotas.cwatershed(surface, spots)
            areas *= threshed
            area_max = areas.max()
            # seg_size = args.seg_size  # 500000 points for each core, parallel
            # seg_num = int(math.ceil(area_max / float(seg_size)))
            print(area_max)
            # 每棵树用不用的数值标记
            for i in range(1, areas.max()):
                if i % 1000 == 0:
                    log("INFO: Detected Trees: " + str(i))
                treepixel = np.where(areas == i)
                tmpmax = 0
                tx, ty = 0, 0
                tmp = 0
                # crown = math.sqrt(len(treepixel[0]) * pixel_size * pixel_size) * 2
                # tx,ty = sum(treepixel[0])/float(len(treepixel[0])),sum(treepixel[1])/float(len(treepixel[1]))
                for m in range(0, len(treepixel[0])):
                    tmp += 1
                    if nuclear[treepixel[0][m]][treepixel[1][m]] > tmpmax:
                        tmpmax = nuclear[treepixel[0][m]][treepixel[1][m]]
                        tx, ty = treepixel[0][m], treepixel[1][m]
                # maxHeight = nuclear[tx, ty]
                if real_coordinate:
                    x = offset_x + ty * pixel_size
                    y = offset_y + tx * pixel_size
                    result.append([random.randint(0, obj_num - 1), x, y])
                else:
                    result.append([random.randint(0, obj_num - 1), tx, ty])

    if out_file == "":
        from session import session
        import os
        out_file = os.path.join(session.get_input_dir(), "instances.txt")
    log("INFO: Total detected trees: ", treeCount)
    fw = open(out_file, 'w')
    for i in range(0, len(result)):
        outstr = objList[result[i][0]] + " " + str(result[i][1]) + " " + str(
            result[i][2])
        fw.write(outstr + "\n")
    fw.close()