def test_descr1():
    meshes = ['29', '36', '29']
    meshes = [OffFile('./meshes/{}.off'.format(mid)).read() for mid in meshes]
    seg_meshes = []
    for m in meshes:
        sg = Segmentation(m)
        sg.reduce_to_k_segments(2)
        for seg in sg.segments:
            seg_meshes.append(seg.mesh)
    meshes = seg_meshes

    for i in range(1, 2):
        scales = [1.0 for x in range(i)]
        m_meshes = [m.copy() for m in meshes]
        evals = [m.scale_principal_eigenvalues(scales) for m in m_meshes]
        descriptors = [
            D2Descriptor(m, max_size=5.0, n_samples=1024 * 100)
            for m in m_meshes
        ]
        for d in descriptors:
            d.plot()
        plt.show()
        l = len(descriptors)
        dists = np.zeros((l, l))
        for i in range(len(descriptors)):
            for j in range(len(descriptors)):
                dists[i][j] = d_dist(descriptors[i], descriptors[j])
        print dists
Exemple #2
0
    def paint_seg_xz(self):
        from matplotlib import rcParams
        # rcParams['font.family'] = 'sans-serif'
        plt.rcParams["font.family"] = "Times New Roman"
        font = {'family' : 'normal',
                # 'weight' : 'bold',
                'size'   : 14}
        plt.rc('font', **font)
        seg = Segmentation(self.full_traj, self.dfull_traj, self.ddfull_traj)
        stroke2 = seg.segmentate_two()[1]
        vel, acc = get_vel_and_acc(stroke2, self.freq)

        goal_error = 0.1

        good_dmp = dmp_process(200, stroke2, vel, acc, 0, goal_error, True)
        bad_dmp = dmp_process(200, stroke2, vel, acc, 0, goal_error, false)
        
        
        plt.figure(1)
        plt.plot((1/self.freq)*np.arange(len(stroke2[0])), [-i for i in stroke2[0]], "b", label='ground truth', linewidth=5, alpha=0.6)
        plt.plot((1/self.freq)*np.arange(len(stroke2[0])), [-i for i in good_dmp[0]], "r", label='DMP*')
        plt.xlabel('t(s)')
        plt.ylabel('x(m)')
        # plt.title('x/m coordinate changing with time/sec')
        plt.legend(loc='upper right', frameon=False)
        # plt.savefig('/home/jingwu/Desktop/CS8803/Project/Dec/dmp_compare/%s'% name1)
        plt.interactive(False)
        plt.show()
Exemple #3
0
  def run( self, u_info ):

    # self, mojo_dir, tmp_dir, /// out_dir, dojoserver

    # register two data sources
    self.__segmentation = Segmentation( u_info.files_path , u_info.tmpdir, self)
    self.__image = Image( u_info.files_path , u_info.tmpdir)

    # and the controller
    self.__controller = Controller( u_info, self.__segmentation.get_database(), self ) ####

    # and the viewer
    self.__viewer = Viewer()



    # and the controller
    if self.__segmentation:
      db = self.__segmentation.get_database()
    else:
      db = None
    self.__controller = Controller( u_info, db, self )



    # and the setup
    self.__setup = Setup(self, u_info.files_path, u_info.tmpdir)

    print('path_gfx: ',path_gfx)
    # running live

    ####
    asyncio.set_event_loop(u_info.worker_loop)

    dojo = tornado.web.Application([
      (r'/dojo/gfx/(.*)', tornado.web.StaticFileHandler, {'path': path_gfx}),
      (r'/ws', Websockets, dict(controller=self.__controller)),
      (r'/(.*)', DojoHandler, dict(logic=self))
    ],debug=True,autoreload=True) #            (r'/dojo/gfx/(.*)', tornado.web.StaticFileHandler, {'path': '/dojo/gfx'})


    # dojo.listen(u_info.port, max_buffer_size=1024*1024*150000)
    server = tornado.httpserver.HTTPServer(dojo)
    server.listen(u_info.port)

    print('*'*80)
    print('*', 'DOJO RUNNING')
    print('*')
    print('*', 'open', '[ http://' + u_info.ip + ':' + str(u_info.port) + '/dojo/ ] ')
    print('*'*80)

    tornado.ioloop.IOLoop.instance().start()
    server.stop()

    # def sig_handler(signum, frame):
    #  IOLoop.current().add_callback_from_signal(receiver.shutdown)

    print("Tornado web server stops.")

    return
