Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,57 +1,57 @@
|
|
1 |
-
#!/usr/bin/env python
|
2 |
-
|
3 |
-
from __future__ import annotations
|
4 |
-
|
5 |
import gradio as gr
|
6 |
-
import torch
|
7 |
-
import uvicorn
|
8 |
-
from fastapi import FastAPI, File, UploadFile
|
9 |
-
from fastapi.responses import HTMLResponse
|
10 |
-
from io import BytesIO
|
11 |
-
import numpy as np
|
12 |
-
import PIL.Image
|
13 |
from preprocessor import Preprocessor
|
14 |
|
15 |
-
|
16 |
preprocessor = Preprocessor()
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
processed_image = preprocessor(input_image)
|
21 |
-
return processed_image
|
22 |
-
|
23 |
-
@app.post("/preprocess/")
|
24 |
-
async def preprocess_image(file: UploadFile = File(...), preprocessor_name: str = 'Canny'):
|
25 |
-
contents = await file.read()
|
26 |
-
image = PIL.Image.open(BytesIO(contents))
|
27 |
-
processed_image = apply_preprocessor(image, preprocessor_name)
|
28 |
-
processed_image_pil = PIL.Image.fromarray(processed_image)
|
29 |
-
buffered = BytesIO()
|
30 |
-
processed_image_pil.save(buffered, format="JPEG")
|
31 |
-
return {"image": buffered.getvalue()}
|
32 |
-
|
33 |
-
@app.get("/")
|
34 |
-
async def main():
|
35 |
-
content = """
|
36 |
-
<body>
|
37 |
-
<form action="/preprocess/" enctype="multipart/form-data" method="post">
|
38 |
-
<input name="file" type="file">
|
39 |
-
<select name="preprocessor_name">
|
40 |
-
<option value="Canny">Canny</option>
|
41 |
-
<option value="Midas">Midas</option>
|
42 |
-
<option value="MLSD">MLSD</option>
|
43 |
-
</select>
|
44 |
-
<input type="submit">
|
45 |
-
</form>
|
46 |
-
</body>
|
47 |
-
"""
|
48 |
-
return HTMLResponse(content)
|
49 |
-
|
50 |
-
def apply_gradio_preprocessor(input_image, preprocessor_name):
|
51 |
preprocessor.load(preprocessor_name)
|
52 |
-
|
53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
if __name__ == "__main__":
|
56 |
-
|
57 |
-
.launch()
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
from preprocessor import Preprocessor
|
3 |
|
4 |
+
# Initialize Preprocessor
|
5 |
preprocessor = Preprocessor()
|
6 |
|
7 |
+
# Define processing function with extended options
|
8 |
+
def process_image(image, preprocessor_name, preprocess_resolution=None, mlsd_value_threshold=None, mlsd_distance_threshold=None, canny_low_threshold=None, canny_high_threshold=None):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
preprocessor.load(preprocessor_name)
|
10 |
+
kwargs = {}
|
11 |
+
|
12 |
+
if preprocess_resolution:
|
13 |
+
kwargs['preprocess_resolution'] = preprocess_resolution
|
14 |
+
if mlsd_value_threshold and preprocessor_name == "MLSD":
|
15 |
+
kwargs['mlsd_value_threshold'] = mlsd_value_threshold
|
16 |
+
if mlsd_distance_threshold and preprocessor_name == "MLSD":
|
17 |
+
kwargs['mlsd_distance_threshold'] = mlsd_distance_threshold
|
18 |
+
if canny_low_threshold and preprocessor_name == "Canny":
|
19 |
+
kwargs['canny_low_threshold'] = canny_low_threshold
|
20 |
+
if canny_high_threshold and preprocessor_name == "Canny":
|
21 |
+
kwargs['canny_high_threshold'] = canny_high_threshold
|
22 |
+
|
23 |
+
return preprocessor(image, **kwargs)
|
24 |
+
|
25 |
+
# UI creation with segmentation options
|
26 |
+
def create_ui():
|
27 |
+
with gr.Blocks() as demo:
|
28 |
+
with gr.Row():
|
29 |
+
image_input = gr.Image(type="pil")
|
30 |
+
preprocessor_dropdown = gr.Dropdown(choices=["ContentShuffle", "Openpose", "Midas", "MLSD", "Canny", "Lineart", "DPT", "UPerNet", "HED", "PidiNet"], label="Preprocessor")
|
31 |
+
preprocess_resolution = gr.Slider(128, 512, step=1, label="Preprocess Resolution", visible=False)
|
32 |
+
# Additional options for MLSD and Canny
|
33 |
+
mlsd_value_threshold = gr.Slider(0.01, 2.0, step=0.01, label="MLSD Value Threshold", visible=False)
|
34 |
+
mlsd_distance_threshold = gr.Slider(0.01, 20.0, step=0.01, label="MLSD Distance Threshold", visible=False)
|
35 |
+
canny_low_threshold = gr.Slider(1, 255, step=1, label="Canny Low Threshold", visible=False)
|
36 |
+
canny_high_threshold = gr.Slider(1, 255, step=1, label="Canny High Threshold", visible=False)
|
37 |
+
submit_button = gr.Button("Process")
|
38 |
+
result_image = gr.Image(label="Processed Image")
|
39 |
+
|
40 |
+
def update_options(preprocessor_name):
|
41 |
+
# Update visibility based on preprocessor choice
|
42 |
+
options_visibility = {
|
43 |
+
'preprocess_resolution': preprocessor_name in ["Openpose", "Midas", "MLSD", "Lineart", "DPT", "UPerNet", "HED", "PidiNet"],
|
44 |
+
'mlsd_value_threshold': preprocessor_name == "MLSD",
|
45 |
+
'mlsd_distance_threshold': preprocessor_name == "MLSD",
|
46 |
+
'canny_low_threshold': preprocessor_name == "Canny",
|
47 |
+
'canny_high_threshold': preprocessor_name == "Canny",
|
48 |
+
}
|
49 |
+
return list(options_visibility.values())
|
50 |
+
|
51 |
+
preprocessor_dropdown.change(fn=update_options, inputs=[preprocessor_dropdown], outputs=[preprocess_resolution, mlsd_value_threshold, mlsd_distance_threshold, canny_low_threshold, canny_high_threshold])
|
52 |
+
submit_button.click(fn=process_image, inputs=[image_input, preprocessor_dropdown, preprocess_resolution, mlsd_value_threshold, mlsd_distance_threshold, canny_low_threshold, canny_high_threshold], outputs=[result_image])
|
53 |
+
|
54 |
+
return demo
|
55 |
|
56 |
if __name__ == "__main__":
|
57 |
+
create_ui().launch()
|
|