gibiee commited on
Commit
8ed9339
·
1 Parent(s): d337a7e

change to new version

Browse files
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ *.png filter=lfs diff=lfs merge=lfs -text
README.md CHANGED
@@ -4,7 +4,7 @@ emoji: 💻
4
  colorFrom: gray
5
  colorTo: green
6
  sdk: gradio
7
- sdk_version: 4.40.0
8
  app_file: app.py
9
  pinned: false
10
  license: mit
 
4
  colorFrom: gray
5
  colorTo: green
6
  sdk: gradio
7
+ sdk_version: 5.34.2
8
  app_file: app.py
9
  pinned: false
10
  license: mit
app.py CHANGED
@@ -1,7 +1,73 @@
 
1
  import gradio as gr
2
  from PIL import Image
3
  import cv2
4
  import numpy as np
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
  js_func = """
7
  function refresh() {
@@ -13,52 +79,75 @@ function refresh() {
13
  }
14
  }
15
  """
 
16
 
17
- def print_pixel(pixel) :
18
- if isinstance(pixel, np.ndarray) :
19
- return tuple(map(int, pixel))
20
- else :
21
- return pixel
22
 
23
- with gr.Blocks(js=js_func) as demo:
 
 
 
 
 
 
 
 
 
 
 
24
 
25
- with gr.Row():
26
- color_map = gr.Image(value='color_map.png', scale=3, type='pil',
27
- interactive=False, show_label=False, show_download_button=False, show_share_button=False)
28
 
29
- with gr.Column(scale=1):
30
- pil_rgb = gr.Textbox(label='PIL RGB', interactive=False)
31
- pil_hsv = gr.Textbox(label='PIL HSV', interactive=False)
32
- pil_grayscale = gr.Textbox(label='PIL Grayscale', interactive=False)
33
-
34
- with gr.Column(scale=1):
35
- opencv_rgb = gr.Textbox(label='OpenCV RGB', interactive=False)
36
- opencv_bgr = gr.Textbox(label='OpenCV BGR', interactive=False)
37
- opencv_hsv = gr.Textbox(label='OpenCV HSV', interactive=False)
38
- opencv_hsv_full = gr.Textbox(label='OpenCV HSV_FULL', interactive=False)
39
- opencv_lab = gr.Textbox(label='OpenCV Lab', interactive=False)
40
- opencv_grayscale = gr.Textbox(label='OpenCV Grayscale', interactive=False)
41
-
42
- def get_select_coords(pil_img: Image, evt: gr.SelectData):
43
- pointX, pointY = evt.index
44
-
45
- pil_rgb = np.array(pil_img.convert('RGB'))[pointY, pointX]
46
- pil_hsv = np.array(pil_img.convert('HSV'))[pointY, pointX]
47
- pil_grayscale = np.array(pil_img.convert('L'))[pointY, pointX]
48
-
49
- img_rgb = np.array(pil_img.convert('RGB'))
50
- img_bgr = cv2.cvtColor(img_rgb, cv2.COLOR_RGB2BGR)
51
-
52
- opencv_bgr = img_bgr[pointY, pointX]
53
- opencv_rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)[pointY, pointX]
54
- opencv_hsv = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2HSV)[pointY, pointX]
55
- opencv_hsv_full = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2HSV_FULL)[pointY, pointX]
56
- opencv_lab = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2Lab)[pointY, pointX]
57
- opencv_grayscale = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2GRAY)[pointY, pointX]
58
-
59
- pixels = [pil_rgb, pil_hsv, pil_grayscale, opencv_bgr, opencv_rgb, opencv_hsv, opencv_hsv_full, opencv_lab, opencv_grayscale]
60
- return [print_pixel(pixel) for pixel in pixels]
 
 
 
 
 
 
 
 
 
 
 
 
 
61
 
62
- color_map.select(get_select_coords, inputs=[color_map], outputs=[pil_rgb, pil_hsv, pil_grayscale, opencv_bgr, opencv_rgb, opencv_hsv, opencv_hsv_full, opencv_lab, opencv_grayscale])
63
 
