TheoBH commited on
Commit
8d254d7
1 Parent(s): cb8adb1

Upload folder using huggingface_hub

Browse files
Files changed (6) hide show
  1. .gitignore +1 -0
  2. README.md +5 -4
  3. app.py +221 -0
  4. load_model.py +16 -0
  5. model.tflite +3 -0
  6. requirements.txt +14 -0
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ /venv/
README.md CHANGED
@@ -1,12 +1,13 @@
1
  ---
2
- title: Furnish AI
3
- emoji: 👁
4
- colorFrom: red
5
- colorTo: green
6
  sdk: gradio
7
  sdk_version: 4.36.1
8
  app_file: app.py
9
  pinned: false
 
10
  ---
11
 
12
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: Furnish_AI
3
+ emoji: 🐢
4
+ colorFrom: purple
5
+ colorTo: pink
6
  sdk: gradio
7
  sdk_version: 4.36.1
8
  app_file: app.py
9
  pinned: false
10
+ license: mit
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,221 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import cv2
3
+ from PIL import Image, ImageDraw
4
+ import mediapipe as mp
5
+ from transformers import pipeline
6
+ from skimage.measure import label, regionprops
7
+ import gradio as gr
8
+ import torch
9
+ import diffusers
10
+ import tqdm as notebook_tqdm
11
+ from diffusers import StableDiffusionInpaintPipeline
12
+ from diffusers import StableDiffusion3Pipeline
13
+ import cv2
14
+ import math
15
+ import gradio as gr
16
+ import numpy as np
17
+ import os
18
+ import mediapipe as mp
19
+ from mediapipe.tasks import python
20
+ from mediapipe.tasks.python import vision
21
+ from mediapipe.tasks.python.components import containers
22
+ from skimage.measure import label, regionprops
23
+ import numpy as np
24
+ import matplotlib.pyplot as plt
25
+ import cv2
26
+ from skimage.measure import label
27
+ from skimage.measure import regionprops
28
+ from PIL import Image
29
+ import torch
30
+ import requests
31
+ import tensorflow as tf
32
+
33
+
34
+ def _normalized_to_pixel_coordinates(
35
+ normalized_x: float, normalized_y: float, image_width: int,
36
+ image_height: int):
37
+ """Converts normalized value pair to pixel coordinates."""
38
+
39
+ # Checks if the float value is between 0 and 1.
40
+ def is_valid_normalized_value(value: float) -> bool:
41
+ return (value > 0 or math.isclose(0, value)) and (value < 1 or
42
+ math.isclose(1, value))
43
+
44
+ if not (is_valid_normalized_value(normalized_x) and
45
+ is_valid_normalized_value(normalized_y)):
46
+ # TODO: Draw coordinates even if it's outside of the image bounds.
47
+ return None
48
+ x_px = min(math.floor(normalized_x * image_width), image_width - 1)
49
+ y_px = min(math.floor(normalized_y * image_height), image_height - 1)
50
+ return x_px, y_px
51
+
52
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
53
+
54
+ pipe = StableDiffusionInpaintPipeline.from_pretrained(
55
+ "stabilityai/stable-diffusion-2-inpainting",
56
+ torch_dtype=torch.float16,
57
+ ).to(device)
58
+
59
+
60
+ BG_COLOR = (192, 192, 192) # gray
61
+ MASK_COLOR = (255, 255, 255) # white
62
+
63
+ RegionOfInterest = vision.InteractiveSegmenterRegionOfInterest
64
+ NormalizedKeypoint = containers.keypoint.NormalizedKeypoint
65
+
66
+ # Create the options that will be used for InteractiveSegmenter
67
+ base_options = python.BaseOptions(model_asset_path='model.tflite')
68
+ options = vision.ImageSegmenterOptions(base_options=base_options,
69
+ output_category_mask=True)
70
+
71
+ def get_bounding_box(mask):
72
+ """Generate bounding box coordinates from a binary mask."""
73
+ contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
74
+ if contours:
75
+ x, y, w, h = cv2.boundingRect(contours[0])
76
+ return x, y, x + w, y + h
77
+ return 0, 0, mask.shape[1], mask.shape[0]
78
+
79
+
80
+ def example_segmentation_function(image_file_path, x, y):
81
+ OVERLAY_COLOR = (255, 105, 180) # Rose
82
+ base_options = python.BaseOptions(model_asset_path='model.tflite')
83
+ options = vision.ImageSegmenterOptions(base_options=base_options, output_category_mask=True)
84
+
85
+ with python.vision.InteractiveSegmenter.create_from_options(options) as segmenter:
86
+ image = mp.Image.create_from_file(image_file_path)
87
+ roi = vision.InteractiveSegmenterRegionOfInterest(
88
+ format=vision.InteractiveSegmenterRegionOfInterest.Format.KEYPOINT,
89
+ keypoint=containers.keypoint.NormalizedKeypoint(x, y)
90
+ )
91
+ segmentation_result = segmenter.segment(image, roi)
92
+ category_mask = segmentation_result.category_mask
93
+ segmentation_mask = category_mask.numpy_view().astype(np.uint8)
94
+
95
+ return segmentation_mask, image
96
+
97
+
98
+
99
+ def segment(image_file_name, x, y, prompt):
100
+ OVERLAY_COLOR = (255, 105, 180) # Rose
101
+
102
+ # Créer le segmenteur
103
+ with python.vision.InteractiveSegmenter.create_from_options(options) as segmenter:
104
+
105
+ # Créer l'image MediaPipe
106
+ image = mp.Image.create_from_file(image_file_name)
107
+
108
+ # Récupérer les masques de catégorie pour l'image
109
+ roi = RegionOfInterest(format=RegionOfInterest.Format.KEYPOINT,
110
+ keypoint=NormalizedKeypoint(x, y))
111
+ segmentation_result = segmenter.segment(image, roi)
112
+ category_mask = segmentation_result.category_mask
113
+ # Trouver la boîte englobante de la région segmentée
114
+ mask = category_mask.numpy_view().astype(np.uint8)
115
+
116
+
117
+ # Trouver la boîte englobante de la région segmentée
118
+ x, y, w, h = cv2.boundingRect(mask)
119
+
120
+ # Convertir l'image BGR en RGB
121
+ image_data = cv2.cvtColor(image.numpy_view(), cv2.COLOR_BGR2RGB)
122
+
123
+ # Créer une image d'incrustation avec la couleur désirée (par exemple, (255, 0, 0) pour le rouge)
124
+ overlay_image = np.zeros(image_data.shape, dtype=np.uint8)
125
+ overlay_image[:] = OVERLAY_COLOR
126
+
127
+ # Créer la condition à partir du tableau category_masks
128
+ alpha = np.stack((category_mask.numpy_view(),) * 3, axis=-1) <= 0.1
129
+
130
+ # Créer un canal alpha à partir de la condition avec l'opacité désirée (par exemple, 0.7 pour 70%)
131
+ alpha = alpha.astype(float) * 0.5 # Réduire l'opacité à 50%
132
+
133
+ # Fusionner l'image originale et l'image d'incrustation en fonction du canal alpha
134
+ output_image = image_data * (1 - alpha) + overlay_image * alpha
135
+ output_image = output_image.astype(np.uint8)
136
+
137
+ # Dessiner un point blanc avec une bordure noire pour indiquer le point d'intérêt
138
+ thickness, radius = 6, -1
139
+ keypoint_px = _normalized_to_pixel_coordinates(x, y, image.width, image.height)
140
+ cv2.circle(output_image, keypoint_px, thickness + 5, (0, 0, 0), radius)
141
+ cv2.circle(output_image, keypoint_px, thickness, (255, 255, 255), radius)
142
+
143
+ # Convert the mask to binary if it's not already
144
+ binary_mask = (mask == 255).astype(np.uint8)
145
+
146
+ # Label the regions in the mask
147
+ labels = label(binary_mask)
148
+
149
+ # Obtain properties of the labeled regions
150
+ props = regionprops(labels)
151
+
152
+ # Initialize bounding box coordinates
153
+ minr, minc, maxr, maxc = 0, 0, 0, 0
154
+
155
+ for prop in props:
156
+ minr, minc, maxr, maxc = prop.bbox
157
+
158
+ # Add a 30-pixel margin
159
+ minr = max(0, minr - 300)
160
+ minc = max(0, minc - 300)
161
+ maxr = min(binary_mask.shape[0], maxr + 400)
162
+ maxc = min(binary_mask.shape[1], maxc + 400)
163
+
164
+ # Create a new black image
165
+ bbox_image = np.zeros_like(binary_mask)
166
+
167
+ # Draw the bounding box in white
168
+ bbox_image[minr:maxr, minc:maxc] = 255
169
+
170
+ print(bbox_image)
171
+ plt.imshow(bbox_image)
172
+ plt.show()
173
+
174
+
175
+ return output_image, bbox_image
176
+
177
+
178
+ def generate(image_file_path, x, y, prompt):
179
+ output_image, bbox_image = segment(image_file_path, x, y, prompt)
180
+
181
+ # Check and process images
182
+ if image_file_path is None or bbox_image is None:
183
+ return None
184
+
185
+ # Read image
186
+ img = Image.open(image_file_path).convert("RGB")
187
+
188
+ # Generate images using images and prompts
189
+ images = pipe(prompt=prompt,
190
+ image=img,
191
+ mask_image=bbox_image,
192
+ generator=torch.Generator(device="cuda").manual_seed(0),
193
+ num_images_per_prompt=3,
194
+ plms=True).images
195
+
196
+ # Create an image grid
197
+ def image_grid(imgs, rows, cols):
198
+ assert len(imgs) == rows*cols
199
+
200
+ w, h = imgs[0].size
201
+ grid = Image.new('RGB', size=(cols*w, rows*h))
202
+ grid_w, grid_h = grid.size
203
+
204
+ for i, img in enumerate(imgs):
205
+ grid.paste(img, box=(i%cols*w, i//cols*h))
206
+ return grid
207
+
208
+ grid_image = image_grid(images, 1, 3)
209
+ return output_image, grid_image
210
+
211
+
212
+ webapp = gr.Interface(fn=generate,
213
+ inputs=[
214
+ gr.Image(type="filepath", label="Upload an image"),
215
+ gr.Slider(minimum=0, maximum=1, step=0.01, label="x"),
216
+ gr.Slider(minimum=0, maximum=1, step=0.01, label="y"),
217
+ gr.Textbox(label="Prompt")],
218
+ outputs=[
219
+ gr.Image(type="pil", label="Segmented Image"),
220
+ gr.Image(type="pil", label="Generated Image Grid")])
221
+ webapp.launch(debug=True)
load_model.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import tensorflow as tf
3
+
4
+ # URL of the model file
5
+ url = "https://storage.googleapis.com/mediapipe-models/interactive_segmenter/magic_touch/float32/1/magic_touch.tflite"
6
+
7
+ # Send a HTTP request to the URL of the file, get is a method that sends a HTTP request to the server and fetches the response
8
+ response = requests.get(url)
9
+
10
+ # Write the response content to a file
11
+ with open('model.tflite', 'wb') as f:
12
+ f.write(response.content)
13
+
14
+ # Load the TFLite model
15
+ interpreter = tf.lite.Interpreter(model_path="model.tflite")
16
+ interpreter.allocate_tensors()
model.tflite ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e24338a717c1b7ad8d159666677ef400babb7f33b8ad60c4d96db4ecf694cd25
3
+ size 6227884
requirements.txt ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ numpy
2
+ opencv-python
3
+ Pillow
4
+ mediapipe
5
+ transformers
6
+ scikit-image
7
+ gradio
8
+ torch
9
+ diffusers
10
+ tqdm
11
+ matplotlib
12
+ requests
13
+ tensorflow
14
+ accelerate