Spaces:
Runtime error
Runtime error
sheiknoorullah@gmail.com
commited on
Commit
·
95bbbb5
1
Parent(s):
e55f2bc
initial commit
Browse files- app.py +101 -0
- requirements.txt +4 -0
app.py
ADDED
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import cv2
|
3 |
+
import numpy as np
|
4 |
+
from PIL import Image
|
5 |
+
|
6 |
+
st.title("OpenCV Image Processing")
|
7 |
+
st.write("This is a simple web app to demonstrate how to use OpenCV with Streamlit.")
|
8 |
+
|
9 |
+
uploaded_file = st.file_uploader(
|
10 |
+
"Choose an image...", type=["png", "jpg", "jpeg"])
|
11 |
+
|
12 |
+
if uploaded_file is not None:
|
13 |
+
st.text("Image uploaded successfully.")
|
14 |
+
st.write("Please select an option from the checkboxes below:")
|
15 |
+
|
16 |
+
options = {
|
17 |
+
"Show Grayscale": False,
|
18 |
+
"Resize Image": False,
|
19 |
+
"Crop Image": False,
|
20 |
+
"Rotate Image": False,
|
21 |
+
"Flip Image": False,
|
22 |
+
}
|
23 |
+
|
24 |
+
for option_name in options:
|
25 |
+
options[option_name] = st.checkbox(option_name)
|
26 |
+
|
27 |
+
st.subheader("Original vs Processed Image")
|
28 |
+
col1, col2 = st.columns(2)
|
29 |
+
|
30 |
+
if uploaded_file is not None: # Check if a file was actually uploaded
|
31 |
+
image = Image.open(uploaded_file).convert(
|
32 |
+
"RGB") # CRUCIAL: Convert to RGB immediately
|
33 |
+
original_image = np.array(image)
|
34 |
+
processed_image = original_image.copy()
|
35 |
+
|
36 |
+
with col1:
|
37 |
+
st.image(original_image, caption="Original Image",
|
38 |
+
channels="RGB") # Display in RGB
|
39 |
+
|
40 |
+
if any(options.values()):
|
41 |
+
if options["Show Grayscale"]:
|
42 |
+
processed_image = cv2.cvtColor(
|
43 |
+
processed_image, cv2.COLOR_RGB2GRAY)
|
44 |
+
|
45 |
+
if options["Resize Image"]:
|
46 |
+
width = st.slider(
|
47 |
+
"New Width", 100, original_image.shape[1] * 2, original_image.shape[1], step=10)
|
48 |
+
height = st.slider(
|
49 |
+
"New Height", 100, original_image.shape[0] * 2, original_image.shape[0], step=10)
|
50 |
+
processed_image = cv2.resize(processed_image, (width, height))
|
51 |
+
if len(processed_image.shape) == 2:
|
52 |
+
processed_image = cv2.cvtColor(
|
53 |
+
processed_image, cv2.COLOR_GRAY2RGB)
|
54 |
+
|
55 |
+
if options["Crop Image"]:
|
56 |
+
x1 = st.slider("X1", 0, original_image.shape[1], 0)
|
57 |
+
y1 = st.slider("Y1", 0, original_image.shape[0], 0)
|
58 |
+
x2 = st.slider(
|
59 |
+
"X2", 0, original_image.shape[1], original_image.shape[1])
|
60 |
+
y2 = st.slider(
|
61 |
+
"Y2", 0, original_image.shape[0], original_image.shape[0])
|
62 |
+
processed_image = processed_image[y1:y2, x1:x2]
|
63 |
+
if len(processed_image.shape) == 2:
|
64 |
+
processed_image = cv2.cvtColor(
|
65 |
+
processed_image, cv2.COLOR_GRAY2RGB)
|
66 |
+
|
67 |
+
if options["Rotate Image"]:
|
68 |
+
angle = st.slider("Rotation Angle", -180, 180, 0)
|
69 |
+
rows, cols = processed_image.shape[:2]
|
70 |
+
M = cv2.getRotationMatrix2D((cols / 2, rows / 2), angle, 1)
|
71 |
+
processed_image = cv2.warpAffine(
|
72 |
+
processed_image, M, (cols, rows))
|
73 |
+
if len(processed_image.shape) == 2:
|
74 |
+
processed_image = cv2.cvtColor(
|
75 |
+
processed_image, cv2.COLOR_GRAY2RGB)
|
76 |
+
|
77 |
+
if options["Flip Image"]:
|
78 |
+
flip_mode = st.selectbox(
|
79 |
+
"Flip Mode", [("Vertical", 0), ("Horizontal", 1), ("Both", -1)])
|
80 |
+
processed_image = cv2.flip(processed_image, flip_mode[1])
|
81 |
+
if len(processed_image.shape) == 2:
|
82 |
+
processed_image = cv2.cvtColor(
|
83 |
+
processed_image, cv2.COLOR_GRAY2RGB)
|
84 |
+
|
85 |
+
with col2:
|
86 |
+
if len(processed_image.shape) == 3:
|
87 |
+
processed_channels = "RGB" # Display in RGB
|
88 |
+
elif len(processed_image.shape) == 2:
|
89 |
+
processed_channels = "GRAY"
|
90 |
+
else:
|
91 |
+
processed_channels = None
|
92 |
+
|
93 |
+
if processed_channels:
|
94 |
+
st.image(processed_image, caption="Processed Image",
|
95 |
+
channels=processed_channels)
|
96 |
+
else:
|
97 |
+
st.write("Unexpected image format after processing.")
|
98 |
+
|
99 |
+
else:
|
100 |
+
with col2:
|
101 |
+
st.write("No processing applied yet.")
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
opencv-python
|
3 |
+
numpy
|
4 |
+
pillow
|