emirhanno commited on
Commit
c97a8b1
1 Parent(s): 8fe6698

inital files

Browse files
Files changed (5) hide show
  1. .DS_Store +0 -0
  2. app.py +107 -0
  3. emirhan.tflite +0 -0
  4. example.jpeg +0 -0
  5. requirements.txt +8 -0
.DS_Store ADDED
Binary file (6.15 kB). View file
 
app.py ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import cv2
3
+ import numpy as np
4
+ import mediapipe as mp
5
+ from mediapipe.tasks import python
6
+ from mediapipe.tasks.python import vision
7
+ from mediapipe.python._framework_bindings import image as image_module
8
+ _Image = image_module.Image
9
+ from mediapipe.python._framework_bindings import image_frame
10
+ _ImageFormat = image_frame.ImageFormat
11
+
12
+ import torch
13
+ from diffusers import StableDiffusionPipeline, StableDiffusionControlNetInpaintPipeline, ControlNetModel
14
+ from PIL import Image
15
+ from compel import Compel
16
+
17
+ # Constants for colors
18
+ BG_COLOR = (0, 0, 0, 255) # gray with full opacity
19
+ MASK_COLOR = (255, 255, 255, 255) # white with full opacity
20
+
21
+ # Create the options that will be used for ImageSegmenter
22
+ base_options = python.BaseOptions(model_asset_path='emirhan.tflite')
23
+ options = vision.ImageSegmenterOptions(base_options=base_options,
24
+ output_category_mask=True)
25
+
26
+ # Initialize ControlNet inpainting pipeline
27
+ controlnet = ControlNetModel.from_pretrained(
28
+ 'lllyasviel/control_v11p_sd15_inpaint',
29
+ torch_dtype=torch.float16,
30
+ ).to("cuda")
31
+
32
+ pipe = StableDiffusionControlNetInpaintPipeline.from_pretrained(
33
+ 'runwayml/stable-diffusion-v1-5',
34
+ controlnet=controlnet,
35
+ torch_dtype=torch.float16,
36
+ ).to("cuda")
37
+
38
+ # Function to segment hair and generate mask
39
+ def segment_hair(image):
40
+ rgba_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGBA)
41
+ rgba_image[:, :, 3] = 0 # Set alpha channel to empty
42
+
43
+ # Create MP Image object from numpy array
44
+ mp_image = _Image(image_format=_ImageFormat.SRGBA, data=rgba_image)
45
+
46
+ # Create the image segmenter
47
+ with vision.ImageSegmenter.create_from_options(options) as segmenter:
48
+ # Retrieve the masks for the segmented image
49
+ segmentation_result = segmenter.segment(mp_image)
50
+ category_mask = segmentation_result.category_mask
51
+
52
+ # Generate solid color images for showing the output segmentation mask.
53
+ image_data = mp_image.numpy_view()
54
+ fg_image = np.zeros(image_data.shape, dtype=np.uint8)
55
+ fg_image[:] = MASK_COLOR
56
+ bg_image = np.zeros(image_data.shape, dtype=np.uint8)
57
+ bg_image[:] = BG_COLOR
58
+
59
+ condition = np.stack((category_mask.numpy_view(),) * 4, axis=-1) > 0.2
60
+ output_image = np.where(condition, fg_image, bg_image)
61
+
62
+ return output_image # Return the RGBA mask
63
+
64
+ # Function to inpaint the hair area using ControlNet
65
+ def inpaint_hair(image, prompt):
66
+ # Segment hair to get the mask
67
+ mask = segment_hair(image)
68
+
69
+ # Convert to PIL image for the inpainting pipeline
70
+ image_pil = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
71
+ mask_pil = Image.fromarray(cv2.cvtColor(mask, cv2.COLOR_RGBA2RGB))
72
+
73
+ # Prepare the inpainting condition
74
+ image_np = np.array(image_pil).astype(np.float32) / 255.0
75
+ mask_np = np.array(mask_pil.convert("L")).astype(np.float32) / 255.0
76
+ image_np[mask_np > 0.5] = -1.0 # Set as masked pixel
77
+ inpaint_condition = torch.from_numpy(np.expand_dims(image_np, 0).transpose(0, 3, 1, 2)).to("cuda")
78
+
79
+ # Generate inpainted image
80
+ generator = torch.Generator("cuda").manual_seed(42)
81
+ output = pipe(
82
+ prompt=prompt,
83
+ image=image_pil,
84
+ mask_image=mask_pil,
85
+ control_image=inpaint_condition,
86
+ num_inference_steps=50,
87
+ guidance_scale=7.5,
88
+ generator=generator
89
+ ).images[0]
90
+
91
+ return np.array(output)
92
+
93
+ # Gradio interface
94
+ iface = gr.Interface(
95
+ fn=inpaint_hair,
96
+ inputs=[
97
+ gr.Image(type="numpy"),
98
+ gr.Textbox(label="Prompt", placeholder="Describe the desired inpainting result...")
99
+ ],
100
+ outputs=gr.Image(type="numpy"),
101
+ title="Hair Inpainting with ControlNet",
102
+ description="Upload an image, and provide a prompt to inpaint the hair area using ControlNet.",
103
+ examples=[["example.jpeg", "dreadlocks"]]
104
+ )
105
+
106
+ if __name__ == "__main__":
107
+ iface.launch()
emirhan.tflite ADDED
Binary file (781 kB). View file
 
example.jpeg ADDED
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ opencv-python-headless
2
+ mediapipe
3
+ numpy
4
+ Pillow
5
+ torch
6
+ diffusers
7
+ transformers
8
+ compel