def make_open_conic(P0, T0, P2, T2, P): ''' Construct an arbitrary open conic arc in three-dimensional space. The resulting NURBS curve consists of either one, two, or four segments connected with C1 continuity. Source: The NURBS Book (2nd Ed.), Pg. 317. ''' P0, T0 = [np.asfarray(V) for V in P0, T0] P2, T2 = [np.asfarray(V) for V in P2, T2] P = np.asfarray(P) P1, w1 = make_one_arc(P0, T0, P2, T2, P) if w1 <= -1.0: raise ParabolaOrHyperbolaOutsideConvexHull(w1) if w1 >= 1.0: # hyperbola or parabola nsegs = 1 else: # ellipse if w1 > 0.0 and util.angle(P0, P1, P2) > 60.0: nsegs = 1 elif w1 < 0.0 and util.angle(P0, P1, P2) > 90.0: nsegs = 4 else: nsegs = 2 n = 2 * nsegs Pw = np.zeros((n + 1, 4)) U = np.zeros(n + 4) j = 2 * nsegs + 1 U[j:] = 1.0 Pw[0] = np.hstack((P0, 1.0)) Pw[n] = np.hstack((P2, 1.0)) if nsegs == 1: Pw[1] = np.hstack((w1 * P1, w1)) cpol = curve.ControlPolygon(Pw=Pw) return curve.Curve(cpol, (2, ), (U, )) Q1, S, R1, wqr = split_arc(P0, P1, w1, P2) if nsegs == 2: Pw[2] = np.hstack((S, 1.0)) Pw[1] = np.hstack((wqr * Q1, wqr)) Pw[3] = np.hstack((wqr * R1, wqr)) U[3] = U[4] = 0.5 cpol = curve.ControlPolygon(Pw=Pw) return curve.Curve(cpol, (2, ), (U, )) Pw[4] = np.hstack((S, 1.0)) w1 = wqr HQ1, HS, HR1, wqr = split_arc(P0, Q1, w1, S) Pw[2] = np.hstack((HS, 1.0)) Pw[1] = np.hstack((wqr * HQ1, wqr)) Pw[3] = np.hstack((wqr * HR1, wqr)) HQ1, HS, HR1, wqr = split_arc(S, R1, w1, P2) Pw[6] = np.hstack((HS, 1.0)) Pw[5] = np.hstack((wqr * HQ1, wqr)) Pw[7] = np.hstack((wqr * HR1, wqr)) for i in xrange(2): U[i + 3] = 0.25 U[i + 5] = 0.5 U[i + 7] = 0.75 cpol = curve.ControlPolygon(Pw=Pw) return curve.Curve(cpol, (2, ), (U, ))
def do_move_action(self, game, action): # if action[0] == 1 and self.shoot_status['cd'] <= 0: # self.shoot_status['fire'] = False # self.shoot_status['angle'] = action[1] # existence = (self.status['bullet_penetration'] - 1) * 5 + 20 # bullet = Bullet(self.position['x'], self.position['y'], existence, self.status['bullet_damage'], { # 'x': math.cos(action[1]) * (10 + self.status['bullet_speed']), # 'y': math.sin(action[1]) * (10 + self.status['bullet_speed']) # }, self.id) # game.map_info['bullets'].append(bullet) # self.shoot_status['cd'] = 50 * math.log(self.status['bullet_reload'] + 1, 10) # else: # self.shoot_status['fire'] = False self.move_direction = { 'up': False, 'down': False, 'left': False, 'right': False } type_num = np.argwhere(action == 1) if type_num == 1: self.move_direction['up'] = True elif type_num == 2: self.move_direction['down'] = True elif type_num == 3: self.move_direction['left'] = True elif type_num == 4: self.move_direction['right'] = True elif type_num == 5: self.move_direction['up'] = True self.move_direction['left'] = True elif type_num == 6: self.move_direction['up'] = True self.move_direction['right'] = True elif type_num == 7: self.move_direction['down'] = True self.move_direction['left'] = True elif type_num == 8: self.move_direction['down'] = True self.move_direction['right'] = True target = None angle = -1 # find the closest stuff and shoot for stuff in game.map_info['stuffs']: dist = util.distance(self.position, stuff.position) if target: if dist < 200 and util.distance(self.position, target.position) > dist: angle = util.angle(self.position, stuff.position) target = stuff else: if dist < 200: angle = util.angle(self.position, stuff.position) target = stuff if target: self.do_shoot_action(game, [1, angle])
def get_slit_tilt(mid1, mid2): points = [mid1, mid2] points.sort(cmp=compare_y_coordinate) bottom, top = points slitvec = top - bottom up = np.array([0, 1]) return angle(up, slitvec)
def agent_move(self): final_action = np.zeros(8) old_state = self.agent.get_state(self) prediction = self.agent.model.predict(old_state.reshape(1, 20)) final_action = to_categorical(np.argmax(prediction[0]), num_classes=9) self.player_agent.do_move_action(self, final_action) new_state = self.agent.get_state(self) reward = self.agent.set_reward(self, self.player_agent.attr['hp'] <= 0, self.player_agent.hit) self.agent.train_short_memory(old_state, final_action, reward, new_state, self.player_agent.attr['hp'] <= 0, self.player_agent.hit) self.agent.remember(old_state, final_action, reward, new_state, self.player_agent.attr['hp'] <= 0, self.player_agent.hit) target = None angle = -1 # find the closest stuff and shoot for stuff in self.map_info['stuffs']: dist = util.distance(self.player_agent.position, stuff.position) if target: if dist < 200 and util.distance(self.player_agent.position, target.position) > dist: angle = util.angle(self.player_agent.position, stuff.position) target = stuff else: if dist < 200: angle = util.angle(self.player_agent.position, stuff.position) target = stuff if util.distance(self.player_agent.position, self.player_user.position) < 200: angle = util.angle(self.player_agent.position, self.player_user.position) target = self.player_user if target: self.player_agent.do_shoot_action(self, [1, angle])
def valid(line): """Return True if a line is valid: if it is within the angle threshold (see above) and it is not too close to an axis (see above). """ x1, y1, x2, y2 = line if util.angle(line, axes[0]) > thresh_angl: return False else: for axis in axes: if util.distance(line, axis) < thresh_dist: return False return True
def update_control(self, dt): '''Updates the control''' self.force = self.potential_field(dt) # Update speed, depending on status if self.idle(): self.set_speed(self.max_speed) else: self.set_speed(norm(self.force)) # Update steering angle if norm(self.force) != 0: self.set_steering(angle(self.orientation_vec(), self.force)) # Update heading, if out of bounds super(Robot, self).update_control(dt)
def seek_food(self): food_in_reach = [f for rho,f in self.visible_food if rho <= self.reach + util.epsilon] if food_in_reach: best_option = max(food_in_reach, key=lambda f: self._food_eval(f)) return Decisions.EAT, best_option else: food_options = [(r, self._rel_phi(f), self._food_eval(f)) for r,f in self.visible_food] predators = [(r, self._rel_phi(p), self._threat_eval(p)) for r,p in self.visible_critters if self._is_predator(p)] competitors = [(util.dist2(self.loc, c.loc), self._rel_phi(c), self._food_competition_eval(c)) for _,c in self.visible_critters if self._food_competitor(c)] desire = {} for dist, heading, _ in food_options: desire[(dist, heading)] = 0 # summing value of food in this direction for rho, phi, value in food_options: ang = util.angle(heading, phi) desire[(dist, heading)] += value/( (ang/self.nav_angleoffset_food) + (rho/self.nav_distance_food) ) # subtracting value of competitors in this direction for comp_dist, phi, competition in competitors: ang = util.angle(heading, phi) desire[(dist, heading)] -= competition/( (ang/self.nav_angleoffset_competitor) + (comp_dist/self.nav_distance_competitor) ) # subtracting value of predators in this direction for rho, phi, threat in predators: ang = util.angle(heading, phi) desire[(dist, heading)] -= threat/( (ang/self.nav_angleoffset_predator) + (rho/self.nav_distance_pred) ) if any(value > 0 for value in desire.values()): dist, heading = max(desire, key=lambda k: desire[k]) dist = dist - self.reach return Decisions.SEEK_FOOD, (dist, heading) else: return None, None
def get_state(self, game): self.stopping = True if game.player.shoot_status['fire']: self.stopping = False self.stopping_step = 0 if self.stopping: self.stopping_step += 1 state = [ game.map_info['stuffs'][0].position['x'] / game.game_width, game.map_info['stuffs'][0].position['y'] / game.game_height, game.player.position['x'] / game.game_width, game.player.position['y'] / game.game_height, util.angle(game.player.position, game.map_info['stuffs'][0].position) / (math.pi * 2) ] return np.asarray(state)
def getPathDuration(self, linSpeed, angSpeed): """ linSpeed : float, angSpeed : float returns : float sums the duration of each segment and each rotation """ lastPoint = None lastSegment = None duration = 0 for x, y in self.path: if lastPoint != None: duration += util.dist(lastPoint, (x, y)) / linSpeed dX = x - lastPoint[0] dY = y - lastPoint[1] if lastSegment != None: a = (180 / math.pi) * util.angle(lastSegment, (dX, dY)) a = abs(a) if a > 90: a = 180 - a duration += a / angSpeed lastSegment = (dX, dY) lastPoint = (x, y) return duration
def getPathDuration(self, linSpeed, angSpeed) : """ linSpeed : float, angSpeed : float returns : float sums the duration of each segment and each rotation """ lastPoint = None lastSegment = None duration = 0 for x,y in self.path : if lastPoint != None : duration += util.dist(lastPoint, (x,y)) / linSpeed dX = x - lastPoint[0] dY = y - lastPoint[1] if lastSegment != None : a = (180/math.pi) * util.angle(lastSegment, (dX,dY)) a = abs(a) if a > 90 : a = 180 - a duration += a / angSpeed lastSegment = (dX,dY) lastPoint = (x,y) return duration
def flee(self): predators = [p for p in self.visible_critters if self._is_predator(p)] if not predators: return None, None danger_arcs = [] biggest_arc = 0 for rho, pred in predators: phi = self._rel_phi(pred) arc = self._threat_eval(pred) / (rho ** (3.0/2)) danger_arcs.append(phi, arc) biggest_arc = max(biggest_arc, arc) if biggest_arc >= pi: scale = pi/biggest_arc - util.epsilon # scale down largest interval to at most 2pi-2ep else: scale = 1 safe_intervals = [] while not safe_intervals: intervals = [(phi-desperation*arc, phi+desperation*arc) for phi,arc in danger_arcs] safe_intervals = util.subtract_intervals(intervals) scale *= 0.8 right, left = max(safe_intervals, key=lambda i: i[1]-i[0]) heading = right + util.angle(right, left)/2 return Decisions.FLEE, (self.max_speed, heading)
def seek_mate(self): mate_in_reach = [m for rho,m in self.visible_critters if self._valid_mate(m) and rho <= self.reach + util.epsilon] if mate_in_reach: best_option = max(mate_in_reach, key=lambda m: self._mate_eval(m)) return Decisions.BREED_SEX, best_option else: predators = [(r, self._rel_phi(p), self._threat_eval(p)) for r,p in self.visible_critters if self._is_predator(p)] desire = {} for dist, mate_option in [(r,m) for r,m in self.visible_critters if self._valid_mate(m)]: heading = self._rel_phi(mate_option) desire[(dist, heading)] = self._mate_eval(mate_option)/(dist/self.nav_distance_mate) # subtracting value of predators in this direction for rho, phi, threat in predators: ang = util.angle(heading, phi) desire[(dist, heading)] -= threat/( (ang/self.nav_angleoffset_predator) + (rho/self.nav_distance_pred) ) if any(value > 0 for value in desire.values()): dist, heading = max(desire, key=lambda k: desire[k]) dist = dist - self.reach return Decisions.SEEK_MATE, (dist, heading) else: return None, None
u.intersection(le[0], se[1]), u.intersection(le[1], se[0]), u.intersection(le[1], se[1])) # Show the raw vertices without epsilon if SHOW: plt.scatter(np.array(v)[:,0], np.array(v)[:,1]) # Calculate the diagonal lines of the bounding box diags = (u.line(v[0], v[3]), u.line(v[1], v[2])) # Calculate the center, width, height, and angle of the bounding box c = u.intersection(diags[0], diags[1]) # Find the center by looking at the intersection w = u.length(v[0], v[1]) + EPSILON # Find the width h = u.length(v[0], v[2]) + EPSILON # Find the height a = u.angle(v[3], v[2]) # Find the angle wBucket = u.bucketCount(wBucket, w, 10) hBucket = u.bucketCount(hBucket, h, 10) aBucket = u.bucketCount(aBucket, a, 10) if SHOW: plt.scatter(c[0], c[1]) plt.annotate(a, (c[0], c[1])) line = str(c[0]) + " " + str(c[1]) + " " + str(w) + " " + str(h) + " " + str(label) + " " + str(a) + "\n" rboxLines.append(line) if PRINT: print(line)
def heading_in_bounds(self): '''Returns if the agent is heading back in bounds''' return abs(angle(self.position(), self.orientation_vec())) > pi/2
def get_state(self, game): self.stopping = True # record previous position if not round(game.player_agent.position['x'], 1) == round( self.prev_pos['x'], 1): self.prev_pos['x'] = game.player_agent.position['x'] self.stopping = False self.stopping_step = 0 if not round(game.player_agent.position['y'], 1) == round( self.prev_pos['y'], 1): self.prev_pos['y'] = game.player_agent.position['y'] self.stopping = False self.stopping_step = 0 if self.stopping: self.stopping_step += 1 # screen = pygame.surfarray.array2d(game.game_display) # return screen.reshape(screen.shape[0], screen.shape[1], 1) target_pos = util.dense_position(game, 5) state = [ round(game.player_agent.position['x'] / game.game_width, 2), # x position round(game.player_agent.position['y'] / game.game_height, 2), # y position round( game.player_agent.velocity['x'] / math.sqrt(game.player_agent.status['move_speed'] + 5), 2), # x movement round( game.player_agent.velocity['y'] / math.sqrt(game.player_agent.status['move_speed'] + 5), 2), # y movement round(game.player_agent.acceleration['up'] / 10, 2), # up acceleration volumn round(game.player_agent.acceleration['down'] / 10, 2), # down acceleration volumn round(game.player_agent.acceleration['left'] / 10, 2), # left acceleration volumn round(game.player_agent.acceleration['right'] / 10, 2), # right acceleration volumn round( abs(game.player_agent.position['x'] - (game.game_width / 2)) / (game.game_width / 2), 2), # player_agent x distance ratio to center round( abs(game.player_agent.position['y'] - (game.game_width / 2)) / (game.game_height / 2), 2), # player_agent y distance ratio to center round( abs(game.player_agent.position['x'] - target_pos['x']) / game.game_width, 2), # target position x round( abs(game.player_agent.position['y'] - target_pos['y']) / game.game_height, 2), # target position y 0, 0, 0, 0, 0, 0, 0, 0 ] # compute the grid-cell value v_x = 0 #max(game.player_agent.position['x'] - game.field['width'] / 2, 0) v_y = 0 #max(game.player_agent.position['y'] - game.field['height'] / 2, 0) player_x = game.player_agent.position['x'] player_y = game.player_agent.position['y'] weight = 0 for stuff in game.map_info['stuffs']: if not util.distance(game.player_agent.position, stuff.position): continue weight += 1 / util.distance(game.player_agent.position, stuff.position) angle = util.angle(game.player_agent.position, stuff.position) angle += 2 * math.pi if angle < 0 else 0 if 0 < angle <= math.pi / 4: state[12] += 1 / util.distance(game.player_agent.position, stuff.position) if math.pi / 4 < angle <= math.pi / 2: state[13] += 1 / util.distance(game.player_agent.position, stuff.position) if math.pi / 2 < angle <= math.pi / 4 * 3: state[14] += 1 / util.distance(game.player_agent.position, stuff.position) if math.pi / 4 * 3 < angle <= math.pi: state[15] += 1 / util.distance(game.player_agent.position, stuff.position) if math.pi < angle <= math.pi / 4 * 5: state[16] += 1 / util.distance(game.player_agent.position, stuff.position) if math.pi / 4 * 5 < angle <= math.pi / 2 * 3: state[17] += 1 / util.distance(game.player_agent.position, stuff.position) if math.pi / 2 * 3 < angle <= math.pi / 4 * 7: state[18] += 1 / util.distance(game.player_agent.position, stuff.position) if math.pi / 4 * 7 < angle <= math.pi * 2: state[19] += 1 / util.distance(game.player_agent.position, stuff.position) if weight > 0: state[12] = round(state[12] / weight, 2) state[13] = round(state[13] / weight, 2) state[14] = round(state[14] / weight, 2) state[15] = round(state[15] / weight, 2) state[16] = round(state[16] / weight, 2) state[17] = round(state[17] / weight, 2) state[18] = round(state[18] / weight, 2) state[19] = round(state[19] / weight, 2) return np.asarray(state)
def find_page(img): """This function uses a Hough line transform to detect the four sides of a piece of paper in the image, applies an inverse warp transformation to correct for perspective distortion, returning the corrected image. """ rows, cols = img.shape[:2] img2 = cv2.GaussianBlur(img, (params.BLUR_RADIUS, params.BLUR_RADIUS), 0) """ util.show('Blurred', img2) """ # use Canny edge deduction to obtain an edge map edge_map_bw = cv2.Canny(img2, params.CANNY_THRESHOLD_LOW, params.CANNY_THRESHOLD_HIGH) """ util.show('Edges', edge_map_bw) """ # use the probablistic Hough transform lines = cv2.HoughLinesP(edge_map_bw, params.HOUGH_DISTANCE_RESOLUTION, params.HOUGH_ANGLE_RESOLUTION, params.HOUGH_THRESHOLD, minLineLength=params.HOUGH_MIN_LINE_LENGTH, maxLineGap=params.HOUGH_MAX_LINE_GAP) if lines is None: util.debug('detected no image outlines') return else: lines = lines[0].tolist() util.debug('detected %d image outlines: %s' % (len(lines), str(lines))) edge_map_color = cv2.cvtColor(edge_map_bw, cv2.COLOR_GRAY2BGR) while True: for a in lines: """ util.debug('current line: %s' % str(a)) """ too_similar = False for b in lines: if a == b: continue t = util.angle(a, b) d = util.distance(a, b) """ util.debug('other line: %s (angle: %f)' % (str(b), t)) """ if d < params.DISTANCE_THRESHOLD and t < params.ANGLE_THRESHOLD: too_similar = True break if too_similar: """ util.debug('removing %s' % str(a)) """ lines.remove(a) break if not too_similar: break util.debug('kept %d image outlines: %s' % (len(lines), str(lines))) for x1, y1, x2, y2 in lines: cv2.line(edge_map_color, (x1, y1), (x2, y2), util.RED, params.LINE_THICKNESS) """ util.show('Lines', edge_map_color) """ if len(lines) != 4: return None corners = set() for a in lines: for b in lines: if a == b: continue i = util.intersection(a, b) if i[0] >= 0 and i[0] < cols and i[1] >= 0 and i[1] < cols: corners.add(i) # take the most extreme four corners to be the corners of the page asc_by_x = lambda seq: sorted(seq, cmp=lambda a, b: int(a[0] - b[0])) asc_by_y = lambda seq: sorted(seq, cmp=lambda a, b: int(a[1] - b[1])) dsc_by_x = lambda seq: sorted(seq, cmp=lambda a, b: int(b[0] - a[0])) dsc_by_y = lambda seq: sorted(seq, cmp=lambda a, b: int(b[1] - a[1])) ul = asc_by_y(asc_by_x(corners))[0] corners.remove(ul) ur = asc_by_y(dsc_by_x(corners))[0] corners.remove(ur) ll = dsc_by_y(asc_by_x(corners))[0] corners.remove(ll) lr = dsc_by_y(dsc_by_x(corners))[0] corners.remove(lr) for x, y in [ul, ur, ll, lr]: cv2.circle(edge_map_color, (x, y), 3, util.GREEN, -1) """ util.show('Lines & corners', edge_map_color) """ # use these four corners to construct a transformation matrix # for the height/width of the new image, we use a bounding box rows2 = max(ll[1], lr[1]) - min(ul[1], ur[1]) cols2 = max(ur[0], lr[0]) - min(ul[0], ll[0]) """ print("source: ", numpy.array([ul, ur, ll, lr]).astype('float32')) print("dest: ", numpy.array( [ [0, 0], [cols2 - 1, 0], [cols2 - 1, rows2 - 1], [0, rows2 - 1] ] ).astype('float32')) """ m = cv2.getPerspectiveTransform( numpy.array([ul, ur, ll, lr]).astype('float32'), numpy.array([[0, 0], [cols2 - 1, 0], [cols2 - 1, rows2 - 1], [0, rows2 - 1]]).astype('float32')) corrected = cv2.warpPerspective(img, m, (cols2, rows2)) """ util.show('Corrected', corrected) """ return corrected
import util, math from shapely.geometry import Point print util.angle(Point(0,-1), Point(0,0), Point(0,1)) print util.find_angle_from_x_axis(Point(0, 0), Point(-1, 1))
def get_state(self, game): self.stopping = True # record previous position if not round(game.player.position['x'], 2) == round( self.prev_pos['x'], 2): self.prev_pos['x'] = game.player.position['x'] self.stopping = False self.stopping_step = 0 if not round(game.player.position['y'], 2) == round( self.prev_pos['y'], 2): self.prev_pos['y'] = game.player.position['y'] self.stopping = False self.stopping_step = 0 if self.stopping: self.stopping_step += 1 # screen = pygame.surfarray.array2d(game.game_display) # return screen.reshape(screen.shape[0], screen.shape[1], 1) target_pos = util.dense_position(game, 5) state = [ # game.player.shoot_status['fire'], # player shoot fire # game.player.shoot_status['angle'], # player shoot angle # game.player.shoot_status['cd'], # player shoot CD time # game.player.attr['hp'] / game.player.attr['maxhp'], # player hp round(game.player.position['x'] / game.game_width, 2), # x position round(game.player.position['y'] / game.game_height, 2), # y position round( game.player.velocity['x'] / math.sqrt(game.player.status['move_speed'] + 5), 2), # x movement round( game.player.velocity['y'] / math.sqrt(game.player.status['move_speed'] + 5), 2), # y movement round(game.player.acceleration['up'] / 10, 2), # up acceleration volumn round(game.player.acceleration['down'] / 10, 2), # down acceleration volumn round(game.player.acceleration['left'] / 10, 2), # left acceleration volumn round(game.player.acceleration['right'] / 10, 2), # right acceleration volumn round( abs(game.player.position['x'] - (game.game_width / 2)) / (game.game_width / 2), 2), # player x distance ratio to center round( abs(game.player.position['y'] - (game.game_width / 2)) / (game.game_height / 2), 2), # player y distance ratio to center round( abs(game.player.position['x'] - target_pos['x']) / game.game_width, 2), # target position x round( abs(game.player.position['y'] - target_pos['y']) / game.game_height, 2), # target position y 0, 0, 0, 0, 0, 0, 0, 0 ] # compute the grid-cell value v_x = 0 #max(game.player.position['x'] - game.field['width'] / 2, 0) v_y = 0 #max(game.player.position['y'] - game.field['height'] / 2, 0) player_x = game.player.position['x'] player_y = game.player.position['y'] weight = 0 for stuff in game.map_info['stuffs']: if not util.distance(game.player.position, stuff.position): continue weight += 1 / util.distance(game.player.position, stuff.position) angle = util.angle(game.player.position, stuff.position) angle += 2 * math.pi if angle < 0 else 0 if 0 < angle <= math.pi / 4: state[12] += 1 / util.distance(game.player.position, stuff.position) if math.pi / 4 < angle <= math.pi / 2: state[13] += 1 / util.distance(game.player.position, stuff.position) if math.pi / 2 < angle <= math.pi / 4 * 3: state[14] += 1 / util.distance(game.player.position, stuff.position) if math.pi / 4 * 3 < angle <= math.pi: state[15] += 1 / util.distance(game.player.position, stuff.position) if math.pi < angle <= math.pi / 4 * 5: state[16] += 1 / util.distance(game.player.position, stuff.position) if math.pi / 4 * 5 < angle <= math.pi / 2 * 3: state[17] += 1 / util.distance(game.player.position, stuff.position) if math.pi / 2 * 3 < angle <= math.pi / 4 * 7: state[18] += 1 / util.distance(game.player.position, stuff.position) if math.pi / 4 * 7 < angle <= math.pi * 2: state[19] += 1 / util.distance(game.player.position, stuff.position) if weight > 0: state[12] = round(state[12] / weight, 2) state[13] = round(state[13] / weight, 2) state[14] = round(state[14] / weight, 2) state[15] = round(state[15] / weight, 2) state[16] = round(state[16] / weight, 2) state[17] = round(state[17] / weight, 2) state[18] = round(state[18] / weight, 2) state[19] = round(state[19] / weight, 2) # for diep in game.map_info['dieps']: # if (current_x < diep.position['x'] < current_x + game.field['width'] / self.grid_size and # current_y < diep.position['y'] < current_y + game.field['height'] / self.grid_size): # if (diep.id == game.player.id): # continue # score += 3 # for stuff in game.map_info['stuffs']: # if (current_x < stuff.position['x'] < current_x + game.field['width'] / self.grid_size and # current_y < stuff.position['y'] < current_y + game.field['height'] / self.grid_size): # score += 1 # for bullet in game.map_info['bullets']: # if (current_x < bullet.position['x'] < current_x + game.field['width'] / self.grid_size and # current_y < bullet.position['y'] < current_y + game.field['height'] / self.grid_size and # not bullet.owner == game.player.id): # score -= 5 # for trap in game.map_info['traps']: # if (current_x < trap.position['x'] < current_x + game.field['width'] / self.grid_size and # current_y < trap.position['y'] < current_y + game.field['height'] / self.grid_size): # score += 1 # state.append(score / 50) return np.asarray(state)
def find_page(img): """This function uses a Hough line transform to detect the four sides of a piece of paper in the image, applies an inverse warp transformation to correct for perspective distortion, returning the corrected image. """ rows, cols = img.shape[:2] img2 = cv2.GaussianBlur(img, (params.BLUR_RADIUS, params.BLUR_RADIUS), 0) """ util.show('Blurred', img2) """ # use Canny edge deduction to obtain an edge map edge_map_bw = cv2.Canny(img2, params.CANNY_THRESHOLD_LOW, params.CANNY_THRESHOLD_HIGH) """ util.show('Edges', edge_map_bw) """ # use the probablistic Hough transform lines = cv2.HoughLinesP( edge_map_bw, params.HOUGH_DISTANCE_RESOLUTION, params.HOUGH_ANGLE_RESOLUTION, params.HOUGH_THRESHOLD, minLineLength=params.HOUGH_MIN_LINE_LENGTH, maxLineGap=params.HOUGH_MAX_LINE_GAP, ) if lines is None: util.debug("detected no image outlines") return else: lines = lines[0].tolist() util.debug("detected %d image outlines: %s" % (len(lines), str(lines))) edge_map_color = cv2.cvtColor(edge_map_bw, cv2.COLOR_GRAY2BGR) while True: for a in lines: """ util.debug('current line: %s' % str(a)) """ too_similar = False for b in lines: if a == b: continue t = util.angle(a, b) d = util.distance(a, b) """ util.debug('other line: %s (angle: %f)' % (str(b), t)) """ if d < params.DISTANCE_THRESHOLD and t < params.ANGLE_THRESHOLD: too_similar = True break if too_similar: """ util.debug('removing %s' % str(a)) """ lines.remove(a) break if not too_similar: break util.debug("kept %d image outlines: %s" % (len(lines), str(lines))) for x1, y1, x2, y2 in lines: cv2.line(edge_map_color, (x1, y1), (x2, y2), util.RED, params.LINE_THICKNESS) """ util.show('Lines', edge_map_color) """ if len(lines) != 4: return None corners = set() for a in lines: for b in lines: if a == b: continue i = util.intersection(a, b) if i[0] >= 0 and i[0] < cols and i[1] >= 0 and i[1] < cols: corners.add(i) # take the most extreme four corners to be the corners of the page asc_by_x = lambda seq: sorted(seq, cmp=lambda a, b: int(a[0] - b[0])) asc_by_y = lambda seq: sorted(seq, cmp=lambda a, b: int(a[1] - b[1])) dsc_by_x = lambda seq: sorted(seq, cmp=lambda a, b: int(b[0] - a[0])) dsc_by_y = lambda seq: sorted(seq, cmp=lambda a, b: int(b[1] - a[1])) ul = asc_by_y(asc_by_x(corners))[0] corners.remove(ul) ur = asc_by_y(dsc_by_x(corners))[0] corners.remove(ur) ll = dsc_by_y(asc_by_x(corners))[0] corners.remove(ll) lr = dsc_by_y(dsc_by_x(corners))[0] corners.remove(lr) for x, y in [ul, ur, ll, lr]: cv2.circle(edge_map_color, (x, y), 3, util.GREEN, -1) """ util.show('Lines & corners', edge_map_color) """ # use these four corners to construct a transformation matrix # for the height/width of the new image, we use a bounding box rows2 = max(ll[1], lr[1]) - min(ul[1], ur[1]) cols2 = max(ur[0], lr[0]) - min(ul[0], ll[0]) """ print("source: ", numpy.array([ul, ur, ll, lr]).astype('float32')) print("dest: ", numpy.array( [ [0, 0], [cols2 - 1, 0], [cols2 - 1, rows2 - 1], [0, rows2 - 1] ] ).astype('float32')) """ m = cv2.getPerspectiveTransform( numpy.array([ul, ur, ll, lr]).astype("float32"), numpy.array([[0, 0], [cols2 - 1, 0], [cols2 - 1, rows2 - 1], [0, rows2 - 1]]).astype("float32"), ) corrected = cv2.warpPerspective(img, m, (cols2, rows2)) """ util.show('Corrected', corrected) """ return corrected
def start(): pygame.init() agent = DQNShootAgent() counter_games = 0 score_plot = [] elapsed_time_plot = [] counter_plot = [] record = 0 while counter_games < 10000: # initialize the network game = Game(800, 600, agent, record) # reward counter current_reward = 0 # measure elapsed time start_time = time.time() if display_option: game.display() old_state = [] while not (len(game.map_info['stuffs']) == 0): if display_option: game.update() pygame.time.wait(speed) final_action = np.zeros(2) if len(game.map_info['stuffs']) > 0: old_state = agent.get_state(game) # agent.learning_rate = math.log(max(1.0001, 1.5 - counter_games / 80)) agent.epslion = 10000 - counter_games if random.randint(0, 10000) < agent.epslion: angle = random.uniform(0, 1) if len(game.map_info['stuffs']): angle = util.angle(game.player.position, game.map_info['stuffs'][0].position) angle += 2 * math.pi if angle < 0 else 0 final_action = [1, angle] else: prediction = agent.model.predict(old_state.reshape(1, 5)) # print('prediction:', prediction) final_action[0] = 1 final_action[1] = prediction[0][0] game.player.do_shoot_action(game, final_action) new_state = [] if len(game.map_info['stuffs']) > 0: new_state = agent.get_state(game) old_state = new_state else: new_state = old_state reward = agent.set_reward(game, game.player.attr['hp'] <= 0, game.player.hit) current_reward += reward if game.player.shoot_status['fire']: print('reward: ', reward, 'action: ', final_action) agent.train_short_memory(old_state, final_action, reward, new_state, game.player.attr['hp'] <= 0, game.player.hit) agent.remember(old_state, final_action, reward, new_state, game.player.attr['hp'] <= 0, game.player.hit) record = max(game.score, record) elapsed_time = time.time() - start_time agent.replay_new(agent.memory) counter_games += 1 print('Game %d, Reward: %d, Elapsed time: %d' % (counter_games, current_reward, elapsed_time)) score_plot.append(current_reward) elapsed_time_plot.append(elapsed_time) counter_plot.append(counter_games) agent.model.save_weights('shoot_weights.hdf5') game.plot_seaborn(counter_plot, score_plot)