Mehak900 commited on
Commit
2269add
·
verified ·
1 Parent(s): 4ed0693

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -0
app.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+
3
+ import gradio as gr
4
+ import cv2
5
+ import numpy as np
6
+ import cupy as cp
7
+
8
+ # CUDA kernel for custom filter (example sharpening)
9
+ custom_filter_kernel = cp.array([[0, -1, 0],
10
+ [-1, 5, -1],
11
+ [0, -1, 0]], dtype=cp.float32)
12
+
13
+ def edge_detection_cuda(image):
14
+ img_gpu = cp.asarray(image, dtype=cp.uint8)
15
+ gray_gpu = cp.asarray(cv2.cvtColor(image, cv2.COLOR_RGB2GRAY))
16
+ edges_gpu = cv2.Canny(cp.asnumpy(gray_gpu), 100, 200)
17
+ return cv2.cvtColor(edges_gpu, cv2.COLOR_GRAY2RGB)
18
+
19
+ def gaussian_blur_cuda(image):
20
+ img_gpu = cp.asarray(image, dtype=cp.float32)
21
+ blurred_gpu = cv2.GaussianBlur(cp.asnumpy(img_gpu), (15, 15), 0)
22
+ return blurred_gpu.astype(np.uint8)
23
+
24
+ def custom_filter_cuda(image):
25
+ img_gpu = cp.asarray(image, dtype=cp.float32)
26
+ img_gpu = cv2.filter2D(cp.asnumpy(img_gpu), -1, cp.asnumpy(custom_filter_kernel))
27
+ return img_gpu.astype(np.uint8)
28
+
29
+ def process_image(image, operation):
30
+ if image is None:
31
+ return None, None, None
32
+
33
+ edge_result = None
34
+ blur_result = None
35
+ custom_result = None
36
+
37
+ if operation == "All":
38
+ # Run operations in parallel using CUDA Streams
39
+ stream1 = cp.cuda.Stream()
40
+ stream2 = cp.cuda.Stream()
41
+ stream3 = cp.cuda.Stream()
42
+
43
+ with stream1:
44
+ edge_result = edge_detection_cuda(image)
45
+ with stream2:
46
+ blur_result = gaussian_blur_cuda(image)
47
+ with stream3:
48
+ custom_result = custom_filter_cuda(image)
49
+
50
+ stream1.synchronize()
51
+ stream2.synchronize()
52
+ stream3.synchronize()
53
+
54
+ elif operation == "Edge Detection":
55
+ edge_result = edge_detection_cuda(image)
56
+ elif operation == "Gaussian Blur":
57
+ blur_result = gaussian_blur_cuda(image)
58
+ elif operation == "Custom Filter":
59
+ custom_result = custom_filter_cuda(image)
60
+
61
+ return edge_result, blur_result, custom_result
62
+
63
+ with gr.Blocks() as demo:
64
+ gr.Markdown("## 🖼️ Parallel Image Processing with CUDA (GPU)")
65
+
66
+ with gr.Row():
67
+ with gr.Column():
68
+ image_input = gr.Image(type="numpy", label="Upload Image")
69
+ process_btn = gr.Button("Process Image", variant="primary")
70
+ with gr.Column():
71
+ operation = gr.Radio(["All", "Edge Detection", "Gaussian Blur", "Custom Filter"],
72
+ label="Choose Operation", value="All")
73
+
74
+ with gr.Row():
75
+ edge_output = gr.Image(label="Edge Detection Result")
76
+ blur_output = gr.Image(label="Gaussian Blur Result")
77
+ custom_output = gr.Image(label="Custom Filter Result")
78
+
79
+ process_btn.click(process_image, inputs=[image_input, operation],
80
+ outputs=[edge_output, blur_output, custom_output])
81
+
82
+ # Launch app
83
+ demo.launch()