Skip to content

Gradio

3분 만에 Python으로 기계 학습 모델용 UI 만들기

Categories

사용 프로젝트

  • Voice-Pro - 음성인식, 번역, 음성합성을 지원하는 Gradio WebUI

캔버스 클릭시 포인트 그리기

import gradio as gr
import numpy as np
import cv2

# 기본 이미지를 생성합니다. (흰색 배경)
img = np.ones((500, 500, 3), dtype=np.uint8) * 255

# 클릭 이벤트 처리 함수
def draw_point(image, evt: gr.SelectData):
    x, y = int(evt.index[0]), int(evt.index[1])

    # 이미지를 numpy array로 변환
    image = np.array(image, dtype=np.uint8)

    # 클릭한 위치에 빨간 점 그리기
    cv2.circle(image, (x, y), 10, (255, 0, 0), -1)
    return image

# Gradio 인터페이스 구성
with gr.Blocks() as demo:
    with gr.Row():
        with gr.Column():
            image = gr.Image(value=img, interactive=True, label="클릭하여 포인트 추가")

    # 이미지 클릭 이벤트 처리
    image.select(draw_point, inputs=[image], outputs=image)

# Gradio 앱 실행
demo.launch()

See also

Favorite site