MCPcopy Create free account
hub / github.com/chenfei-wu/TaskMatrix / Image2Normal

Class Image2Normal

visual_chatgpt.py:707–738  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

705
706
707class Image2Normal:
708 def __init__(self, device):
709 print("Initializing Image2Normal")
710 self.depth_estimator = pipeline("depth-estimation", model="Intel/dpt-hybrid-midas")
711 self.bg_threhold = 0.4
712
713 @prompts(name="Predict Normal Map On Image",
714 description="useful when you want to detect norm map of the image. "
715 "like: generate normal map from this image, or predict normal map of this image. "
716 "The input to this tool should be a string, representing the image_path")
717 def inference(self, inputs):
718 image = Image.open(inputs)
719 original_size = image.size
720 image = self.depth_estimator(image)['predicted_depth'][0]
721 image = image.numpy()
722 image_depth = image.copy()
723 image_depth -= np.min(image_depth)
724 image_depth /= np.max(image_depth)
725 x = cv2.Sobel(image, cv2.CV_32F, 1, 0, ksize=3)
726 x[image_depth < self.bg_threhold] = 0
727 y = cv2.Sobel(image, cv2.CV_32F, 0, 1, ksize=3)
728 y[image_depth < self.bg_threhold] = 0
729 z = np.ones_like(x) * np.pi * 2.0
730 image = np.stack([x, y, z], axis=2)
731 image /= np.sum(image ** 2.0, axis=2, keepdims=True) ** 0.5
732 image = (image * 127.5 + 127.5).clip(0, 255).astype(np.uint8)
733 image = Image.fromarray(image)
734 image = image.resize(original_size)
735 updated_image_path = get_new_image_name(inputs, func_name="normal-map")
736 image.save(updated_image_path)
737 print(f"\nProcessed Image2Normal, Input Image: {inputs}, Output Depth: {updated_image_path}")
738 return updated_image_path
739
740
741class NormalText2Image:

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected