def on_touch_up(self, touch): global userShape touch.ud['line'].points userShape = downsampleShape(userShape, numPoints_shapeModeler, xyxyFormat=True) shapeCentre = ShapeModeler.getShapeCentre(userShape) for i in range(len(wordToLearn)): if (shapeCentre[0] > (self.width / len(wordToLearn)) * i): shapeIndex_demoFor = i shapeType = wordManager.shapeAtIndexInCurrentCollection( shapeIndex_demoFor) print('Received demo for letter ' + shapeType) userShape = np.reshape(userShape, (-1, 1)) #explicitly make it 2D array with only one column shape = wordManager.respondToDemonstration(shapeIndex_demoFor, userShape) wordManager.save_all(shapeIndex_demoFor) userShape = [] self.canvas.remove(touch.ud['line']) showShape(shape, shapeIndex_demoFor)
def on_touch_up(self, touch): global userShape wordToLearn = wordManager.getCurrentCollection() if len(userShape) < 5: closeFigures(figuresToClose = range(1,len(wordToLearn)+1)) wordToLearn = raw_input("Next word:\n") newWord(wordToLearn) userInputCapture.display_grid(numColumns = len(wordToLearn)) else: userShape = downsampleShape(userShape,numPoints_shapeModeler,xyxyFormat=True) shapeCentre = ShapeModeler.getShapeCentre(userShape) for i in range(len(wordToLearn)): if(shapeCentre[0] > (self.width/len(wordToLearn))*i): shapeIndex_demoFor = i shapeType = wordManager.shapeAtIndexInCurrentCollection(shapeIndex_demoFor) print('Received demo for letter ' + shapeType) userShape = numpy.reshape(userShape, (-1, 1)); #explicitly make it 2D array with only one column userShape = ShapeModeler.normaliseShapeHeight(numpy.array(userShape)) shape = wordManager.respondToDemonstration(shapeType, userShape) userShape = [] self.canvas.remove(touch.ud['line']) if shape != -1: showShape(shape, shapeIndex_demoFor)
def on_touch_up(self, touch): global userShape touch.ud['line'].points userShape = downsampleShape(userShape,numPoints_shapeModeler,xyxyFormat=True) shapeCentre = ShapeModeler.getShapeCentre(userShape) shapeType = letter print('Received reference for letter ' + shapeType) userShape = numpy.reshape(userShape, (-1, 1)); userShape = ShapeModeler.normaliseShapeHeight(numpy.array(userShape)) filename = dataSetFile print('saving in'+filename) # scan the dataset : lines = [] try: with open(filename, 'r') as f: lines.append(f.readline()) nb_samples = int(lines[0].strip()) for i in range(nb_samples+1): lines.append(f.readline()) except IOError: raise RuntimeError("no reading permission for file"+filename) nb_samples+=1 # past the dataset : try: with open(filename, 'w') as f: f.write('nb_sample:\n') f.write('%i\n'%nb_samples) f.write('nb_pts:\n') f.write('70\n') f.write('ref:\n') f.write(' '.join(map(str,userShape.T[0]))+'\n') f.write('...\n') for i in range(nb_samples-1): f.write(lines[i+2]) except IOError: raise RuntimeError("no writing permission for file"+filename) userShape = [] self.canvas.remove(touch.ud['line'])
def onUserDrawnShapeReceived(path, shapePreprocessingMethod, positionToShapeMappingMethod): global activeShapeForDemonstration_type #preprocess to turn multiple strokes into one path if(shapePreprocessingMethod == 'longestStroke'): path = processShape_longestStroke(strokes); else: path = processShape_firstStroke(strokes); #identify type of shape which demonstration was for location = ShapeModeler.getShapeCentre(path); if(positionToShapeMappingMethod == 'basedOnColumnOfScreen'): shapeType_demoFor = getShapeCode_basedOnColumnOfScreen(location); elif(positionToShapeMappingMethod == 'basedOnRowOfScreen'): shapeType_demoFor = getShapeCode_basedOnColumnOfScreen(location); elif(positionToShapeMappingMethod == 'basedOnShapeAtPosition'): shapeType_demoFor = getShapeCode_basedOnShapeAtPosition(location); else: shapeType_demoFor = getShapeCode_basedOnClosestShapeToPosition(location); #block the space from robot use try: display_shape_at_location = rospy.ServiceProxy('display_shape_at_location', displayShapeAtLocation); request = displayShapeAtLocationRequest(); request.shape_type_code = shapeType_demoFor; request.location.x = location[0]; request.location.y = location[1]; #todo: allow for blocking different sized shapes response = display_shape_at_location(request); result = response.success; #todo: do something if unsuccessful except rospy.ServiceException, e: rospy.logerr("Service call failed: %s",e);
def on_touch_up(self, touch): global userShape touch.ud['line'].points userShape = downsampleShape(userShape,numPoints_shapeModeler,xyxyFormat=True) shapeCentre = ShapeModeler.getShapeCentre(userShape) for i in range(len(wordToLearn)): if(shapeCentre[0] > (self.width/len(wordToLearn))*i): shapeIndex_demoFor = i shapeType = wordManager.shapeAtIndexInCurrentCollection(shapeIndex_demoFor) print('Received demo for letter ' + shapeType) userShape = np.reshape(userShape, (-1, 1)); #explicitly make it 2D array with only one column shape = wordManager.respondToDemonstration(shapeIndex_demoFor, userShape) wordManager.save_all(shapeIndex_demoFor) userShape = [] self.canvas.remove(touch.ud['line']) showShape(shape, shapeIndex_demoFor)
def find_letter(self, path): x,y = ShapeModeler.getShapeCentre(path) return self.closest_letter(x, y)
def on_touch_up(self, touch): global userShape word = [] total_score = 0 rest = userShape scan = downsampleShape(userShape, numPoints_shapeModeler, xyxyFormat=True) shapeCentre = ShapeModeler.getShapeCentre(scan) scan = np.reshape(scan, (-1, 1)) #explicitly make it 2D array with only one column scan = ShapeModeler.normaliseShapeHeight(np.array(scan)) scores = np.array(separator(scan)) scores = scores / np.max(scores) scores = scores * np.abs(scores) scores[scores < 0.1] = 0 indices = np.array(range(len(scores))) sep = indices[scores > 0] #print zip(sep[:-1],sep[1:]) for i in sep: x = scan[i] if len(scan[scan[i:numPoints_shapeModeler] < x]) > 0: scores[i] = 0 if len(scan[scan[:i] > x]) > 0: scores[i] = 0 u = True while u: u = False sep = indices[scores > 0] for i, j in zip(sep[:-1], sep[1:]): if j - i < 5: u = True if scores[i] > scores[j]: scores[j] = 0 else: scores[i] = 0 plt.clf() ShapeModeler.showShape_score(scan, scores) word = [] sep = indices[scores > 0] for i, j in zip(sep[:-1], sep[1:]): shape = scan[i:j + 1].T.tolist()[0] + scan[numPoints_shapeModeler + i:numPoints_shapeModeler + j + 1].T.tolist()[0] shape = downsampleShape(shape, numPoints_shapeModeler) shape = np.reshape(shape, (-1, 1)) shape = ShapeModeler.normaliseShapeHeight(np.array(shape)) best_letter = '?' errors = {} values = [] for letter in abc: space = spaces[letter] error = space.getMinDist(shape) errors[letter] = error values.append(error) best_letter = min(errors, key=errors.get) word.append(best_letter) print 'found ' + str(word) print '######################' print '' userShape = [] self.canvas.remove(touch.ud['line'])
def on_touch_up(self, touch): global userShape word = [] total_score =0 rest = userShape scan = downsampleShape(userShape, numPoints_shapeModeler,xyxyFormat=True) shapeCentre = ShapeModeler.getShapeCentre(scan) scan = np.reshape(scan, (-1, 1)); #explicitly make it 2D array with only one column scan = ShapeModeler.normaliseShapeHeight(np.array(scan)) scores = np.array(separator(scan)) scores = scores/np.max(scores) scores = scores*np.abs(scores) scores[scores<0.1]=0 indices = np.array(range(len(scores))) sep = indices[scores>0] #print zip(sep[:-1],sep[1:]) for i in sep: x = scan[i] if len(scan[scan[i:numPoints_shapeModeler]<x])>0: scores[i]=0 if len(scan[scan[:i]>x])>0: scores[i]=0 u = True while u: u = False sep = indices[scores>0] for i,j in zip(sep[:-1],sep[1:]): if j-i<5: u = True if scores[i]>scores[j]: scores[j] = 0 else: scores[i] = 0 plt.clf() ShapeModeler.showShape_score(scan,scores) word = [] sep = indices[scores>0] for i,j in zip(sep[:-1],sep[1:]): shape = scan[i:j+1].T.tolist()[0] + scan[numPoints_shapeModeler+i:numPoints_shapeModeler+j+1].T.tolist()[0] shape = downsampleShape(shape, numPoints_shapeModeler) shape = np.reshape(shape,(-1,1)) shape = ShapeModeler.normaliseShapeHeight(np.array(shape)) best_letter = '?' errors = {} values = [] for letter in abc: space = spaces[letter] error = space.getMinDist(shape) errors[letter] = error values.append(error) best_letter = min(errors, key = errors.get) word.append(best_letter) print 'found ' + str(word) print '######################' print '' userShape = [] self.canvas.remove(touch.ud['line'])