Demo750 commited on
Commit
b4b7ecd
1 Parent(s): 3ec95c7

Update Webpage.py

Browse files
Files changed (1) hide show
  1. Webpage.py +21 -9
Webpage.py CHANGED
@@ -4,10 +4,14 @@ import Predict
4
  import XGBoost_utils
5
  import numpy as np
6
  import cv2 as cv
 
 
7
 
8
  GENERAL_CATEGORY = {'Potatoes / Vegetables / Fruit': 0, 'Chemical products': 1, 'Photo / Film / Optical items': 2, 'Catering industry': 3, 'Industrial products other': 4, 'Media': 5, 'Real estate': 6, 'Government': 7, 'Personnel advertisements': 8, 'Cars / Commercial vehicles': 9, 'Cleaning products': 10, 'Retail': 11, 'Fragrances': 12, 'Footwear / Leather goods': 13, 'Software / Automation': 14, 'Telecommunication equipment': 15, 'Tourism': 16, 'Transport/Communication companies': 17, 'Transport services': 18, 'Insurances': 19, 'Meat / Fish / Poultry': 20, 'Detergents': 21, 'Foods General': 22, 'Other services': 23, 'Banks and Financial Services': 24, 'Office Products': 25, 'Household Items': 26, 'Non-alcoholic beverages': 27, 'Hair, Oral and Personal Care': 28, 'Fashion and Clothing': 29, 'Other products and Services': 30, 'Paper products': 31, 'Alcohol and Other Stimulants': 32, 'Medicines': 33, 'Recreation and Leisure': 34, 'Electronics': 35, 'Home Furnishings': 36, 'Products for Business Use': 37}
9
  CATEGORIES = list(GENERAL_CATEGORY.keys())
10
  CATEGORIES.sort()
 
 
11
 
12
  def calculate_areas(prompts, brand_num, pictorial_num, text_num):
13
  image_entire = prompts["image"]
@@ -33,8 +37,8 @@ def calculate_areas(prompts, brand_num, pictorial_num, text_num):
33
  text_surf += np.abs((x1-x2)*(y1-y2))
34
 
