Upload main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Dependencies
|
| 2 |
+
"""
|
| 3 |
+
1) gradio==3.4
|
| 4 |
+
2) httpx==0.32.2
|
| 5 |
+
3) setuptools
|
| 6 |
+
"""
|
| 7 |
+
import gradio as gr
|
| 8 |
+
import imageio
|
| 9 |
+
|
| 10 |
+
def Mask(img):
|
| 11 |
+
"""
|
| 12 |
+
Function to process the input image and generate a mask.
|
| 13 |
+
|
| 14 |
+
Args:
|
| 15 |
+
img (dict): Dictionary containing the base image and the mask image.
|
| 16 |
+
|
| 17 |
+
Returns:
|
| 18 |
+
tuple: A tuple containing the base image and the mask image.
|
| 19 |
+
"""
|
| 20 |
+
try:
|
| 21 |
+
# Save the mask image to a file
|
| 22 |
+
imageio.imwrite("output_image.png", img["mask"])
|
| 23 |
+
return img["image"], img["mask"]
|
| 24 |
+
except KeyError as e:
|
| 25 |
+
# Handle case where expected keys are not in the input dictionary
|
| 26 |
+
return f"Key error: {e}", None
|
| 27 |
+
except Exception as e:
|
| 28 |
+
# Handle any other unexpected errors
|
| 29 |
+
return f"An error occurred: {e}", None
|
| 30 |
+
def main():
|
| 31 |
+
# Create the Gradio interface
|
| 32 |
+
with gr.Blocks() as demo:
|
| 33 |
+
with gr.Row():
|
| 34 |
+
img = gr.Image(tool="sketch", label="Base Image", show_label=True)
|
| 35 |
+
img1 = gr.Image()
|
| 36 |
+
img2 = gr.Image(label="Mask Image", show_label=True)
|
| 37 |
+
btn = gr.Button(label="Generate Mask")
|
| 38 |
+
|
| 39 |
+
# Set the button click action
|
| 40 |
+
btn.click(Mask, inputs=img, outputs=[img1, img2])
|
| 41 |
+
|
| 42 |
+
# Launch the Gradio interface
|
| 43 |
+
demo.launch(debug=True,share=True,server_port=8888)
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
if __name__=='__main__':
|
| 47 |
+
main()
|