Exemple #4
0
def train():
    feature_mapper = FeatureMapper()
    score = Score()
    segmentation = Segmentation()

    feature_mapper.load_weights("./immutable_weights/feature_mapper")
    # score.load_weights("./weights/score")
    # segmentation.load_weights("./weights/segmentation")

    opt = Adam(learning_rate=5e-5)
    with open("../data/data_classification_train.json") as json_file:
        data = json.load(json_file)
    data_index = 0
    while str(data_index) in data:
        img = get_img(
            "../pictures/pictures_classification_train/{}.png".format(
                data_index))
        true_masks = get_true_mask(data[str(data_index)])

        features = feature_mapper(img)

        def get_loss():
            segmentation_prediction = segmentation(features)
            score_prediction = score(features)
            show_evaluation(segmentation_prediction, true_masks, data_index)
            return calculate_loss(segmentation_prediction, score_prediction,
                                  true_masks)

        opt.minimize(get_loss,
                     [score.trainable_weights, segmentation.trainable_weights])

        if (data_index % 100 == 99):
            score.save_weights("./weights/score")
            segmentation.save_weights("./weights/segmentation")
        data_index += 1
Exemple #5
0
    def read(self, consumer, offset, size):
        """I am the main entry point, from which FileNode.read() can get
        data. I feed the consumer with the desired range of ciphertext. I
        return a Deferred that fires (with the consumer) when the read is
        finished.

        Note that there is no notion of a 'file pointer': each call to read()
        uses an independent offset= value.
        """
        # for concurrent operations: each gets its own Segmentation manager
        if size is None:
            size = self._verifycap.size
        # ignore overruns: clip size so offset+size does not go past EOF, and
        # so size is not negative (which indicates that offset >= EOF)
        size = max(0, min(size, self._verifycap.size - offset))

        read_ev = self._download_status.add_read_event(offset, size, now())
        if IDownloadStatusHandlingConsumer.providedBy(consumer):
            consumer.set_download_status_read_event(read_ev)
            consumer.set_download_status(self._download_status)

        lp = log.msg(format="imm Node(%(si)s).read(%(offset)d, %(size)d)",
                     si=base32.b2a(self._verifycap.storage_index)[:8],
                     offset=offset,
                     size=size,
                     level=log.OPERATIONAL,
                     parent=self._lp,
                     umid="l3j3Ww")
        if self._history:
            sp = self._history.stats_provider
            sp.count("downloader.files_downloaded", 1)  # really read() calls
            sp.count("downloader.bytes_downloaded", size)
        if size == 0:
            read_ev.finished(now())
            # no data, so no producer, so no register/unregisterProducer
            return defer.succeed(consumer)

        # for concurrent operations, each read() gets its own Segmentation
        # manager
        s = Segmentation(self, offset, size, consumer, read_ev, lp)

        # this raises an interesting question: what segments to fetch? if
        # offset=0, always fetch the first segment, and then allow
        # Segmentation to be responsible for pulling the subsequent ones if
        # the first wasn't large enough. If offset>0, we're going to need an
        # extra roundtrip to get the UEB (and therefore the segment size)
        # before we can figure out which segment to get. TODO: allow the
        # offset-table-guessing code (which starts by guessing the segsize)
        # to assist the offset>0 process.
        d = s.start()

        def _done(res):
            read_ev.finished(now())
            return res

        d.addBoth(_done)
        return d
def convert_to_segmentation_schema(filename, segmentation):
    # Create a segmentation object to serialize
    seg_schema = Segmentation()
    seg_schema.media.filename = filename

    # For each segment returned by the ina_speech_segmenter, add
    # a corresponding segment formatted to json spec
    for segment in segmentation:
        seg_schema.addSegment(segment[0], segment[0], float(segment[1]),
                              float(segment[2]))

    return seg_schema
