Spaces:
Runtime error
Runtime error
liuyizhang
commited on
Commit
•
ed7763f
1
Parent(s):
434a891
update app.py
Browse files- GroundingDINO/groundingdino/version.py +1 -1
- app.py +24 -8
GroundingDINO/groundingdino/version.py
CHANGED
@@ -1 +1 @@
|
|
1 |
-
__version__ =
|
|
|
1 |
+
__version__ = '0.1.0'
|
app.py
CHANGED
@@ -1,12 +1,15 @@
|
|
1 |
|
2 |
-
import subprocess, os, sys
|
3 |
|
4 |
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
|
5 |
|
6 |
-
|
|
|
7 |
|
8 |
result = subprocess.run(['pip', 'list'], check=True)
|
9 |
-
print(f'pip list
|
|
|
|
|
10 |
|
11 |
if not os.path.exists('./sam_vit_h_4b8939.pth'):
|
12 |
result = subprocess.run(['wget', 'https://dl.fbaipublicfiles.com/segment_anything/sam_vit_h_4b8939.pth'], check=True)
|
@@ -210,12 +213,22 @@ def run_grounded_sam(image_path, text_prompt, task_type, inpaint_prompt, box_thr
|
|
210 |
# load image
|
211 |
image_pil, image = load_image(image_path.convert("RGB"))
|
212 |
|
|
|
|
|
213 |
# visualize raw image
|
214 |
-
image_pil.save(os.path.join(output_dir, "
|
215 |
|
216 |
# run grounding dino model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
217 |
boxes_filt, pred_phrases = get_grounding_output(
|
218 |
-
groundingdino_model, image, text_prompt, box_threshold, text_threshold, device=
|
219 |
)
|
220 |
|
221 |
size = image_pil.size
|
@@ -250,9 +263,10 @@ def run_grounded_sam(image_path, text_prompt, task_type, inpaint_prompt, box_thr
|
|
250 |
}
|
251 |
# import ipdb; ipdb.set_trace()
|
252 |
image_with_box = plot_boxes_to_image(image_pil, pred_dict)[0]
|
253 |
-
image_path = os.path.join(output_dir, "
|
254 |
image_with_box.save(image_path)
|
255 |
image_result = cv2.cvtColor(cv2.imread(image_path), cv2.COLOR_BGR2RGB)
|
|
|
256 |
return image_result
|
257 |
elif task_type == 'segment':
|
258 |
assert sam_checkpoint, 'sam_checkpoint is not found!'
|
@@ -265,9 +279,10 @@ def run_grounded_sam(image_path, text_prompt, task_type, inpaint_prompt, box_thr
|
|
265 |
for box, label in zip(boxes_filt, pred_phrases):
|
266 |
show_box(box.numpy(), plt.gca(), label)
|
267 |
plt.axis('off')
|
268 |
-
image_path = os.path.join(output_dir, "
|
269 |
plt.savefig(image_path, bbox_inches="tight")
|
270 |
image_result = cv2.cvtColor(cv2.imread(image_path), cv2.COLOR_BGR2RGB)
|
|
|
271 |
return image_result
|
272 |
elif task_type == 'inpainting':
|
273 |
assert inpaint_prompt, 'inpaint_prompt is not found!'
|
@@ -277,9 +292,10 @@ def run_grounded_sam(image_path, text_prompt, task_type, inpaint_prompt, box_thr
|
|
277 |
image_pil = Image.fromarray(image)
|
278 |
|
279 |
image = sd_pipe(prompt=inpaint_prompt, image=image_pil, mask_image=mask_pil).images[0]
|
280 |
-
image_path = os.path.join(output_dir, "
|
281 |
image.save(image_path)
|
282 |
image_result = cv2.cvtColor(cv2.imread(image_path), cv2.COLOR_BGR2RGB)
|
|
|
283 |
return image_result
|
284 |
else:
|
285 |
print("task_type:{} error!".format(task_type))
|
|
|
1 |
|
2 |
+
import subprocess, os, sys, time
|
3 |
|
4 |
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
|
5 |
|
6 |
+
result = subprocess.run(['pip', 'install', '-e', 'GroundingDINO'], check=True)
|
7 |
+
print(f'pip install GroundingDINO = {result}')
|
8 |
|
9 |
result = subprocess.run(['pip', 'list'], check=True)
|
10 |
+
print(f'pip list = {result}')
|
11 |
+
|
12 |
+
sys.path.insert(0, './GroundingDINO')
|
13 |
|
14 |
if not os.path.exists('./sam_vit_h_4b8939.pth'):
|
15 |
result = subprocess.run(['wget', 'https://dl.fbaipublicfiles.com/segment_anything/sam_vit_h_4b8939.pth'], check=True)
|
|
|
213 |
# load image
|
214 |
image_pil, image = load_image(image_path.convert("RGB"))
|
215 |
|
216 |
+
file_temp = int(time.time())
|
217 |
+
|
218 |
# visualize raw image
|
219 |
+
# image_pil.save(os.path.join(output_dir, f"raw_image_{file_temp}.jpg"))
|
220 |
|
221 |
# run grounding dino model
|
222 |
+
groundingdino_device = 'cpu'
|
223 |
+
if device != 'cpu':
|
224 |
+
try:
|
225 |
+
from groundingdino import _C
|
226 |
+
groundingdino_device = 'cuda:0'
|
227 |
+
except:
|
228 |
+
warnings.warn("Failed to load custom C++ ops. Running on CPU mode Only in groundingdino!")
|
229 |
+
|
230 |
boxes_filt, pred_phrases = get_grounding_output(
|
231 |
+
groundingdino_model, image, text_prompt, box_threshold, text_threshold, device=groundingdino_device
|
232 |
)
|
233 |
|
234 |
size = image_pil.size
|
|
|
263 |
}
|
264 |
# import ipdb; ipdb.set_trace()
|
265 |
image_with_box = plot_boxes_to_image(image_pil, pred_dict)[0]
|
266 |
+
image_path = os.path.join(output_dir, f"grounding_dino_output_{file_temp}.jpg")
|
267 |
image_with_box.save(image_path)
|
268 |
image_result = cv2.cvtColor(cv2.imread(image_path), cv2.COLOR_BGR2RGB)
|
269 |
+
os.remove(image_path)
|
270 |
return image_result
|
271 |
elif task_type == 'segment':
|
272 |
assert sam_checkpoint, 'sam_checkpoint is not found!'
|
|
|
279 |
for box, label in zip(boxes_filt, pred_phrases):
|
280 |
show_box(box.numpy(), plt.gca(), label)
|
281 |
plt.axis('off')
|
282 |
+
image_path = os.path.join(output_dir, f"grounding_seg_output_{file_temp}.jpg")
|
283 |
plt.savefig(image_path, bbox_inches="tight")
|
284 |
image_result = cv2.cvtColor(cv2.imread(image_path), cv2.COLOR_BGR2RGB)
|
285 |
+
os.remove(image_path)
|
286 |
return image_result
|
287 |
elif task_type == 'inpainting':
|
288 |
assert inpaint_prompt, 'inpaint_prompt is not found!'
|
|
|
292 |
image_pil = Image.fromarray(image)
|
293 |
|
294 |
image = sd_pipe(prompt=inpaint_prompt, image=image_pil, mask_image=mask_pil).images[0]
|
295 |
+
image_path = os.path.join(output_dir, f"grounded_sam_inpainting_output_{file_temp}.jpg")
|
296 |
image.save(image_path)
|
297 |
image_result = cv2.cvtColor(cv2.imread(image_path), cv2.COLOR_BGR2RGB)
|
298 |
+
os.remove(image_path)
|
299 |
return image_result
|
300 |
else:
|
301 |
print("task_type:{} error!".format(task_type))
|