Advertisement
Guest User

ESRGAN video scaling script

a guest
Apr 26th, 2019
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. import sys
  2. import os.path
  3. import glob
  4. import cv2
  5. import numpy as np
  6. import torch
  7. import architecture as arch
  8.  
  9. model_path = sys.argv[1] # models/RRDB_ESRGAN_x4.pth OR models/RRDB_PSNR_x4.pth
  10. device = torch.device('cuda') # if you want to run on CPU, change 'cuda' -> cpu
  11. # device = torch.device('cpu')
  12.  
  13. test_img_folder = 'LR_V/*'
  14.  
  15. model = arch.RRDB_Net(3, 3, 64, 23, gc=32, upscale=4, norm_type=None, act_type='leakyrelu', \
  16. mode='CNA', res_scale=1, upsample_mode='upconv')
  17. model.load_state_dict(torch.load(model_path), strict=True)
  18. model.eval()
  19. for k, v in model.named_parameters():
  20. v.requires_grad = False
  21. model = model.to(device)
  22.  
  23. print('Model path {:s}. \nTesting...'.format(model_path))
  24.  
  25. idx = 0
  26. for path in glob.glob(test_img_folder):
  27. idx += 1
  28. base = os.path.splitext(os.path.basename(path))[0]
  29. print(idx, base)
  30. cap = cv2.VideoCapture(path)
  31. width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) * 4
  32. height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) * 4
  33. fps = int(cap.get(cv2.CAP_PROP_FPS))
  34. out = cv2.VideoWriter('results/{:s}.avi'.format(base), cv2.VideoWriter_fourcc('M','J','P','G'), fps, (width,height))
  35. while True:
  36. ret, img = cap.read();
  37. if img is None:
  38. break
  39. img = img * 1.0 / 255
  40. img = torch.from_numpy(np.transpose(img[:, :, [2, 1, 0]], (2, 0, 1))).float()
  41. img_LR = img.unsqueeze(0)
  42. img_LR = img_LR.to(device)
  43.  
  44. output = model(img_LR).data.squeeze().float().cpu().clamp_(0, 1).numpy()
  45. output = np.transpose(output[[2, 1, 0], :, :], (1, 2, 0))
  46. output = (output * 255.0).round()
  47. output = np.uint8(output)
  48. out.write(output)
  49. cap.release()
  50. out.release()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement