-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate_batch.py
68 lines (48 loc) · 1.58 KB
/
generate_batch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import glob
import os
import time
import shutil
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
import tensorflow_hub as hub
from PIL import Image
os.environ['TFHUB_DOWNLOAD'] = 'True'
IMAGE_PATH = 'original.png'
SAVED_MODEL_PATH = "https://tfhub.dev/captain-pool/esrgan-tf2/1"
def preprocess_image(image_path):
hr_image = tf.image.decode_image(tf.io.read_file(image_path))
if hr_image.shape[-1] == 4:
hr_image = hr_image[..., : -1]
hr_size = (tf.convert_to_tensor(hr_image.shape[:-1]) // 4) * 4
hr_image = tf.image.crop_to_bounding_box(
hr_image, 0, 0, hr_size[0], hr_size[1])
hr_image = tf.cast(hr_image, tf.float32)
return tf.expand_dims(hr_image, 0)
def save_image(image, filename):
if not isinstance(image, Image.Image):
image = tf.clip_by_value(image, 0, 255)
image = Image.fromarray(tf.cast(image, tf.uint8).numpy())
image.save(f"{filename}.jpg")
# hr_image = preprocess_image(IMAGE_PATH)
model = hub.load(SAVED_MODEL_PATH)
# sr_image = model(hr_image)
# sr_image = tf.squeeze(sr_image)
# save_image(tf.squeeze(sr_image), filename='sr_image')
FRAME_DIR = 'frames2/'
os.chdir(FRAME_DIR)
files = glob.glob(f"*.jpg")
try:
os.mkdir("SR/")
except FileExistsError:
shutil.rmtree("SR/")
os.mkdir("SR/")
itr = 1
for file in files:
print(f"Processing image {itr}...")
lr_image = preprocess_image(file)
sr_image = model(lr_image)
sr_image = tf.squeeze(sr_image)
image_title = str(file).split(".")[0]
save_image(tf.squeeze(sr_image), filename=f"SR/{image_title}")
itr += 1