paul hilders commited on
Commit
c3ca2bd
1 Parent(s): fcc28d6

Add labels to image

Browse files
Files changed (1) hide show
  1. app.py +15 -2
app.py CHANGED
@@ -8,6 +8,7 @@ import torch
8
  import CLIP.clip as clip
9
 
10
  import spacy
 
11
 
12
  import os
13
  os.system('python -m spacy download en_core_web_sm')
@@ -77,6 +78,14 @@ iface = gr.Interface(fn=run_demo,
77
  ["example_images/dogs_on_bed.png", "Cat"]])
78
 
79
  # NER demo:
 
 
 
 
 
 
 
 
80
  def NER_demo(image, text):
81
  # Apply NER to extract named entities, and run the explainability method
82
  # for each named entity.
@@ -87,13 +96,17 @@ def NER_demo(image, text):
87
  highlighed_entities.append((ent_text, ent_label))
88
 
89
  # As the default image, we run the default demo on the input image and text:
 
 
90
  overlapped, highlighted_text = run_demo(image, text)
91
 
92
  # Then, we run the demo for each of the named entities:
93
- gallery_images = [(overlapped, "Default explanation")]
94
  for ent_text, ent_label in highlighed_entities:
95
  overlapped_ent, highlighted_text_ent = run_demo(image, ent_text)
96
- gallery_images.append((overlapped_ent, ent_text))
 
 
97
 
98
  return highlighed_entities, gallery_images
99
 
 
8
  import CLIP.clip as clip
9
 
10
  import spacy
11
+ from PIL import Image, ImageFont, ImageDraw, ImageOps
12
 
13
  import os
14
  os.system('python -m spacy download en_core_web_sm')
 
78
  ["example_images/dogs_on_bed.png", "Cat"]])
79
 
80
  # NER demo:
81
+ def add_label_to_img(img):
82
+ img = ImageOps.expand(img, border=10, fill=(255,255,255))
83
+ draw = ImageDraw.Draw(img)
84
+ font = ImageFont.truetype("FONTS/arial.ttf", 36)
85
+ draw.text((0,0),"Sample Text",(0,255,255),font=font)
86
+
87
+ return img
88
+
89
  def NER_demo(image, text):
90
  # Apply NER to extract named entities, and run the explainability method
91
  # for each named entity.
 
96
  highlighed_entities.append((ent_text, ent_label))
97
 
98
  # As the default image, we run the default demo on the input image and text:
99
+ font = ImageFont.truetype("FONTS/arial.ttf", 36)
100
+
101
  overlapped, highlighted_text = run_demo(image, text)
102
 
103
  # Then, we run the demo for each of the named entities:
104
+ gallery_images = [overlapped]
105
  for ent_text, ent_label in highlighed_entities:
106
  overlapped_ent, highlighted_text_ent = run_demo(image, ent_text)
107
+ overlapped_ent_labelled = add_label_to_img(overlapped_ent)
108
+
109
+ gallery_images.append(overlapped_ent_labelled)
110
 
111
  return highlighed_entities, gallery_images
112