def processing(filename): from deepdreamer import model, load_image, recursive_optimize import numpy as np import PIL.Image layer_tensor = model.layer_tensors[1] img_result = load_image(filename='{}'.format(filename)) img_result = recursive_optimize( layer_tensor=layer_tensor, image=img_result, # how clear is the dream vs original image num_iterations=5, step_size=1.0, rescale_factor=0.5, # How many "passes" over the data. More passes, the more granular the gradients will be. num_repeats=5, blend=1) img_result = np.clip(img_result, 0.0, 255.0) img_result = img_result.astype(np.uint8) result = PIL.Image.fromarray(img_result, mode='RGB') filename = filename.split(".") filename = str(filename[0]) + "_out.jpg" result.save(f'{filename}')
def show_all_dream_image_layers(file_name = "test.png",show=True): """ Args: file_name: image to deep dream on show: to show the image Saves and shows the dream image for all 10 layers """ for num in range(1,11): layer_tensor = model.layer_tensors[num] img_result = load_image(filename='{}'.format(file_name)) img_result = recursive_optimize(layer_tensor=layer_tensor, image=img_result, # how clear is the dream vs original image num_iterations=100, step_size=1.0, rescale_factor=0.5, # How many "passes" over the data. More passes, the more granular the gradients will be. num_repeats=8, blend=0.2) img_result = np.clip(img_result, 0.0, 255.0) img_result = img_result.astype(np.uint8) result = PIL.Image.fromarray(img_result, mode='RGB') result.save('{}_{}.png'.format(file_name[:-4], num)) if show: result.show()
def DreamImage(fileDirectory, layers, iterations, stepSize, rescaleFactor, repeats, blend, pictureName, outputDirectory): """ Amplyfies the patterns the inception model sees in one image. """ # Load the input image. filePath = fileDirectory + pictureName baseImage = load_image(filename='{}'.format(filePath)) # Loop through all passed layers, amplify the inception model patterns. for layer in layers: try: img_result = recursive_optimize( layer_tensor=model.layer_tensors[layer], image=baseImage, num_iterations=iterations, step_size=stepSize, rescale_factor=rescaleFactor, num_repeats=repeats, blend=blend) # Save the output image. img_result = np.clip(img_result, 0.0, 255.0) img_result = img_result.astype(np.uint8) result = PIL.Image.fromarray(img_result, mode='RGB') result.save( "{}Layer{}_Iter{}_Step{}_Rescale{}_Repeats{}_Blend{}.jpg". format(outputDirectory, layer, iterations, stepSize, rescaleFactor, repeats, blend)) except Exception as ex: print(ex)
def BlendDreams(blendDirectory, fps, dreamLength, nameDream1, nameDream2, dreamsDirectory, blendLength, trimPercent): """ Takes the images from two dreems and creates a blend transition. """ # Create new folder for the blend images. blendName = "{}{}_TO_{}".format(blendDirectory, nameDream1, nameDream2) os.mkdir(blendName) # Copy last image of the two dreams. copy2("{}{}/img_{}.jpg".format(dreamsDirectory, nameDream1, fps * dreamLength) , "{}/img_0.jpg".format(blendName)) copy2("{}{}/img_{}.jpg".format(dreamsDirectory, nameDream2, fps * dreamLength) , "{}/img_9999.jpg".format(blendName)) # Open the two images. imageOld = PIL.Image.open("{}/img_0.jpg".format(blendName)) imageNew = PIL.Image.open("{}/img_9999.jpg".format(blendName)) # Calculate rescale factor. xSize, ySize = imageOld.size xTrim = xSize * trimPercent / 100 yTrim = ySize * trimPercent / 100 # Stepwise transition between the two images. for i in range(0, blendLength * fps): # Blend two images and save the result. imageIntermediate = PIL.Image.blend(imageOld, imageNew, i/(blendLength*fps)) imageIntermediate.save("{}/img_{}.jpg".format(blendName, i + 1)) # Reload the image and zoom in. xTrim = xTrim + (xSize * trimPercent / 100) * -((i - blendLength * fps / 2) / (blendLength * fps / 2)) yTrim = yTrim + (ySize * trimPercent / 100) * -((i - blendLength * fps / 2) / (blendLength * fps / 2)) xTrimRounded = round(xTrim) yTrimRounded = round(yTrim) imageIntermediate = load_image("{}/img_{}.jpg".format(blendName, i + 1)) imageIntermediate = imageIntermediate[yTrimRounded:ySize - yTrimRounded, xTrimRounded:xSize - xTrimRounded] imageIntermediate = imresize(imageIntermediate, (ySize, xSize), "nearest") # Save the final image. imageIntermediate = np.clip(imageIntermediate, 0.0, 255.0) imageIntermediate = imageIntermediate.astype(np.uint8) result = PIL.Image.fromarray(imageIntermediate, mode="RGB") result.save("{}/img_{}.jpg".format(blendName, i + 1))
def singleDream(self, output, layer=3): # Chose Layer to enhance layer_tensor = model.layer_tensors[layer] file_name = self.__image img_result = load_image(filename='{}'.format(file_name)) # Call recursive optimization to generate dream img_result = recursive_optimize( layer_tensor=layer_tensor, image=img_result, # how clear is the dream vs original image num_iterations=20, step_size=1.0, rescale_factor=0.5, # How many "passes" over the data. More passes, the more granular the gradients will be. num_repeats=8, blend=0.2) # Make sure output results are valid pixel values img_result = np.clip(img_result, 0.0, 255.0) img_result = img_result.astype(np.uint8) # Generate and save image out of numpy array result = PIL.Image.fromarray(img_result, mode='RGB') result.save("./dream/" + self.__name + "/" + output)
from deepdreamer import model, load_image, recursive_optimize import numpy as np import PIL.Image # determines layer to apply to image layerTensor = model.layer_tensors[1] # instantiates img filename and img object filename = "casey-horner-1268624-unsplash.jpg" img = load_image(filename='{}'.format(filename)) img_result = recursive_optimize( layer_tensor=layerTensor, image=img, # determines image clarity num_iterations=20, step_size=1.0, rescale_factor=0.5, # How many passes over the data. More passes means more granular gradients num_repeats=8, blend=0.2) img_result = np.clip(img_result, 0.0, 255.0) img_result = img_result.astype(np.uint8) result = PIL.Image.fromarray(img_result, mode='RGB') result.save('dream_image_out.jpg') result.show()
from deepdreamer import model, load_image, recursive_optimize layer_tensor = model.layer_tensors[3] image_name = 'img0.jpg' x_size = 1024 y_size = 819 counts = 0 max_count = 50 for i in range(0, 999): if os.path.isfile('images/img{}.jpg'.format(i + 1)): print('file exists') else: img_result = load_image(filename='images/img{}.jpg'.format(i)) x_trim = 2 y_trim = 1 img_result = img_result[0 + x_trim:y_size - y_trim, 0 + y_trim:x_size - x_trim] img_result = cv2.resize(img_result, (x_size, y_size)) img_result[:, :, 0] += 2 img_result[:, :, 1] += 2 img_result[:, :, 2] += 2 img_result = np.clip(img_result, 0.0, 255.0) img_result = img_result.astype(np.uint8)
dream_name = 'paris2' x_size = 800 y_size = 450 created_count = 0 max_count = 50 for i in range(0, 9999999999999999999999): if os.path.isfile('dream/{}/img_{}.jpg'.format(dream_name, i + 1)): print('{} already exists, continuing along...'.format(i + 1)) else: img_result = load_image( filename='dream/{}/img_{}.jpg'.format(dream_name, i)) # for the zooming the effect x_trim = 2 y_trim = 1 img_result = img_result[0 + x_trim:y_size - y_trim, 0 + y_trim:x_size - x_trim] img_result = cv2.resize(img_result, (x_size, y_size)) # for modifying the general colors and brightness of results. # +2 is slowly dimmer # +3 is slowly brighter img_result[:, :, 0] += 2 #reds img_result[:, :, 1] += 2 #greens
We could probably come up with some sort of formula. The deeper the layer is, the more iterations and repeats you will want. Layer 3: 20 iterations, 0.5 rescale, and 8 repeats is decent start Layer 10: 40 iterations and 25 repeats is good. ''' from deepdreamer import model, load_image, recursive_optimize import numpy as np import PIL.Image layer_tensor = model.layer_tensors[3] file_name = "the-starry-night/the-starry-night-800x450.jpg" img_result = load_image(filename='{}'.format(file_name)) img_result = recursive_optimize( layer_tensor=layer_tensor, image=img_result, # how clear is the dream vs original image num_iterations=20, step_size=1.0, rescale_factor=0.5, # How many "passes" over the data. More passes, the more granular the gradients will be. num_repeats=8, blend=0.2) img_result = np.clip(img_result, 0.0, 255.0) img_result = img_result.astype(np.uint8) result = PIL.Image.fromarray(img_result, mode='RGB')
def DreamFrames(dreamDirectory, layers, iterations, stepSize, rescaleFactor, repeats, blend , fps, dreamName, dreamLength, changeLayerAfterSec, trimPercent): """ Creates and saves the frames (images) for a dream video. """ # Randomly chose initial values for RGB correction. redCorrection = int(random.choice("23")) greenCorrection = int(random.choice("23")) blueCorrection = int(random.choice("23")) for i in range(0, dreamLength * fps): # Check if the image has already been processed. if not os.path.isfile("{}{}/img_{}.jpg".format(dreamDirectory, dreamName, i + 1)): # Load the input/previous image. img_result = load_image(filename="{}{}/img_{}.jpg".format(dreamDirectory, dreamName, i)) # Get the size of the image. xSize = len(img_result[0]) ySize = len(img_result) # Trim the image to create a "zoom-in" effect. xTrim = math.ceil(xSize * trimPercent / 100) yTrim = math.ceil(ySize * trimPercent / 100) img_result = img_result[yTrim:ySize - yTrim - 9, xTrim:xSize - xTrim - 9] img_result = imresize(img_result, (ySize, xSize), "nearest") # Adjust the brightness of the image by ensuring that the red green and blue portions of the # image are above/below the defined thresholds. upperBrightnessLevel = xSize*ySize*255*0.8 lowerBrightnessLevel = xSize*ySize*255*0.2 redSum = numpy.sum(img_result[:, :, 0]) if redSum < lowerBrightnessLevel: redCorrection = int(random.choice("34")) elif redSum > upperBrightnessLevel: redCorrection = int(random.choice("12")) img_result[:, :, 0] += redCorrection greenSum = numpy.sum(img_result[:, :, 0]) if greenSum < lowerBrightnessLevel: greenCorrection = int(random.choice("34")) elif greenSum > upperBrightnessLevel: greenCorrection = int(random.choice("12")) img_result[:, :, 1] += greenCorrection blueSum = numpy.sum(img_result[:, :, 0]) if blueSum < lowerBrightnessLevel: blueCorrection = int(random.choice("34")) elif blueSum > upperBrightnessLevel: blueCorrection = int(random.choice("12")) img_result[:, :, 2] += blueCorrection img_result = np.clip(img_result, 0.0, 255.0) img_result = img_result.astype(np.uint8) # Select the inception model layer for next dream iteration. framesPerLayer = fps * changeLayerAfterSec # Option 1: Automatically loop through layers 10..2 #layer = int(11 - (math.ceil((i - math.floor(i/(9*framesPerLayer))*(9*framesPerLayer)) / framesPerLayer))) # Option 2: Cycle through layers list layer = layers[int(math.ceil(i - (math.floor(i/(framesPerLayer*len(layers)))) * (framesPerLayer*len(layers)))/framesPerLayer)] # Select the number of iterations for the selected layer. iterations = int(min(21.0, layer * math.pi) + 2) # Amplify the inception model patterns in the image. img_result = recursive_optimize(layer_tensor=model.layer_tensors[layer], image=img_result, num_iterations=iterations, step_size=stepSize, rescale_factor=rescaleFactor, num_repeats=1, blend=blend) # Save the output image. img_result = np.clip(img_result, 0.0, 255.0) img_result = img_result.astype(np.uint8) result = PIL.Image.fromarray(img_result, mode="RGB") print("{}{}/img_{}.jpg".format(dreamDirectory, dreamName, i + 1)) result.save("{}{}/img_{}.jpg".format(dreamDirectory, dreamName, i + 1)) else: print("Already processed image {}".format(i))
def create_dream_frames(file_name = "test.png", target_dream_layer = 3, total_frames=5, zoom_in=False): """ Args: file_name: orginal image file target_dream_layer: targeted dream layer style total_frames: total number of frames of dream images to generate Returns: dream_layer_dir_path: directory path storing the frames file_name: name of original image file Creates the total number of frames, inside dream_frames/img_name """ curr_path = os.getcwd() dream_frames_path = os.path.join(curr_path, 'dream_frames') # zoom-in effect directory if zoom_in: dream_frames_path = dream_frames_path + '_zoom_in' # check and create '/dream_frames' directory if not os.path.exists(dream_frames_path): os.mkdir(dream_frames_path) # /file_name dir dream_dir_path = os.path.join(dream_frames_path, '{}'.format(format(file_name[:-4]))) if not os.path.exists(dream_dir_path): os.mkdir(dream_dir_path) # /layer_num dream_layer_dir_path = os.path.join(dream_dir_path, 'layer_{}'.format(target_dream_layer)) if not os.path.exists(dream_layer_dir_path): os.mkdir(dream_layer_dir_path) dream_layer_tensor = model.layer_tensors[target_dream_layer] # saves original image as starter image img_path = os.path.join(dream_layer_dir_path, 'img_0.png') result = PIL.Image.open(file_name) (x_size, y_size) = result.size result.save(img_path) # generates the frames for i in range(total_frames-1): img_result = load_image(filename='{}{}.png'.format(img_path[:-5], i)) if zoom_in: x_trim = 10 y_trim = 10 img_result = img_result[0+x_trim:y_size-y_trim, 0+y_trim:x_size-x_trim] img_result = cv2.resize(img_result, (x_size, y_size)) img_result[:, :, 0] += 3 # reds img_result[:, :, 1] += 3 # greens img_result[:, :, 2] += 3 # blues img_result = np.clip(img_result, 0.0, 255.0) img_result = img_result.astype(np.uint8) img_result = recursive_optimize(layer_tensor=dream_layer_tensor, image=img_result, num_iterations=15, step_size=1.0, rescale_factor=0.7, num_repeats=1, blend=0.2) img_result = np.clip(img_result, 0.0, 255.0) img_result = img_result.astype(np.uint8) result = PIL.Image.fromarray(img_result, mode='RGB') result.save('{}{}.png'.format(img_path[:-5], i+1)) print('{}/{} DONE'.format(i+1,total_frames)) return dream_layer_dir_path, file_name
def startDreaming(self): # Define dream name dream_name = self.__name # Create output folder for dream frames if needed if not os.path.exists("./dream/" + dream_name): os.makedirs("./dream/" + dream_name) # Copy initial image as the first Frame shutil.copyfile(self.__image, "./dream/" + dream_name + "/img_0.jpg") # Open first frame and get its size im = PIL.Image.open(self.__image) x_size, y_size = im.size # Dream for at most max_count frames created_count = 0 max_count = 30 * 60 * 15 # Max of 15 min at 30 FPS for i in range(0, 9999999999999999): # Search for already existing frames if os.path.isfile('dream/{}/img_{}.jpg'.format(dream_name, i + 1)): print('{} already exists, continuing along...'.format(i + 1)) else: # Define layer boundaries starting_layer = 1 max_layer = 11 change_after = 30 # Calculate current layer - auto increase layers over time / over i layer = min( int((1 / change_after) * i) + starting_layer, max_layer) print("Enhancing layer {}".format(layer)) layer_tensor = model.layer_tensors[layer] # Loading image img_result = load_image( filename='dream/{}/img_{}.jpg'.format(dream_name, i)) # Define zoom speed in pixels x_trim = 2 y_trim = 1 # Zoom image img_result = img_result[0 + x_trim:y_size - y_trim, 0 + y_trim:x_size - x_trim] img_result = cv2.resize(img_result, (x_size, y_size)) # Use these to modify the general colors and brightness of results. # results tend to get dimmer or brighter over time, so you want to # manually adjust this over time. # +2 is slowly dimmer # +3 is slowly brighter img_result[:, :, 0] += 2 # reds img_result[:, :, 1] += 2 # greens img_result[:, :, 2] += 2 # blues # Again make sure to keep pixel values in range img_result = np.clip(img_result, 0.0, 255.0) img_result = img_result.astype(np.uint8) # Recursively optimize / dream image with 2 repeats per frame img_result = recursive_optimize(layer_tensor=layer_tensor, image=img_result, num_iterations=2, step_size=1.0, rescale_factor=0.5, num_repeats=1, blend=0.2) # Clip again img_result = np.clip(img_result, 0.0, 255.0) img_result = img_result.astype(np.uint8) # Performing histogram equalizaiton img_yuv = cv2.cvtColor(img_result, cv2.COLOR_BGR2YUV) # equalize the histogram of the Y channel img_yuv[:, :, 0] = cv2.equalizeHist(img_yuv[:, :, 0]) # convert the YUV image back to RGB format equalized_image = cv2.cvtColor(img_yuv, cv2.COLOR_YUV2BGR) img_result = cv2.addWeighted(img_result, 0.98, equalized_image, 0.02, 0) # Sharpen n times for s in range(0): blurred = cv2.GaussianBlur(img_result, (3, 3), 0) img_result = cv2.addWeighted(img_result, 1.5, blurred, -0.5, 0) # Generate image from numpy array and save it into dream folder result = PIL.Image.fromarray(img_result, mode='RGB') result.save('dream/{}/img_{}.jpg'.format(dream_name, i + 1)) created_count += 1 if created_count > max_count: break
def CreateVideo(fileDirectory, fps, nameDream1, nameDream2, dreamsDirectory, blendDirectory): """ Takes the images from two dreams as well as the blend images from them to create a video. """ # Get first image. imageOne = load_image( filename="{}{}/img_{}.jpg".format(dreamsDirectory, nameDream1, 0)) width = len(imageOne[0]) height = len(imageOne) # Define output properties. fourCC = cv2.VideoWriter_fourcc(*"XVID") output = cv2.VideoWriter( "{}{}_TO_{}.avi".format(fileDirectory, nameDream1, nameDream2), fourCC, fps, (width, height)) processedFrame = 0 # Add images of first dream to video. while os.path.isfile("{}{}/img_{}.jpg".format(dreamsDirectory, nameDream1, processedFrame)): print("{}{}/img_{}.jpg".format(dreamsDirectory, nameDream1, processedFrame)) # Show initial image for 2 seconds. if "img_0.jpg" in "{}{}/img_{}.jpg".format(dreamsDirectory, nameDream1, processedFrame): for i in range(0, 2 * fps): output.write( cv2.imread("{}{}/img_{}.jpg".format( dreamsDirectory, nameDream1, processedFrame))) output.write( cv2.imread("{}{}/img_{}.jpg".format(dreamsDirectory, nameDream1, processedFrame))) processedFrame += 1 # Add blend images to video. blendFrame = 1 while os.path.isfile("{}{}/img_{}.jpg".format( blendDirectory, "{}_TO_{}".format(nameDream1, nameDream2), blendFrame + 1)): print("{}{}/img_{}.jpg".format( blendDirectory, "{}_TO_{}".format(nameDream1, nameDream2), blendFrame + 1)) output.write( cv2.imread("{}{}/img_{}.jpg".format( blendDirectory, "{}_TO_{}".format(nameDream1, nameDream2), blendFrame))) blendFrame += 1 # Add images of seconf dream to video. while os.path.isfile("{}{}/img_{}.jpg".format(dreamsDirectory, nameDream2, processedFrame - 1)): print("{}{}/img_{}.jpg".format(dreamsDirectory, nameDream2, processedFrame - 1)) # Show last image for 2 seconds. if "img_0.jpg" in "{}{}/img_{}.jpg".format(dreamsDirectory, nameDream2, processedFrame - 1): for i in range(0, 2 * fps): output.write( cv2.imread("{}{}/img_{}.jpg".format( dreamsDirectory, nameDream2, processedFrame - 1))) # Create video. output.write( cv2.imread("{}{}/img_{}.jpg".format(dreamsDirectory, nameDream2, processedFrame - 1))) processedFrame -= 1 # Close VideoWriter. output.release()