def fast_evaluate(args, basedir, contents, idx=0): # basedir to save the data style_model = Net(ngf=args.ngf) style_model.load_state_dict(torch.load(args.model), False) style_model.eval() if args.cuda: style_model.cuda() style_loader = StyleLoader(args.style_folder, args.style_size, cuda=args.cuda) for content_image in contents: idx += 1 content_image = utils.tensor_load_rgbimage(content_image, size=args.content_size, keep_asp=True).unsqueeze(0) if args.cuda: content_image = content_image.cuda() content_image = Variable(utils.preprocess_batch(content_image)) for isx in range(style_loader.size()): style_v = Variable(style_loader.get(isx).data) style_model.setTarget(style_v) output = style_model(content_image) filename = os.path.join(basedir, "{}_{}.png".format(idx, isx + 1)) utils.tensor_save_bgrimage(output.data[0], filename, args.cuda) print(filename)
def run_demo(args, mirror=False): style_model = Net(ngf=args.ngf) style_model.load_state_dict(torch.load(args.model)) style_model.eval() if args.cuda: style_loader = StyleLoader(args.style_folder, args.style_size) style_model.cuda() else: style_loader = StyleLoader(args.style_folder, args.style_size, False) # Define the codec and create VideoWriter object height = args.demo_size width = int(4.0 / 3 * args.demo_size) swidth = int(width / 4) sheight = int(height / 4) if args.record: fourcc = cv2.VideoWriter_fourcc('F', 'M', 'P', '4') out = cv2.VideoWriter('output.mp4', fourcc, 20.0, (2 * width, height)) cam = cv2.VideoCapture(0) cam.set(3, width) cam.set(4, height) key = 0 idx = 0 while True: # read frame idx += 1 ret_val, img = cam.read() if mirror: img = cv2.flip(img, 1) cimg = img.copy() img = np.array(img).transpose(2, 0, 1) # changing style if idx % 20 == 1: style_v = style_loader.get(int(idx / 20)) style_v = Variable(style_v.data) style_model.setTarget(style_v) img = torch.from_numpy(img).unsqueeze(0).float() if args.cuda: img = img.cuda() img = Variable(img) img = style_model(img) if not args.cuda: simg = style_v.cpu().data[0].numpy() img = img.cpu().clamp(0, 255).data[0].numpy() else: simg = style_v.data().numpy() img = img.clamp(0, 255).data[0].numpy() img = img.transpose(1, 2, 0).astype('uint8') simg = simg.transpose(1, 2, 0).astype('uint8') # display simg = cv2.resize(simg, (swidth, sheight), interpolation=cv2.INTER_CUBIC) cimg[0:sheight, 0:swidth, :] = simg img = np.concatenate((cimg, img), axis=1) cv2.imshow('MSG Demo', img) # cv2.imwrite('stylized/%i.jpg'%idx,img) key = cv2.waitKey(1) if args.record: out.write(img) # Hit 'q' on the keyboard to quit! if key & 0xFF == ord('q'): break cam.release() if args.record: out.release() cv2.destroyAllWindows()
def run_demo(args, mirror=False): style_model = Net(ngf=args.ngf) input('style_model =') style_model.load_state_dict(torch.load(args.model)) input('style_model.load') style_model.eval() if args.cuda: style_loader = StyleLoader(args.style_folder, args.style_size) style_model.cuda() else: style_loader = StyleLoader(args.style_folder, args.style_size, False) # Define the codec and create VideoWriter object height = 480 width = 640 fps = 24 # cam = cv2.VideoCapture(0) # cam.set(3, width) # cam.set(4, height) video = cv2.VideoCapture('tvMesh2.mp4') video.set(3, width) video.set(4, height) video.set(5, fps) # print(video.get(5)) if args.record: fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v') out = cv2.VideoWriter('breakWhiteConv.mov', fourcc, 24, (640, 480), True) key = 0 idx = 0 counter = 1 while True: # read frame idx += 1 ret_val, frame = video.read() # ret_val, frame = cam.read() counter += 1 if counter == video.get(7): video.set(1, 0) counter = 0 img = np.array(frame).transpose(2, 0, 1) # changing style if idx % 20 == 1: style_v = style_loader.get(int(idx / 20)) style_v = Variable(style_v.data) style_model.setTarget(style_v) img = torch.from_numpy(img).unsqueeze(0).float() if args.cuda: img = img.cuda() img = Variable(img) img = style_model(img) if args.cuda: # simg = style_v.cpu().data[0].numpy() img = img.cpu().clamp(0, 255).data[0].numpy() else: # simg = style_v.data().numpy() img = img.clamp(0, 255).data[0].numpy() img = img.transpose(1, 2, 0).astype('uint8') # display # simg = cv2.resize(simg,(swidth, sheight), interpolation = cv2.INTER_CUBIC) # cimg[0:sheight,0:swidth,:]=simg # fimg = np.concatenate((cimg,img),axis=1) if args.record: out.write(img) cv2.imshow('MSG Demo', img) #cv2.imwrite('stylized/%i.jpg'%idx,img) if cv2.waitKey(1) & 0xFF == ord('q'): break cam.release() if args.record: out.release() cv2.destroyAllWindows()
def run_demo(args, mirror=False): # style_model = Net(ngf=args.ngf) # style_model.load_state_dict(torch.load(args.model)) model_dict = torch.load(args.model) model_dict_clone = model_dict.copy() # We can't mutate while iterating for key, value in model_dict_clone.items(): if key.endswith(('running_mean', 'running_var')): del model_dict[key] style_model = Net(ngf=args.ngf) style_model.load_state_dict(model_dict, False) style_model.eval() if args.cuda: style_loader = StyleLoader(args.style_folder, args.style_size) style_model.cuda() else: style_loader = StyleLoader(args.style_folder, args.style_size, False) # Define the codec and create VideoWriter object height = args.demo_size width = int(4.0 / 3 * args.demo_size) swidth = int(width / 4) sheight = int(height / 4) if args.record: fourcc = cv2.VideoWriter_fourcc('F', 'M', 'P', '4') out = cv2.VideoWriter('output.mp4', fourcc, 20.0, (2 * width, height)) cam = cv2.VideoCapture(0) cam.set(3, width) cam.set(4, height) key = 0 idx = 0 while True: # read frame idx += 1 ret_val, img = cam.read() if mirror: img = cv2.flip(img, 1) cimg = img.copy() img = np.array(img).transpose(2, 0, 1) # changing style if idx % 10 == 1: if int(idx / 10) == 21: break style_v = style_loader.get(int(idx / 10)) print('style version ----------------> ', idx) style_v = Variable(style_v.data) # print('style change to ', style_v) style_model.setTarget(style_v) img = torch.from_numpy(img).unsqueeze(0).float() if args.cuda: img = img.cuda() img = Variable(img) ## handle image img = style_model(img) if args.cuda: simg = style_v.cpu().data[0].numpy() img = img.cpu().clamp(0, 255).data[0].numpy() else: simg = style_v.data().numpy() img = img.clamp(0, 255).data[0].numpy() img = img.transpose(1, 2, 0).astype('uint8') simg = simg.transpose(1, 2, 0).astype('uint8') # display simg = cv2.resize(simg, (swidth, sheight), interpolation=cv2.INTER_CUBIC) cimg[0:sheight, 0:swidth, :] = simg img = np.concatenate((cimg, img), axis=1) cv2.imshow('MSG Demo', img) #cv2.imwrite('stylized/%i.jpg'%idx,img) key = cv2.waitKey(1) if args.record: out.write(img) if key == 27: break cam.release() if args.record: out.release() cv2.destroyAllWindows()
def run_demo(cuda, record): if cuda: ctx = mx.gpu(0); os.environ['MXNET_CUDNN_AUTOTUNE_DEFAULT'] = '0' else: ctx = mx.cpu(0) model = 'models/21styles.params' ngf = 128 style_size = 512 style_folder = 'images/styles/' demo_size = 480 # demo window height mirror = False style_loader = StyleLoader(style_folder, style_size, ctx) style_model = Net(ngf=ngf) style_model.load_parameters(model, ctx=ctx) # Define the codec and create VideoWriter object height = demo_size width = int(4.0/3*demo_size) swidth = int(width/4); sheight = int(height/4) if record: vDir, vFile = './video/', 'output21.mp4' vPath = vDir + vFile try: if not os.path.exists(vDir): os.makedirs(vDir) except OSError as e: print(e); sys.exit(1) fourcc = cv2.VideoWriter_fourcc('m','p','4','v') out = cv2.VideoWriter(vPath, fourcc, 20.0, (2*width, height)) cam = cv2.VideoCapture(0) cam.set(3, width); cam.set(4, height) key, idx = 0, 0 wName = 'STYLIZED VIDEO W:'+ str(width) +' H:'+ str(height) cv2.namedWindow(wName,cv2.WINDOW_NORMAL) cv2.resizeWindow(wName, width*2, height) while True: # read frame idx += 1 ret_val, img = cam.read() if mirror: img = cv2.flip(img, 1) cimg = img.copy() img = np.array(img).transpose(2, 0, 1).astype(float) img = F.expand_dims(mx.nd.array(img, ctx=ctx), 0) # changing styles if idx%20 == 1: style_v = style_loader.get(int(idx/20)) style_model.set_target(style_v) img = style_model(img) simg = np.squeeze(style_v.asnumpy()) simg = simg.transpose(1, 2, 0).astype('uint8') img = F.clip(img[0], 0, 255).asnumpy() img = img.transpose(1, 2, 0).astype('uint8') # display simg = cv2.resize(simg,(swidth, sheight), interpolation = cv2.INTER_CUBIC) simg = cv2.cvtColor(simg, cv2.COLOR_BGR2RGB) cimg[0:sheight,0:swidth,:]=simg img = np.concatenate((cimg, cv2.COLOR_BGR2RGB)),axis=1) cv2.imshow(wName, img) key = cv2.waitKey(1) if record: out.write(img) if key == 27: # Esc break
def run_demo(): style_model = Net(ngf=this_ngf) model_dict = torch.load(model21) model_dict_clone = model_dict.copy() for key, value in model_dict_clone.items(): #v0.4 if key.endswith(('running_mean', 'running_var')): #v0.4 del model_dict[key] style_model.load_state_dict(model_dict, True) style_model.eval() #if cuda: style_loader = StyleLoader(style_folder, style_size) style_model.cuda() #else: # style_loader = StyleLoader(style_folder, style_size, False) # Define the codec and create VideoWriter object height = demo_size width = int(4.0/3*demo_size) swidth = int(width/4) sheight = int(height/4) #if record: # fourcc = cv2.VideoWriter_fourcc('F','M','P','4') # out = cv2.VideoWriter('output.mp4', fourcc, 20.0, (2*width, height)) cam = cv2.VideoCapture(0) cam.set(3, width) cam.set(4, height) key = 0 idx = 0 while True: # read frame idx += 1 ret_val, img = cam.read() #if mirror: # img = cv2.flip(img, 1) #cimg = img.copy()###############################OR-CAM img = np.array(img).transpose(2, 0, 1) # changing style if idx%20 == 1: style_v = style_loader.get(int(idx/20)) style_v = Variable(style_v.data) style_model.setTarget(style_v) img=torch.from_numpy(img).unsqueeze(0).float() #if cuda: img=img.cuda() img = Variable(img) img = style_model(img) #print(type(img)) #if cuda: #simg = style_v.cpu().data[0].numpy() img=img.clamp(0, 255).data[0].cpu().detach().numpy() ##x_gpu = cp.array() ##cp.asnumpy(x_gpu) #img=img.data #img=img.numpy() #print(type(img)) #else: # simg = style_v.data.numpy() # img = img.clamp(0, 255).data[0].numpy() img = img.transpose(1, 2, 0).astype('uint8') #simg = simg.transpose(1, 2, 0).astype('uint8') # display #simg = cv2.resize(simg,(swidth, sheight), interpolation = cv2.INTER_CUBIC) #cimg[0:sheight,0:swidth,:]=simg #img = np.concatenate((cimg,img),axis=1) img = cv2.resize(img, None, fx=3.0,fy=3.0, interpolation=cv2.INTER_LINEAR) kernel = np.ones((5,5),np.float32)/25 img = cv2.filter2D(img,-1,kernel) cv2.imshow('MSG Demo', img) #cv2.imwrite('stylized/%i.jpg'%idx,img) key = cv2.waitKey(1) #if args.record: # out.write(img) if key == 27: break cam.release() #if args.record: # out.release() cv2.destroyAllWindows()
def run_demo(cuda, record, vfile): model = 'models/21styles.params' ngf = 128 style_size = 512 style_folder = 'images/styles/' mirror = False vDir = './video/' vPath = vDir + vfile oFile = 'output21-' + vfile wM, hM = 640, 480 if cuda: ctx = mx.gpu(0) os.environ['MXNET_CUDNN_AUTOTUNE_DEFAULT'] = '0' else: ctx = mx.cpu(0) style_loader = StyleLoader(style_folder, style_size, ctx) style_model = Net(ngf=ngf) style_model.load_parameters(model, ctx=ctx) metadata = ffprobe(vPath) fps = metadata["video"]["@avg_frame_rate"] # print(json.dumps(metadata["video"], indent=4)) w, h = int(metadata["video"]["@width"]), int(metadata["video"]["@height"]) downsize = h > hM if downsize: w = 2 * int(w * hM / h / 2) h = hM # downsize = w > wM # if downsize : # h = 2 * int(h * wM / w / 2); w = wM swidth = int(w / 4) sheight = int(h / 4) wName = vfile + ' STYLIZED VIDEO fps:' + fps + ' W:' + str( w) + ' H:' + str(h) if record: out = FFmpegWriter(vDir + oFile, inputdict={ '-r': str(fps), '-s': '{}x{}'.format(2 * w, h) }, outputdict={ '-r': str(fps), '-c:v': 'h264' }) key, idx = 0, 0 cv2.namedWindow(wName, cv2.WINDOW_NORMAL) cv2.resizeWindow(wName, 2 * w, h) for img in vreader(vPath): idx += 1 if downsize: img = cv2.resize(img, (w, h), interpolation=cv2.INTER_AREA) if mirror: img = cv2.flip(img, 1) cimg = img.copy() img = np.array(img).transpose(2, 0, 1).astype(float) img = F.expand_dims(mx.nd.array(img, ctx=ctx), 0) # changing styles if idx % 50 == 1: style_v = style_loader.get(int(idx / 20)) style_model.set_target(style_v) img = style_model(img) simg = np.squeeze(style_v.asnumpy()) simg = simg.transpose(1, 2, 0).astype('uint8') img = F.clip(img[0], 0, 255).asnumpy() img = img.transpose(1, 2, 0).astype('uint8') # display simg = cv2.resize(simg, (swidth, sheight), interpolation=cv2.INTER_CUBIC) cimg[0:sheight, 0:swidth, :] = simg img = np.concatenate((cimg, cv2.cvtColor(img, cv2.COLOR_BGR2RGB)), axis=1) if record: out.writeFrame(img) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) cv2.imshow(wName, img) key = cv2.waitKey(1) if key == 27: # Esc break if record: out.close() transferAudio(vPath, vDir, oFile) print("Done OK. Created Stylised Video file", vDir + oFile) print("fps :", fps, " W:", w, " H:", h) cv2.destroyAllWindows()
def run_demo(args, mirror=False): # style_model = Net(ngf=args.ngf) # style_model.load_state_dict(torch.load(args.model)) model_dict = torch.load(args.model) model_dict_clone = model_dict.copy() # We can't mutate while iterating for key, value in model_dict_clone.items(): if key.endswith(('running_mean', 'running_var')): del model_dict[key] style_model = Net(ngf=args.ngf) style_model.load_state_dict(model_dict, False) style_model.eval() if args.cuda: style_loader = StyleLoader(args.style_folder, args.style_size) style_model.cuda() else: style_loader = StyleLoader(args.style_folder, args.style_size, False) # Define the codec and create VideoWriter object height = args.demo_size width = int(4.0 / 3 * args.demo_size) swidth = int(width / 4) sheight = int(height / 4) #lxy if args.video == '': videofile = '/home/xyliu/Videos/sports/dance.mp4' else: videofile = args.video print('handle video\'s path is', videofile) cam = cv2.VideoCapture(videofile) video_length = int(cam.get(cv2.CAP_PROP_FRAME_COUNT)) ret_val, img = cam.read() i = 1 ## output name if args.output == '': dirname = os.path.dirname(videofile) basename = os.path.basename(videofile) output_name = os.path.join(dirname, 'tra_' + basename) else: output_name = args.output if args.record: fshape = img.shape fheight, fwidth = fshape[:2] fourcc = cv2.VideoWriter_fourcc(*'mp4v') # out = cv2.VideoWriter('new_output.mp4', fourcc, 20.0, (2*width, height)) out = cv2.VideoWriter(output_name, fourcc, 20.0, (fwidth * 2, fheight)) cam.set(3, width) cam.set(4, height) key = 0 idx = 0 while (cam.isOpened()) and ret_val == True and i < video_length: # read frame idx += 1 ret_val, img = cam.read() i += 1 if mirror: img = cv2.flip(img, 1) cimg = img.copy() img = np.array(img).transpose(2, 0, 1) # changing style if idx % 20 == 1: style_v = style_loader.get(int(idx / 20)) style_v = Variable(style_v.data) # print('style change to ', style_v) style_model.setTarget(style_v) img = torch.from_numpy(img).unsqueeze(0).float() if args.cuda: img = img.cuda() img = Variable(img) ## handle image img = style_model(img) if args.cuda: simg = style_v.cpu().data[0].numpy() img = img.cpu().clamp(0, 255).data[0].numpy() else: simg = style_v.data().numpy() img = img.clamp(0, 255).data[0].numpy() img = img.transpose(1, 2, 0).astype('uint8') simg = simg.transpose(1, 2, 0).astype('uint8') # display simg = cv2.resize(simg, (swidth, sheight), interpolation=cv2.INTER_CUBIC) cimg[0:sheight, 0:swidth, :] = simg img = np.concatenate((cimg, img), axis=1) cv2.imshow('MSG Demo', img) #cv2.imwrite('stylized/%i.jpg'%idx,img) key = cv2.waitKey(1) if args.record: out.write(img) if key == 27: break cam.release() if args.record: out.release() cv2.destroyAllWindows() print('video save in ', output_name)
def run_demo(args, mirror=False): style_model = Net(ngf=args.ngf) # loading trained model specified by user style_model.load_state_dict(torch.load(args.model)) style_model.eval() # processing the style specified and applying it to image if args.cuda: style_loader = StyleLoader(args.style_folder, args.style_size) style_model.cuda() else: style_loader = StyleLoader(args.style_folder, args.style_size, False) # Define the codec and create VideoWriter object # creating another adjacent video height = args.demo_size width = int(4.0/3*args.demo_size) swidth = int(width/4) sheight = int(height/4) # if I put option record, it will save my video if args.record: fourcc = cv2.VideoWriter_fourcc('F','M','P','4') out = cv2.VideoWriter('output.mp4', fourcc, 20.0, (2*width, height)) # capture video camera cam = cv2.VideoCapture(0) cam.set(3, width) cam.set(4, height) key = 0 idx = 0 # read it frame by frame, and flip image to other side and apply style while True: # read frame idx += 1 ret_val, img = cam.read() if mirror: img = cv2.flip(img, 1) cimg = img.copy() img = np.array(img).transpose(2, 0, 1) # changing style if idx%20 == 1: style_v = style_loader.get(int(idx/20)) style_v = Variable(style_v.data) style_model.setTarget(style_v) img=torch.from_numpy(img).unsqueeze(0).float() if args.cuda: img=img.cuda() img = Variable(img) img = style_model(img) if not args.cuda: simg = style_v.cpu().data[0].numpy() img = img.cpu().clamp(0, 255).data[0].numpy() else: simg = style_v.data().numpy() img = img.clamp(0, 255).data[0].numpy() img = img.transpose(1, 2, 0).astype('uint8') simg = simg.transpose(1, 2, 0).astype('uint8') # display simg = cv2.resize(simg,(swidth, sheight), interpolation = cv2.INTER_CUBIC) cimg[0:sheight,0:swidth,:]=simg img = np.concatenate((cimg,img),axis=1) cv2.imshow('MSG Demo', img) #cv2.imwrite('stylized/%i.jpg'%idx,img) key = cv2.waitKey(1) if args.record: out.write(img) if key == 27: break cam.release() if args.record: out.release() cv2.destroyAllWindows()