File size: 11,350 Bytes
ad243cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4c4c8ef
302545f
4c4c8ef
 
e567105
 
4c4c8ef
e567105
 
 
 
302545f
e567105
 
4c4c8ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e567105
 
4c4c8ef
e567105
4c4c8ef
 
117abec
ad243cd
623c086
 
ad243cd
623c086
 
 
 
ad243cd
623c086
 
ad243cd
459da92
 
 
 
 
 
 
 
ad243cd
 
 
459da92
 
 
ad243cd
4c4c8ef
459da92
 
 
 
4c4c8ef
 
ad243cd
459da92
 
 
 
 
 
 
 
91cc48b
459da92
 
 
 
195428b
ad243cd
 
 
 
459da92
 
 
 
ad243cd
 
 
459da92
ad243cd
 
 
459da92
 
f3bbb27
459da92
f3bbb27
 
 
 
ad243cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import torch
import cv2
import os
import numpy as np
import shutil
from models.anime_gan import GeneratorV1
from models.anime_gan_v2 import GeneratorV2
from models.anime_gan_v3 import GeneratorV3
from utils.common import load_checkpoint, RELEASED_WEIGHTS
from utils.image_processing import resize_image, normalize_input, denormalize_input
from utils import read_image, is_image_file
from tqdm import tqdm
# from torch.cuda.amp import autocast

try:
    import matplotlib.pyplot as plt
except ImportError:
    plt = None

try:
    import moviepy.video.io.ffmpeg_writer as ffmpeg_writer
    from moviepy.video.io.VideoFileClip import VideoFileClip
except ImportError:
    ffmpeg_writer = None
    VideoFileClip = None


VALID_FORMATS = {
    'jpeg', 'jpg', 'jpe',
    'png', 'bmp',
}

def auto_load_weight(weight, version=None, map_location=None):
    """Auto load Generator version from weight."""
    weight_name = os.path.basename(weight).lower()
    if version is not None:
        version = version.lower()
        assert version in {"v1", "v2", "v3"}, f"Version {version} does not exist"
        # If version is provided, use it.
        cls = {
            "v1": GeneratorV1,
            "v2": GeneratorV2,
            "v3": GeneratorV3
        }[version]
    else:
        # Try to get class by name of weight file    
        # For convenenice, weight should start with classname
        # e.g: Generatorv2_{anything}.pt
        if weight_name in RELEASED_WEIGHTS:
            version = RELEASED_WEIGHTS[weight_name][0]
            return auto_load_weight(weight, version=version, map_location=map_location)

        elif weight_name.startswith("generatorv2"):
            cls = GeneratorV2
        elif weight_name.startswith("generatorv3"):
            cls = GeneratorV3
        elif weight_name.startswith("generator"):
            cls = GeneratorV1
        else:
            raise ValueError((f"Can not get Model from {weight_name}, "
                               "you might need to explicitly specify version"))
    model = cls()
    load_checkpoint(model, weight, strip_optimizer=True, map_location=map_location)
    model.eval()
    return model