64
  demo.launch()
 
1
+ import glob
2
  import gradio as gr
3
  from PIL import Image
4
  import cv2
5
  import numpy as np
6
+ import pandas as pd
7
+ from collections import Counter
8
+
9
+ def check_pixel_value(img_pil_rgba: Image.Image, evt: gr.SelectData):
10
+ pointX, pointY = evt.index
11
+
12
+ pixel_pil_rgba = np.array(img_pil_rgba)[pointY, pointX]
13
+ pixel_pil_hsv = np.array(img_pil_rgba.convert('HSV'))[pointY, pointX]
14
+ pixel_pil_grayscale = np.array(img_pil_rgba.convert('L'))[pointY, pointX]
15
+
16
+ img_cv_rgba = np.array(img_pil_rgba)
17
+ img_cv_bgra = cv2.cvtColor(img_cv_rgba, cv2.COLOR_RGBA2BGRA)
18
+ pixel_cv_bgra = img_cv_bgra[pointY, pointX]
19
+ pixel_cv_rgba = cv2.cvtColor(img_cv_bgra, cv2.COLOR_BGRA2RGBA)[pointY, pointX]
20
+ pixel_cv_hsv = cv2.cvtColor(img_cv_bgra, cv2.COLOR_BGR2HSV)[pointY, pointX]
21
+ pixel_cv_hsv_full = cv2.cvtColor(img_cv_bgra, cv2.COLOR_BGR2HSV_FULL)[pointY, pointX]
22
+ pixel_cv_lab = cv2.cvtColor(img_cv_bgra, cv2.COLOR_BGR2Lab)[pointY, pointX]
23
+ pixel_cv_grayscale = cv2.cvtColor(img_cv_bgra, cv2.COLOR_BGRA2GRAY)[pointY, pointX]
24
+
25
+ pixels = [pixel_pil_rgba, pixel_pil_hsv, pixel_pil_grayscale, pixel_cv_bgra, pixel_cv_rgba, pixel_cv_hsv, pixel_cv_hsv_full, pixel_cv_lab, pixel_cv_grayscale]
26
+ for i, pixel in enumerate(pixels):
27
+ if isinstance(pixel, np.ndarray) :
28
+ pixels[i] = tuple(map(int, pixel))
29
+ return pixels
30
+
31
+ def check_pixel_distribution(img_pil_rgba, mask_pil=None):
32
+ if mask_pil is None:
33
+ rgba_array = np.array(img_pil_rgba.convert('RGBA'))
34
+ r_pixels, g_pixels, b_pixels, a_pixels = rgba_array[:,:,0], rgba_array[:,:,1], rgba_array[:,:,2], rgba_array[:,:,3]
35
+ else:
36
+ mask_bool = np.where(np.array(mask_pil.convert('L')) > 128, True, False)
37
+ rgba_array = np.array(img_pil_rgba.convert('RGBA'))[mask_bool]
38
+ r_pixels, g_pixels, b_pixels, a_pixels = rgba_array[:,0], rgba_array[:,1], rgba_array[:,2], rgba_array[:,3]
39
+
40
+ r_counter = Counter(r_pixels.flatten().tolist())
41
+ g_counter = Counter(g_pixels.flatten().tolist())
42
+ b_counter = Counter(b_pixels.flatten().tolist())
43
+ a_counter = Counter(a_pixels.flatten().tolist())
44
+
45
+ img_pil_hsv = img_pil_rgba.convert('HSV')
46
+ hsv_array = np.array(img_pil_hsv)
47
+ h_pixels, s_pixels, v_pixels = hsv_array[:,:,0], hsv_array[:,:,1], hsv_array[:,:,2]
48
+ h_counter = Counter(h_pixels.flatten().tolist())
49
+ s_counter = Counter(s_pixels.flatten().tolist())
50
+ v_counter = Counter(v_pixels.flatten().tolist())
51
+
52
+ dict = {
53
+ 'value': list(range(256)),
54
+ 'R_count': [r_counter.get(i, 0) for i in range(256)],
55
+ 'G_count': [g_counter.get(i, 0) for i in range(256)],
56
+ 'B_count': [b_counter.get(i, 0) for i in range(256)],
57
+ 'A_count': [a_counter.get(i, 0) for i in range(256)],
58
+ 'H_count': [h_counter.get(i, 0) for i in range(256)],
59
+ 'S_count': [s_counter.get(i, 0) for i in range(256)],
60
+ 'V_count': [v_counter.get(i, 0) for i in range(256)]
61
+ }
62
+ df = pd.DataFrame(dict)
63
+ return [df] * 7
64
+
65
+ def extract_from_editor(input_img, editor) :
66
+ cut_img_array = np.array(input_img.convert('RGBA'))
67
+ mask = editor['layers'][0].split()[3]
68
+ mask_bool = np.where(np.array(mask.convert('L')) > 128, True, False)
69
+ cut_img_array[~mask_bool] = (128, 128, 128, 255)
70
+ return Image.fromarray(cut_img_array), mask
71
 
