Example #1
0
def read_data(datasetDirectory, lines_to_jump):
    data_letters = {}
    datasets = glob.glob(datasetDirectory + '/*.dat')
    for dataset in datasets:
        name = os.path.splitext(os.path.basename(dataset))[0]
        if name in alphabet:
            try:
                with open(dataset, 'r') as data:
                    i=0
                    for line in data.readlines():
                        i+=1
                        if i>lines_to_jump:
                            shape = literal_eval(('['+ line +']').replace('[,','['))
                            shape = np.reshape(shape, (-1, 1))
                            data_stroke = Stroke()
                            data_stroke.stroke_from_xxyy(np.reshape(shape,len(shape)))
                            #data_stroke.uniformize()
                            try:
                                data_stroke.normalize_wrt_max()
                            except ValueError:
                                print name+" is empty"
                            data_letters.setdefault(name,[]).append(data_stroke)
            except IOError:
                raise RuntimeError("no reading permission for file"+dataset )
    return data_letters
Example #2
0
 def respond_to_demonstration_letter(self, demonstration, letter, grade, mode='midway'):
     demo_stroke = Stroke()
     demo_stroke.stroke_from_xxyy(np.reshape(demonstration,len(demonstration)))
     #demo_stroke.uniformize()
     demo_stroke.normalize_wrt_max()
     if mode == 'midway':
         learned_stroke = stroke.midway(demo_stroke, self.generated_letters[letter], grade)
         self.generated_letters[letter] = learned_stroke
         save_learned_allograph(self.robot_path, letter, learned_stroke)
         _,score = stroke.euclidian_distance(demo_stroke, self.refs[letter])
     #if mode = 'PCA' 
     #if mode = 'sigNorm' (mixture of sigma-log-normal)
     #if mode = 'CNN' (1-D convolutionnal neural networks)
     return self.shape_message(letter),score
Example #3
0
def read_data(datasetDirectory, lines_to_jump):
    data_letters = {}
    datasets = glob.glob(datasetDirectory + '/*.dat')
    for dataset in datasets:
        name = os.path.splitext(os.path.basename(dataset))[0]
        if name in alphabet:
            try:
                with open(dataset, 'r') as data:
                    i = 0
                    for line in data.readlines():
                        i += 1
                        if i > lines_to_jump:
                            shape = literal_eval(
                                ('[' + line + ']').replace('[,', '['))
                            shape = np.reshape(shape, (-1, 1))
                            data_stroke = Stroke()
                            data_stroke.stroke_from_xxyy(
                                np.reshape(shape, len(shape)))
                            #data_stroke.uniformize()
                            try:
                                data_stroke.normalize_wrt_max()
                            except ValueError:
                                print(name + " is empty")
                            data_letters.setdefault(name,
                                                    []).append(data_stroke)
            except IOError:
                raise RuntimeError("no reading permission for file" + dataset)
    return data_letters
Example #4
0
    def respond_to_demonstration_letter(self,
                                        demonstration,
                                        letter,
                                        mode='midway',
                                        mode_param=0.5):
        demo_stroke = Stroke()
        demo_stroke.stroke_from_xxyy(
            np.reshape(demonstration, len(demonstration)))
        demo_stroke.normalize_wrt_max()

        if mode == 'midway':
            learned_stroke = stroke.midway(demo_stroke,
                                           self.generated_letters[letter],
                                           mode_param)
            self.generated_letters[letter] = learned_stroke
            save_learned_allograph(self.child_path, letter, learned_stroke)
            _, score = stroke.euclidian_distance(demo_stroke,
                                                 self.refs[letter])

        if mode == 'simple':
            learned_stroke = stroke.weigthedSum(demo_stroke,
                                                self.generated_letters[letter],
                                                mode_param)
            self.generated_letters[letter] = learned_stroke
            save_learned_allograph(self.child_path, letter, learned_stroke)
            _, score = stroke.euclidian_distance(demo_stroke,
                                                 self.refs[letter])

        return self.shape_message(letter), score
Example #5
0
                                feedback = -1
                            else:
                                feedback -= 1

                    if bug_on_last_line:
                        time = bug_variables[0]
                        name = bug_variables[1]
                        ischouchou = bug_variables[2]

                        if found_demo_bug2:
                            string2 = str(found_demo_bug2.group("path2"))
                            string = string_bug + string2
                            try:
                                path = np.array(
                                    literal_eval(string))  #.replace('"','')))
                                graph = Stroke()
                                graph.stroke_from_xxyy(path)
                                graph.downsampleShape(70)

                                if name in prev_strokes:
                                    _, score = stroke.euclidian_distance(
                                        prev_strokes[name], graph)
                                    prev_strokes[name].reset()
                                    prev_strokes[name].stroke_from_xxyy(path)
                                    prev_strokes[name].downsampleShape(70)
                                else:
                                    score = -1.
                                    prev_strokes[name] = Stroke()
                                    prev_strokes[name].stroke_from_xxyy(path)
                                    prev_strokes[name].downsampleShape(70)
