Spaces:
Sleeping
Sleeping
# -*- coding: utf-8 -*- | |
""" | |
Created on Mon Jun 10 09:08:38 2024 | |
@author: faceb | |
""" | |
import gradio as gr | |
import cv2 | |
import numpy as np | |
def choose_one_methods_and_execute( | |
one_img, # ๅ็ | |
method, # ๆนๆณ | |
start_blue, # ๅป่ๆฏ | |
start_green, # ๅป่ๆฏ | |
rotation_options # ๆ่ฝๅ็ | |
): | |
if method == "ๅป่ๆฏ": | |
return background_removal( | |
one_img, | |
start_blue # ไธๅ ฑๆๅ ญๅ็็ทๅ | |
) | |
elif method == "ๅๅๆ่ฝ": | |
return rotate_image( | |
one_img, | |
rotation_options | |
) | |
def background_removal(one_img, start_blue): | |
lower_bound = np.array([10, 10, start_blue]) # RGB | |
upper_bound = np.array([200, 100, 100]) | |
mask = cv2.inRange(one_img, lower_bound, upper_bound) | |
masked_img = cv2.bitwise_and(one_img, one_img, mask = mask) | |
return gr.update(visible = True), masked_img | |
def rotate_image(one_img, rotate_image): | |
if rotate_image == 90: | |
img = cv2.rotate(one_img, cv2.ROTATE_90_CLOCKWISE) | |
elif rotate_image == 180: | |
img = cv2.rotate(one_img, cv2.ROTATE_180) | |
elif rotate_image == 270: | |
pass | |
else: | |
pass | |
return gr.update(visible = True), img | |
with gr.Blocks() as demo: | |
# with gr.Tab("ๅฝฑๅ่็") as image_tab: | |
upload_image = gr.Image( | |
label = "ๅ็ไธๅณ" | |
) | |
image_processing_methods = gr.Dropdown( # ไนๅพๅ ๅ ฅๅ ถไปๅ่ฝๅพ๏ผ่จๅพ่ฆๅจ้่ฃกๅ ๅ ฅ้ธ้ | |
label = "ๅๅ่็ๆๅทง", | |
choices = [ | |
"ๅป่ๆฏ", | |
"ๅๅๆ่ฝ" | |
] | |
) | |
with gr.Row(): | |
start_blue = gr.Slider( | |
minimum = 0, | |
maximum = 255, | |
label = "่่ฒไฝ็็ท" | |
) | |
start_red = gr.Slider( | |
minimum = 0, | |
maximum = 255, | |
label = "็ด ่ฒไฝ็็ท" | |
) | |
start_green = gr.Slider( | |
minimum = 0, | |
maximum = 255, | |
label = "็ถ ่ฒไฝ็็ท" | |
) | |
with gr.Row(): | |
end_blue = gr.Slider( | |
minimum = 0, | |
maximum = 255, | |
label = "่่ฒ้ซ็็ท" | |
) | |
end_red =gr.Slider( | |
minimum = 0, | |
maximum = 255, | |
label = "็ด ่ฒ้ซ็็ท" | |
) | |
end_green = gr.Slider( | |
minimum = 0, | |
maximum = 255, | |
label = "็ถ ่ฒ้ซ็็ท" | |
) | |
# ๅ็ๆ่ฝๆธๅบฆ็ญ่จญๅฎ | |
rotation_options = gr.Dropdown( | |
choices = [90, 180, 270], | |
max_choices = 1, | |
label = "่ซ้ธๆๅ็ๆ่ฝๅบฆๆธ" | |
) | |
image_button = gr.Button( | |
value = "้ๅงๅๆ" | |
) | |
display_image = gr.Image( | |
label = "็ตๆ", | |
visible = False | |
) | |
# with gr.Tab("ๅฝฑ็่็") as video_tab: | |
# upload_video = gr.Video( | |
# label = "ๅฝฑ็ไธๅณ" | |
# ) | |
image_button.click( | |
fn = choose_one_methods_and_execute, | |
inputs = [ | |
upload_image, # ๅ็ | |
image_processing_methods, # ๆนๆณ | |
start_blue, | |
start_green, | |
rotation_options | |
], | |
outputs = [display_image, display_image] | |
) | |
demo.launch(debug = True) |