Spaces:
Running
on
T4
Running
on
T4
liuyizhang
commited on
Commit
•
ddc4ac1
1
Parent(s):
15f7af5
update app.py
Browse files
app.py
CHANGED
@@ -416,35 +416,38 @@ def create_title_image(word1, word2, word3, width, font_path='./assets/OpenSans-
|
|
416 |
# Create a new image with the specified width and white background
|
417 |
image = Image.new('RGB', (width, 60), (255, 255, 255))
|
418 |
|
419 |
-
|
420 |
-
|
421 |
-
|
422 |
-
# Keep increasing the font size until all words fit within the desired width
|
423 |
-
while True:
|
424 |
-
# Create a draw object for the image
|
425 |
-
draw = ImageDraw.Draw(image)
|
426 |
-
|
427 |
-
word_spacing = font_size / 2
|
428 |
-
# Draw each word in the appropriate color
|
429 |
-
x_offset = word_spacing
|
430 |
-
draw.text((x_offset, 0), word1, color_red, font=font)
|
431 |
-
x_offset += font.getsize(word1)[0] + word_spacing
|
432 |
-
draw.text((x_offset, 0), word2, color_black, font=font)
|
433 |
-
x_offset += font.getsize(word2)[0] + word_spacing
|
434 |
-
draw.text((x_offset, 0), word3, color_blue, font=font)
|
435 |
-
|
436 |
-
word_sizes = [font.getsize(word) for word in [word1, word2, word3]]
|
437 |
-
total_width = sum([size[0] for size in word_sizes]) + word_spacing * 3
|
438 |
-
|
439 |
-
# Stop increasing font size if the image is within the desired width
|
440 |
-
if total_width <= width:
|
441 |
-
break
|
442 |
-
|
443 |
-
# Increase font size and reset the draw object
|
444 |
-
font_size -= 1
|
445 |
-
image = Image.new('RGB', (width, 50), (255, 255, 255))
|
446 |
font = ImageFont.truetype(font_path, font_size)
|
447 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
448 |
|
449 |
return image
|
450 |
|
@@ -502,7 +505,7 @@ def relate_anything(input_image, k):
|
|
502 |
|
503 |
current_pil_image = pil_image.copy()
|
504 |
current_pil_image.alpha_composite(mask_image)
|
505 |
-
|
506 |
title_image = create_title_image('Red', relation, 'Blue', current_pil_image.size[0])
|
507 |
concate_pil_image = concatenate_images_vertical(current_pil_image, title_image)
|
508 |
pil_image_list.append(concate_pil_image)
|
@@ -704,13 +707,13 @@ if __name__ == "__main__":
|
|
704 |
args = parser.parse_args()
|
705 |
print(f'args = {args}')
|
706 |
|
707 |
-
|
708 |
get_sam_vit_h_4b8939()
|
709 |
load_groundingdino_model()
|
710 |
load_sam_model()
|
711 |
-
|
712 |
load_lama_cleaner_model()
|
713 |
-
|
714 |
|
715 |
os.system("pip list")
|
716 |
|
|
|
416 |
# Create a new image with the specified width and white background
|
417 |
image = Image.new('RGB', (width, 60), (255, 255, 255))
|
418 |
|
419 |
+
try:
|
420 |
+
# Load the specified font
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
421 |
font = ImageFont.truetype(font_path, font_size)
|
422 |
+
|
423 |
+
# Keep increasing the font size until all words fit within the desired width
|
424 |
+
while True:
|
425 |
+
# Create a draw object for the image
|
426 |
+
draw = ImageDraw.Draw(image)
|
427 |
+
|
428 |
+
word_spacing = font_size / 2
|
429 |
+
# Draw each word in the appropriate color
|
430 |
+
x_offset = word_spacing
|
431 |
+
draw.text((x_offset, 0), word1, color_red, font=font)
|
432 |
+
x_offset += font.getsize(word1)[0] + word_spacing
|
433 |
+
draw.text((x_offset, 0), word2, color_black, font=font)
|
434 |
+
x_offset += font.getsize(word2)[0] + word_spacing
|
435 |
+
draw.text((x_offset, 0), word3, color_blue, font=font)
|
436 |
+
|
437 |
+
word_sizes = [font.getsize(word) for word in [word1, word2, word3]]
|
438 |
+
total_width = sum([size[0] for size in word_sizes]) + word_spacing * 3
|
439 |
+
|
440 |
+
# Stop increasing font size if the image is within the desired width
|
441 |
+
if total_width <= width:
|
442 |
+
break
|
443 |
+
|
444 |
+
# Increase font size and reset the draw object
|
445 |
+
font_size -= 1
|
446 |
+
image = Image.new('RGB', (width, 50), (255, 255, 255))
|
447 |
+
font = ImageFont.truetype(font_path, font_size)
|
448 |
+
draw = None
|
449 |
+
except Exception as e:
|
450 |
+
pass
|
451 |
|
452 |
return image
|
453 |
|
|
|
505 |
|
506 |
current_pil_image = pil_image.copy()
|
507 |
current_pil_image.alpha_composite(mask_image)
|
508 |
+
|
509 |
title_image = create_title_image('Red', relation, 'Blue', current_pil_image.size[0])
|
510 |
concate_pil_image = concatenate_images_vertical(current_pil_image, title_image)
|
511 |
pil_image_list.append(concate_pil_image)
|
|
|
707 |
args = parser.parse_args()
|
708 |
print(f'args = {args}')
|
709 |
|
710 |
+
set_device()
|
711 |
get_sam_vit_h_4b8939()
|
712 |
load_groundingdino_model()
|
713 |
load_sam_model()
|
714 |
+
load_sd_model()
|
715 |
load_lama_cleaner_model()
|
716 |
+
load_ram_model()
|
717 |
|
718 |
os.system("pip list")
|
719 |
|