Exemple #7
0
    def create_letter(self):
        seg = Segmentation(self.full_traj, self.dfull_traj, self.ddfull_traj)
        strokes = seg.segmentate_two()

        # create p
        for stroke in strokes:
            vel, acc = get_vel_and_acc(stroke, self.freq)
            if strokes.index(stroke) == 0:
                dmp_stroke = dmp_process(200, stroke, vel, acc)
                generate_data(dmp_stroke, self.name+"1")
            if strokes.index(stroke) == 1:
                z_change = dmp_process(200, stroke, vel, acc, 0, 0.15)
                dmp_stroke = dmp_process(200, stroke, vel, acc)
                dmp_stroke[2] = z_change[2]
                generate_data(dmp_stroke, self.name+"2")
            # plt.plot([ -i for i in stroke[0]], stroke[2], label='stroke %d' % strokes.index(stroke), linewidth=5, alpha=0.6)
            plt.plot([ -i for i in dmp_stroke[0]], dmp_stroke[2], label='DMP_stroke %d' % strokes.index(stroke))

        # create B
        # for stroke in strokes:
        #     vel, acc = get_vel_and_acc(stroke, self.freq)
        #     if strokes.index(stroke) == 0:
        #         dmp_stroke = dmp_process(200, stroke, vel, acc)
        #     if strokes.index(stroke) == 1:
        #         z_change = dmp_process(200, stroke, vel, acc, 0, 0.15)
        #         dmp_stroke = dmp_process(200, stroke, vel, acc)
        #         dmp_stroke[2] = z_change[2]
        #     # plt.plot([ -i for i in stroke[0]], stroke[2], label='stroke %d' % strokes.index(stroke), linewidth=5, alpha=0.6)
        #     plt.plot([ -i for i in dmp_stroke[0]], dmp_stroke[2], label='DMP_stroke %d' % strokes.index(stroke))
        
        # zz_change = dmp_process(200, strokes[-1], vel, acc, -0.15, 0)
        # dmp_stroke2 = dmp_process(200, strokes[-1], vel, acc)
        # dmp_stroke2[2] = zz_change[2]
        # plt.plot([ -i for i in dmp_stroke2[0]], dmp_stroke2[2], label='DMP_stroke 3')

        # create D
        # for stroke in strokes:
        #     vel, acc = get_vel_and_acc(stroke, self.freq)
        #     zdmp_stroke = dmp_process(200, stroke, vel, acc, 0.0, -0.1)
        #     xdmp_stroke = dmp_process(200, stroke, vel, acc, 0.0, 0.1)
        #     plt.plot([ -i for i in xdmp_stroke[0]], zdmp_stroke[2], label='DMP_stroke %d' % strokes.index(stroke))



        plt.xticks(np.arange(0.35, 0.70, step=0.05))
        plt.yticks(np.arange(0.10, 0.40, step=0.05))
        plt.xlabel('x(m)')
        plt.ylabel('y(m)')
        # plt.title('Create new letter by DMP of writing belongs to %s' % self.name[2:])
        plt.legend(loc='upper right', frameon=False)
        # plt.savefig('/home/jingwu/Desktop/CS8803/Project/Dec/%s_new_letter.png' % self.name)
        # plt.savefig('/home/jingwu/Desktop/CS8803/Project/Dec/%s_B.png')
        plt.show()
Exemple #8
0
    def read(self, consumer, offset, size):
        """I am the main entry point, from which FileNode.read() can get
        data. I feed the consumer with the desired range of ciphertext. I
        return a Deferred that fires (with the consumer) when the read is
        finished.

        Note that there is no notion of a 'file pointer': each call to read()
        uses an independent offset= value.
        """
        # for concurrent operations: each gets its own Segmentation manager
        if size is None:
            size = self._verifycap.size
        # ignore overruns: clip size so offset+size does not go past EOF, and
        # so size is not negative (which indicates that offset >= EOF)
        size = max(0, min(size, self._verifycap.size-offset))

        read_ev = self._download_status.add_read_event(offset, size, now())
        if IDownloadStatusHandlingConsumer.providedBy(consumer):
            consumer.set_download_status_read_event(read_ev)
            consumer.set_download_status(self._download_status)

        lp = log.msg(format="imm Node(%(si)s).read(%(offset)d, %(size)d)",
                     si=base32.b2a(self._verifycap.storage_index)[:8],
                     offset=offset, size=size,
                     level=log.OPERATIONAL, parent=self._lp, umid="l3j3Ww")
        if self._history:
            sp = self._history.stats_provider
            sp.count("downloader.files_downloaded", 1) # really read() calls
            sp.count("downloader.bytes_downloaded", size)
        if size == 0:
            read_ev.finished(now())
            # no data, so no producer, so no register/unregisterProducer
            return defer.succeed(consumer)

        # for concurrent operations, each read() gets its own Segmentation
        # manager
        s = Segmentation(self, offset, size, consumer, read_ev, lp)

        # this raises an interesting question: what segments to fetch? if
        # offset=0, always fetch the first segment, and then allow
        # Segmentation to be responsible for pulling the subsequent ones if
        # the first wasn't large enough. If offset>0, we're going to need an
        # extra roundtrip to get the UEB (and therefore the segment size)
        # before we can figure out which segment to get. TODO: allow the
        # offset-table-guessing code (which starts by guessing the segsize)
        # to assist the offset>0 process.
        d = s.start()
        def _done(res):
            read_ev.finished(now())
            return res
        d.addBoth(_done)
        return d