72
  js_func = """
73
  function refresh() {
 
79
  }
80
  }
81
  """
82
+ with gr.Blocks(js=js_func) as demo :
83
 
84
+ with gr.Tab("Check Pixel-Value") :
85
+ with gr.Row() :
86
+ with gr.Column(scale=3):
87
+ input_img = gr.Image(sources=['upload'], type='pil', image_mode='RGBA', scale=3, show_label=False)
88
+ gr.Examples(examples=sorted(glob.glob('samples/*')), inputs=input_img)
89
 
90
+ with gr.Column(scale=1):
91
+ pil_rgb = gr.Textbox(label='PIL RGBA', interactive=False)
92
+ pil_hsv = gr.Textbox(label='PIL HSV', interactive=False)
93
+ pil_grayscale = gr.Textbox(label='PIL Grayscale', interactive=False)
94
+
95
+ with gr.Column(scale=1):
96
+ cv_rgba = gr.Textbox(label='OpenCV RGBA', interactive=False)
97
+ cv_bgra = gr.Textbox(label='OpenCV BGRA', interactive=False)
98
+ cv_hsv = gr.Textbox(label='OpenCV HSV', interactive=False)
99
+ cv_hsv_full = gr.Textbox(label='OpenCV HSV_FULL', interactive=False)
100
+ cv_lab = gr.Textbox(label='OpenCV Lab', interactive=False)
101
+ cv_grayscale = gr.Textbox(label='OpenCV Grayscale', interactive=False)
102
 
103
+ input_img.select(check_pixel_value, inputs=[input_img], outputs=[pil_rgb, pil_hsv, pil_grayscale, cv_bgra, cv_rgba, cv_hsv, cv_hsv_full, cv_lab, cv_grayscale])
 
 
104
 
