r3gm commited on
Commit
c2c3c5c
1 Parent(s): db93cb9

Create image_processor.py

Browse files
Files changed (1) hide show
  1. image_processor.py +130 -0
image_processor.py ADDED
@@ -0,0 +1,130 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import spaces
2
+ import gradio as gr
3
+ from stablepy import Preprocessor
4
+
5
+ PREPROCESSOR_TASKS_LIST = [
6
+ "Canny",
7
+ "Openpose",
8
+ "DPT",
9
+ "Midas",
10
+ "ZoeDepth",
11
+ "DepthAnything",
12
+ "HED",
13
+ "PidiNet",
14
+ "TEED",
15
+ "Lineart",
16
+ "LineartAnime",
17
+ "Anyline",
18
+ "Lineart standard",
19
+ "SegFormer",
20
+ "UPerNet",
21
+ "ContentShuffle",
22
+ "Recolor",
23
+ "Blur",
24
+ "MLSD",
25
+ "NormalBae",
26
+ ]
27
+
28
+ preprocessor = Preprocessor()
29
+
30
+
31
+ def process_inputs(
32
+ image,
33
+ name,
34
+ resolution,
35
+ precessor_resolution,
36
+ low_threshold,
37
+ high_threshold,
38
+ value_threshod,
39
+ distance_threshold,
40
+ recolor_mode,
41
+ recolor_gamma_correction,
42
+ blur_k_size,
43
+ pre_openpose_extra,
44
+ hed_scribble,
45
+ pre_pidinet_safe,
46
+ pre_lineart_coarse,
47
+ use_cuda,
48
+ ):
49
+ if not image:
50
+ raise ValueError("To use this, simply upload an image.")
51
+
52
+ preprocessor.load(name, False)
53
+
54
+ params = dict(
55
+ image_resolution=resolution,
56
+ detect_resolution=precessor_resolution,
57
+ low_threshold=low_threshold,
58
+ high_threshold=high_threshold,
59
+ thr_v=value_threshod,
60
+ thr_d=distance_threshold,
61
+ mode=recolor_mode,
62
+ gamma_correction=recolor_gamma_correction,
63
+ blur_sigma=blur_k_size,
64
+ hand_and_face=pre_openpose_extra,
65
+ scribble=hed_scribble,
66
+ safe=pre_pidinet_safe,
67
+ coarse=pre_lineart_coarse,
68
+ )
69
+
70
+ if use_cuda:
71
+ @spaces.GPU(duration=15)
72
+ def wrapped_func():
73
+ preprocessor.to("cuda")
74
+ return preprocessor(image, **params)
75
+ return wrapped_func()
76
+
77
+ return preprocessor(image, **params)
78
+
79
+
80
+ def preprocessor_tab():
81
+ with gr.Row():
82
+ with gr.Column():
83
+ pre_image = gr.Image(label="Image", type="pil", sources=["upload"])
84
+ pre_options = gr.Dropdown(label="Preprocessor", choices=PREPROCESSOR_TASKS_LIST, value=PREPROCESSOR_TASKS_LIST[0])
85
+ pre_img_resolution = gr.Slider(
86
+ minimum=64, maximum=4096, step=64, value=1024, label="Image Resolution",
87
+ info="The maximum proportional size of the generated image based on the uploaded image."
88
+ )
89
+ pre_start = gr.Button(value="PROCESS IMAGE", variant="primary")
90
+ with gr.Accordion("Advanced Settings", open=False):
91
+ with gr.Column():
92
+ pre_processor_resolution = gr.Slider(minimum=64, maximum=2048, step=64, value=512, label="Preprocessor Resolution")
93
+ pre_low_threshold = gr.Slider(minimum=1, maximum=255, step=1, value=100, label="'CANNY' low threshold")
94
+ pre_high_threshold = gr.Slider(minimum=1, maximum=255, step=1, value=200, label="'CANNY' high threshold")
95
+ pre_value_threshold = gr.Slider(minimum=1, maximum=2.0, step=0.01, value=0.1, label="'MLSD' Hough value threshold")
96
+ pre_distance_threshold = gr.Slider(minimum=1, maximum=20.0, step=0.01, value=0.1, label="'MLSD' Hough distance threshold")
97
+ pre_recolor_mode = gr.Dropdown(label="'RECOLOR' mode", choices=["luminance", "intensity"], value="luminance")
98
+ pre_recolor_gamma_correction = gr.Number(minimum=0., maximum=25., value=1., step=0.001, label="'RECOLOR' gamma correction")
99
+ pre_blur_k_size = gr.Number(minimum=0, maximum=100, value=9, step=1, label="'BLUR' sigma")
100
+ pre_openpose_extra = gr.Checkbox(value=True, label="'OPENPOSE' face and hand")
101
+ pre_hed_scribble = gr.Checkbox(value=False, label="'HED' scribble")
102
+ pre_pidinet_safe = gr.Checkbox(value=False, label="'PIDINET' safe")
103
+ pre_lineart_coarse = gr.Checkbox(value=False, label="'LINEART' coarse")
104
+ pre_use_cuda = gr.Checkbox(value=False, label="Use CUDA")
105
+
106
+ with gr.Column():
107
+ pre_result = gr.Image(label="Result", type="pil", interactive=False, format="png")
108
+
109
+ pre_start.click(
110
+ fn=process_inputs,
111
+ inputs=[
112
+ pre_image,
113
+ pre_options,
114
+ pre_img_resolution,
115
+ pre_processor_resolution,
116
+ pre_low_threshold,
117
+ pre_high_threshold,
118
+ pre_value_threshold,
119
+ pre_distance_threshold,
120
+ pre_recolor_mode,
121
+ pre_recolor_gamma_correction,
122
+ pre_blur_k_size,
123
+ pre_openpose_extra,
124
+ pre_hed_scribble,
125
+ pre_pidinet_safe,
126
+ pre_lineart_coarse,
127
+ pre_use_cuda,
128
+ ],
129
+ outputs=[pre_result],
130
+ )