def __init__(self, rect=None, health=100, barImg=None, barImg2=None, borderImg=None, x=10, y=13, bx=5, by=-4): """ Initialize the healthBar's variables and draw the healthBar once. barImg and borderImg are the images to draw, and x and y are the position to draw at. self.length is the length of the bar. bx and by represent how much the border should be moved from the main bar. barImg2 is the part of the bar that is shown if health is missing. barImg2 should be the same length as barImg. """ self.rect = rect if rect else pygame.Rect() super().__init__(name=None, rect=self.rect) self.maxHealth = health self.health = health self.x = x self.y = y self.bx = bx self.by = by if barImg: self.barImg = helper.load_image(barImg) self.length = self.barImg.get_rect()[2] self.width = self.barImg.get_rect()[3] if barImg2: self.barImg2 = helper.load_image(barImg2) if borderImg: self.borderImg = helper.load_image(borderImg) self.draw(self.x, self.y, self.bx, self.by)
def load(cont_path, style_path, device, img_size): ''' A small function for loading and preparing the necessities.\n `cont_path`: Path/Link to the content image.\n `style_path`: Path/Link to the style image.\n `device`: The device for the model and the images.\n `img_size`: The desired size for the image. ''' content_image = load_image(cont_path, device, img_size) _, _, w, h = content_image.shape style_image = load_image(style_path, device, (w, h)) target = content_image.clone().requires_grad_(True).to(device) vgg = models.vgg19(pretrained=True).features.eval().to(device) content_features = get_features(content_image, vgg, layers) style_features = get_features(style_image, vgg, layers) style_grams = { layer: gram_matrix(style_features[layer]) for layer in style_features } return content_features, style_grams, target, vgg
def __init__(self, type, pos): # Call Sprite initializer pygame.sprite.Sprite.__init__(self) self.image, self.rect = helper.load_image('tile' + type + '.bmp') self.altImage, self.altRect = None, None if type is '1': self.altImage, self.altRect = helper.load_image('tile' + type + '_hl.bmp') self.rect.center = pos self.type = type
def __init__(self, cardData, gridImg, oImg, xImg, iImg, name=None, pos=None, **kwargs): """ Initializes ViewCard. Takes cardData to render from. Take gridImg as background and o/x/iImg as RPS symbols. Optional pos argument is (x, y) tuple defining the top left corner to render at. Optional name argument for the name of the element """ # Load the grid image if gridImg: self._gridImg = helper.load_image(gridImg) else: raise AssertionError('Missing background for ViewCard.') # Set the rectangle accordingly temp_rect = self._gridImg.get_rect() # Move the rectangle to the desired position if pos: temp_rect.move_ip(*pos) super().__init__(name=name, rect=temp_rect, **kwargs) # Save the card data, raise error if none if cardData: self._cardData = cardData else: raise AssertionError('Missing card data for ViewCard.') # Load the symbol images if oImg and xImg and iImg: self._oImg = helper.load_image(oImg) self._xImg = helper.load_image(xImg) self._iImg = helper.load_image(iImg) if (self._oImg.get_width() == self._xImg.get_width() == self._iImg.get_width()): self._symbolSize = self._oImg.get_width() else: raise AssertionError('Symbol sizes don\'t match.') else: raise AssertionError('Missing RPS symbols for ViewCard.') # Finally, update our image self.update()
def validation_score(model_path, write_output=False): model = Inception3D(input_shape=(configs.LENGTH, configs.IMG_HEIGHT, configs.IMG_WIDTH, configs.CHANNELS), weights_path=model_path) # steerings and images steering_labels = path.join(configs.VAL_DIR, 'labels.csv') df_truth = pd.read_csv(steering_labels, usecols=['frame_id', 'steering_angle'], index_col=None) esum = 0 inputs = [] predictions = [] start_time = time.time() for i in range(configs.LENGTH): file = configs.VAL_DIR + "center/" + str(df_truth['frame_id'].loc[i]) + ".jpg" img = helper.load_image(file) inputs.append(img) # Run through all images for i in range(configs.LENGTH, len(df_truth)): img = helper.load_image(configs.VAL_DIR + "center/" + str(df_truth['frame_id'].loc[i]) + ".jpg") inputs.pop(0) inputs.append(img) prediction = model.model.predict(np.array([inputs]))[0] prediction = prediction[0] actual_steers = df_truth['steering_angle'].loc[i] e = (actual_steers - prediction) ** 2 esum += e predictions.append(prediction) if len(predictions) % 1000 == 0: print('.') print("time per step: %s seconds" % ((time.time() - start_time) / len(predictions))) if write_output: print("Writing predictions...") pd.DataFrame({"steering_angle": predictions}).to_csv('./result.csv', index=False, header=True) print("Done!") else: print("Not writing outputs") print("Done") return math.sqrt(esum / len(predictions))
def evaluate(image): attention_plot = np.zeros((max_length, attention_features_shape)) hidden = decoder.reset_state(batch_size=1) temp_input = tf.expand_dims(load_image(image)[0], 0) img_tensor_val = image_features_extract_model(temp_input) img_tensor_val = tf.reshape( img_tensor_val, (img_tensor_val.shape[0], -1, img_tensor_val.shape[3])) features = encoder(img_tensor_val) dec_input = tf.expand_dims([tokenizer.word_index['<start>']], 0) result = [] for i in range(max_length): predictions, hidden, attention_weights = decoder( dec_input, features, hidden) attention_plot[i] = tf.reshape(attention_weights, (-1, )).numpy() predicted_id = tf.random.categorical(predictions, 1)[0][0].numpy() result.append(tokenizer.index_word[predicted_id]) if tokenizer.index_word[predicted_id] == '<end>': return result, attention_plot dec_input = tf.expand_dims([predicted_id], 0) attention_plot = attention_plot[:len(result), :] return result, attention_plot
def main(): args = parser.parse_args() images = pd.read_json(args.file) if (args.extract_histogram): images['histogram'] = np.empty if (args.extract_structure): images['structure'] = np.empty if (args.extract_features): images['keypoints'] = np.empty images['descriptors'] = np.empty for idx, row in images.iterrows(): print(f'Processing images: {idx+1} of {len(images)}', end='\r') image = helper.load_image(row['image']) image = helper.resize(image, width=args.width, height=args.height) if (args.extract_histogram): images.at[idx, 'histogram'] = helper.calcHist(image, args.bins) if (args.extract_structure): structure = image if args.color else helper.convert_to_black_white( image) images.at[idx, 'structure'] = helper.resize(structure, args.size, args.size) if (args.extract_features): key, desc = helper.extract_from_image(image, args.keypoints) images.at[idx, 'keypoints'], images.at[idx, 'descriptors'] = key, desc print('\nDone!') images.to_pickle(args.output)
def createSubmission(model, window_size): submission_filename = 'submission.csv' image_filenames = [] prediction_test_dir = "predictions_test/" if not os.path.isdir(prediction_test_dir): os.mkdir(prediction_test_dir) pred_filenames = [] for i in range(1, constants.TEST_SIZE + 1): image_filename = '../dataset/test_set_images/test_' + str( i) + "/test_" + str(i) + ".png" image_filenames.append(image_filename) test_imgs = [ load_image(image_filenames[i]) for i in range(constants.TEST_SIZE) ] for i in range(constants.TEST_SIZE): pimg = get_prediction(test_imgs[i], model, window_size) #save prediction next to the image cimg = concatenate_images(test_imgs[i], pimg) Image.fromarray(cimg).save(prediction_test_dir + "prediction_mask_" + str(i) + ".png") w = pimg.shape[0] h = pimg.shape[1] gt_img_3c = numpy.zeros((w, h, 3), dtype=numpy.uint8) gt_img8 = img_float_to_uint8(pimg) gt_img_3c[:, :, 0] = gt_img8 gt_img_3c[:, :, 1] = gt_img8 gt_img_3c[:, :, 2] = gt_img8 pred_filename = prediction_test_dir + "prediction_" + str(i + 1) + ".png" Image.fromarray(gt_img_3c).save(pred_filename) pred_filenames.append(pred_filename) masks_to_submission(submission_filename, *pred_filenames)
def image(): path = request.args.get("path") print path with tempfile.NamedTemporaryFile(suffix=".png") as tmpfile: urllib.urlretrieve(path, tmpfile.name) current_image, shape = load_image(tmpfile.name) conv6_val, output_val = sess.run( [conv6, output], feed_dict={ images_tf: np.array([current_image]) }) label_predictions = output_val.argmax(axis=1) classmap_vals = sess.run( classmap, feed_dict={ labels_tf: label_predictions, conv6: conv6_val }) classmap_vis = map(lambda x: ((x-x.min())/(x.max()-x.min())), classmap_vals) ori = skimage.transform.resize(current_image, [shape[0], shape[1]]) vis = skimage.transform.resize(classmap_vis[0], [shape[0], shape[1]]) plt.clf() plt.axis("off") plt.imshow(ori) plt.imshow(vis, cmap=plt.cm.jet, alpha=0.5, interpolation='nearest') with tempfile.NamedTemporaryFile(suffix=".png") as tmpfile: plt.savefig(tmpfile.name, transparent=True, bbox_inches='tight', pad_inches=0) return send_file(tmpfile.name, mimetype='image/png')
def do_for_file(self, file_path, output_folder="output"): filename, extension = os.path.splitext(file_path) output_folder += "/" org_image = util.load_image(file_path) util.save_image(output_folder + file_path, org_image) if len(org_image.shape ) >= 3 and org_image.shape[2] == 3 and self.channels == 1: input_y_image = util.convert_rgb_to_y(org_image, jpeg_mode=self.jpeg_mode) scaled_image = util.resize_image_by_pil(input_y_image, self.scale) output_y_image = self.do(input_y_image) scaled_ycbcr_image = util.convert_rgb_to_ycbcr( util.resize_image_by_pil(org_image, self.scale), jpeg_mode=self.jpeg_mode) image = util.convert_y_and_cbcr_to_rgb(output_y_image, scaled_ycbcr_image[:, :, 1:3], jpeg_mode=self.jpeg_mode) else: scaled_image = util.resize_image_by_pil(org_image, self.scale) image = self.do(org_image) util.save_image(output_folder + filename + "_result" + extension, image) return 0
def __init__(self, game, gamepos, **kwargs): self._pos = gamepos self._screenpos = ((game.width//2)//game.dummytile.image.width,(game.height//2)//game.dummytile.image.height) self.animationlength = 0.40#seconds self.lastmove = datetime.datetime.now() self.maxhp = 100 self.hp = 100 self.animationset = helper.make_animation(helper.load_image('character.png'),64,64,anchorx=16,anchory=0) self.stationaryimg = helper.load_image('character.png') self.stationaryset = [] self.animationframes = len(self.animationset) self.direction = 'down' for x in xrange(0,4): self.stationaryset.append(self.stationaryimg.get_region(0,64*x,64,64)) self.stationaryset[x].anchor_x=16 self.stationaryset[x].anchor_y=0 self.sprite = sprite.Sprite(game, game.middleground, None, image_data=self.stationaryset[1], x=gamepos[0], y=gamepos[1], anchorx=16, anchory=0, **kwargs)
def setTileSheet(self, sheetName): """ Loads in the tilesheet and re-renders the box """ self._tileSheet = helper.load_image(sheetName) # Render the image self.render()
def show_segment(image_name='2092'): ground_truth, img = load_image(image_name) img.show() for i in range(ground_truth.shape[1]): segment = ground_truth[0, i] bound = segment[0, 0][1] segmented_image = get_segment(bound, img) plt.figure(i + 1) plt.imshow(segmented_image) plt.show()
def __init__(self, group): super().__init__(group) self.group = group self.image = pygame.transform.scale(load_image('box.jpg'), (BOX_SIZE, BOX_SIZE)) self.mask = pygame.mask.from_surface(self.image) self.rect = self.image.get_rect() self.rect.x = randint(BOX_SIZE // 2, WIDTH - BOX_SIZE // 2) self.rect.y = randint(-HEIGHT * 2, HEIGHT * 2) self.start_x = self.rect.x self.start_y = self.rect.y
def getsprites(self): wall, rect = load_image('wall2.png') coin, rect = load_image('coin2.png', -1) player, rect = load_image('player2.png', -1) ladder, rect = load_image('ladder.png', -1) donkey, rect = load_image('donkey.png', -1) fireball, rect = load_image('fireball.png', -1) queen, rect = load_image('princess.png', -1) #plain,rect=load_image('plain.png',-1) return [coin, wall, player, ladder, donkey, fireball, queen]
def getsprites(self): wall,rect=load_image('wall2.png') coin,rect=load_image('coin2.png',-1) player,rect=load_image('player2.png',-1) ladder,rect=load_image('ladder.png',-1) donkey,rect=load_image('donkey.png',-1) fireball,rect=load_image('fireball.png',-1) queen,rect=load_image('princess.png',-1) #plain,rect=load_image('plain.png',-1) return [coin,wall,player,ladder,donkey,fireball,queen]
def visualize_accel(model_path, label_path): print(colored('Preparing', 'blue')) model = Inception3D(weights_path=model_path, input_shape=(configs.LENGTH, configs.IMG_HEIGHT, configs.IMG_WIDTH, 3)) # read the steering labels and image path labels = pd.read_csv(label_path).values inputs = [] starting_index = 9000 end_index = 0 # init_speed = labels[starting_index][2] for i in range(starting_index, starting_index + configs.LENGTH): img = helper.load_image(configs.TRAIN_DIR + "frame" + str(i) + ".jpg") inputs.append(img) print(colored('Started', 'blue')) # Run through all images for i in range(starting_index + configs.LENGTH + 1, len(labels) - 1 - end_index): for event in pygame.event.get(): if event.type == pygame.QUIT: break img = helper.load_image( "/hdd/ssd_2/dataset/speedchallenge/data/train/" + str(labels[i][1]), auto_resize=False) in_frame = cv2.resize(img, (configs.IMG_WIDTH, configs.IMG_HEIGHT)) inputs.pop(0) inputs.append(in_frame) cmds = model.model.predict(np.array([np.asarray(inputs)]))[0] label_accel = (labels[i][2] - labels[i - 1][2]) * 20 pygame_loop(label=label_accel, prediction=cmds, img=img)
def __init__(self, image_file, image_data=None, **kwargs): #init standard variables self.image_file = image_file if (image_data is None): self.image = helper.load_image(image_file) else: self.image = image_data self.x = 0 self.y = 0 self.dead = False #Update the dict if they sent in any keywords self.__dict__.update(kwargs)
def __init__(self, group, x, y, image_name): super().__init__(group) self.group = group self.image = pygame.transform.scale(load_image(image_name), (BULLET_WIDTH, BULLET_HEIGHT)) self.x = x self.y = y self.direction = 1 self.mask = pygame.mask.from_surface(self.image) self.rect = self.image.get_rect() self.rect.bottom = self.y self.rect.left = self.x - self.rect.width
def __init__(self, color, type, pos, x=-1, y=-1): # Call Sprite initializer pygame.sprite.Sprite.__init__(self) self.image, self.rect = helper.load_image('piece_' + color + type + '.bmp', -1) self.rect.center = pos self.type = type self.color = color # Position on the board [0...14], -1 means pre-placement, -2 means captured self.x = x self.y = y self.x_velocity = 0 self.y_velocity = 0 self.trapped = False
def getImages(self): block,rect = load_image('./images/block.png') pellet,rect = load_image('./images/pellet.png',-1) snake,rect = load_image('./images/snake.png',-1) monster,rect = load_image('./images/monster_01.png',-1) scared_monster,rect = load_image('./images/monster_scared_01.png',-1) super_pellet,rect = load_image('./images/super_pellet.png',-1) return [pellet,block,snake,monster,scared_monster,super_pellet]
def build_input_batch(self, batch_dir): for i in range(self.batch_num): if self.index_in_epoch >= self.train.input.count: self.init_epoch_index() self.epochs_completed += 1 image_no = self.batch_index[self.index_in_epoch] self.batch_input[i] = util.load_image( batch_dir + "/" + INPUT_IMAGE_DIR + "/%06d.bmp" % image_no, print_console=False) batch_input_quad = util.load_image(batch_dir + "/" + INTERPOLATED_IMAGE_DIR + "/%06d.bmp" % image_no, print_console=False) util.convert_to_multi_channel_image(self.batch_input_quad[i], batch_input_quad, self.scale) batch_true_quad = util.load_image( batch_dir + "/" + TRUE_IMAGE_DIR + "/%06d.bmp" % image_no, print_console=False) util.convert_to_multi_channel_image(self.batch_true_quad[i], batch_true_quad, self.scale) self.index_in_epoch += 1
def __init__(self, game, _group, image_file, animation=None, image_data=None, anchorx=0, anchory=0, **kwargs): #init standard variables if animation is not None: framewidth,frameheight = animation image = helper.make_animation(helper.load_image(image_file),framewidth,frameheight,anchorx,anchory)[2] elif (image_data is None): image = helper.load_from_cache(game.rawcache, image_file, anchorx, anchory) else: image = image_data pyglet.sprite.Sprite.__init__(self, image, batch=game.batch, group=_group) self.x = kwargs["x"] self.y = kwargs["y"] #Update the dict if they sent in any keywords self.__dict__.update(kwargs)
def __init__(self, board, screen): self.board = board self.screen = screen #Create The Backgound self.background = pygame.Surface(self.screen.get_size()) self.background = self.background.convert() self.background.fill((255, 255, 255)) #test character self.papixel = helper.load_image('papixel.png').convert() transColor = self.papixel.get_at((0, 0)) self.papixel.set_colorkey(transColor) #add character to background self.background.blit(self.papixel, (500, 500)) #Display The Background self.screen.blit(self.background, (0, 0)) pygame.display.flip() pa = helper.load_image('papixel.png').convert() transColor = pa.get_at((0, 0)) pa.set_colorkey(transColor) pa = pygame.transform.scale( pa, (helper.getResolution(), helper.getResolution())) #picture = pygame.transform.scale(picture, (1280, 720)) ns = helper.load_image('ns.png').convert() transColor = ns.get_at((0, 0)) ns.set_colorkey(transColor) ns = pygame.transform.scale( ns, (helper.getResolution(), helper.getResolution())) unitImage["pa"] = pa unitImage["ns"] = ns self.render(self.board)
def __init__(self, group, player_bullets_group, enemy_group, enemy_bullets_group, obstacles_group): super().__init__(group) self.player_bullets_group = player_bullets_group self.image = pygame.transform.scale(load_image('player.png'), (PLAYER_WIDTH, PLAYER_HEIGHT)) self.start_x = WIDTH // 2 self.start_y = HEIGHT // 2 self.motion = STOP self.enemy_group = enemy_group self.enemy_bullets_group = enemy_bullets_group self.obstacles_group = obstacles_group self.life = 100 self.mask = pygame.mask.from_surface(self.image) self.rect = self.image.get_rect() self.respawn()
def crop_image(image_path , cordinates): image = load_image(image_path) if image is None: return x1 , y1 = cordinates[0][0] , cordinates[0][1] x2, y2= cordinates[1][0] , cordinates[1][1] region = image.crop((x1 , y1 , x2 , y2)) new_name = get_new_name(image_path , "Cropped") region.save(new_name) messagebox.showinfo(title = "Successful" , message = "Image saved as {}".format(new_name))
def __init__(self, group, player_bullets_group, enemies_bullets_group, player): super().__init__(group) self.group = group self.player_bullets_group = player_bullets_group self.enemies_bullets_group = enemies_bullets_group self.image = pygame.transform.scale(load_image('enemy.png'), (PLAYER_WIDTH, PLAYER_HEIGHT)) self.start_x = randint(PLAYER_WIDTH // 2, WIDTH - PLAYER_WIDTH // 2) self.start_y = PLAYER_HEIGHT self.player = player self.mask = pygame.mask.from_surface(self.image) self.rect = self.image.get_rect() self.rect.bottom = self.start_y self.rect.left = self.start_x - self.rect.width
def draw_rect_on(image_path, cordinates): image = load_image(image_path) if image is None: return x1, y1 = cordinates[0][0], cordinates[0][1] x2, y2 = cordinates[1][0], cordinates[1][1] draw_image = ImageDraw.Draw(image) draw_image.rectangle((x1, y1, x2, y2), outline="black") new_name = get_new_name(image_path, "drawn") image.save(new_name) messagebox.showinfo(title="Successful", message="Image saved as {}".format(new_name))
def __init__(self, surface, name=None, pos=None, **kwargs): """ Initializes the element. Surface is the surface which will be displayed, and will just be directly rendered to the view element's position. """ temp_image = helper.load_image(surface) # Set the rectangle accordingly temp_rect = temp_image.get_rect() if pos: temp_rect.move_ip(pos[0], pos[1]) if type(temp_image) != pygame.Surface: raise(Exception("Surface passed to view element is wrong type!")) super().__init__(name=name, rect=temp_rect, **kwargs) self.image = temp_image
def classify_image(path_to_image, checkpoint, top_k, category_names, gpu): if not torch.cuda.is_available() and gpu: raise ( "No gpu available to train the network. Please remove the --gpu argument to train using the cpu" ) device = ('cuda' if gpu else 'cpu') model = helper.load_model(checkpoint) image_tensor = torch.tensor(helper.load_image(path_to_image)) (probs, classes) = helper.predict(image_tensor, model, top_k, device) if category_names != None: #convert the classes array to hold the string representation of the category with open(category_names, 'r') as f: cat_to_name = json.load(f) classes = [cat_to_name[class_] for class_ in classes] return (classes, probs)
def main(): # Create parser object parser = argparse.ArgumentParser(description="Neural Network Image Classifier Training") # Define argument for parser object parser.add_argument('--save_dir', type=str, help='Define save directory for checkpoints model as a string') parser.add_argument('--arch', dest='arch', action ='store', type = str, default = 'densenet', help='choose a tranfer learning model or architechture') parser.add_argument('--learning_rate', dest = 'learning_rate', action='store', type=float, default=0.001, help='Learning Rate for Optimizer') parser.add_argument('--hidden_units', dest = 'hidden_units', action='store', type=int, default=512, help='Define number of hidden unit') parser.add_argument('--epochs', dest = 'epochs', action='store', type=int, default=1, help='Number of Training Epochs') parser.add_argument('--gpu', dest = 'gpu', action='store_true', default = 'False', help='Use GPU if --gpu') parser.add_argument('--st', action = 'store_true', default = False, dest = 'start', help = '--st to start predicting') # Parse the argument from standard input args = parser.parse_args() # Print out the passing/default parameters print('-----Parameters------') print('gpu = {!r}'.format(args.gpu)) print('epoch(s) = {!r}'.format(args.epochs)) print('arch = {!r}'.format(args.arch)) print('learning_rate = {!r}'.format(args.learning_rate)) print('hidden_units = {!r}'.format(args.hidden_units)) print('start = {!r}'.format(args.start)) print('----------------------') if args.start == True: class_labels, trainloaders, testloaders, validloaders = helper.load_image() model = helper.load_pretrained_model(args.arch, args.hidden_units) criterion = nn.NLLLoss() optimizer = optim.Adam(model.classifier.parameters(), lr = args.learning_rate) helper.train_model(model, args.learning_rate, criterion, trainloaders, validloaders, args.epochs, args.gpu) helper.test_model(model, testloaders, args.gpu) model.to('cpu') # saving checkpoints helper.save_checkpoint({ 'arch': args.arch, 'state_dict': model.state_dict(), 'optimizer': optimizer.state_dict(), 'hidden_units': args.hidden_units, 'class_labels': class_labels }) print('model checkpoint saved')
def get_pieces(self): #Initialize all of the game pieces and load them. #Return a list of all of the pieces in the order which #they should append(IE baseBlock is 0, so it should be the 0th #element startBlock, rect = load_image('startBlock.png',-1) baseBlock, rect = load_image('baseTile.png',-1) doubleLetter, rect = load_image('doubleLetter.png',-1) doubleWord, rect = load_image('doubleWord.png',-1) tripleLetter, rect = load_image('tripleLetter.png',-1) tripleWord, rect = load_image('tripleWord.png',-1) return [baseBlock, doubleLetter, doubleWord, tripleLetter, tripleWord,startBlock]
detector = Detector(config.weight_path, n_labels) c1,c2,c3,c4,conv5, conv6, gap, output = detector.inference(images_tf) classmap = detector.get_classmap(labels_tf, conv6) sess = tf.InteractiveSession() saver = tf.train.Saver() saver.restore( sess, model_path ) for start, end in zip( range( 0, len(testset)+batch_size, batch_size), range(batch_size, len(testset)+batch_size, batch_size)): current_data = testset[start:end] current_image_paths = current_data['image_path'].values current_images = np.array(map(lambda x: load_image(x), current_image_paths)) good_index = np.array(map(lambda x: x is not None, current_images)) current_data = current_data[good_index] current_image_paths = current_image_paths[good_index] current_images = np.stack(current_images[good_index]) current_labels = current_data['label'].values current_label_names = current_data['label_name'].values conv6_val, output_val = sess.run( [conv6, output], feed_dict={ images_tf: current_images })
def do_for_evaluate(self, file_path, output_directory="output", output=True, print_console=True): filename, extension = os.path.splitext(file_path) output_directory += "/" true_image = util.set_image_alignment(util.load_image(file_path), self.scale) if true_image.shape[2] == 3 and self.channels == 1: input_y_image = util.build_input_image(true_image, channels=self.channels, scale=self.scale, alignment=self.scale, convert_ycbcr=True, jpeg_mode=self.jpeg_mode) # for color images if output: input_bicubic_y_image = util.resize_image_by_pil( input_y_image, self.scale) true_ycbcr_image = util.convert_rgb_to_ycbcr( true_image, jpeg_mode=self.jpeg_mode) output_y_image = self.do(input_y_image, input_bicubic_y_image) psnr, ssim = util.compute_psnr_and_ssim(true_ycbcr_image[:, :, 0:1], output_y_image, border_size=self.scale) loss_image = util.get_loss_image(true_ycbcr_image[:, :, 0:1], output_y_image, border_size=self.scale) output_color_image = util.convert_y_and_cbcr_to_rgb( output_y_image, true_ycbcr_image[:, :, 1:3], jpeg_mode=self.jpeg_mode) util.save_image(output_directory + file_path, true_image) util.save_image( output_directory + filename + "_input" + extension, input_y_image) util.save_image( output_directory + filename + "_input_bicubic" + extension, input_bicubic_y_image) util.save_image( output_directory + filename + "_true_y" + extension, true_ycbcr_image[:, :, 0:1]) util.save_image( output_directory + filename + "_result" + extension, output_y_image) util.save_image( output_directory + filename + "_result_c" + extension, output_color_image) util.save_image( output_directory + filename + "_loss" + extension, loss_image) else: true_y_image = util.convert_rgb_to_y(true_image, jpeg_mode=self.jpeg_mode) output_y_image = self.do(input_y_image) psnr, ssim = util.compute_psnr_and_ssim(true_y_image, output_y_image, border_size=self.scale) elif true_image.shape[2] == 1 and self.channels == 1: # for monochrome images input_image = util.build_input_image(true_image, channels=self.channels, scale=self.scale, alignment=self.scale) output_image = self.do(input_image) psnr, ssim = util.compute_psnr_and_ssim(true_image, output_image, border_size=self.scale) if output: util.save_image(output_directory + file_path, true_image) util.save_image( output_directory + filename + "_result" + extension, output_image) else: psnr = ssim = None if print_console: print("PSNR:%f SSIM:%f" % (psnr, ssim)) return psnr, ssim
def main(): """this function is called when the program starts. it initializes everything it needs, then runs in a loop until the function returns.""" #Initialize Everything pygame.init() pygame.font.init() # you have to call this at the start, # if you want to use this module. myfont = pygame.font.SysFont("Comic Sans MS", helper.getTextSize()) pygame.display.set_caption('XCom - the Unknown Noob') pygame.mouse.set_visible(1) #tiles = {} # papixel = pygame.Surface((60,60)) # #papixel = papixel.convert() # papixel.fill((0, 0, 255)) f = open("map.txt") tiles = {} for line in f.readlines(): cols = line.split() passable = (cols[2] == "True") tiles[(int(cols[0]), int(cols[1]))] = board.Tile( (int(cols[0]), int(cols[1])), passable, int(cols[3]), int(cols[4]), int(cols[5]), int(cols[6])) #print(tiles) board1 = board.Board(15, 20, tiles) wep_assault = soldier.Weapon("Assault Rifle", 3, 5, 3, [ 25, 20, 18, 16, 14, 12, 10, 8, 6, 4, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, -5, -10, -15, -20, -25, -30 ], 0, 2) wep_shotgun = soldier.Weapon("Shotgun", 4, 7, 3, [ 45, 40, 32, 24, 16, 8, 4, 0, 0, -4, -8, -16, -32, -40, -70, -80, -90, -100 ], 20, 3) wep_sniper = soldier.Weapon("Sniper Rifle", 4, 6, 3, [ -35, -30, -27, -24, -21, -18, -15, -12, -9, -6, -3, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10 ], 10, 3, True) soldiers = [] soldiers.append([]) soldiers.append([]) for i in range(0, board1.width): for j in range(0, board1.height): exec( compile( open("soldier.txt", "rb").read(), "soldier.txt", 'exec')) #exec(compile(open("map.txt", "rb").read(), "map.txt", 'exec')) screen = pygame.display.set_mode( ((helper.getResolution() + 4) * board1.width, (helper.getResolution() + 4) * board1.height)) papixel = helper.load_image('papixel.png').convert() transColor = papixel.get_at((0, 0)) papixel.set_colorkey(transColor) papixel = pygame.transform.scale( papixel, (helper.getResolution(), helper.getResolution())) #picture = pygame.transform.scale(picture, (1280, 720)) ns = helper.load_image('ns.png').convert() transColor = ns.get_at((0, 0)) ns.set_colorkey(transColor) ns = pygame.transform.scale( ns, (helper.getResolution(), helper.getResolution())) #pygame.draw.rect(blank, (50,140,200), (0,0,60,60), 2) # Initialize time for checking click x = 0 y = 0 renderer = Renderer.Renderer(board1, screen) controller = Controller.Controller() count = 0 srcTile = None desTile = None ID = None currentTile = None displayHover = 0 currentSide = 0 dummyAI = DummyAI('noob1') xcomwin = 0 alienwin = 0 def resetBoard(): nonlocal board1, renderer, controller, count, srcTile, desTile, ID, currentTile \ ,displayHover, currentSide, wep_sniper, wep_assault, wep_shotgun, soldiers f = open("map.txt") tiles = {} for line in f.readlines(): cols = line.split() passable = (cols[2] == "True") tiles[(int(cols[0]), int(cols[1]))] = board.Tile( (int(cols[0]), int(cols[1])), passable, int(cols[3]), int(cols[4]), int(cols[5]), int(cols[6])) #print(tiles) board1 = board.Board(15, 20, tiles) for i in range(0, board1.width): for j in range(0, board1.height): exec( compile( open("soldier.txt", "rb").read(), "soldier.txt", 'exec')) # exec(compile(open("map.txt", "rb").read(), "map.txt", 'exec')) print("HI!") renderer = Renderer.Renderer(board1, screen) controller = Controller.Controller() count = 0 srcTile = None desTile = None ID = None currentTile = None displayHover = 0 currentSide = 0 testList = dummyAI.dangerBoard(board1, soldiers, currentSide, soldiers[currentSide][0]) for i in range(0, board1.width): for j in range(0, board1.height): print("%8.2f " % testList[i][j], end="") print("NS") print(testList[19][13]) print(testList[17][12]) try: while 1: if mode == 1: event = pygame.event.wait() else: event = pygame.event.poll() if event.type == pygame.QUIT: break if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE or event.unicode == 'q': print("XCOM wins: " + str(xcomwin)) print("ALIENS wins: " + str(alienwin)) break if xcomwin + alienwin == 15: print("XCOM wins: " + str(xcomwin)) print("ALIENS wins: " + str(alienwin)) break if mode == 1 or mode == 2: coord2 = pygame.mouse.get_pos() currentTile = controller.getTile(board1, coord2) displayHover = eventHandler.hoverDisplay( displayHover, currentTile, currentSide, myfont, renderer, count, board1) if mode == 3: if level == 1: if currentSide == 0: dummyAI.improvedRandomExecution( board1, soldiers, currentSide) srcTile = dummyAI.srcTile desTile = dummyAI.desTile ID = dummyAI.ID else: if currentSide == 0: dummyAI.execution(board1, soldiers, currentSide) srcTile = dummyAI.srcTile desTile = dummyAI.desTile ID = dummyAI.ID if (mode == 2 or mode == 3): if level == 1: if currentSide == 1: dummyAI.improvedRandomExecution( board1, soldiers, currentSide) srcTile = dummyAI.srcTile desTile = dummyAI.desTile ID = dummyAI.ID else: if currentSide == 1: dummyAI.execution(board1, soldiers, currentSide) srcTile = dummyAI.srcTile desTile = dummyAI.desTile ID = dummyAI.ID if mode == 1 or mode == 2: if pygame.mouse.get_pressed()[0]: if ((time.time() - x) > 0.5): x = time.time() #print ("You have opened a chest!") count, srcTile, desTile = eventHandler.mouseButtonHandler( count, controller, board1, soldiers, currentSide, renderer, srcTile, desTile) #board2 = controller.makemove(board1, coord1,coord2) # else: # print("you pressed too fast") #coord = pygame.mouse.get_pos() if event.type == pygame.KEYDOWN: if (count == 1) or (count == 2): if ((time.time() - y) > 0.5): y = time.time() count, ID, srcTile, desTile = eventHandler.buttonActionHander( event, controller, srcTile, renderer, board1, desTile) # possibleTiles = controller.possibleTiles(board1, srcTile, ID) # renderer.renderPossibleTiles(possibleTiles) else: print("you press too fast") if mode == 3: if (srcTile == None): if currentSide == 1: currentSide = 0 print("XCOM's Turn") for u in soldiers[currentSide]: u.actionPoints = 2 else: currentSide = 1 print("Alien's Turn") for u in soldiers[currentSide]: u.actionPoints = 2 if mode == 2: if (srcTile == None): if currentSide == 1: currentSide = 0 print("XCOM's Turn") for u in soldiers[currentSide]: u.actionPoints = 2 if (srcTile != None) and (desTile != None) and (ID != None): #print("action perform") print(controller.performAction(board1, srcTile, desTile, ID)) srcTile = None desTile = None ID = None renderer.render(board1) switch = True for u in soldiers[currentSide]: if u.actionPoints > 0 and u.health > 0: switch = False if switch: if currentSide == 0: currentSide = 1 print("Alien Activity") else: currentSide = 0 print("XCOM's Turn") for u in soldiers[currentSide]: u.actionPoints = 2 if helper.checkWinCondition(board1) == 0: print("XCOM wins yay") xcomwin = xcomwin + 1 resetBoard() elif helper.checkWinCondition(board1) == 1: print("XCOM noob, Alien win") alienwin = alienwin + 1 resetBoard() pygame.display.flip() finally: pygame.quit()
def init_sprites(self): self.bullets = [] self.monsters = [] self.ship = SpaceShip(self.width - 150, 10, x=100,y=100) self.bullet_image = helper.load_image("bullet.png") self.monster_image = helper.load_image("monster.png")
def render(self, board): dummy = pygame.Surface(self.screen.get_size()) dummy = dummy.convert() dummy.fill((255, 255, 255)) papixel = pygame.Surface( (helper.getResolution(), helper.getResolution())) papixel = papixel.convert() papixel.fill((255, 255, 255)) wall = helper.load_image('wall.png').convert() transColor = wall.get_at((0, 0)) wall.set_colorkey(transColor) #blank = pygame.Surface((60,60)) #blank = papixel.convert() #blank.fill((255, 255, 255)) #pygame.draw.rect(blank, (50,140,200), (0,0,60,60), 2) self.board = board for j in range(0, self.board.height): for i in range(0, self.board.width): blank = pygame.Surface( (helper.getResolution() + 4, helper.getResolution() + 4)) blank = blank.convert() blank.fill((255, 255, 150)) #pygame.draw.rect(blank, (50,140,200), (0,0,60,60), 2) res = helper.getResolution() if (self.board.tiles[i][j].coverN != 0): pygame.draw.line( blank, (0, 255 - self.board.tiles[i][j].coverN / 50 * 255, 0), (0, 0), (res, 0), 4) if (self.board.tiles[i][j].coverS != 0): pygame.draw.line( blank, (0, 255 - self.board.tiles[i][j].coverS / 50 * 255, 0), (0, res + 2), (res, res + 2), 4) if (self.board.tiles[i][j].coverW != 0): pygame.draw.line( blank, (0, 255 - self.board.tiles[i][j].coverW / 50 * 255, 0), (0, 0), (0, res + 2), 4) if (self.board.tiles[i][j].coverE != 0): pygame.draw.line( blank, (0, 255 - self.board.tiles[i][j].coverE / 50 * 255, 0), (res + 2, 0), (res + 2, res), 4) #self.coverN = coverN #self.coverE = coverE #self.coverS = coverS #self.coverW = coverW if (self.board.tiles[i][j].unit != None): blank.blit(unitImage[self.board.tiles[i][j].unit.image], (2, 2)) #test = 1 elif (self.board.tiles[i][j].passable == False): blank.blit(wall, (2, 2)) else: blank.blit(papixel, (2, 2)) dummy.blit(blank, (i * (res + 4), j * (res + 4))) #Display The Background self.screen.blit(dummy, (0, 0)) self.background = dummy pygame.display.flip()
def test_loop(results_paths): """ for visualizing the model with the comma AI test dataset. The ds doesn't contain training labels. :param model_path: the path of the trained Keras model :param model_type: the type of model, rgb, flow or rgb-flow :return: None """ print(colored('Preparing', 'blue')) # read the steering labels and image path files = os.listdir(configs.TEST_DIR) inputs = [] starting_index = 8000 end_index = 1000 for i in range(starting_index, starting_index + configs.LENGTH): img = helper.load_image(configs.TEST_DIR + "frame" + str(i) + ".jpg") inputs.append(img) print(colored('Started', 'blue')) # Run through all images for i in range(starting_index + configs.LENGTH + 1, len(files) - 1 - end_index): for event in pygame.event.get(): if event.type == pygame.QUIT: break img = helper.load_image(configs.TEST_DIR + "frame" + str(i) + ".jpg", resize=False) in_frame = cv2.resize(img, (configs.IMG_WIDTH, configs.IMG_HEIGHT)) inputs.pop(0) inputs.append(in_frame) pygame_loop(prediction=prediction, img=img) # ========================================== previous = helper.load_image(configs.TEST_DIR + "frame" + str(starting_index) + ".jpg") for i in range(starting_index, starting_index + configs.LENGTH): img = helper.load_image(configs.TEST_DIR + "frame" + str(i) + ".jpg") in_frame = cv2.resize(img, (configs.IMG_WIDTH, configs.IMG_HEIGHT)) flow = helper.optical_flow(previous=previous, current=in_frame) inputs.append(flow) previous = helper.load_image(configs.TEST_DIR + "frame" + str(starting_index + configs.LENGTH) + ".jpg") for i in range(starting_index + configs.LENGTH + 1, len(files) - 1 - end_index): for event in pygame.event.get(): if event.type == pygame.QUIT: break img = helper.load_image(configs.TEST_DIR + "frame" + str(i) + ".jpg", resize=False) in_frame = cv2.resize(img, (configs.IMG_WIDTH, configs.IMG_HEIGHT)) flow = helper.optical_flow(previous, in_frame) inputs.pop(0) inputs.append(flow) input_array = np.array([np.asarray(inputs)]) prediction = model.model.predict(input_array)[0][0] pygame_loop(prediction=prediction, img=img)
cluster_numbers_list = [5] # image_names_list = load() image_names_list = ['23080','24063','216053'] print(image_names_list) with open("k_means_outputs/kMeans.txt", "w") as text_file: for z in range(len(image_names_list)): image_name = image_names_list[z] print("------------------------") print("image number = ", z) print("------------------------") for n in range(len(cluster_numbers_list)): total_entropy = 0 total_f_measure = 0 cluster_numbers = cluster_numbers_list[n] ground_truth, img = load_image(image_name) image_data, img = get_image_data(image_name) assignments, centroids = k_means(image_data, cluster_numbers) clustered_image = np.zeros([image_data.shape[0], image_data.shape[1], 3], dtype=np.uint8) red_component = np.random.randint(256, size=cluster_numbers) blue_component = np.random.randint(256, size=cluster_numbers) green_component = np.random.randint(256, size=cluster_numbers) k = 0 for i in range(image_data.shape[0]): for j in range(image_data.shape[1]): # clustered_image[i][j] = [red_component[assignments[k]], green_component[assignments[k]], # blue_component[assignments[k]]] clustered_image[i][j] = centroids[assignments[k]] k += 1 # plt.imshow(clustered_image) # plt.show()
def validation_score(model_path, type, save=False, debugging=False): model = i3d(input_shape=(configs.LENGTH, configs.IMG_HEIGHT, configs.IMG_WIDTH, configs.CHANNELS), weights_path=model_path) # read the steering labels and image path df_truth = pd.read_csv( '/home/neil/dataset/speedchallenge/data/validation.csv').values e_sum = 0 inputs = [] predictions = [] if type == 'rgb': start_time = time.time() for i in range(configs.LENGTH): file = configs.TRAIN_DIR + str(df_truth[i][1]) img = helper.load_image(file) inputs.append(img) # Run through all images for i in range(configs.LENGTH, len(df_truth)): p = configs.TRAIN_DIR + str(df_truth[i][1]) img = helper.load_image(p) inputs.pop(0) inputs.append(img) prediction = model.model.predict(np.array([np.asarray(inputs) ]))[0][0] actual_speed = df_truth[i][2] e_sum += (actual_speed - prediction)**2 predictions.append(prediction) if len(predictions) % 1000 == 0: print('.') elif type == 'rgb-flow': print('Started') start_time = time.time() previous = helper.load_image(configs.TRAIN_DIR + str(df_truth[0][1])) for i in range(1, configs.LENGTH + 1): img = helper.load_image(configs.TRAIN_DIR + str(df_truth[i][1])) rgbImg = helper.optical_flow_rgb(previous=previous, current=img) if debugging: fig = plt.figure(figsize=(3, 1)) fig.add_subplot(1, 3, 1) plt.imshow(rgbImg) fig.add_subplot(1, 3, 2) plt.imshow(previous) fig.add_subplot(1, 3, 3) plt.imshow(img) plt.show() previous = img inputs.append(rgbImg) previous = helper.load_image(configs.TRAIN_DIR + str(df_truth[configs.LENGTH + 1][1])) for i in range(configs.LENGTH + 2, len(df_truth)): img = helper.load_image(configs.TRAIN_DIR + str(df_truth[i][1])) rgb_flow = helper.optical_flow_rgb(previous, img) if debugging: plt.imshow(rgb_flow) plt.show() inputs.pop(0) inputs.append(rgb_flow) input_array = np.array([np.asarray(inputs)]) prediction = model.model.predict(input_array)[0][0] actual_steers = df_truth[i][2] e = (actual_steers - prediction)**2 e_sum += e predictions.append(prediction) if len(predictions) % 1000 == 0: print('.') elif type == 'flow': print('Started') start_time = time.time() previous = helper.load_image(configs.TRAIN_DIR + str(df_truth[0][1])) for i in range(1, configs.LENGTH + 1): img = helper.load_image(configs.TRAIN_DIR + str(df_truth[i][1])) flow = helper.optical_flow(previous=previous, current=img) inputs.append(flow) previous = helper.load_image(configs.TRAIN_DIR + str(df_truth[configs.LENGTH + 1][1])) for i in range(configs.LENGTH + 2, len(df_truth)): img = helper.load_image(configs.TRAIN_DIR + str(df_truth[i][1])) flow = helper.optical_flow(previous, img) inputs.pop(0) inputs.append(flow) prediction = model.model.predict(np.array([np.asarray(inputs) ]))[0][0] actual_steers = df_truth[i][2] e_sum += (actual_steers - prediction)**2 predictions.append(prediction) if len(predictions) % 1000 == 0: print('.') else: raise Exception('Sorry, the model type is not recognized') print("time per step: %s seconds" % ((time.time() - start_time) / len(predictions))) if save: print("Writing predictions...") pd.DataFrame({ "steering_angle": predictions }).to_csv('./result.csv', index=False, header=True) print("Done!") return e_sum / len(predictions)