def video_2_frames(VIDEOS_DIR, TARGET_IMAGES_DIR): files = glob(VIDEOS_DIR + '/' + '*.png') num = 1 # Remove and make a directory. if os.path.exists(TARGET_IMAGES_DIR): shutil.rmtree(TARGET_IMAGES_DIR) # Delete an entire directory tree if not os.path.exists(TARGET_IMAGES_DIR): os.makedirs(TARGET_IMAGES_DIR) # Make a directory for file in files: e_frame = np.array(Image.open(file)) cube_frame = py360convert.e2c(e_frame, face_w=256, mode="bilinear", cube_format="list") for i in range(6): cube_img = Image.fromarray(cube_frame[i]) if not os.path.exists(TARGET_IMAGES_DIR + '/' + str(i)): os.makedirs(TARGET_IMAGES_DIR + '/' + str(i)) # Make a directory cube_img.save(TARGET_IMAGES_DIR + '/' + str(i) + '/' + os.path.basename(file)) print( 'Save', TARGET_IMAGES_DIR + '/' + str(i) + '/' + os.path.basename(file)) print('Save ' + str(num) + ' frame') num += 1 print('Save a diretory')
def equirect_to_cubemap(args, equirect, width, height, cube_side, tempdir): import py360convert tempdir = tempdir / 'cubemap' tempdir.mkdir() with Image.open(equirect) as equirect_img: equirect_array = np.array(equirect_img) # HACK: py360convert flips these faces for some reason; apply corrections transforms = { 'U': np.flipud, 'R': np.fliplr, 'B': np.fliplr, } faces = {} for face, array in py360convert.e2c(equirect_array, cube_side, cube_format='dict').items(): with Image.fromarray(transforms.get(face, lambda x: x)(array)) as img: faces[face] = tempdir / (face + equirect.suffix) img.save(faces[face]) cubemap = Cubemap( px=faces['R'], nx=faces['L'], py=faces['U'], ny=faces['D'], pz=faces['F'], nz=faces['B'], ) return cubemap
def panorama_to_cubemap(panorama_img, face_size=1024, cube_format="dice"): if not isinstance(panorama_img, np.ndarray): panorama_img = imageio.imread(panorama_img) cubemap_img = py360convert.e2c( e_img=panorama_img, face_w=face_size, cube_format=cube_format ) return cubemap_img
def saliency(fixmap_input): (h, w) = fixmap_input.shape equi_output = np.zeros((h, w, 3)) for shf in range(0, 360, 10): fixmap_l = fixmap_input[:, 0:int(shf / 360 * w)] fixmap_r = fixmap_input[:, int(shf / 360 * w):w] fixmap = np.concatenate((fixmap_r, fixmap_l), axis=1) fixmap[fixmap > 0] = 255 fixmap = np.expand_dims(fixmap, axis=2) fixmap = np.concatenate((fixmap, fixmap, fixmap), axis=2) cube_list = py360convert.e2c(fixmap, cube_format='list') #cube_dice = py360convert.e2c(fixmap) #cube_h = py360convert.cube_dice2h(cube_dice) # the inverse is cube_h2dice #cube_dict = py360convert.cube_h2dict(cube_h) # the inverse is cube_dict2h cube_list_b = cube_list for c_key in range(6): cimg = cube_list[c_key] #blur = cv2.blur(cimg,(gau_filter, gau_filter)) blur = ndi.gaussian_filter(cimg, gau_filter) cube_list_b[c_key] = blur # try: # imageio.imwrite(dir_location + sound_type[st] + '/' + vid_name + "/frame.png", cimg) # imageio.imwrite(dir_location + sound_type[st] + '/' + vid_name + "/frame_b.png", blur) # except: # pass #cube_h_b = py360convert.cube_dict2h(cube_dict_b) #cube_dice_b = py360convert.cube_h2dice(cube_h_b) equi_b = py360convert.c2e(cube_list, h, w, cube_format='list') equi_b_l = equi_b[:, 0:w - int(shf / 360 * w), :] equi_b_r = equi_b[:, w - int(shf / 360 * w):w, :] equi_b = np.concatenate((equi_b_r, equi_b_l), axis=1) equi_output = equi_output + equi_b equi_output = equi_output / 36.0 equi_output = equi_output / equi_output.max() return equi_output
def get_canvas(frame): cubes = projector.e2c(frame, cube_format='dict') F = cubes['F'] D = cubes['D'] B = cubes['B'] L = cubes['L'] R = cubes['R'] U = cubes['U'] FDB = np.concatenate([F, D, np.flipud(B)]) LDR = np.rot90( np.concatenate([L, np.rot90(D, -1), np.fliplr(np.rot90(R, -2))], 0), 1) centre = np.ones((768, 768, 3)).astype(LDR.dtype) * 255 centre[256:-256, :] = LDR centre[:, 256:-256, :] = FDB top = np.concatenate([L, F, np.fliplr(R)], 1) down = np.concatenate([np.rot90(L, -2), np.flipud(B), np.flipud(R)], 1) left = np.concatenate( [np.rot90(F, 1), np.rot90(L, 1), np.rot90(np.flipud(B), -1)]) right = np.concatenate( [np.rot90(F, -1), np.fliplr(np.rot90(R, 1)), np.fliplr(np.rot90(B))]) pad = 10 canvas = np.ones( (256 * 2 + 768 + pad * 2, 256 * 2 + 768 + pad * 2, 3)).astype( centre.dtype) * 255 canvas[256 + pad:256 + pad + 768, 256 + pad:256 + pad + 768] = centre canvas[:256, 256 + pad:256 + pad + 768] = top canvas[-256:, 256 + pad:256 + pad + 768] = down canvas[256 + pad:256 + pad + 768, :256] = left canvas[256 + pad:256 + pad + 768, -256:] = right return canvas
import os for j in [1,2,3,4,5,6,7,8]: predataset='/home/wang/workspace/21SmartCity/scene'+str(j) outpath='/home/wang/workspace/cubeImg/scene'+str(j) if not os.path.exists(outpath): os.makedirs(outpath) fileset = os.listdir(predataset) imgspath=[] for i in fileset: if i[-3:] == "jpg" : imgspath.append(os.path.join(predataset,i)) for i in imgspath: cube_dice = np.array(Image.open(i)) cubes = py360convert.e2c(cube_dice, 1200) a = cubes[1200:2400, :1200] b = cubes[1200:2400, 1200:2400] c = cubes[1200:2400, 2400:3600] d = cubes[1200:2400, 3600:4800] newname = i.split('/')[-1][:-9] outputImgPath = os.path.join(outpath,newname+'_0.jpg') plt.imsave(outputImgPath,a) outputImgPath = os.path.join(outpath,newname+'_1.jpg') plt.imsave(outputImgPath,b) outputImgPath = os.path.join(outpath,newname+'_2.jpg') plt.imsave(outputImgPath,c) outputImgPath = os.path.join(outpath,newname+'_3.jpg') plt.imsave(outputImgPath,d) print(i)
import py360convert import glob import os import PIL from PIL import Image from PIL import ImageFilter import numpy as np import matplotlib.pyplot as plt import numba.cuda as cuda import numba from utils import project, copy sample = './sample/001.png' imgIn = np.array(Image.open(sample)) img_flat = py360convert.e2c(imgIn, face_w=512, mode='bilinear', cube_format='horizon') print(img_flat.shape) plt.imshow(img_flat) plt.show() imgIn_gpu = cuda.to_device (img_flat) imgOut_gpu = cuda.device_array(shape=(1024, 2048, 3), dtype=np.uint8) imgOut_cpu = np.zeros(shape=(1024, 2048, 3), dtype=np.uint8) project[(64, 128), (16, 16)](imgOut_gpu, imgIn_gpu, 1024, 2048, 512) imgOut_gpu.copy_to_host(imgOut_cpu) cuda.synchronize() plt.imshow(imgOut_cpu) plt.show()