当前位置:   article > 正文

多重controlnet控制(使用huggingface提供的API)_controlnet api

controlnet api

huggingface相关diffusers等库的下载暂不提供,可以轻易找到。

直接放代码。

  1. import torch
  2. import datetime
  3. import cv2
  4. import numpy as np
  5. from PIL import Image
  6. import PIL.Image
  7. import PIL.ImageOps
  8. from controlnet_aux import OpenposeDetector
  9. from diffusers import StableDiffusionControlNetPipeline, ControlNetModel, UniPCMultistepScheduler, \
  10. StableDiffusionPipeline
  11. def canny_img_process(imagepath, low_threshold, high_threshold):
  12. img = cv2.imread(imagepath)
  13. image = cv2.Canny(img, low_threshold, high_threshold)
  14. # zero_start = image.shape[1] // 4
  15. # zero_end = zero_start + image.shape[1] // 2
  16. # image[:, zero_start:zero_end] = 0
  17. image = image[:, :, None]
  18. image = np.concatenate([image, image, image], axis=2)
  19. image = Image.fromarray(image)
  20. return image
  21. def openpose_img_process(imagepath):
  22. image = PIL.Image.open(imagepath)
  23. openpose = OpenposeDetector.from_pretrained(
  24. # 'lllyasviel/ControlNet'
  25. './ControlNet_Cache/',
  26. filename="body_pose_model.pth",
  27. hand_filename="hand_pose_model.pth",
  28. face_filename="facenet.pth"
  29. )
  30. image = PIL.ImageOps.exif_transpose(image)
  31. image = image.convert("RGB")
  32. image = openpose(image)
  33. return image
  34. def multi_control_sd(prompt, control_mode=None, images_PATH=None, low_threshold=100,
  35. high_threshold=200, negative_prompt=None, GenNum=20, num_inference_steps=50, sketch_mode=True,
  36. controlnet_conditioning_scale=None):
  37. """
  38. :param control_mode: only support canny openpose
  39. """
  40. controlnets = []
  41. inference_img = []
  42. for mode, image_path in zip(control_mode, images_PATH):
  43. if mode == "canny":
  44. controlnets.append(ControlNetModel.from_pretrained("./Control/canny/", torch_dtype=torch.float16))
  45. cur_img = canny_img_process(image_path, low_threshold, high_threshold)
  46. inference_img.append(cur_img)
  47. if mode == "openpose":
  48. if sketch_mode:
  49. controlnets.append(ControlNetModel.from_pretrained("./Control/openpose/", torch_dtype=torch.float16))
  50. image = PIL.Image.open(image_path)
  51. inference_img.append(image)
  52. else:
  53. controlnets.append(ControlNetModel.from_pretrained("./Control/openpose/", torch_dtype=torch.float16))
  54. cur_img = openpose_img_process(image_path)
  55. inference_img.append(cur_img)
  56. pipe = StableDiffusionControlNetPipeline.from_pretrained("./stable-diffusion-v1-5/", controlnet=controlnets,
  57. torch_dtype=torch.float16)
  58. pipe = pipe.to("cuda")
  59. pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
  60. pipe.enable_model_cpu_offload()
  61. for index in range(GenNum):
  62. print("第{}次生成".format(index + 1))
  63. # random seed
  64. seed_id = int(datetime.datetime.now().timestamp())
  65. generator = torch.Generator("cuda").manual_seed(seed_id)
  66. final_image = \
  67. pipe(prompt, inference_img, num_inference_steps=num_inference_steps, negative_prompt=negative_prompt,
  68. generator=generator, controlnet_conditioning_scale=controlnet_conditioning_scale).images[0] # PIL格式 (https://pillow.readthedocs.io/en/stable/)
  69. time = datetime.datetime.now().strftime('%Y_%m_%d_%H_%M')
  70. name = time + "_" + str(seed_id)
  71. final_image.save("./control_generate_image/{}.png".format(name))
  72. if __name__ == '__main__':
  73. prompt = "a giant standing in a fantasy landscape, best quality"
  74. #prompt = "a boy is watching a cute cat"
  75. negative_prompt = "nsfw,disfigured,long neck, deformed,ugly, malformed hands,floating limbs"
  76. control_mode = ["openpose", "canny"]
  77. images_path = ["./person_pose.png", "./landscape.png"]
  78. control_scale = [1.0, 0.6]
  79. multi_control_sd(prompt, control_mode, images_path, negative_prompt=negative_prompt, num_inference_steps=399,
  80. controlnet_conditioning_scale=control_scale)
  81. # control_stable_diffusion(prompt, canny_mode=False, openpose_mode=True, imagepath="./ceshi.png",
  82. # GenNum=10, num_inference_steps=50,sketch_mode=False)
  83. # simple_stable_diffusion(prompt,negative_prompt)

以上设置都下载了相关权重文件,所以可以本地使用。在openpose处理部分需要修改源码才能实现本地部署,不然的话会连接huggingface官方,离线就不能运行了。相关操作如下:

首先进入 from_pretrained的源码,然后做如下修改:

  1. def from_pretrained(cls, pretrained_model_or_path, filename=None, hand_filename=None, face_filename=None, cache_dir=None):
  2. if pretrained_model_or_path == "lllyasviel/ControlNet":
  3. filename = filename or "annotator/ckpts/body_pose_model.pth"
  4. hand_filename = hand_filename or "annotator/ckpts/hand_pose_model.pth"
  5. face_filename = face_filename or "facenet.pth"
  6. face_pretrained_model_or_path = "lllyasviel/Annotators"
  7. else:
  8. filename = filename or "body_pose_model.pth"
  9. hand_filename = hand_filename or "hand_pose_model.pth"
  10. face_filename = face_filename or "facenet.pth"
  11. face_pretrained_model_or_path = pretrained_model_or_path
  12. # body_model_path = hf_hub_download(pretrained_model_or_path, filename, cache_dir=cache_dir)
  13. # hand_model_path = hf_hub_download(pretrained_model_or_path, hand_filename, cache_dir=cache_dir)
  14. # face_model_path = hf_hub_download(face_pretrained_model_or_path, face_filename, cache_dir=cache_dir)
  15. body_model_path = pretrained_model_or_path + filename
  16. hand_model_path = pretrained_model_or_path + hand_filename
  17. face_model_path = pretrained_model_or_path + face_filename
  18. body_estimation = Body(body_model_path)
  19. hand_estimation = Hand(hand_model_path)
  20. face_estimation = Face(face_model_path)
  21. return cls(body_estimation, hand_estimation, face_estimation)

该部分是用来处理人体姿态的,有很多其他的方法,也可以不使用他们huggingface提供的一个方法。这样就省了这个操作。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/690110
推荐阅读
相关标签
  

闽ICP备14008679号