• 生成视频 zeroscope_v2_576w 学习笔记


    目录

    生成视频代码:

    维度报错:

    解决方法,修改代码:


    已开源:

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

     

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

    生成视频代码:

    1. import torch
    2. from diffusers import DiffusionPipeline, DPMSolverMultistepScheduler
    3. from diffusers.utils import export_to_video
    4. import os
    5. # os.environ['HTTP_PROXY'] = 'http://127.0.0.1:7890'
    6. os.environ["HF_TOKEN"] = "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"
    7. # os.environ['HTTPS_PROXY'] = 'https://127.0.0.1:7890'
    8. # pipe = DiffusionPipeline.from_pretrained(r"D:\360安全浏览器下载", torch_dtype=torch.float16)
    9. pipe = DiffusionPipeline.from_pretrained("cerspense/zeroscope_v2_576w", torch_dtype=torch.float16,use_auth_token=os.environ["HF_TOKEN"])
    10. pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
    11. pipe.enable_model_cpu_offload()
    12. prompt = "Darth Vader is surfing on waves"
    13. video_frames = pipe(prompt, num_inference_steps=40, height=320, width=576, num_frames=24).frames
    14. video_path = export_to_video(video_frames)
    15. print(video_path)

    维度报错:

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

    解决方法,修改代码

    1. def export_to_video(
    2. video_frames: Union[List[np.ndarray], List[PIL.Image.Image]], output_video_path: str = None, fps: int = 10
    3. ) -> str:
    4. if is_opencv_available():
    5. import cv2
    6. else:
    7. raise ImportError(BACKENDS_MAPPING["opencv"][1].format("export_to_video"))
    8. if output_video_path is None:
    9. output_video_path = tempfile.NamedTemporaryFile(suffix=".mp4").name
    10. # Convert PIL images to numpy arrays if needed
    11. if isinstance(video_frames[0], PIL.Image.Image):
    12. video_frames = [np.array(frame) for frame in video_frames]
    13. # Ensure the frames are in the correct format
    14. if isinstance(video_frames[0], np.ndarray):
    15. # Check if frames are 4-dimensional and handle accordingly
    16. if len(video_frames[0].shape) == 4:
    17. video_frames = [frame[0] for frame in video_frames]
    18. # Convert frames to uint8
    19. video_frames = [(frame * 255).astype(np.uint8) for frame in video_frames]
    20. # Ensure all frames are in (height, width, channels) format
    21. h, w, c = video_frames[0].shape
    22. fourcc = cv2.VideoWriter_fourcc(*"mp4v")
    23. video_writer = cv2.VideoWriter(output_video_path, fourcc, fps=fps, frameSize=(w, h))
    24. for frame in video_frames:
    25. img = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
    26. video_writer.write(img)
    27. video_writer.release()
    28. return output_video_path
    29. def export_to_video_o(
    30. video_frames: Union[List[np.ndarray], List[PIL.Image.Image]], output_video_path: str = None, fps: int = 10
    31. ) -> str:
    32. if is_opencv_available():
    33. import cv2
    34. else:
    35. raise ImportError(BACKENDS_MAPPING["opencv"][1].format("export_to_video"))
    36. if output_video_path is None:
    37. output_video_path = tempfile.NamedTemporaryFile(suffix=".mp4").name
    38. if isinstance(video_frames[0], np.ndarray):
    39. video_frames = [(frame * 255).astype(np.uint8) for frame in video_frames]
    40. elif isinstance(video_frames[0], PIL.Image.Image):
    41. video_frames = [np.array(frame) for frame in video_frames]
    42. fourcc = cv2.VideoWriter_fourcc(*"mp4v")
    43. h, w, c = video_frames[0].shape
    44. video_writer = cv2.VideoWriter(output_video_path, fourcc, fps=fps, frameSize=(w, h))
    45. for i in range(len(video_frames)):
    46. img = cv2.cvtColor(video_frames[i], cv2.COLOR_RGB2BGR)
    47. video_writer.write(img)
    48. return output_video_path

  • 相关阅读:
    2022-安洵杯
    【73_3】数据结构c语言版
    MDM主数据平台使用总结
    Blueprints - 虚幻中的行为树(Behavior Tree)
    和ChatGPT 比一比谁更懂Kubernetes?
    【Kingbase FlySync】命令模式:安装部署同步软件,实现KES到KES实现同步
    PHP请求API接口案例采集电商平台数据获取淘宝/天猫优惠券查询示例
    【Gensim概念】02/3 NLP玩转 word2vec
    力扣labuladong——一刷day45
    EF Core 配置模型
  • 原文地址:https://blog.csdn.net/jacke121/article/details/139871353