Exemple #9
0
class Gui:
    seg = None

    app = wx.App()
    win = wx.Frame(None, title="Chinese words segmentation", size=(600, 300))
    panel = wx.Panel(win)
    btnSegment = wx.Button(panel, label="Segment")
    txtSentence = wx.TextCtrl(panel)
    txtResult = wx.TextCtrl(panel, style=wx.TE_MULTILINE | wx.VSCROLL)

    def __init__(self):
        self.seg = Segmentation(self)
        #load the dict
        self.load()
        #LOOP
        self.init_gui()

    def load(self):
        self.seg.read_file()

    def segment(self, event):
        self.txtResult.Clear()
        sentence = self.txtSentence.GetValue()
        result = self.seg.get_segmentation(sentence)
        self.txtResult.SetValue(result)

    def onKeyPressed(self, event):
        keycode = event.GetKeyCode()
        #if ENTER pressed
        if keycode == 13:
            self.segment(event)

    def init_gui(self):
        self.btnSegment.Bind(wx.EVT_BUTTON, self.segment)
        self.txtSentence.Bind(wx.EVT_KEY_UP, self.onKeyPressed)

        hbox = wx.BoxSizer()
        hbox.Add(self.txtSentence, proportion=1, flag=wx.EXPAND)
        hbox.Add(self.btnSegment, proportion=0, flag=wx.LEFT, border=5)

        vbox = wx.BoxSizer(wx.VERTICAL)
        vbox.Add(hbox, proportion=0, flag=wx.EXPAND | wx.ALL, border=5)
        vbox.Add(self.txtResult,
                 proportion=1,
                 flag=wx.EXPAND | wx.LEFT | wx.BOTTOM | wx.RIGHT,
                 border=5)

        self.panel.SetSizer(vbox)

        self.win.Center()
        self.win.Show()
        self.app.MainLoop()
Exemple #10
0
def process_data(folder_name):
    for _k in xrange(_K):
        data_dir = os.listdir("..//" + folder_name + "/" + str(_k))
        for label in data_dir:
            for sample in os.listdir("..//" + folder_name + "/" + str(_k) + "/" + label):
                if not str(sample).endswith(".txt"):
                    continue
                file = open("..//" + folder_name + "/" + label + "/" + sample, 'r')
                content = file.read()
                file.close()
                segmentation = Segmentation()
                token_list = segmentation.cut(content)
                write_to_file("..//" + folder_name + "_tokens/" + str(_k) + "/" + label, sample, token_list,
                              label)
Exemple #11
0
 def create_segmentation(self):
     """Generate the segmentation analysis from the audio file. The new
     analysis is saved as a png file and also it is saved the oracle created
     by vmo."""
     if self.audio_path.get():
         if self.output_path.get():
             self.segmentation = Segmentation(self.audio_path.get())
             self.segmentation.output_png(self.segmentation_png)
             self.segmentation.save_oracle(self.oracle_path)
             self.message_info("Segmentation done")
         else:
             self.message_error("Set output path")
     else:
         self.message_error("Set the audio file path")
def generate_patches(filepath, n_segments):
    path, filename = os.path.split(filepath)
    m = OffFile('./meshes/{}/{}.off'.format(path, filename)).read()
    patch_seg = None
    sf = SegFile('./patches/{}/{}_patches.seg'.format(path, filename))
    if os.path.isfile('./patches/{}/{}_patches.seg'.format(path, filename)):
        patch_seg = sf.read(m)
    else:
        if not os.path.exists('./patches/{}'.format(path)):
            os.makedirs('./patches/{}'.format(path))
        patch_seg = Segmentation(m)
        patch_seg.reduce_to_k_segments(n_segments)
        sf.write(patch_seg)
    return m, patch_seg