Example #6
0
        log_path2 = load_log2(child)

        i = 0
        with open(log_path1, 'r') as log:

            for line in log.readlines():

                found_demo = new_demo.search(line)

                if found_demo :#and opens[0]:

                    string = str(found_demo.group("path"))[:-1]
                    name = found_demo.group("letter")
                    try:
                        path = np.array(literal_eval(string.replace('"','')))
                        graph = Stroke()
                        graph.stroke_from_xxyy(path)
                        score = minScore(learning_manager.robot_data,graph,name)
                        if name in letter_score:
                            letter_score[name] += [score]
                        else:
                            letter_score[name] = [score]
                    except SyntaxError:
                        pass
        i = 0
        with open(log_path1, 'r') as log:
        
            for line in log.readlines():

                found_demo = new_demo.search(line)
Example #7
0
                                    float('nan'), 1, f2f
                                ])
#                       lines = []
#                       current_demo = {}
#                       current_learn = {}
#                       feedback = float('nan')
#                       word_done = False
#                       nb_fb=1.

                    if found_learn:
                        string = str(found_learn.group("path"))  #[:-1]
                        name = found_learn.group("letter")

                        path = np.array(
                            literal_eval(string))  #.replace('"','')))
                        graph = Stroke()
                        graph.stroke_from_xxyy(path)
                        graph.downsampleShape(70)
                        current_learn[name] = graph

                    if found_word:
                        word = str(found_word.group("word"))[:-1]
                        if word_done:
                            for liste in lines:
                                letter = liste[2]
                                if letter in current_demo and letter in current_learn:
                                    _, score = stroke.euclidian_distance(
                                        current_demo[letter],
                                        current_learn[letter])
                                    wr.writerow([
                                        liste[0], liste[1], liste[2], liste[3],
Example #8
0
                            if math.isnan(feedback):
                                feedback = -1
                            else:
                                feedback -= 1

                    if bug_on_last_line:
                        time = bug_variables[0]
                        name = bug_variables[1]
                        ischouchou = bug_variables[2]

                        if found_demo_bug2:
                            string2 = str(found_demo_bug2.group("path2"))
                            string = string_bug + string2
                            try:
                                path = np.array(literal_eval(string))  # .replace('"','')))
                                graph = Stroke()
                                graph.stroke_from_xxyy(path)
                                graph.downsampleShape(70)

                                if name in prev_strokes:
                                    _, score = stroke.euclidian_distance(prev_strokes[name], graph)
                                    prev_strokes[name].reset()
                                    prev_strokes[name].stroke_from_xxyy(path)
                                    prev_strokes[name].downsampleShape(70)
                                else:
                                    score = -1.0
                                    prev_strokes[name] = Stroke()
                                    prev_strokes[name].stroke_from_xxyy(path)
                                    prev_strokes[name].downsampleShape(70)

                                liste = []
Example #9
0
        log_path2 = load_log2(child)

        i = 0
        with open(log_path1, 'r') as log:

            for line in log.readlines():

                found_demo = new_demo.search(line)

                if found_demo:  #and opens[0]:

                    string = str(found_demo.group("path"))[:-1]
                    name = found_demo.group("letter")
                    try:
                        path = np.array(literal_eval(string.replace('"', '')))
                        graph = Stroke()
                        graph.stroke_from_xxyy(path)
                        score = minScore(learning_manager.robot_data, graph,
                                         name)
                        if name in letter_score:
                            letter_score[name] += [score]
                        else:
                            letter_score[name] = [score]
                    except SyntaxError:
                        pass
        i = 0
        with open(log_path1, 'r') as log:

            for line in log.readlines():

                found_demo = new_demo.search(line)
Example #10
0
from allograph.learning_manager import save_learned_allograph as save_letter
from allograph.stroke import Stroke
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.graphics import Color, Ellipse, Line
import inspect
import argparse
parser = argparse.ArgumentParser(description='Learn a collection of drawings')
parser.add_argument('draw', action="store", help='The draw to be learnt')

fileName = inspect.getsourcefile(Stroke)
installDirectory = fileName.split('/lib')[0]
datasetDirectory = installDirectory + '/share/allograph/letter_model_datasets/bad_letters'
robotDirectory = installDirectory + '/share/allograph/robot_tries/start'
letter = Stroke()


class MyPaintWidget(Widget):
    def on_touch_down(self, touch):
        with self.canvas:

            #self.canvas.clear()
            Color(1, 1, 0)
            d = 30.
            touch.ud['line'] = Line(points=(touch.x, touch.y))

    def on_touch_move(self, touch):
        global lastStroke
        touch.ud['line'].points += [touch.x, touch.y]
        letter.append(touch.x, touch.y)
Example #11
0
from allograph.learning_manager import save_learned_allograph as save_letter
from allograph.stroke import Stroke
import allograph.stroke as stroke
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.graphics import Color, Ellipse, Line
import inspect
import argparse
parser = argparse.ArgumentParser(description='Learn a collection of drawings')
parser.add_argument('draw', action="store", help='The draw to be learnt')

letter1 = Stroke()
letter2 = Stroke()
turn = True


class MyPaintWidget(Widget):
    def on_touch_down(self, touch):
        with self.canvas:

            #self.canvas.clear()
            Color(1, 1, 0)
            d = 30.
            touch.ud['line'] = Line(points=(touch.x, touch.y))

    def on_touch_move(self, touch):
        global lastStroke
        touch.ud['line'].points += [touch.x, touch.y]
        if turn:
            letter1.append(touch.x, touch.y)
        else: