-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpreprocess.py
54 lines (46 loc) · 1.4 KB
/
preprocess.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
import cv2
from PIL import Image
import os
from controlnet_aux.processor import Processor
from argparse import ArgumentParser
def controlnet_preprocess(video_file, processor_type):
processor = Processor(processor_type)
cap = cv2.VideoCapture(video_file)
post_images = []
while True:
ret, img = cap.read()
if not ret:
break
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = Image.fromarray(img)
result = processor(img, to_pil=True)
post_images.append(result)
return post_images
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument(
"--video",
required=True,
type=str,
help='video file name'
)
parser.add_argument(
"--type",
type=str,
required=True,
help="preprocess type"
)
parser.add_argument(
"--to_gif",
action="store_true",
help="output type, gif or not"
)
args = parser.parse_args()
images = controlnet_preprocess(args.video, args.type)
if args.to_gif:
from diffusers.utils import export_to_gif
export_to_gif(images, f"{args.type}.gif")
else:
os.makedirs(args.type, exist_ok=False)
for i, image in enumerate(images):
image.save(os.path.join(args.type, f"{i}.png"))