image_processing_service / image_processing_service.py
Jian-An's picture
Upload image_processing_service.py
36a5cb9 verified
# -*- 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)