import gradio as gr import cv2 import numpy as np from skimage import io def edge_detection(image, threshold1, threshold2): """邊緣檢測""" ###先將圖片轉成灰度 gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) ###再用Canny算法檢測邊緣 edges = cv2.Canny(gray, threshold1, threshold2) return edges def line_detection(image, threshold, min_line_length, max_line_gap): """直線檢測""" ##前面先做邊緣檢測的部分 gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) edges = cv2.Canny(gray, 50, 150) ##用Hough找出直線 lines = cv2.HoughLinesP(edges, 1, np.pi / 180, threshold, minLineLength=min_line_length, maxLineGap=max_line_gap) line_image = np.zeros_like(image) ##之後再化回原圖 if lines is not None: for line in lines: x1, y1, x2, y2 = line[0] cv2.line(line_image, (x1, y1), (x2, y2), (0, 255, 0), 2) combined = cv2.addWeighted(image, 0.8, line_image, 1, 0) return combined def image_inpainting(image, mask_radius): """圖像修復""" mask = np.zeros(image.shape[:2], dtype=np.uint8) cv2.circle(mask, (image.shape[1]//2, image.shape[0]//2), mask_radius, 255, -1) inpainted = cv2.inpaint(image, mask, 3, cv2.INPAINT_TELEA) return inpainted # Example images for easy testing example_images = [ ("Edge Image 1", "https://www.sanrio.com.tw/wp-content/uploads/2018/09/25.%E6%98%8E%E6%98%9F%E4%BB%8B%E7%B4%B9KU-02.png"), ("Edge Image 2", "https://web.hocom.tw/Uploads/Product/21380_470807_ji9n9psb.jpg"), ("Line Image 1", "https://p3-sdbk2-media.byteimg.com/tos-cn-i-xv4ileqgde/5cc733d480b64417a7b9a5adc53e4f72~tplv-xv4ileqgde-resize-w:750.image"), ("Line Image 2", "https://imgs.699pic.com/images/321/926/661.jpg!detail.v1"), ("Inpaint Image 1","https://img95.699pic.com/xsj/0v/ig/gj.jpg!/fh/300"), ("Inpaint Image 2","https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcS0bxiS49IMEuZs7JjZx54JPw8x6s-YaONtPQ&s") ] # Gradio interface with gr.Blocks() as demo: gr.Markdown("## Computer Vision Web Application") with gr.Tab("Edge Detection"): with gr.Row(): with gr.Column(): img_input = gr.Image(type="numpy", label="Input Image") threshold1 = gr.Slider(0, 255, 50, step=1, label="Threshold 1") threshold2 = gr.Slider(0, 255, 150, step=1, label="Threshold 2") with gr.Column(): edge_output = gr.Image(label="Edge Detection Result") edge_button = gr.Button("Run Edge Detection") edge_button.click(edge_detection, inputs=[img_input, threshold1, threshold2], outputs=edge_output) with gr.Tab("Line Detection"): with gr.Row(): with gr.Column(): img_input_line = gr.Image(type="numpy", label="Input Image") threshold = gr.Slider(1, 100, 50, step=1, label="Threshold") min_line_length = gr.Slider(10, 200, 50, step=1, label="Min Line Length") max_line_gap = gr.Slider(1, 50, 10, step=1, label="Max Line Gap") with gr.Column(): line_output = gr.Image(label="Line Detection Result") line_button = gr.Button("Run Line Detection") line_button.click(line_detection, inputs=[img_input_line, threshold, min_line_length, max_line_gap], outputs=line_output) with gr.Tab("Image Inpainting"): with gr.Row(): with gr.Column(): img_input_inpaint = gr.Image(type="numpy", label="Input Image") mask_radius = gr.Slider(10, 100, 30, step=1, label="Mask Radius") with gr.Column(): inpaint_output = gr.Image(label="Inpainting Result") inpaint_button = gr.Button("Run Inpainting") inpaint_button.click(image_inpainting, inputs=[img_input_inpaint, mask_radius], outputs=inpaint_output) gr.Markdown("### Example Images") for label, url in example_images: gr.Markdown(f"[{label}]({url})") # Launch Gradio app demo.launch()