Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from skimage import io, filters, color
|
| 3 |
+
import cv2
|
| 4 |
+
import numpy as np
|
| 5 |
+
from skimage.restoration import inpaint
|
| 6 |
+
|
| 7 |
+
def edge_detection(image, threshold1, threshold2):
|
| 8 |
+
gray_image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
|
| 9 |
+
edges = cv2.Canny(gray_image, threshold1, threshold2)
|
| 10 |
+
return edges
|
| 11 |
+
|
| 12 |
+
def image_segmentation(image, compactness):
|
| 13 |
+
from skimage.segmentation import slic
|
| 14 |
+
segments = slic(image, compactness=compactness, n_segments=200)
|
| 15 |
+
return color.label2rgb(segments, image, kind='avg')
|
| 16 |
+
|
| 17 |
+
def inpaint_image(image, mask_threshold):
|
| 18 |
+
gray_image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
|
| 19 |
+
_, mask = cv2.threshold(gray_image, mask_threshold, 255, cv2.THRESH_BINARY)
|
| 20 |
+
mask = mask.astype(bool)
|
| 21 |
+
inpainted = inpaint.inpaint_biharmonic(image, mask, multichannel=True)
|
| 22 |
+
return inpainted
|
| 23 |
+
|
| 24 |
+
def app():
|
| 25 |
+
with gr.Blocks() as demo:
|
| 26 |
+
gr.Markdown("# 计算机视觉功能展示")
|
| 27 |
+
gr.Markdown("使用 Scikit-Image 和 OpenCV 实现的一些基本功能")
|
| 28 |
+
with gr.Tab("边缘检测"):
|
| 29 |
+
with gr.Row():
|
| 30 |
+
input_image = gr.Image(label="输入图像")
|
| 31 |
+
edge_result = gr.Image(label="检测结果")
|
| 32 |
+
with gr.Row():
|
| 33 |
+
threshold1 = gr.Slider(0, 255, value=100, step=1, label="阈值1")
|
| 34 |
+
threshold2 = gr.Slider(0, 255, value=200, step=1, label="阈值2")
|
| 35 |
+
edge_button = gr.Button("运行边缘检测")
|
| 36 |
+
edge_button.click(edge_detection, inputs=[input_image, threshold1, threshold2], outputs=edge_result)
|
| 37 |
+
with gr.Tab("图像分割"):
|
| 38 |
+
with gr.Row():
|
| 39 |
+
input_image = gr.Image(label="输入图像")
|
| 40 |
+
seg_result = gr.Image(label="分割结果")
|
| 41 |
+
with gr.Row():
|
| 42 |
+
compactness = gr.Slider(0.1, 100, value=10, step=0.1, label="紧凑度")
|
| 43 |
+
seg_button = gr.Button("运行图像分割")
|
| 44 |
+
seg_button.click(image_segmentation, inputs=[input_image, compactness], outputs=seg_result)
|
| 45 |
+
with gr.Tab("图像修复"):
|
| 46 |
+
with gr.Row():
|
| 47 |
+
input_image = gr.Image(label="输入图像")
|
| 48 |
+
inpaint_result = gr.Image(label="修复结果")
|
| 49 |
+
with gr.Row():
|
| 50 |
+
mask_threshold = gr.Slider(0, 255, value=128, step=1, label="遮罩阈值")
|
| 51 |
+
inpaint_button = gr.Button("运行图像修复")
|
| 52 |
+
inpaint_button.click(inpaint_image, inputs=[input_image, mask_threshold], outputs=inpaint_result)
|
| 53 |
+
return demo
|
| 54 |
+
|
| 55 |
+
if __name__ == "__main__":
|
| 56 |
+
app().launch()
|