105
+
106
+ with gr.Tab("Check Pixel-Distribution") :
107
+ with gr.Row():
108
+ with gr.Column(scale=1):
109
+ input_img = gr.Image(sources=['upload'], type='pil', image_mode='RGBA', scale=3, show_label=False)
110
+ gr.Examples(examples=sorted(glob.glob('samples/*')), inputs=input_img)
111
+
112
+ with gr.Column(scale=3):
113
+ with gr.Row():
114
+ r_barplot = gr.BarPlot(x='value', y='R_count', x_lim=[0, 255], x_bin=16)
115
+ g_barplot = gr.BarPlot(x='value', y='G_count', x_lim=[0, 255], x_bin=16)
116
+ b_barplot = gr.BarPlot(x='value', y='B_count', x_lim=[0, 255], x_bin=16)
117
+ a_barplot = gr.BarPlot(x='value', y='A_count', x_lim=[0, 255], x_bin=16)
118
+ with gr.Row():
119
+ h_barplot = gr.BarPlot(x='value', y='H_count', x_lim=[0, 255], x_bin=16)
120
+ s_barplot = gr.BarPlot(x='value', y='S_count', x_lim=[0, 255], x_bin=16)
121
+ v_barplot = gr.BarPlot(x='value', y='V_count', x_lim=[0, 255], x_bin=16)
122
+
123
+ input_img.change(fn=check_pixel_distribution, inputs=[input_img], outputs=[r_barplot, g_barplot, b_barplot, a_barplot, h_barplot, s_barplot, v_barplot])
124
+
125
+
126
+ with gr.Tab("Check Pixel-Distribution by Mask") :
127
+ gr.Markdown("gr.ImageEditor에서 알파 채널이 무시되는 이슈가 있기 때문에, gr.Image를 통해 이미지를 업로드합니다.")
128
+ with gr.Row() :
129
+ with gr.Column(scale=1):
130
+ input_img = gr.Image(sources=['upload'], type='pil', image_mode='RGBA', show_label=False)
131
+ input_editor = gr.ImageEditor(sources=['upload'], type='pil', image_mode='RGBA', layers=False, show_label=False, interactive=True)
132
+
133
+ btn = gr.Button('Check', variant='primary')
134
+ cut_img = gr.Image(type='pil', image_mode='RGBA', show_label=False, show_download_button=False, show_share_button=False, interactive=False)
135
+ mask_img = gr.Image(type='pil', image_mode='L', show_label=False, show_download_button=False, show_share_button=False, interactive=False)
136
+ gr.Examples(examples=sorted(glob.glob('samples/*')), inputs=input_img)
137
+
138
+ input_img.change(fn=lambda x:x, inputs=[input_img], outputs=[input_editor])
139
+
140
+ with gr.Column(scale=2):
141
+ with gr.Row():
142
+ r_barplot = gr.BarPlot(x='value', y='R_count', x_lim=[0, 255], x_bin=16)
143
+ g_barplot = gr.BarPlot(x='value', y='G_count', x_lim=[0, 255], x_bin=16)
144
+ b_barplot = gr.BarPlot(x='value', y='B_count', x_lim=[0, 255], x_bin=16)
145
+ a_barplot = gr.BarPlot(x='value', y='A_count', x_lim=[0, 255], x_bin=16)
146
+ with gr.Row():
147
+ h_barplot = gr.BarPlot(x='value', y='H_count', x_lim=[0, 255], x_bin=16)
148
+ s_barplot = gr.BarPlot(x='value', y='S_count', x_lim=[0, 255], x_bin=16)
149
+ v_barplot = gr.BarPlot(x='value', y='V_count', x_lim=[0, 255], x_bin=16)
150
 
151
+ btn.click(fn=extract_from_editor, inputs=[input_img, input_editor], outputs=[cut_img, mask_img]).success(fn=check_pixel_distribution, inputs=[input_img, mask_img], outputs=[r_barplot, g_barplot, b_barplot, a_barplot, h_barplot, s_barplot, v_barplot])
152
 
153
  demo.launch()
color_map.png DELETED
Binary file (43.3 kB)
 
requirements.txt CHANGED
@@ -1,2 +1,3 @@
1
- pillow==10.4.0
2
- opencv-python-headless==4.10.0.84
 
 
1
+ pillow==11.3.0
2
+ opencv-python-headless==4.12.0.88
3
+ matplotlib==3.10.3
samples/color_map.png ADDED

Git LFS Details

  • SHA256: 5e3e20c3226dd7c41dcc82d98f05e59ece2e599f33d820ad84789aa534d4cf02
  • Pointer size: 130 Bytes
  • Size of remote file: 43.3 kB
samples/retriever.jpg ADDED
samples/water.png ADDED

Git LFS Details

  • SHA256: 0f37ba197730cd03da9ab6f229df1594c695284e3fc677b443948b6edb59a8e4
  • Pointer size: 131 Bytes
  • Size of remote file: 410 kB