class Predictor:
    def __init__(self, weight='hayao', device='cpu', amp=True):
        # if not torch.cuda.is_available():
        #     device = 'cpu'
        #     # Amp not working on cpu
        #     amp = False

        self.amp = False  # Automatic Mixed Precision
        #self.device_type = 'cuda' if device.startswith('cuda') else 'cpu'
        self.device_type = 'cpu'
        self.device = torch.device(device)
        self.G = auto_load_weight(weight, map_location=device)
        self.G.to(self.device)

    def transform_and_show(
        self,
        image_path,
        figsize=(18, 10),
        save_path=None
    ):
        image = resize_image(read_image(image_path))
        anime_img = self.transform(image)
        anime_img = anime_img.astype('uint8')

        fig = plt.figure(figsize=figsize)
        fig.add_subplot(1, 2, 1)
        # plt.title("Input")
        plt.imshow(image)
        plt.axis('off')
        fig.add_subplot(1, 2, 2)
        # plt.title("Anime style")
        plt.imshow(anime_img[0])
        plt.axis('off')
        plt.tight_layout()
        plt.show()
        if save_path is not None:
            plt.savefig(save_path)

    def transform(self, image, denorm=True):
        '''
        Transform a image to animation

        @Arguments:
            - image: np.array, shape = (Batch, width, height, channels)

        @Returns:
            - anime version of image: np.array
        '''
        with torch.no_grad():
            image = self.preprocess_images(image)
            # image = image.to(self.device)
            # with autocast(self.device_type, enabled=self.amp):
                # print(image.dtype, self.G)
            fake = self.G(image)
            fake = fake.detach().cpu().numpy()
            # Channel last
            fake = fake.transpose(0, 2, 3, 1)

            if denorm:
                fake = denormalize_input(fake, dtype=np.uint8)
            return fake

    def transform_image(self,image):
        # if not is_image_file(save_path):
        #     raise ValueError(f"{save_path} is not valid")

        # image = read_image(file_path)
        #
        # if image is None:
        #     raise ValueError(f"Could not get image from {file_path}")

        anime_img = self.transform(resize_image(image))[0]
        return anime_img
        # cv2.imwrite(save_path, anime_img[..., ::-1])
        # print(f"Anime image saved to {save_path}")

    def transform_in_dir(self, img_dir, dest_dir, max_images=0, img_size=(512, 512)):
        '''
        Read all images from img_dir, transform and write the result
        to dest_dir

        '''
        os.makedirs(dest_dir, exist_ok=True)

        files = os.listdir(img_dir)
        files = [f for f in files if self.is_valid_file(f)]
        print(f'Found {len(files)} images in {img_dir}')

        if max_images:
            files = files[:max_images]

        for fname in tqdm(files):
            image = cv2.imread(os.path.join(img_dir, fname))[:,:,::-1]
            image = resize_image(image)
            anime_img = self.transform(image)[0]
            ext = fname.split('.')[-1]
            fname = fname.replace(f'.{ext}', '')
            cv2.imwrite(os.path.join(dest_dir, f'{fname}.jpg'), anime_img[..., ::-1])



    def transform_video(self, input_path, output_path, batch_size=4, start=0, end=0):
        end = end or None

        # if not os.path.isfile(input_path):
        #     raise FileNotFoundError(f'{input_path} does not exist')

        # output_dir = "/".join(output_path.split("/")[:-1])
        # os.makedirs(output_dir, exist_ok=True)
        # is_gg_drive = '/drive/' in output_path
        # temp_file = ''

        # if is_gg_drive:
        #     temp_file = f'tmp_anime.{output_path.split(".")[-1]}'

        def transform_and_write(frames, count, writer):
            anime_images = self.transform(frames)
            for i in range(count):
                img = np.clip(anime_images[i], 0, 255).astype(np.uint8)
                writer.write(img)

        video_capture = cv2.VideoCapture(input_path)
        frame_width = int(video_capture.get(cv2.CAP_PROP_FRAME_WIDTH))
        frame_height = int(video_capture.get(cv2.CAP_PROP_FRAME_HEIGHT))
        fps = int(video_capture.get(cv2.CAP_PROP_FPS))
        frame_count = int(video_capture.get(cv2.CAP_PROP_FRAME_COUNT))

        if start or end:
            start_frame = int(start * fps)
            end_frame = int(end * fps) if end else frame_count
            video_capture.set(cv2.CAP_PROP_POS_FRAMES, start_frame)
            frame_count = end_frame - start_frame

        video_writer = cv2.VideoWriter(
            output_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (frame_width, frame_height))

        print(f'Transforming video {input_path}, {frame_count} frames, size: ({frame_width}, {frame_height})')

        batch_shape = (batch_size, frame_height, frame_width, 3)
        frames = np.zeros(batch_shape, dtype=np.uint8)
        frame_idx = 0

        try:
            for _ in tqdm(range(frame_count)):
                ret, frame = video_capture.read()
                if not ret:
                    break
                frames[frame_idx] = frame
                frame_idx += 1
                if frame_idx == batch_size:
                    transform_and_write(frames, frame_idx, video_writer)
                    frame_idx = 0
        except Exception as e:
            print(e)
        finally:
            video_capture.release()
            video_writer.release()

        # if temp_file:
        #     shutil.move(temp_file, output_path)

        # print(f'Animation video saved to {output_path}')

    def transform_video1(self, video, batch_size, start, end):
        #end = end or None

        # if not os.path.isfile(input_path):
        #     raise FileNotFoundError(f'{input_path} does not exist')

        # output_dir = "/".join(output_path.split("/")[:-1])
        # os.makedirs(output_dir, exist_ok=True)
        # is_gg_drive = '/drive/' in output_path
        # temp_file = ''

        # if is_gg_drive:
        #     temp_file = f'tmp_anime.{output_path.split(".")[-1]}'

        # def transform_and_save(self, frames, count):
        #     transformed_frames = []
        #     anime_images = self.transform(frames)
        #     for i in range(count):
        #         img = np.clip(anime_images[i], 0, 255).astype(np.uint8)
        #         transformed_frames.append(img)
        #     return transformed_frames
        def transform_and_write(frames, count, video_buffer):
            anime_images = self.transform(frames)
            for i in range(count):
                img = np.clip(anime_images[i], 0, 255).astype(np.uint8)
                success, encoded_image = cv2.imencode('.jpg', img)
                if success:
                    video_buffer.append(encoded_image.tobytes())

        video_capture = cv2.VideoCapture(video)
        frame_width = int(video_capture.get(cv2.CAP_PROP_FRAME_WIDTH))
        frame_height = int(video_capture.get(cv2.CAP_PROP_FRAME_HEIGHT))
        fps = int(video_capture.get(cv2.CAP_PROP_FPS))
        frame_count = int(video_capture.get(cv2.CAP_PROP_FRAME_COUNT))
        
        print(f'Transforming video {frame_count} frames, size: ({frame_width}, {frame_height})')

        if start or end:
            start_frame = int(start * fps)
            end_frame = int(end * fps) if end else frame_count
            video_capture.set(cv2.CAP_PROP_POS_FRAMES, start_frame)
            frame_count = end_frame - start_frame

        # frame_count = len(video_frames)
        # transformed_video_frames = []
        video_buffer = []

        # batch_shape = (batch_size) + video_frames[0].shape
        # frames = np.zeros(batch_shape, dtype=np.uint8)
        # frame_idx = 0
        batch_shape = (batch_size, frame_height, frame_width, 3)
        frames = np.zeros(batch_shape, dtype=np.uint8)
        frame_idx = 0

        try:
            for _ in range(frame_count):
                ret, frame = video_capture.read()
                if not ret:
                    break
                frames[frame_idx] = frame
                frame_idx += 1
                if frame_idx == batch_size:
                    transform_and_write(frames, frame_idx, video_buffer)
                    frame_idx = 0
        except Exception as e:
            print(e)
        finally:
            video_capture.release()

        return video_buffer



        
    def preprocess_images(self, images):
        '''
        Preprocess image for inference

        @Arguments:
            - images: np.ndarray

        @Returns
            - images: torch.tensor
        '''
        images = images.astype(np.float32)

        # Normalize to [-1, 1]
        images = normalize_input(images)
        images = torch.from_numpy(images)

        images = images.to(self.device)

        # Add batch dim
        if len(images.shape) == 3:
            images = images.unsqueeze(0)

        # channel first
        images = images.permute(0, 3, 1, 2)

        return images


    @staticmethod
    def is_valid_file(fname):
        ext = fname.split('.')[-1]
        return ext in VALID_FORMATS