Exemple #13
0
class Gui:
    seg = None    

    app = wx.App()
    win = wx.Frame(None, title = "Chinese words segmentation", size = (600, 300))
    panel = wx.Panel(win)
    btnSegment = wx.Button(panel, label = "Segment")
    txtSentence = wx.TextCtrl(panel)
    txtResult = wx.TextCtrl(panel, style = wx.TE_MULTILINE | wx.VSCROLL)
    
    def __init__(self):
        self.seg = Segmentation(self)
        #load the dict
        self.load()
        #LOOP
        self.init_gui()

    def load(self):
        self.seg.read_file()

    def segment(self, event):
        self.txtResult.Clear()
        sentence = self.txtSentence.GetValue()
        result = self.seg.get_segmentation(sentence)
        self.txtResult.SetValue(result)

    def onKeyPressed(self, event):
        keycode = event.GetKeyCode()
        #if ENTER pressed
        if keycode == 13:
            self.segment(event)

    def init_gui(self):
        self.btnSegment.Bind(wx.EVT_BUTTON, self.segment)
        self.txtSentence.Bind(wx.EVT_KEY_UP, self.onKeyPressed)

        hbox = wx.BoxSizer()
        hbox.Add(self.txtSentence, proportion = 1, flag = wx.EXPAND)
        hbox.Add(self.btnSegment, proportion = 0, flag = wx.LEFT, border = 5)

        vbox = wx.BoxSizer(wx.VERTICAL)
        vbox.Add(hbox, proportion = 0, flag = wx.EXPAND | wx.ALL, border = 5)
        vbox.Add(self.txtResult, proportion = 1, flag = wx.EXPAND | wx.LEFT | wx.BOTTOM | wx.RIGHT, border = 5)

        self.panel.SetSizer(vbox)

        self.win.Center()
        self.win.Show()
        self.app.MainLoop()
Exemple #14
0
def convert_to_segmentation_schema(filename, segmentation):
    media = SegmentationMedia()
    media.filename = filename
    # Create a segmentation object to serialize
    seg_schema = Segmentation(media=media)

    # For each segment returned by the ina_speech_segmenter, add
    # a corresponding segment formatted to json spec
    row = 0
    for segment in segmentation:
        row += 1
        if row == 1:
            continue
        seg_schema.addSegment(segment[0], segment[0], float(segment[1]),
                              float(segment[2]))
    return seg_schema
Exemple #15
0
 def paint_segment(self):
     seg = Segmentation(self.full_traj, self.dfull_traj, self.ddfull_traj)
     strokes = seg.segmentate_two()
     for stroke in strokes:
         vel, acc = get_vel_and_acc(stroke, self.freq)
         dmp_stroke = dmp_process(100, stroke, vel, acc)
         plt.plot([ -i for i in stroke[0]], stroke[2], label='stroke %d' % strokes.index(stroke), linewidth=5, alpha=0.6)
         plt.plot([ -i for i in dmp_stroke[0]], stroke[2], label='DMP_stroke %d' % strokes.index(stroke))
     plt.xticks(np.arange(0.45, 0.65, step=0.05))
     plt.yticks(np.arange(0.25, 0.40, step=0.05))
     plt.xlabel('X/m')
     plt.ylabel('Y/m')
     plt.title('Segmentated trajectory and trajectory computed by DMP of writing belongs to %s' % self.name[2:])
     plt.legend(loc='upper right', frameon=False)
     # plt.savefig('/home/jingwu/Desktop/CS8803/Project/Dec/segment/%s_segment.png' % self.name)
     plt.show()
Exemple #16
0
    def read(self, mesh):
        """Reads in the .seg file and returns a Segmentation using the
        associated mesh.

        Parameters
        ----------
        mesh : :obj:`Mesh3D`
            The 3D mesh associated with this segmentation.

        Returns
        -------
        :obj:`Segmentation`
            A segmentation created from the given mesh and the .seg file.
        """
        f = open(self.filepath_, 'r')

        face_to_segment = []
        for line in f:
            vals = line.split()
            if len(vals) > 0:
                val = int(vals[0])
                face_to_segment.append(val)
        f.close()

        return Segmentation(mesh, face_to_segment)
Exemple #17
0
def run_unet_resnet(istrain=False, tissue='liver', batch_size=8):
    return Segmentation.run_liver(
        istrain=istrain,
        model_name='unet_resnet',
        modelcheckpoint='cache/{0}/model/unet_resnet.hdf5'.format(tissue),
        batch_size=batch_size,
        nb_epoch=50,
        isliver=True if tissue == 'liver' else False)
Exemple #18
0
def run_unet_gen_448_448_padecho(istrain=False):
    return Segmentation.run_breast(
        istrain=istrain,
        model_name='unet5',
        modelcheckpoint='cache/breast/model/unet_gen_448_448_padecho.hdf5',
        batch_size=16,
        is_datagen=True,
        images_npy='cache/breast/datasets/images_pad_echo_448_448_tf.npy',
        masks_npy='cache/breast/datasets/masks_pad_echo_448_448_tf.npy')
