-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
55 lines (42 loc) · 1.46 KB
/
utils.py
File metadata and controls
55 lines (42 loc) · 1.46 KB
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
import os
import tempfile
import cv2
import numpy as np
import PIL
from kornia.color import lab_to_rgb, rgb_to_lab
from skimage.exposure import match_histograms
from torchvision.transforms import ToPILImage, ToTensor
def apply_lab_color_matching(image, reference_image):
image = ToTensor()(image).unsqueeze(0)
reference_image = ToTensor()(reference_image).unsqueeze(0)
image = rgb_to_lab(image)
reference_image = rgb_to_lab(reference_image)
output = match_histograms(
np.array(image[0].permute(1, 2, 0)),
np.array(reference_image[0].permute(1, 2, 0)),
channel_axis=-1,
)
output = ToTensor()(output).unsqueeze(0)
output = lab_to_rgb(output)
output = ToPILImage()(output[0])
return output
def load_video(path, height=1024, width=1024):
cap = cv2.VideoCapture(path)
frames = []
while True:
ret, frame = cap.read()
if not ret:
break
frames.append(PIL.Image.fromarray(frame).resize((height, width)))
cap.release()
return frames
def export_to_video(images, output_path, fps=10):
with tempfile.TemporaryDirectory() as temp_dir:
for i, image in enumerate(images):
frame_path = os.path.join(temp_dir, f"frame_{i:04d}.png")
image.save(frame_path)
(
ffmpeg.input(os.path.join(temp_dir, "frame_%04d.png"), framerate=fps)
.output(output_path, pix_fmt="yuv420p")
.run(overwrite_output=True)
)