Spaces:
Running
Running
Update Webpage.py
Browse filesMake it more concise
- Webpage.py +40 -26
Webpage.py
CHANGED
@@ -10,6 +10,9 @@ CATEGORIES = list(GENERAL_CATEGORY.keys())
|
|
10 |
CATEGORIES.sort()
|
11 |
|
12 |
def calculate_areas(prompts, brand_num, pictorial_num, text_num):
|
|
|
|
|
|
|
13 |
points_all = prompts["points"]
|
14 |
brand_surf = 0
|
15 |
for i in range(brand_num):
|
@@ -33,29 +36,26 @@ def calculate_areas(prompts, brand_num, pictorial_num, text_num):
|
|
33 |
x1 = points_all[-2][0]; y1 = points_all[-2][1]
|
34 |
x2 = points_all[-2][3]; y2 = points_all[-2][4]
|
35 |
ad_size += np.abs((x1-x2)*(y1-y2))
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
whole_size = 0
|
38 |
-
|
39 |
-
x2 = points_all[-1][3]; y2 = points_all[-1][4]
|
40 |
-
whole_size += np.abs((x1-x2)*(y1-y2))
|
41 |
|
42 |
-
return (brand_surf/whole_size*100, pictorial_surf/whole_size*100, text_surf/whole_size*100, ad_size/whole_size*100)
|
43 |
|
44 |
|
45 |
-
def attention(
|
46 |
brand_num, pictorial_num, text_num,
|
47 |
category, ad_location, gaze_type):
|
48 |
-
text_detection_model_path = 'EAST-Text-Detection/frozen_east_text_detection.pb'
|
49 |
-
LDA_model_pth = 'LDA_Model_trained/lda_model_best_tot.model'
|
50 |
-
training_ad_text_dictionary_path = 'LDA_Model_trained/object_word_dictionary'
|
51 |
-
training_lang_preposition_path = 'LDA_Model_trained/dutch_preposition'
|
52 |
-
|
53 |
-
# caption_ad = XGBoost_utils.Caption_Generation(ad)
|
54 |
-
# caption_context = XGBoost_utils.Caption_Generation(context)
|
55 |
-
# ad_topic = XGBoost_utils.Topic_emb(caption_ad)
|
56 |
-
# ctpg_topic = XGBoost_utils.Topic_emb(caption_context)
|
57 |
-
ad_topic = np.random.randn(1,768)
|
58 |
-
ctpg_topic = np.random.randn(1,768)
|
59 |
|
60 |
prod_group = np.zeros(38)
|
61 |
prod_group[GENERAL_CATEGORY[category]] = 1
|
@@ -67,13 +67,18 @@ def attention(whole_display_prompt, ad, context,
|
|
67 |
else:
|
68 |
ad_loc = None
|
69 |
|
70 |
-
brand_percent, visual_percent, text_percent, adv_size_percent = calculate_areas(whole_display_prompt, brand_num, pictorial_num, text_num)
|
71 |
surfaces = [brand_percent, visual_percent, text_percent, adv_size_percent*10/100]
|
72 |
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
|
|
|
|
|
|
|
|
|
|
77 |
|
78 |
|
79 |
Gaze = Predict.Ad_Gaze_Prediction(input_ad_path=ad, input_ctpg_path=context, ad_location=ad_loc,
|
@@ -89,9 +94,16 @@ def greet(name, intensity):
|
|
89 |
|
90 |
demo = gr.Interface(
|
91 |
fn=attention,
|
92 |
-
inputs=[
|
93 |
-
|
94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
gr.Number(label="Number of brand bounding boxes drawn"),
|
96 |
gr.Number(label="Number of pictorial bounding boxes drawn"),
|
97 |
gr.Number(label="Number of text bounding boxes drawn"),
|
@@ -100,8 +112,10 @@ demo = gr.Interface(
|
|
100 |
gr.Textbox(label="Gaze Type", info="Enter Ad or Brand")
|
101 |
],
|
102 |
outputs=[gr.Number(label="Predicted Gaze (sec)")],
|
103 |
-
title="Gaze Prediction",
|
104 |
-
description="
|
|
|
|
|
105 |
theme=gr.themes.Soft()
|
106 |
)
|
107 |
|
|
|
10 |
CATEGORIES.sort()
|
11 |
|
12 |
def calculate_areas(prompts, brand_num, pictorial_num, text_num):
|
13 |
+
image_entire = prompts["image"]
|
14 |
+
w, h = image_entire.size
|
15 |
+
image_entire = np.array(image_entire.convert('RGB'))
|
16 |
points_all = prompts["points"]
|
17 |
brand_surf = 0
|
18 |
for i in range(brand_num):
|
|
|
36 |
x1 = points_all[-2][0]; y1 = points_all[-2][1]
|
37 |
x2 = points_all[-2][3]; y2 = points_all[-2][4]
|
38 |
ad_size += np.abs((x1-x2)*(y1-y2))
|
39 |
+
ad_image = image_entire[int(y1):int(y2), int(x1):int(x2), :]
|
40 |
+
left_margin = x1; right_margin = w-x2
|
41 |
+
if left_margin >= right_margin:
|
42 |
+
context_image = image_entire[:, :int(x1), :]
|
43 |
+
else:
|
44 |
+
context_image = image_entire[:, int(x2):, :]
|
45 |
|
46 |
whole_size = 0
|
47 |
+
whole_size += w*h
|
|
|
|
|
48 |
|
49 |
+
return (brand_surf/whole_size*100, pictorial_surf/whole_size*100, text_surf/whole_size*100, ad_size/whole_size*100, ad_image, context_image)
|
50 |
|
51 |
|
52 |
+
def attention(notes, whole_display_prompt,
|
53 |
brand_num, pictorial_num, text_num,
|
54 |
category, ad_location, gaze_type):
|
55 |
+
text_detection_model_path = '../XGBoost_Prediction_Model/EAST-Text-Detection/frozen_east_text_detection.pb'
|
56 |
+
LDA_model_pth = '../XGBoost_Prediction_Model/LDA_Model_trained/lda_model_best_tot.model'
|
57 |
+
training_ad_text_dictionary_path = '../XGBoost_Prediction_Model/LDA_Model_trained/object_word_dictionary'
|
58 |
+
training_lang_preposition_path = '../XGBoost_Prediction_Model/LDA_Model_trained/dutch_preposition'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
prod_group = np.zeros(38)
|
61 |
prod_group[GENERAL_CATEGORY[category]] = 1
|
|
|
67 |
else:
|
68 |
ad_loc = None
|
69 |
|
70 |
+
brand_percent, visual_percent, text_percent, adv_size_percent, ad_image, context_image = calculate_areas(whole_display_prompt, brand_num, pictorial_num, text_num)
|
71 |
surfaces = [brand_percent, visual_percent, text_percent, adv_size_percent*10/100]
|
72 |
|
73 |
+
# caption_ad = XGBoost_utils.Caption_Generation(Image.fromarray(np.uint8(ad_image)))
|
74 |
+
# caption_context = XGBoost_utils.Caption_Generation(Image.fromarray(np.uint8(context_image)))
|
75 |
+
# ad_topic = XGBoost_utils.Topic_emb(caption_ad)
|
76 |
+
# ctpg_topic = XGBoost_utils.Topic_emb(caption_context)
|
77 |
+
ad_topic = np.random.randn(1,768)
|
78 |
+
ctpg_topic = np.random.randn(1,768)
|
79 |
+
|
80 |
+
ad = cv.resize(ad_image, (640, 832))
|
81 |
+
context = cv.resize(context_image, (640, 832))
|
82 |
|
83 |
|
84 |
Gaze = Predict.Ad_Gaze_Prediction(input_ad_path=ad, input_ctpg_path=context, ad_location=ad_loc,
|
|
|
94 |
|
95 |
demo = gr.Interface(
|
96 |
fn=attention,
|
97 |
+
inputs=[gr.Markdown("""
|
98 |
+
Instruction:
|
99 |
+
1. Click to upload or drag the entire image that contains BOTH ad and its context;
|
100 |
+
2. Draw bounding boxes in the order of:
|
101 |
+
(a) Brand element(s)
|
102 |
+
(b) Pictorial element(s)
|
103 |
+
(c) Text element(s)
|
104 |
+
(d) The advertisement.
|
105 |
+
NOTE: Each ad element can have more than 1 boxes."""),
|
106 |
+
ImagePrompter(label="Upload Entire (Ad+Context) Image, and Draw Bounding Boxes", sources=['upload'], type="pil"),
|
107 |
gr.Number(label="Number of brand bounding boxes drawn"),
|
108 |
gr.Number(label="Number of pictorial bounding boxes drawn"),
|
109 |
gr.Number(label="Number of text bounding boxes drawn"),
|
|
|
112 |
gr.Textbox(label="Gaze Type", info="Enter Ad or Brand")
|
113 |
],
|
114 |
outputs=[gr.Number(label="Predicted Gaze (sec)")],
|
115 |
+
title="Ad Gaze Prediction",
|
116 |
+
description="""This app accompanies: "Contextual Advertising with Theory-Informed Machine Learning", manuscript submitted to the Journal of Marketing.
|
117 |
+
App Version: 1.0, Date: 10/24/2024.
|
118 |
+
Warning: Due to computational efficiency, current version has not activated LLM generated ad topics. In future version, LLM topics will be activated in GPU environment.""",
|
119 |
theme=gr.themes.Soft()
|
120 |
)
|
121 |
|