35
  ad_size = 0
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
@@ -74,12 +78,18 @@ def attention(notes, whole_display_prompt,
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,
85
  text_detection_model_path=text_detection_model_path, LDA_model_pth=LDA_model_pth,
@@ -87,7 +97,7 @@ def attention(notes, whole_display_prompt,
87
  ad_embeddings=ad_topic, ctpg_embeddings=ctpg_topic,
88
  surface_sizes=surfaces, Product_Group=prod_group,
89
  obj_detection_model_pth=None, num_topic=20, Gaze_Time_Type=gaze_type)
90
- return np.round(Gaze,2)
91
 
92
  def greet(name, intensity):
93
  return "Hello " * intensity + name + "!"
@@ -98,9 +108,9 @@ demo = gr.Interface(
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"),
@@ -108,10 +118,12 @@ demo = gr.Interface(
108
  gr.Number(label="Number of pictorial bounding boxes drawn"),
109
  gr.Number(label="Number of text bounding boxes drawn"),
110
  gr.Dropdown(CATEGORIES, label="Product Category"),
111
- gr.Textbox(label="Ad Location", info="Enter left or right or full"),
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.
 
4
  import XGBoost_utils
5
  import numpy as np
6
  import cv2 as cv
7
+ import torch
8
+ from PIL import Image
9
 
10
  GENERAL_CATEGORY = {'Potatoes / Vegetables / Fruit': 0, 'Chemical products': 1, 'Photo / Film / Optical items': 2, 'Catering industry': 3, 'Industrial products other': 4, 'Media': 5, 'Real estate': 6, 'Government': 7, 'Personnel advertisements': 8, 'Cars / Commercial vehicles': 9, 'Cleaning products': 10, 'Retail': 11, 'Fragrances': 12, 'Footwear / Leather goods': 13, 'Software / Automation': 14, 'Telecommunication equipment': 15, 'Tourism': 16, 'Transport/Communication companies': 17, 'Transport services': 18, 'Insurances': 19, 'Meat / Fish / Poultry': 20, 'Detergents': 21, 'Foods General': 22, 'Other services': 23, 'Banks and Financial Services': 24, 'Office Products': 25, 'Household Items': 26, 'Non-alcoholic beverages': 27, 'Hair, Oral and Personal Care': 28, 'Fashion and Clothing': 29, 'Other products and Services': 30, 'Paper products': 31, 'Alcohol and Other Stimulants': 32, 'Medicines': 33, 'Recreation and Leisure': 34, 'Electronics': 35, 'Home Furnishings': 36, 'Products for Business Use': 37}
11
  CATEGORIES = list(GENERAL_CATEGORY.keys())
12
  CATEGORIES.sort()
13
+ LOCATIONS = ['Left', 'Right', 'Full']
14
+ GAZE_TYPE = ['Ad', 'Brand']
15
 
16
  def calculate_areas(prompts, brand_num, pictorial_num, text_num):
17
  image_entire = prompts["image"]
 
37
  text_surf += np.abs((x1-x2)*(y1-y2))
38
 
39
  ad_size = 0
40
+ x1 = points_all[-1][0]; y1 = points_all[-1][1]
41
+ x2 = points_all[-1][3]; y2 = points_all[-1][4]
42
  ad_size += np.abs((x1-x2)*(y1-y2))
43
  ad_image = image_entire[int(y1):int(y2), int(x1):int(x2), :]
44
  left_margin = x1; right_margin = w-x2
 
78
  # caption_context = XGBoost_utils.Caption_Generation(Image.fromarray(np.uint8(context_image)))
79
  # ad_topic = XGBoost_utils.Topic_emb(caption_ad)
80
  # ctpg_topic = XGBoost_utils.Topic_emb(caption_context)
81
+ np.random.seed(42)
82
  ad_topic = np.random.randn(1,768)
83
  ctpg_topic = np.random.randn(1,768)
84
 
85
  ad = cv.resize(ad_image, (640, 832))
86
+ print('ad shape: ', ad.shape)
87
  context = cv.resize(context_image, (640, 832))
88
 
89
+ adv_imgs = torch.permute(torch.tensor(ad), (2,0,1)).unsqueeze(0)
90
+ ctpg_imgs = torch.permute(torch.tensor(context), (2,0,1)).unsqueeze(0)
91
+ ad_locations = torch.tensor([1,0]).unsqueeze(0)
92
+ heatmap = Predict.HeatMap_CNN(adv_imgs, ctpg_imgs, ad_locations, Gaze_Type='AG')
93
 
94
  Gaze = Predict.Ad_Gaze_Prediction(input_ad_path=ad, input_ctpg_path=context, ad_location=ad_loc,
95
  text_detection_model_path=text_detection_model_path, LDA_model_pth=LDA_model_pth,
 
97
  ad_embeddings=ad_topic, ctpg_embeddings=ctpg_topic,
98
  surface_sizes=surfaces, Product_Group=prod_group,
99
  obj_detection_model_pth=None, num_topic=20, Gaze_Time_Type=gaze_type)
100
+ return np.round(Gaze,2), Image.fromarray(np.flip(heatmap, axis=2))
101
 
102
  def greet(name, intensity):
103
  return "Hello " * intensity + name + "!"
 
108
  Instruction:
109
  1. Click to upload or drag the entire image that contains BOTH ad and its context;
110
  2. Draw bounding boxes in the order of:
111
+    (a) Brand element(s) (skip if N.A.)
112
+    (b) Pictorial element(s) (skip if N.A.)
113
+    (c) Text element(s) (skip if N.A.)
114
     (d) The advertisement.
115
  NOTE: Each ad element can have more than 1 boxes."""),
116
  ImagePrompter(label="Upload Entire (Ad+Context) Image, and Draw Bounding Boxes", sources=['upload'], type="pil"),
 
118
  gr.Number(label="Number of pictorial bounding boxes drawn"),
119
  gr.Number(label="Number of text bounding boxes drawn"),
120
  gr.Dropdown(CATEGORIES, label="Product Category"),
121
+ gr.Dropdown(LOCATIONS, label='Ad Location'),
122
+ gr.Dropdown(GAZE_TYPE, label='Gaze Type')
123
  ],
124
+ examples=[['Demo/004.jpg']],
125
+ outputs=[gr.Number(label="Predicted Gaze (sec)"),
126
+ gr.Image(label="Heatmap by ResNet50 (Hotter/Redder regions show more contribution)")],
127
  title="Ad Gaze Prediction",
128
  description="""This app accompanies: "Contextual Advertising with Theory-Informed Machine Learning", manuscript submitted to the Journal of Marketing.
129
  App Version: 1.0, Date: 10/24/2024.