logeswari commited on
Commit
ed03306
·
1 Parent(s): f3be440
Files changed (2) hide show
  1. app.py +102 -0
  2. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import cv2
3
+ import numpy as np
4
+ from PIL import Image
5
+
6
+ st.markdown("<h1 style='color:blue;'>Image Processing(comparison view)</h1>", unsafe_allow_html=True)
7
+
8
+ # Upload image in the sidebar
9
+ uploaded_file = st.sidebar.file_uploader("Upload an Image", type=["png", "jpg", "jpeg"])
10
+
11
+ if uploaded_file is not None:
12
+ image = Image.open(uploaded_file)
13
+ img_array = np.array(image)
14
+
15
+ # Select an operation using a dropdown
16
+ option = st.selectbox("Choose an operation:", [
17
+ "None", "Convert to Grayscale", "Rotate Image", "Apply Text Overlay", "Blur Image", "Convert to Color Space", "Edge Detection"
18
+ ])
19
+
20
+ # Convert to Grayscale
21
+ if option == "Convert to Grayscale":
22
+ gray_image = cv2.cvtColor(img_array, cv2.COLOR_RGB2GRAY)
23
+
24
+ col1, col2 = st.columns(2)
25
+ with col1:
26
+ st.image(image, caption="Original Image", use_container_width=True)
27
+ with col2:
28
+ st.image(gray_image, caption="Grayscale Image", use_container_width=True)
29
+
30
+ # Rotate Image
31
+ elif option == "Rotate Image":
32
+ angle = st.slider("Select Rotation Angle", -180, 180, 0)
33
+ (h, w) = img_array.shape[:2]
34
+ center = (w // 2, h // 2)
35
+ matrix = cv2.getRotationMatrix2D(center, angle, 1.0)
36
+ rotated_image = cv2.warpAffine(img_array, matrix, (w, h))
37
+
38
+ col1, col2 = st.columns(2)
39
+ with col1:
40
+ st.image(image, caption="Original Image", use_container_width=True)
41
+ with col2:
42
+ st.image(rotated_image, caption=f"Rotated by {angle}°", use_container_width=True)
43
+
44
+ # Apply Text Overlay
45
+ elif option == "Apply Text Overlay":
46
+ text = st.text_input("Enter text to overlay on the image", "Hello, Streamlit!")
47
+ image_copy = img_array.copy()
48
+ font = cv2.FONT_HERSHEY_SIMPLEX
49
+ cv2.putText(image_copy, text, (50, 50), font, 1, (255, 0, 0), 2, cv2.LINE_AA)
50
+
51
+ col1, col2 = st.columns(2)
52
+ with col1:
53
+ st.image(image, caption="Original Image", use_container_width=True)
54
+ with col2:
55
+ st.image(image_copy, caption="Image with Text Overlay", use_container_width=True)
56
+
57
+ # Blur Image
58
+ elif option == "Blur Image":
59
+ blur_level = st.slider("Select Blur Level", 1, 20, 5)
60
+ kernel_size = (blur_level * 2 + 1, blur_level * 2 + 1) # Ensure it's always an odd number
61
+ blurred_image = cv2.GaussianBlur(img_array, kernel_size, 0)
62
+
63
+ col1, col2 = st.columns(2)
64
+ with col1:
65
+ st.image(image, caption="Original Image", use_container_width=True)
66
+ with col2:
67
+ st.image(blurred_image, caption=f"Blurred (Level {blur_level})", use_container_width=True)
68
+
69
+ # Convert to Color Space (Fixed BGR to RGB)
70
+ elif option == "Convert to Color Space":
71
+ color_space = st.selectbox("Choose a color space:", ["RGB", "BGR2RGB", "Grayscale"])
72
+
73
+ converted_image = None # Initialize to avoid `NoneType` errors
74
+
75
+ if color_space == "RGB":
76
+ converted_image = img_array # Already in RGB format
77
+ elif color_space == "BGR2RGB":
78
+ converted_image = cv2.cvtColor(img_array, cv2.COLOR_BGR2RGB) # Fixed BGR to RGB conversion
79
+ elif color_space == "Grayscale":
80
+ converted_image = cv2.cvtColor(img_array, cv2.COLOR_RGB2GRAY)
81
+
82
+ if converted_image is not None:
83
+ col1, col2 = st.columns(2)
84
+ with col1:
85
+ st.image(image, caption="Original Image", use_container_width=True)
86
+ with col2:
87
+ st.image(converted_image, caption=f"{color_space} Image", use_container_width=True)
88
+
89
+ # Edge Detection (Canny)
90
+ elif option == "Edge Detection":
91
+ low_threshold = st.slider("Lower Threshold", 0, 255, 50)
92
+ high_threshold = st.slider("Upper Threshold", 0, 255, 150)
93
+
94
+ gray_image = cv2.cvtColor(img_array, cv2.COLOR_RGB2GRAY) # Convert to grayscale first
95
+ edges = cv2.Canny(gray_image, low_threshold, high_threshold) # Apply Canny Edge Detection
96
+
97
+ col1, col2 = st.columns(2)
98
+ with col1:
99
+ st.image(image, caption="Original Image", use_container_width=True)
100
+ with col2:
101
+ st.image(edges, caption="Edge Detection (Canny)", use_container_width=True)
102
+
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ streamlit
2
+ opencv-python
3
+ numpy
4
+ pillow