def normalize_cvimage(cvimage, size=INPUT_SHAPE, mean=None): result = data.resize(cvimage, size) print("premean") if mean is not None: result = result - mean print("pre_result") return result / 255
def telemetry(sid, data): if data: # The current steering angle of the car steering_angle = data["steering_angle"] # The current throttle of the car throttle = data["throttle"] # The current speed of the car speed = data["speed"] # The current image from the center camera of the car imgString = data["image"] image = Image.open(BytesIO(base64.b64decode(imgString))) image_array = resize(np.asarray(image)) steering_angle = float( model.predict(image_array[None, :, :, :], batch_size=1)) throttle = controller.update(float(speed)) print(steering_angle, throttle) send_control(steering_angle, throttle) # save frame if args.image_folder != '': timestamp = datetime.utcnow().strftime('%Y_%m_%d_%H_%M_%S_%f')[:-3] image_filename = os.path.join(args.image_folder, timestamp) image.save('{}.jpg'.format(image_filename)) else: # NOTE: DON'T EDIT THIS. sio.emit('manual', data={}, skip_sid=True)
def run_level(): time = pg.time.get_ticks() while True: dt = pg.time.get_ticks() - time time += dt dt = min(20, dt) for e in pg.event.get(): if e.type == QUIT: data.calculate_dimensions(False) return elif e.type == VIDEORESIZE: data.resize(e.w, e.h, True) data.lvlDriver.resize() else: if not data.lvlDriver.input(e): data.calculate_dimensions(False) return data.lvlDriver.tick(dt) pg.display.flip()
def get_data_from_file(filepath, size=256, mean=None): '''Get image from file ready to be used with the deep neural network''' import data image = cv2.imread(filepath) image = data.resize(image, size) if mean is not None: image = image - mean image = (image) / 255 bits = filepath.split("/") name = bits[-1] return image, name
def get_data_from_folder(test_image_folder, mean=None, size=256): '''Extracts images from image folder and gets them ready for use with the deep neural network''' # dir = os.path.dirname(os.path.abspath(__file__)) images = [] names = [] for subdir, dir, files in os.walk(test_image_folder): for file in files: if file.endswith(".jpg") or file.endswith(".png") or file.endswith( ".jpeg"): filepath = os.path.join(subdir, file) image = cv2.imread(filepath) if image is not None: image = data.resize(image, size=size) names.append(file) if mean is not None: image = image - mean image = image / 255 images.append(image) return np.array(images), names
def choose_level(): reset() if isfile(data.LEVELS): with open(data.LEVELS, 'rb') as file: file_data = file.read() # Loop through the data while file_data: level = Level() file_data = level.from_bytes(file_data) level_data[levels].append(level) if isfile(data.WAVES): with open(data.WAVES, 'rb') as file: file_data = file.read() # Loop through data while len(file_data) > 0: arr, file_data = load_spawn_list(file_data) level_data[spawns].append(arr) resize() while True: for e in pg.event.get(): if e.type == QUIT: save_data() return False elif e.type == VIDEORESIZE: data.resize(e.w, e.h, False) resize() elif e.type == MOUSEMOTION: pos = pg.mouse.get_pos() for i, array in enumerate(level_data): if rects[i].collidepoint(*pos): pos = [pos[0] - rects[i].x, pos[1] - rects[i].y] idx = (pos[1] // item_w) * row_len + (pos[0] // item_w) if idx > len(array): idx = -1 else: idx = -1 if 0 <= idx < len(array) and idx != hovering[i]: hovering[i] = idx if i == levels: previews[i] = array[idx].draw(data.screen_w // 4) else: previews[i] = draw_spawn_list(array[idx], data.screen_w // 4, w=rects[i].w - 1) pg.display.get_surface().fill((0, 0, 0), preview_rects[i]) pg.display.get_surface().blit(previews[i], preview_rects[i]) elif e.type == MOUSEBUTTONUP: if e.button == BUTTON_LEFT: global delete, edit, selected_lvl pos = pg.mouse.get_pos() done = False for i, r in enumerate(rects): if r.collidepoint(*pos): pos = [pos[0] - r.x, pos[1] - r.y] row, col = pos[1] // item_w, pos[0] // item_w idx = row * row_len + col if idx < len(level_data[i]): if delete: history.append([i, idx, level_data[i][idx]]) del level_data[i][idx] if i == levels: # Unselect if selected_lvl >= idx: selected_lvl = -1 update_title() else: # Update blacklist for j in range(len(spawn_blacklist)): if spawn_blacklist[j] > idx: spawn_blacklist[j] -= 1 elif spawn_blacklist[j] == idx: del spawn_blacklist[j] draw() elif edit: obj = level_data[i][idx] result = new_level(lvl=obj) if i == levels else new_wave(spawn_list=obj) if result: level_data[i][idx] = result resize() else: item_r = pg.Rect(col * item_w, row * item_w, item_w, item_w) if i == levels: # Remove yellow border if selected_lvl != -1: row, col = selected_lvl // row_len, selected_lvl % row_len pg.draw.rect(surfaces[i], (0, 0, 0), (col * item_w, row * item_w, item_w, item_w), 2) selected_lvl = idx if idx != selected_lvl else -1 # Draw yellow border if selected_lvl != -1: pg.draw.rect(surfaces[i], (255, 255, 0), item_r, 2) else: if idx in spawn_blacklist: spawn_blacklist.remove(idx) surfaces[i].fill((0, 0, 0), item_r) surfaces[i].blit(back_img, item_r) text_s = lvl_font.render(str(idx + 1), 1, (255, 255, 255)) surfaces[i].blit(text_s, text_s.get_rect(center=item_r.center)) else: spawn_blacklist.append(idx) surfaces[i].blit(overlay, item_r) update_title() d = pg.display.get_surface() d.fill((0, 0, 0), r) d.blit(surfaces[i], r.topleft, area=((0, -off[i]), r.size)) done = True break if not done: for i, key in enumerate(button_order): if button_rects[key].collidepoint(*pos): if key == "new level": result = new_level() if result: level_data[levels].append(result) resize() elif key == "new wave": result = new_wave() if result: level_data[spawns].append(result) resize() elif key == "edit": edit = not edit delete = False draw() elif key == "delete": delete = not delete edit = False draw() elif key == "undo": if len(history) > 0: idx1, idx2, val = history[-1] level_data[idx1].insert(idx2, val) del history[-1] if idx1 == levels: # Update selected if selected_lvl >= idx2: selected_lvl += 1 update_title() else: # Update spawn blacklist for j in range(len(spawn_blacklist)): if spawn_blacklist[j] >= idx2: spawn_blacklist[j] += 1 resize() break elif e.button == BUTTON_WHEELUP or e.button == BUTTON_WHEELDOWN: pos = pg.mouse.get_pos() up = e.button == BUTTON_WHEELUP for i, rect in enumerate(rects): if rect.collidepoint(*pos): off[i] += (data.screen_w // (row_len * 6)) * (1 if up else -1) max_off = rect.h - surfaces[i].get_size()[1] if off[i] < max_off: off[i] = max_off if off[i] > 0: off[i] = 0 d = pg.display.get_surface() d.fill((0, 0, 0), rect) d.blit(surfaces[i], rect.topleft, area=((0, -off[i]), rect.size)) break elif e.type == KEYUP and e.key == K_RETURN and selected_lvl != -1 and \ len(spawn_blacklist) != len(level_data[spawns]): save_data() data.calculate_dimensions(True) data.lvlDriver.set_level(level_data[levels][selected_lvl], [a for i, a in enumerate(level_data[spawns]) if i not in spawn_blacklist]) return True pg.display.flip()
def preprocess_func(x, y): x = read_image(x) x = decode_png(x) x = resize(x, img_height, img_width) return x, y
def prep_func(f, x, y): x = read_image(x) x = decode_png(x) x = resize(x, img_height, img_width) return f, x, y
def new_wave(spawn_list=()): global show_cursor, spawns reset() spawns = list(spawn_list) while True: # Update whether to show the cursor or not temp = show_cursor show_cursor = (pg.time.get_ticks() // 400) % 2 == 0 if temp != show_cursor: draw() for e in pg.event.get(): if e.type == QUIT: return elif e.type == VIDEORESIZE: data.resize(e.w, e.h, False) resize() elif e.type == MOUSEBUTTONUP: if e.button == BUTTON_LEFT: global slider, selected, current if slider == -1: # Offset is include in the rectangles pos = pg.mouse.get_pos() if rects["Count"].collidepoint(*pos): selected = "Count" elif rects["Time"].collidepoint(*pos): selected = "Time" elif rects["Model"].collidepoint(*pos): global model_idx model_idx = (model_idx + 1) % len(models) current.model = models[model_idx] elif rects["Flip"].collidepoint(*pos): current.flip = not current.flip elif rects["Add"].collidepoint(*pos): if current.num_enemies >= 1 and current.duration >= 100: spawns.append(current) reset_current() elif rects["Undo"].collidepoint(*pos): if len(spawns) > 0: current = spawns[-1] spawns = spawns[:-1] draw() update_sliders() elif rects["Clear"].collidepoint(*pos): reset_current() elif rects["Save"].collidepoint(*pos): if len(spawns) > 0: return spawns.copy() else: continue draw() else: slider = -1 elif e.button == BUTTON_WHEELDOWN or e.button == BUTTON_WHEELUP: if rects["Enemies"].collidepoint(*pg.mouse.get_pos()): global slider_off slider_off += rects["SlideBar"].h // 3 * (1 if e.button == BUTTON_WHEELUP else -1) max_off = rects["Enemies"].h - surfaces["Enemies"].get_size()[1] if slider_off < max_off: slider_off = max_off if slider_off > 0: slider_off = 0 d = pg.display.get_surface() d.fill((0, 0, 0), rects["Enemies"]) d.blit(surfaces["Enemies"], rects["Enemies"].topleft, area=((0, -slider_off), rects["Enemies"].size)) elif e.type == MOUSEBUTTONDOWN and e.button == BUTTON_LEFT: pos = pg.mouse.get_pos() if rects["Enemies"].collidepoint(*pos): r = rects["Enemies"] pos = [pos[0] - r.x, pos[1] - r.y - slider_off] # Make sure we are clicking on the slider if slider_ends[0] <= pos[0] <= slider_ends[1]: idx = pos[1] // rects["SlideBar"].h if idx < len(current.chances.keys()): slider = idx elif e.type == MOUSEMOTION and slider != -1: pos = pg.mouse.get_pos() pos = [pos[0] - rects["Enemies"].x, pos[1] - rects["Enemies"].y + slider_off] # Get slider fractions frac = (pos[0] - slider_ends[0]) / rects["SlideBar"].w if frac < 0: frac = 0 elif frac > 1: frac = 1 current.chances[slider] = frac # Update slider update_sliders([slider]) # Update wave image d = pg.display.get_surface() r = rects["Current"] d.fill((0, 0, 0), r) d.blit(current.draw_img(*r.size, True), r.topleft) elif e.type == KEYUP: if e.key == K_BACKSPACE: if selected == "Count": current.num_enemies = current.num_enemies // 10 else: current.duration = current.duration // 10 elif pg.key.name(e.key).isnumeric(): val = int(pg.key.name(e.key)) if selected == "Count": current.num_enemies = min(current.num_enemies * 10 + val, 99) else: current.duration = min(current.duration * 10 + val, 100000) else: continue draw() pg.display.flip()
def normalize_cvimage(cvimage, size=256, mean=None): result = data.resize(cvimage, size) if mean is not None: result = result - mean return result / 255
def new_level(lvl=None): reset() if lvl: global level level = lvl draw() mode = 0 while True: for e in pg.event.get(): if e.type == QUIT: return elif e.type == VIDEORESIZE: data.resize(e.w, e.h, False) draw() elif e.type == MOUSEBUTTONUP and e.button == BUTTON_LEFT: global selected, last_level, current pos = pg.mouse.get_pos() if rect.collidepoint(*pos): pos = [(pos[0] - rect.x) / rect.w, (pos[1] - rect.y) / rect.h] if current.idx == START: current.pos = pos # If we are overriding other level, save them for undo button if level.len > 1: last_level = level level.paths.clear() level.add(current) current = Start() elif current.idx == LINE: level.add(current) current = Line(start=level.end) elif current.idx == CIRCLE: if mode == 0: mode = 1 else: level.add(current) current = Circle() mode = 0 elif pos[0] - data.off_x < side_w: idx = (pos[1] - data.off_y) * len(buttons) // data.screen_w button = buttons[idx] # Clicked save, make sure we have at least one non-start path if button == "Save & Exit" and level.len >= 2: return level # Select background image elif button == "Background": file = choose_file([".png", ".jpg"]) if file: # Get parts of the file name = file[file.rfind("/") + 1:] extension = name[name.index("."):] name = name[:-len(extension)] # Target file target = "res/levels/" + name + extension # Make sure we don't overwrite a file i = -1 while isfile(target): with open(file, 'rb') as f1, open(target, 'rb') as f2: # Files are the same, jus use this file if f1.read() == f2.read(): break # Files are different, change the name else: i += 1 target = "res/levels/" + name + "_{}".format( i) + extension # If the target isn't an existing file, copy our old file to it if not isfile(target): copyfile(file, target) level.img = target data.calculate_dimensions(False) draw() # Clicked undo elif button == "Undo": if level.len > 0: level.paths.pop(-1) elif last_level.len > 0: level.paths = last_level.paths.copy() last_level.paths.clear() else: continue # Change the index so that it recreates the current path object if level.len == 0: current = Start() selected = "Start" draw_options() else: if selected == "Start": current = Start() elif selected == "Line": current = Line(start=level.end) elif selected == "Circle": current = Circle() mode = 0 draw_level() elif selected != buttons[idx] and level.len > 0: selected = buttons[idx] draw_options() if selected == "Start": current = Start() elif selected == "Line": current = Line(start=level.end) elif selected == "Circle": current = Circle() mode = 0 elif e.type == MOUSEMOTION: pos = pg.mouse.get_pos() pos = [(pos[0] - rect.x) / rect.w, (pos[1] - rect.y) / rect.h] pos_ = [min(max(i, 0.), 1.) for i in pos] if current.idx == START: current.pos = pos_ for i in range(2): if abs(.5 - current.pos[i]) < .025: current.pos[i] = .5 elif current.idx == LINE: current.end = pos_ dx, dy = current.end[0] - current.start[0], current.end[ 1] - current.start[1] if abs(dx) < .025: current.end[0] = current.start[0] if abs(dy) < .025: current.end[1] = current.start[1] if abs(abs(dx) - abs(dy)) < .025: r = math.sqrt(dx * dx + dy * dy) delta = r * math.sqrt(2) / 2 current.end[0] = current.start[0] + math.copysign( delta, dx) current.end[1] = current.start[1] + math.copysign( delta, dy) elif current.idx == CIRCLE: if mode == 0: # Get end of last segment end = level.end for i in range(2): if abs(end[i] - pos[i]) < .025: pos[i] = end[i] r = data.get_distance(end, pos) # Set initial circle angle current.theta_i = data.get_angle_pixels(pos, end) current.theta_f = current.theta_i + data.TWO_PI if pos[0] - r >= 0 and pos[0] + r <= 1 and pos[ 1] - r >= 0 and pos[1] + r <= 1: current.center = pos current.radius = r else: trig = [ math.cos(current.theta_i), -math.sin(current.theta_i) ] diameter = 1 for i in range(2): # Distance to negative edge d = end[i] # Number of diameters from starting point to negative edge v = .5 + trig[i] / 2 if v > 0 and d / v < diameter: diameter = d / v # Same for positive edge d = 1 - d v = 1 - v if v > 0 and d / v < diameter: diameter = d / v radius = diameter / 2 # Set center position and radius current.center = [ end[i] - radius * trig[i] for i in range(2) ] current.radius = radius else: # Calculate change in angle dtheta = data.get_angle_pixels(current.center, pos) - ( current.theta_f % data.TWO_PI) # Make sure we are going in the correct direction if dtheta > math.pi: dtheta -= data.TWO_PI elif dtheta < -math.pi: dtheta += data.TWO_PI current.theta_f += dtheta diff = current.theta_f - current.theta_i ten_pi = data.TWO_PI * 5 if diff > ten_pi: current.theta_f = ten_pi + current.theta_i elif diff < -ten_pi: current.theta_f = -ten_pi + current.theta_i half_pi, quarter_pi = math.pi / 2, math.pi / 4 factor = (diff + quarter_pi) // half_pi if abs(diff - half_pi * factor) < half_pi / 10: current.theta_f = factor * half_pi + current.theta_i draw_level() pg.display.flip()