-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment_App.py
More file actions
192 lines (168 loc) Β· 6.52 KB
/
Assignment_App.py
File metadata and controls
192 lines (168 loc) Β· 6.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import streamlit as st
import cv2
import numpy as np
from PIL import Image
st.set_page_config(page_title="OpenCV Playground", layout="wide")
st.title("π§ OpenCV Learning Playground")
st.markdown("### Learn and Interact with OpenCV through a Visual Interface")
with st.expander("βΉοΈ Instructions & Info"):
st.markdown("""
This app helps you **interactively explore OpenCV** through two main categories:
**1. Image Manipulation & Transformation**
- Resize / Scale
- Crop
- Rotate
- Flip (Vertical / Horizontal / Both)
**2. Drawing on Images**
- Draw Line
- Draw Rectangle
- Draw Circle
- Add Text
After performing any operation, you can **preview the result** and download the modified image.
""")
uploaded_file = st.sidebar.file_uploader(
"π Upload an image", type=["jpg", "jpeg", "png"]
)
if uploaded_file:
file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
img = cv2.imdecode(file_bytes, 1)
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
h, w = img.shape[:2]
choice = st.sidebar.radio(
"Choose Operation Category",
["Image Manipulation & Transformation", "Basic Drawing Techniques"],
)
def select_position(section, img_shape, size=(100, 100)):
h, w = img_shape[:2]
sw, sh = size
positions = {
"center": ((w - sw) // 2, (h - sh) // 2),
"left": (0, (h - sh) // 2),
"right": (w - sw, (h - sh) // 2),
"top left": (0, 0),
"top right": (w - sw, 0),
"bottom left": (0, h - sh),
"bottom right": (w - sw, h - sh),
}
if section in positions:
return positions[section]
else:
x = st.sidebar.number_input("Custom X", 0, w, 0)
y = st.sidebar.number_input("Custom Y", 0, h, 0)
return (x, y)
result = img.copy()
if choice == "Image Manipulation & Transformation":
transform_option = st.sidebar.selectbox(
"Choose Transformation", ["Resize", "Crop", "Rotate", "Flip"]
)
if transform_option == "Resize":
scale = st.sidebar.slider("Scale factor", 0.1, 3.0, 1.0, 0.1)
result = cv2.resize(img, (int(w * scale), int(h * scale)))
elif transform_option == "Crop":
section = st.sidebar.selectbox(
"Select Crop Section",
[
"center",
"left",
"top left",
"bottom left",
"right",
"top right",
"bottom right",
"custom",
],
)
crop_w = st.sidebar.slider("Crop Width", 10, w, 100)
crop_h = st.sidebar.slider("Crop Height", 10, h, 100)
x, y = select_position(section, img.shape, size=(crop_w, crop_h))
x, y = int(x), int(y)
result = img[y : y + crop_h, x : x + crop_w]
elif transform_option == "Rotate":
angle = st.sidebar.slider("Rotation Angle", -180, 180, 0)
center = (w // 2, h // 2)
M = cv2.getRotationMatrix2D(center, angle, 1.0)
result = cv2.warpAffine(img, M, (w, h))
elif transform_option == "Flip":
direction = st.sidebar.selectbox(
"Flip Direction", ["vertical", "horizontal", "both"]
)
flip_code = {"vertical": 0, "horizontal": 1, "both": -1}
result = cv2.flip(img, flip_code[direction])
elif choice == "Basic Drawing Techniques":
draw_option = st.sidebar.selectbox(
"Choose Drawing Type", ["Line", "Rectangle", "Circle", "Text"]
)
if draw_option in ["Line", "Rectangle", "Circle", "Text"]:
section = st.sidebar.selectbox(
"Select Position",
[
"center",
"left",
"top left",
"bottom left",
"right",
"top right",
"bottom right",
"custom",
],
)
if draw_option == "Line":
x, y = select_position(section, img.shape)
result = cv2.line(
img.copy(), (int(x), int(y)), (int(x) + 100, int(y)), (255, 255, 255), 5
)
elif draw_option == "Rectangle":
rect_w = 100
rect_h = 100
x, y = select_position(section, img.shape, size=(rect_w, rect_h))
result = cv2.rectangle(
img.copy(),
(int(x), int(y)),
(int(x) + rect_w, int(y) + rect_h),
(255, 0, 0),
5,
)
elif draw_option == "Circle":
radius = 50
x, y = select_position(section, img.shape, size=(radius * 2, radius * 2))
result = cv2.circle(
img.copy(), (int(x) + radius, int(y) + radius), radius, (0, 255, 0), 5
)
elif draw_option == "Text":
user_text = st.sidebar.text_input("Enter Text", "Hello OpenCV")
font = cv2.FONT_HERSHEY_SIMPLEX
font_scale = 2
thickness = 3
(text_w, text_h), _ = cv2.getTextSize(
user_text, font, font_scale, thickness
)
x, y = select_position(section, img.shape, size=(text_w, text_h))
y += text_h
result = cv2.putText(
img.copy(),
user_text,
(int(x), int(y)),
font,
font_scale,
(0, 255, 255),
thickness,
)
if result is not None:
st.subheader("πΈ Comparison: Original vs Modified")
col1, col2 = st.columns(2)
with col1:
st.image(img_rgb, caption="Original", use_container_width=True)
with col2:
st.image(
cv2.cvtColor(result, cv2.COLOR_BGR2RGB),
caption="Modified",
use_container_width=True,
)
st.download_button(
label="π₯ Download Modified Image",
data=cv2.imencode(".jpg", result)[1].tobytes(),
file_name="processed_image.jpg",
mime="image/jpeg",
)
else:
st.info("Please upload an image to begin.")