def resample(points, n): eta = PathLength(points) / (n - 1) delta = 0.0 newpoints = [points[0]] length = len(points) i = 1 while i < length: if points[i].id == points[i - 1].id: d = Distance(points[i - 1], points[i]) if (delta + d) >= eta: qx = points[i - 1].x + ( (eta - delta) / d) * (points[i].x - points[i - 1].x) qy = points[i - 1].y + ( (eta - delta) / d) * (points[i].y - points[i - 1].y) q = Point.Point(qx, qy, points[i].id) newpoints.append(q) points.insert(i, q) length = length + 1 delta = 0.0 else: delta = delta + d i = i + 1 if len(newpoints) == n - 1: p = Point.Point(points[length - 1].x, points[length - 1].y, points[length - 1].id) newpoints.append(p) return newpoints
def recognize(self, points): p = [ Point.Point(30, 7, 1), Point.Point(103, 7, 1), Point.Point(66, 7, 2), Point.Point(66, 87, 2) ] t0 = datetime.datetime.now() points = resample(points, NumPoints) points = scale(points) points = translateto(points, Origin) b = float('inf') u = -1 for i in range(0, len(self.pointclouds)): d = self.greedycloudmatch(points, self.pointclouds[i]) if d < b: b = d u = i t1 = datetime.datetime.now() if u == -1: r1 = Result.Result("No match.", 0.0, t1 - t0) r = r1 else: r2 = Result.Result(self.pointclouds[u].name, max((b - 2.0) / -2.0, 0.0), t1 - t0) r = r2 return r
def __init__(self, x_1, y_1, x_2, y_2, point1=None, point2=None): if point1 is None and point2 is None: point1 = Point(x_1, y_1) point2 = Point(x_2, y_2) super().__init__(point1, point2) self.sprite = self.generate_sprite((125, 0, 125))
def __init__(self, x_1, y_1, x_2, y_2, point1=None, point2=None): if point1 is None and point2 is None: point1 = Point(x_1, y_1) point2 = Point(x_2, y_2) self.distance_lines = [] super().__init__(point1, point2) self.sprite = self.generate_sprite((0, 125, 125)) self._generate_distance_lines()
def __init__(self): self._screen = pygame.display.set_mode(self.SIZE) pygame.display.set_caption(self.TITLE) self._done = False self.line = Line() self.lineByPerceptron = Line() self._points = [ Point(random.uniform(-1, 1), random.uniform(-1, 1)) for _ in range(50) ] self.training_point = Point(random.uniform(-1, 1), random.uniform(-1, 1)) self.trainingIndex = 0 self.brain = Perceptron(3)
def translateto(points, pt): c = centroid(points) newpoints = [] for i in range(0, len(points)): qx = points[i].x = pt.x - c.x qy = points[i].y + pt.y - c.y p = Point.Point(qx, qy, points[i].id) newpoints.append(p) return newpoints
def centroid(points): x = 0.0 y = 0.0 for i in range(0, len(points)): x = x + points[i].x y = y + points[i].y x = x / len(points) y = y / len(points) p = Point.Point(x, y, 0) return p
def scale(points): minX = +float('inf') maxX = -float('inf') minY = +float('inf') maxY = -float('inf') for i in range(0, len(points)): minX = min(minX, points[i].x) minY = min(minY, points[i].y) maxX = max(maxX, points[i].x) maxY = max(maxY, points[i].y) size = max(maxX - minX, maxY - minY) newpoints = [] for i in range(0, len(points)): qx = (points[i].x - minX) / size qy = (points[i].y - minY) / size newpoints.append(Point.Point(qx, qy, points[i].id)) return newpoints
def _update(self): """Updates all the entities and train the neural network""" for point in self._points: inputs = [point.x, point.y, point.bias] guess = self.brain.guess(inputs) if guess == point.label: point.color = GREEN else: point.color = RED # The point that serves as a training input_training = [ self.training_point.x, self.training_point.y, self.training_point.bias ] self.brain.train(input_training, self.training_point.label) self.training_point = Point(random.uniform(-1, 1), random.uniform(-1, 1))
def update(self): self.line_of_sight = Line(Point(vector=self.robots[0].center), Point(vector=self.robots[1].center)) self.line_of_sight.update()
import entity.Point as Point from Processors.Pointprocessor import * NumPoints = 32 Origin = Point.Point(0, 0, 0) class PointCloud: def __init__(self, name, points): self.name = name self.points = resample(points, NumPoints) self.points = scale(points) self.points = translateto(points, Origin)
class Recognizer: pointclouds = [] pc = PointCloud.PointCloud("T", [ Point.Point(30, 7.0, 1), Point.Point(103, 7, 1), Point.Point(66, 7, 2), Point.Point(66, 87, 2) ]) pointclouds.append(pc) def recognize(self, points): p = [ Point.Point(30, 7, 1), Point.Point(103, 7, 1), Point.Point(66, 7, 2), Point.Point(66, 87, 2) ] t0 = datetime.datetime.now() points = resample(points, NumPoints) points = scale(points) points = translateto(points, Origin) b = float('inf') u = -1 for i in range(0, len(self.pointclouds)): d = self.greedycloudmatch(points, self.pointclouds[i]) if d < b: b = d u = i t1 = datetime.datetime.now() if u == -1: r1 = Result.Result("No match.", 0.0, t1 - t0) r = r1 else: r2 = Result.Result(self.pointclouds[u].name, max((b - 2.0) / -2.0, 0.0), t1 - t0) r = r2 return r def greedycloudmatch(self, points, P): e = 0.50 step = int(math.floor(math.pow(len(points), 1.0 - e))) minimum = float('inf') for i in range(0, len(points), step): d1 = self.clouddistance(points, P.points, i) d2 = self.clouddistance(P.points, points, i) minimum = min(minimum, min(d1, d2)) return minimum def clouddistance(self, pts1, pts2, start): matched = [] for k in range(0, len(pts1)): matched.append(0) sum = 0 i = start while i != start: index = -1 min = 2147483647 for j in range(0, len(matched)): if matched[j] == 0: d = PointCloud.PointCloud.Distance(pts1[i], pts2[j]) if d < min: min = d index = j matched[index] = 1 weight = 1 - ((i - start + len(pts1)) % len(pts1)) / len(pts1) sum = sum + weight * min i = (i + 1) % len(pts1) return sum
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the names of the University of Washington nor Microsoft, nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Jacob O. Wobbrock OR Andrew D. Wilson OR Yang Li BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ''' from RecognizeService import Recognizer from entity import Point # input points p=[Point.Point(30,7,1),Point.Point(103,7,1),Point.Point(66,7,2),Point.Point(66,87,2)] r=Recognizer.Recognizer() print(r.recognize(p).name)
def _generate_distance_line(self, origin, offset): start = origin end = [origin[0] + offset[0], origin[1] + offset[1]] line = Line(Point(vector=start), Point(vector=end)) return line
def __init__(self, size_x, size_y): self.walls = [] # Create slightly oversized map container self.container = Square(Point(-1, -1), Point(size_x + 1, size_y + 1)) self.wall_sprites = pygame.sprite.Group()