def recognize_shape(self): for k in self.templates: for p in k[0]: self.cloud.append(Point(p[0], p[1], p[2])) #print(points) self.temp.append(Template(k[1], self.cloud)) #print(temp) for k in self.listFigurePoints: self.drawnFigure.append(Point(k[0], k[1], k[2])) recognizer = Recognizer(self.temp) self.recognitionResult = recognizer.recognize(self.drawnFigure, n=self.sampleNumber)
def read(eventstream): # Train the recognizer on the list of template files, # then execute the recognizer on the eventstream file. # ------------------------------------------------------------------------- tf = open("templates.dat", 'r') templates = [] for raw_lines in tf: line = raw_lines.strip() touch_num = 0 gf = open(line, 'r') gname = gf.readline().strip() gpoints = [] for lines in gf: gfline = lines.strip() if gfline == "BEGIN": touch_num += 1 elif gfline == "END": continue else: x, y = int(gfline.split(',')[0]), int(gfline.split(',')[1]) gpoints.append(Point(x, y, touch_num)) t = Template(gname, gpoints) templates.append(t) gname = "" gpoints = [] recog = Recognizer(templates) # ------------------------------------------------------------------------- # Extract the necessary points from the event stream. gestures = [] # list of lists of points gesture = [] # list of points touch_num = 0 es = open(eventstream, 'r') for raw_line in es: line = raw_line.strip() if line == "MOUSEDOWN": touch_num += 1 elif line == "MOUSEUP": continue elif line == "RECOGNIZE": # It's assumed recognize clears after recognition. gestures.append(gesture) gesture = [] touch_num = 0 else: x, y = int(line.split(',')[0]), int(line.split(',')[1]) gesture.append(Point(x, y, touch_num)) es.close() for gesture in gestures: gname, accuracy = recog.recognize(gesture) print(gname)
def read_event_points(event_file): event_points = [] stroke_count = 0 # event_file = "D:/College/Semester 8/Natural User Interaction/Assignment1_$P/A1_TestingFiles/eventfiles/arrowhead_eventfile.txt" EventObject = open(event_file, "r") for line in EventObject: line = line.strip() if (line == 'MOUSEDOWN'): stroke_count = stroke_count + 1 elif (line == 'MOUSEUP'): continue elif (line == 'RECOGNIZE'): result = read_template_points(event_points) # print result event_points = [] stroke_count = 0 else: row = line.split(',') x = int(row[0]) y = int(row[1]) event_points.append(Point(x, y, stroke_count))
def test_recognition_successful(): tmpl = Template('X', [ Point(0, 0, 1), Point(1, 1, 1), Point(0, 1, 2), Point(1, 0, 2) ]) recognizer = Recognizer([tmpl]) result = recognizer.recognize([ Point(30, 146, 1), Point(106, 222, 1), Point(30, 225, 2), Point(106, 146, 2) ]) assert result[0] == 'X'
def test_recognition_failed(): tmpl = Template('X', [ Point(0, 0, 1), Point(1, 1, 1), Point(0, 1, 2), Point(1, 0, 2) ]) recognizer = Recognizer([tmpl]) result = recognizer.recognize([ Point(30, 146, 1), Point(306, 222, 1), ]) assert result[0] is None
def read_template_points(event_points): all_templates = [] path = os.path.join('Templates') if (os.listdir(path) == []): print "No Templates." else: for filename in os.listdir(path): new_path = path + "/" + filename TemplateObject = open(new_path, "r") line = TemplateObject.readline() filename = line.strip() stroke_count = 0 template_points = [] for line in TemplateObject: line = line.strip() row = line.split(',') if (line == 'BEGIN'): stroke_count = stroke_count + 1 elif (line == 'END'): continue else: row = line.split(',') x = int(row[0]) y = int(row[1]) template_points.append(Point(x, y, stroke_count)) new_template = Template(filename, template_points) all_templates.append(new_template) recognizer = Recognizer(all_templates) result = recognizer.recognize(event_points) print(result) # Output: ('X', 0.733770116545184)
from dollarpy import Recognizer, Template, Point # Define 'Template' gestures, each consisting of a name and a list of 'Point' elements. # These 'Point' elements have 'x' and 'y' coordinates and optionally the stroke index a point belongs to. tmpl_1 = Template('X', [Point(0, 0), Point(1, 1), Point(0, 1), Point(1, 0)]) tmpl_2 = Template('line', [Point(0, 0), Point(1, 0)]) tmpl_3 = Template('line', [Point(0, 0), Point(1, 1)]) tmpl_4 = Template('line', [Point(0, 0), Point(0, 1)]) tmpl_5 = Template( 'square', [Point(0, 0), Point(0, 1), Point(1, 1), Point(1, 0)]) # Create a 'Recognizer' object and pass the created 'Template' objects as a list. recognizer = Recognizer([tmpl_1, tmpl_2]) # Call 'recognize(...)' to match a list of 'Point' elements to the previously defined templates. result = recognizer.recognize([ Point(31, 141, 1), Point(109, 222, 1), Point(22, 219, 2), Point(113, 146, 2) ]) print(result) # Output: ('X', 0.733770116545184)
def createArray(magnitude, z): tempArray = [] # print(len(magnitude)) for i in range(len(magnitude)): tempArray.append(Point(magnitude[i], z[i], 1)) return tempArray
import cv2 import time from dollarpy import Recognizer, Template, Point tmpl_1 = Template('X', [ Point(0, 0, 1), Point(1, 1, 1), Point(0, 1, 2), Point(1, 0, 2)]) tmpl_2 = Template('line', [ Point(0, 0), Point(1, 0)]) recognizer = Recognizer([tmpl_1, tmpl_2]) cap = cv2.VideoCapture(0)####### 0 indicates primary camera as for me i use my secondary circle=True prevx=0 prevy=0 framect=0 result=list() laserpoints=list() while(True): ret, frame = cap.read() blur = cv2.GaussianBlur(frame,(5,5),0) height, width, channels = frame.shape gray = cv2.cvtColor(blur, cv2.COLOR_BGR2GRAY)##############han3ml Gray Scaling n3rf aktar no2ta bright we deh hatob2a red dot###### (minVal, maxVal, minLoc, maxLoc) = cv2.minMaxLoc(gray) x , y = maxLoc intensity= gray[y,x] if intensity<254: circle=False for i in range(-1,1): for k in range(-1,1):
def test_point_repr(): point = Point(1, 2, 3) assert str(point) == '(1, 2), stroke 3'
def get_points_from_class(template_points): a = [] for i in range(len(template_points)): a.append( Point(int(template_points[i][0]), int(template_points[i][1]), 1)) return a