kargaranamir commited on
Commit
ccea513
1 Parent(s): ea0859d

del resize, change output

Browse files
Files changed (1) hide show
  1. app.py +22 -32
app.py CHANGED
@@ -1,6 +1,10 @@
1
  import gradio as gr
2
  import torch
3
  from utils import *
 
 
 
 
4
 
5
  torch.hub.download_url_to_file(
6
  'https://github.com/aalto-ui/aim/raw/aim2/backend/data/tests/input_values/wikipedia.org_website.png',
@@ -10,54 +14,32 @@ torch.hub.download_url_to_file(
10
  'aalto.fi_website.png')
11
 
12
 
 
13
  def inference(img, template, angel):
14
  color_image = cv2.imread(img.name, cv2.IMREAD_COLOR)
15
- height, width, _ = color_image.shape
16
-
17
- # Resize if it is bigeer than 960 * 800
18
- if width > height:
19
- if width > 960: # 3/4 * 1280
20
- coef_div = width / 960.0
21
- color_image = cv2.resize(color_image, dsize=(int(width / coef_div), int(height / coef_div)),
22
- interpolation=cv2.INTER_CUBIC)
23
- else:
24
- if height > 800: # 800
25
- coef_div = height / 800.0
26
- color_image = cv2.resize(color_image, dsize=(int(width / coef_div), int(height / coef_div)),
27
- interpolation=cv2.INTER_CUBIC)
28
 
29
  HSV_image = cv2.cvtColor(color_image, cv2.COLOR_BGR2HSV)
30
  selected_harmomic_scheme = HarmonicScheme(str(template), int(angel))
31
- new_HSV_image = best_harmomic_scheme.hue_shifted(HSV_image, num_superpixels=-1)
32
 
33
  # Compute shifted histogram
34
  histo_1 = count_hue_histogram(HSV_image)
35
  histo_2 = count_hue_histogram(new_HSV_image)
36
 
37
  # Create Hue Plots
38
- fig1 = plothis(histo_1, best_harmomic_scheme, "Source Hue")
39
  fig_1_cv = get_img_from_fig(fig1)
40
- fig2 = plothis(histo_2, best_harmomic_scheme, "Target Hue")
41
  fig_2_cv = get_img_from_fig(fig2)
42
-
43
- # Stack Hue Plots
44
- vis = np.concatenate((fig_1_cv, fig_2_cv), axis=0)
45
  # Convert HSV to BGR
46
  result_image = cv2.cvtColor(new_HSV_image, cv2.COLOR_HSV2BGR)
47
 
48
- # Final output
49
- canvas = np.full((800, 960, 3), (255, 255, 255), dtype=np.uint8)
50
- # compute center offset
51
- x_center = (960 - width) // 2
52
- y_center = (800 - height) // 2
53
- # copy img image into center of result image
54
- canvas[y_center:y_center + height, x_center:x_center + width] = result_image
55
 
56
- # Combine
57
- output = np.concatenate((vis, canvas), axis=1)
58
- cv2.imwrite('output.png', output)
59
 
60
- return ['output.png']
61
 
62
 
63
  title = 'Color Harmonization'
@@ -66,6 +48,12 @@ article = "<p style='text-align: center'></p>"
66
  examples = [['wikipedia.org_website.png'], ['aalto.fi_website.png']]
67
  css = ".output_image, .input_image {height: 40rem !important; width: 100% !important;}"
68
 
 
 
 
 
 
 
69
  gr.Interface(
70
  inference,
71
  [gr.inputs.Image(type='file', label='Input'),
@@ -73,10 +61,12 @@ gr.Interface(
73
  default="X",
74
  label="Template"),
75
  gr.inputs.Slider(0, 359, label="Angle")],
76
- [gr.outputs.Image(type='file', label='Color Harmonization of Output Image')],
 
 
77
  title=title,
78
  description=description,
79
  article=article,
80
  examples=examples,
81
- css=css,
82
  ).launch(debug=True, enable_queue=True)
 
1
  import gradio as gr
2
  import torch
3
  from utils import *
4
+ import cv2
5
+ import numpy as np
6
+ import matplotlib.pyplot as plt
7
+ import io
8
 
9
  torch.hub.download_url_to_file(
10
  'https://github.com/aalto-ui/aim/raw/aim2/backend/data/tests/input_values/wikipedia.org_website.png',
 
14
  'aalto.fi_website.png')
15
 
16
 
17
+
18
  def inference(img, template, angel):
19
  color_image = cv2.imread(img.name, cv2.IMREAD_COLOR)
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  HSV_image = cv2.cvtColor(color_image, cv2.COLOR_BGR2HSV)
22
  selected_harmomic_scheme = HarmonicScheme(str(template), int(angel))
23
+ new_HSV_image = selected_harmomic_scheme.hue_shifted(HSV_image, num_superpixels=-1)
24
 
25
  # Compute shifted histogram
26
  histo_1 = count_hue_histogram(HSV_image)
27
  histo_2 = count_hue_histogram(new_HSV_image)
28
 
29
  # Create Hue Plots
30
+ fig1 = plothis(histo_1, selected_harmomic_scheme, "Source Hue")
31
  fig_1_cv = get_img_from_fig(fig1)
32
+ fig2 = plothis(histo_2, selected_harmomic_scheme, "Target Hue")
33
  fig_2_cv = get_img_from_fig(fig2)
 
 
 
34
  # Convert HSV to BGR
35
  result_image = cv2.cvtColor(new_HSV_image, cv2.COLOR_HSV2BGR)
36
 
37
+ cv2.imwrite('fig_1_cv.jpg', fig_1_cv)
38
+ cv2.imwrite('fig_2_cv.jpg', fig_1_cv)
39
+ cv2.imwrite('result_image.jpg', result_image)
 
 
 
 
40
 
 
 
 
41
 
42
+ return ['result_image.jpg', 'fig_1_cv.jpg', 'fig_2_cv.jpg']
43
 
44
 
45
  title = 'Color Harmonization'
 
48
  examples = [['wikipedia.org_website.png'], ['aalto.fi_website.png']]
49
  css = ".output_image, .input_image {height: 40rem !important; width: 100% !important;}"
50
 
51
+ title = 'Color Harmonization'
52
+ description = 'Compute Color Harmonization with Different Templates'
53
+ article = "<p style='text-align: center'></p>"
54
+ examples = [['wikipedia.org_website.png'], ['aalto.fi_website.png']]
55
+ # css = ".output_image, .input_image {height: 40rem !important; width: 100% !important;}"
56
+
57
  gr.Interface(
58
  inference,
59
  [gr.inputs.Image(type='file', label='Input'),
 
61
  default="X",
62
  label="Template"),
63
  gr.inputs.Slider(0, 359, label="Angle")],
64
+ [gr.outputs.Image(type='file', label='Color Harmonization of Output Image'),
65
+ gr.outputs.Image(type='file', label='Source Hue'),
66
+ gr.outputs.Image(type='file', label='Target Hue'),],
67
  title=title,
68
  description=description,
69
  article=article,
70
  examples=examples,
71
+ # css=css,
72
  ).launch(debug=True, enable_queue=True)