Exemple #19
0
def segmentate(G, V, k=0.1):
    """
	Method to segmentate a given weighted undirected Graph w.r.t a threshold
	function depending on the factor k.
	Args:
		G:	weighted undirected Graph
		V:	list of vertices (ordered!!)
		k:	threshold factor
	"""
    E = G.edges(data="weight")
    print "STATUS: sorting edges"
    E = sorted(E, key=lambda x: x[2])
    seg = Segmentation(len(V), k=k)
    print "STATUS: running segmentation"
    for e in E:
        (v1, v2, w) = e
        i = V.index(v1)
        j = V.index(v2)
        if w <= seg.MInt(i, j):
            seg.union(i, j, weight=w)
    return seg
Exemple #20
0
def predict_image(im_path):
    plankton = Segmentation(im_path, target_shape=(75, 75, 3))
    plankton.segment()
    padded = plankton.get_padded()
    feat = plankton.get_features()
    feat = np.array(feat)
    padded = preprocess_input(np.array(padded, dtype=np.float32))
    x_img = padded.reshape(1, padded.shape[0], padded.shape[1], padded.shape[2])
    x_feat = feat.reshape(1, feat.shape[0])
    x_feat = mms.transform(x_feat)
    y_hat = model.predict([x_img, x_feat])
    valid = False
    labels = {}
    results = []
    for i, y in enumerate(y_hat.flatten()):
        if y > 0.5:
            valid = True
            labels[i] = y
    if valid:
        #sort by value. lower prob to higher prob. (given it is above threshold prob)
        labels = sorted(labels.items(), key=lambda x: x[1], reverse=False)
        save_image(im_path, labels)
        label_hat = class_map[labels[0][0]]
        y_hat = [str(x) for x in y_hat.flatten()]
        results.append(im_path)
        results.append(label_hat)
        results.extend(y_hat)
        return results
    return None
Exemple #21
0
def segment_all(one_obj_path, output_path):
    for image in glob.glob(one_obj_path + "input/*"):
        name = image[len(one_obj_path + "input/"):-4]
        print("processing {}".format(name))
        #fg = image.replace("input/", "output_fg/").replace(".jpg", ".png")
        #bg = image.replace("input/", "output_bg/").replace(".jpg", ".png")
        masks = []
        im = prepare_image(image)
        #fg = prepare_image(fg)
        #bg = prepare_image(bg)
        fg = None
        bg = None
        for i in range(5):
            s = Segmentation("1obj_{}".format(i) + name,
                             im,
                             bg_hint=bg,
                             fg_hint=fg,
                             plot_during_training=True,
                             output_path=output_path)
            s.optimize()
            masks.append(s.best_result.mask)
        save_image("1obj_" + name + "_final_mask", median(masks), output_path)
Exemple #22
0
    def process(self, frame, name="TrainingSamples/Image_"):
        # preprocessing for contour detection
        preprocessed = PreProcessing().background_contour_removal(frame)

        # find contours using algorithm by Suzuki et al. (1985)
        contours, hierarchy = cv.findContours(preprocessed, cv.RETR_TREE,
                                              cv.CHAIN_APPROX_NONE)

        # limit observed contours
        if len(contours) > 500:
            contours = contours[:500]

        # ignore first contour, as it is outer border of the frame
        contours = contours[1:]
        hierarchy = hierarchy[0][1:] - 1
        hierarchy = np.where(hierarchy < 0, -1, hierarchy)

        if len(contours) == 0:
            return preprocessed

        # initialize contour object from each contour in contour list
        binarized = PreProcessing().custom_binarize(frame)
        contourList = [
            Contour(contour=cnt, imgShape=frame.shape, frameBinary=binarized)
            for cnt in contours
        ]

        # filter, classify and group segmented contours
        sg = Segmentation(contourList, hierarchy, frame.shape)
        sg.group_and_classify()

        filtered = sg.get_contours()

        if len(filtered) == 0:
            return preprocessed

        # colouring preprocessing for ease in debugging
        preprocessed = cv.cvtColor(preprocessed, cv.COLOR_GRAY2BGR)

        lines = LineOrdering(filtered).get_lines(frame)

        # label contours with additional positional information
        lines = sg.label_contours(lines)

        for l in range(len(lines)):
            line = lines[l]
            for i in range(len(line)):
                cnt = line[i]
                cv.putText(frame,
                           str(l) + str(i), (cnt.center[0], cnt.center[1]),
                           cv.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 1)

        solutions = [
            self.solver.solve([cnt.unwrap() for cnt in line], frame)
            for line in lines if len(line) > 2
        ]

        return preprocessed  # orderedImage
