生成视频 zeroscope_v2_576w 学习笔记

目录

生成视频代码:

维度报错:

解决方法,修改代码:


已开源:

视频生成模型 Zeroscope开源 免费无水印

视频生成模型 Zeroscope_v2_576w 开源 - 腾讯云开发者社区-腾讯云

生成视频代码:

python 复制代码
import torch
from diffusers import DiffusionPipeline, DPMSolverMultistepScheduler
from diffusers.utils import export_to_video
import os
# os.environ['HTTP_PROXY'] = 'http://127.0.0.1:7890'

os.environ["HF_TOKEN"] = "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"
# os.environ['HTTPS_PROXY'] = 'https://127.0.0.1:7890'

# pipe = DiffusionPipeline.from_pretrained(r"D:\360安全浏览器下载", torch_dtype=torch.float16)
pipe = DiffusionPipeline.from_pretrained("cerspense/zeroscope_v2_576w", torch_dtype=torch.float16,use_auth_token=os.environ["HF_TOKEN"])
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
pipe.enable_model_cpu_offload()

prompt = "Darth Vader is surfing on waves"
video_frames = pipe(prompt, num_inference_steps=40, height=320, width=576, num_frames=24).frames
video_path = export_to_video(video_frames)
print(video_path)

维度报错:

bash 复制代码
Traceback (most recent call last):
  File "E:\project\jijia\aaa.py", line 18, in <module>
    video_path = export_to_video(video_frames)
  File "D:\ProgramData\miniconda3\envs\pysd\lib\site-packages\diffusers\utils\export_utils.py", line 135, in export_to_video
    h, w, c = video_frames[0].shape
ValueError: too many values to unpack (expected 3)

解决方法,修改代码:

python 复制代码
def export_to_video(
        video_frames: Union[List[np.ndarray], List[PIL.Image.Image]], output_video_path: str = None, fps: int = 10
) -> str:
    if is_opencv_available():
        import cv2
    else:
        raise ImportError(BACKENDS_MAPPING["opencv"][1].format("export_to_video"))

    if output_video_path is None:
        output_video_path = tempfile.NamedTemporaryFile(suffix=".mp4").name

    # Convert PIL images to numpy arrays if needed
    if isinstance(video_frames[0], PIL.Image.Image):
        video_frames = [np.array(frame) for frame in video_frames]

    # Ensure the frames are in the correct format
    if isinstance(video_frames[0], np.ndarray):
        # Check if frames are 4-dimensional and handle accordingly
        if len(video_frames[0].shape) == 4:
            video_frames = [frame[0] for frame in video_frames]

        # Convert frames to uint8
        video_frames = [(frame * 255).astype(np.uint8) for frame in video_frames]

    # Ensure all frames are in (height, width, channels) format
    h, w, c = video_frames[0].shape
    fourcc = cv2.VideoWriter_fourcc(*"mp4v")
    video_writer = cv2.VideoWriter(output_video_path, fourcc, fps=fps, frameSize=(w, h))

    for frame in video_frames:
        img = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
        video_writer.write(img)

    video_writer.release()
    return output_video_path

def export_to_video_o(
    video_frames: Union[List[np.ndarray], List[PIL.Image.Image]], output_video_path: str = None, fps: int = 10
) -> str:
    if is_opencv_available():
        import cv2
    else:
        raise ImportError(BACKENDS_MAPPING["opencv"][1].format("export_to_video"))
    if output_video_path is None:
        output_video_path = tempfile.NamedTemporaryFile(suffix=".mp4").name

    if isinstance(video_frames[0], np.ndarray):
        video_frames = [(frame * 255).astype(np.uint8) for frame in video_frames]

    elif isinstance(video_frames[0], PIL.Image.Image):
        video_frames = [np.array(frame) for frame in video_frames]

    fourcc = cv2.VideoWriter_fourcc(*"mp4v")
    h, w, c = video_frames[0].shape
    video_writer = cv2.VideoWriter(output_video_path, fourcc, fps=fps, frameSize=(w, h))
    for i in range(len(video_frames)):
        img = cv2.cvtColor(video_frames[i], cv2.COLOR_RGB2BGR)
        video_writer.write(img)
    return output_video_path
相关推荐
桃子酱紫君10 分钟前
华为配置篇-BGP实验
开发语言·华为·php
庸俗今天不摸鱼21 分钟前
【万字总结】前端全方位性能优化指南(十)——自适应优化系统、遗传算法调参、Service Worker智能降级方案
前端·性能优化·webassembly
QTX1873022 分钟前
JavaScript 中的原型链与继承
开发语言·javascript·原型模式
shaoing25 分钟前
MySQL 错误 报错:Table ‘performance_schema.session_variables’ Doesn’t Exist
java·开发语言·数据库
黄毛火烧雪下28 分钟前
React Context API 用于在组件树中共享全局状态
前端·javascript·react.js
Apifox39 分钟前
如何在 Apifox 中通过 CLI 运行包含云端数据库连接配置的测试场景
前端·后端·程序员
一张假钞41 分钟前
Firefox默认在新标签页打开收藏栏链接
前端·firefox
高达可以过山车不行42 分钟前
Firefox账号同步书签不一致(火狐浏览器书签同步不一致)
前端·firefox
m0_5937581043 分钟前
firefox 136.0.4版本离线安装MarkDown插件
前端·firefox
掘金一周1 小时前
金石焕新程 >> 瓜分万元现金大奖征文活动即将回归 | 掘金一周 4.3
前端·人工智能·后端