def main():
    (input_file, input_segmentation_json, remove_type, output_file,
     kept_segments_file) = sys.argv[1:6]

    # Turn segmentation json file into segmentation object
    with open(input_segmentation_json, 'r') as file:
        seg_data = Segmentation().from_json(json.load(file))

    # Remove silence and get a list of kept segments
    kept_segments = remove_silence(remove_type, seg_data, input_file,
                                   output_file)

    # Write kept segments to json file
    mgm_utils.write_json_file(kept_segments, kept_segments_file)
    exit(0)
Exemple #24
0
def run_unet(istrain=False,
             tissue='liver',
             batch_size=8,
             image_size=(512, 512),
             model_pretrain=None):
    return Segmentation.run_liver(
        istrain=istrain,
        model_name='unet',
        modelcheckpoint='cache/{0}/model/unet_{1}.hdf5'.format(
            tissue, image_size[0]),
        batch_size=batch_size,
        nb_epoch=50,
        model_pretrain=model_pretrain,
        isliver=True if tissue == 'liver' else False,
        image_size=image_size)
    def __init__(self, parent=None):
        super(StartQT4, self).__init__(parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        #self.ui.web.page().mainFrame().addToJavaScriptWindowObject('self', self)
        #self.ui.web.setHtml(getHTML(lat, lon))
        self.ui.web.setHtml(html)
        self.ui.web.page().settings().setAttribute(
            QWebSettings.DeveloperExtrasEnabled, True)
        inspector = QWebInspector()
        inspector.setPage(self.ui.web.page())
        inspector.setVisible(True)

        self.ui.web.page().loadFinished.connect(self.load_finished)

        # here we connect signals with our slots
        self.ctimer = QtCore.QTimer()
        QtCore.QObject.connect(self.ctimer, QtCore.SIGNAL('timeout()'),
                               self.ctimerloop)
        self.ui.okButton.clicked.connect(self.onClick)
        self.imagesParser = ImageInfoParser()
        self.readgoogleimages = ReadGoogleImages()
        self.segmentation = Segmentation()
def main():
	(input_file, input_segmentation_json, remove_type, output_file, kept_segments_file) = sys.argv[1:6]

	print("Reading segmentation file")
	# Turn segmentation json file into segmentation object
	with open(input_segmentation_json, 'r') as file:
		seg_data = Segmentation().from_json(json.load(file))
	
	print("Removing silence to get a list of kept segments")
	# Remove silence and get a list of kept segments
	kept_segments = remove_silence(remove_type, seg_data, input_file, output_file)

	print("Writing  output json file")
	# Write kept segments to json file
	write_kept_segments_json(kept_segments, kept_segments_file)
	exit(0)
Exemple #27
0
def _test():
    from segmentation import Segmentation
    import input_output

    s = Segmentation(5)
    c1 = Nuage(input_output.get_clouds([0], 1, size=.01)[0], s)
    c2 = torch.tensor([[0., 0, 0], [1, 1, 1], [-1, -1, -1], [.2, .3, .0]])
    c2 = Nuage(c2, s)
    n_mlp = 2
    latent_size = 25088
    grid_points = 4
    epochs = 5
    latent = input_output.get_latent([0], 1, nPerObj=1)[0]

    # équivalence des deux fonctions de loss (avec points écrits à la main)
    print("seg", Nuage.chamfer_seg(c1, c2))
    print("quad", Nuage.chamfer_quad(c1.liste_points, c2.liste_points))
    assert Nuage.chamfer_seg(c1,
                             c2) == Nuage.chamfer_quad(c1.liste_points,
                                                       c2.liste_points)

    # presque équivalence de convergence
    # (utilise les mêmes nombres aléatoires pour avoir le même résultat)
    torch.manual_seed(0)
    np.random.seed(0)
    reconstructeur1 = Reconstructeur(n_mlp,
                                     latent_size,
                                     grid_points,
                                     s,
                                     quadratic=False)
    torch.manual_seed(0)
    np.random.seed(0)
    reconstructeur2 = Reconstructeur(n_mlp,
                                     latent_size,
                                     grid_points,
                                     s,
                                     quadratic=True)

    res1 = fit_reconstructeur(reconstructeur1, ([latent], [c1]), epochs)
    print()
    res2 = fit_reconstructeur(reconstructeur2, ([latent], [c1]), epochs)

    # différences sûrement dues à des erreurs d'arrondi
    assert np.all(
        np.abs(np.array(res1["loss_train"]) -
               np.array(res2["loss_train"])) < 1e-3)
    print("tests passés")
def error(coords_list):
    seeds = fixed_seeds.copy()
    print(f'coordinate list : {coords_list}')
    print(f'seeds : {seeds}')
    for index in range(len(coords_list) // 2):
        coordinate = (int(coords_list[2 * index]),
                      int(coords_list[2 * index + 1]))
        seeds.update({coordinate: colour})
    segmentation = Segmentation(image_array,
                                beta,
                                seeds,
                                image_name,
                                reference_path=target_segmentation_path)
    segmentation.solve()
    segmentation.build_segmentation_image()
    return segmentation.compute_error()
Exemple #29
0
def main():

    (segmentation_json, adj_json, output_json) = sys.argv[1:4]

    # Turn adjustment data into list of kept segments
    with open(adj_json, 'r') as file:
        adj_data = json.load(file)

    # Turn segmentation json into objects
    with open(segmentation_json, 'r') as file:
        seg = Segmentation().from_json(json.load(file))

    # List of adjustments (start, end, adjustment)
    offset_adj = []
    # Last ending position for iterating through kept segments
    last_end = 0.00
    # Running tally of removed segment lengths
    current_adj = 0.00

    # For each segment that was kept, keep track of the gaps to know how much to adjust
    for kept_segment in adj_data:
        print(kept_segment + ":" + str(adj_data[kept_segment]))
        start = float(kept_segment)
        end = adj_data[kept_segment]
        # If the start of this segment is after the last end, we have a gap
        if (start >= last_end):
            # Keep track of the gap in segments
            current_adj = current_adj + (start - last_end)
            # Add it to a list of adjustments
            offset_adj.append(
                Adjustment(start - current_adj, end - current_adj,
                           current_adj))
        # Keep track of the last segment end
        last_end = end
    print("#OFFSET ADJUSTMENTS")
    for adj in offset_adj:
        print(str(adj.start) + ":" + str(adj.end) + ":" + str(adj.adjustment))
    # For each word, find the corresponding adjustment
    for segment in seg.segments:
        adjust_segment(segment, offset_adj)

    # Write the resulting json
    mgm_utils.write_json_file(seg, output_json)
Exemple #30
0
def make_prediction():
    txt = request.get_json()['text']
    model.eval()
    # Clean the data from the raw input
    sentence = clean_data(np.array([txt]))
    # Segment and tokenize the clean data
    tokenized = [
        TOKENIZER(t) for t in Segmentation(sentence).runSegmentation()
    ]
    indexed = [TEXT.vocab.stoi[t] for t in tokenized[0]]
    length = [len(indexed)]

    tensor = torch.LongTensor(indexed).unsqueeze(1)
    length_tensor = torch.LongTensor(length)
    prediction = torch.sigmoid(model(tensor, length_tensor))

    result = {'text': txt, 'label': prediction.item()}
    session.append(result)
    return jsonify({'result': result})
Exemple #31
0
def predict_image(im_path):
    plankton = Segmentation(im_path, target_shape=(75, 75, 3))
    plankton.segment()
    padded = plankton.get_padded()
    feat = plankton.get_features()
    feat = np.array(feat)
    padded = preprocess_input(np.array(padded, dtype=np.float32))
    x_img = padded.reshape(1, padded.shape[0], padded.shape[1],
                           padded.shape[2])
    x_feat = feat.reshape(1, feat.shape[0])
    x_feat = mms.transform(x_feat)
    y_hat = model.predict([x_img, x_feat])
    label_hat = class_map[np.argmax(y_hat)]
    save_image(im_path, y_hat)
    y_hat = [str(x) for x in y_hat.flatten()]
    results = []
    results.append(im_path)
    results.append(label_hat)
    results.extend(y_hat)
    return results
    parser.add_argument('--num_points',
                        type=int,
                        default=2048,
                        help='Num of points to use')
    parser.add_argument('--model_path',
                        type=str,
                        default='',
                        metavar='N',
                        help='Path to load model')
    args = parser.parse_args()
    return args


if __name__ == '__main__':
    args = get_parser()
    if args.eval == False:
        if args.task == 'reconstruct':
            reconstruction = Reconstruction(args)
            reconstruction.run()
        elif args.task == 'classify':
            classification = Classification(args)
            classification.run()
        elif args.task == 'segment':
            segmentation = Segmentation(args)
            segmentation.run()
    else:
        inference = Inference(args)
        feature_dir = inference.run()
        svm = SVM(feature_dir)
        svm.run()
Exemple #33
0
 def __init__(self):
     self.seg = Segmentation(self)
     #load the dict
     self.load()
     #LOOP
     self.init_gui()