Spaces:
Sleeping
Sleeping
File size: 7,957 Bytes
646cbea a5876d5 646cbea ebe1e51 ae2d0b8 646cbea 351426d a8f6bb0 a5876d5 646cbea ffa3a7c 646cbea 1ecd6cd a5876d5 1ecd6cd 17bc18e bf581d9 17bc18e e98dd3a 17bc18e fa2c20b 287fc3c e44663b fa2c20b b75e1d4 d4e0a5a 5b61ae3 fa2c20b d4e0a5a 25f2c4d 6067216 d4e0a5a a318b47 9aea4fb 25ad053 351426d c8f176a d4e0a5a 6600bdb 351426d f456ac7 b81affe d27b0d5 87184ce b81affe 1c28c69 c8f176a b81affe ef96fd2 183807f cbfc587 466a7f9 cbfc587 183807f b75e1d4 183807f 9c86860 38c6de3 71339ea 38c6de3 ad01c42 1ecd6cd e5fd138 edec72e a318b47 b75e1d4 8b4edfe 287fc3c 4f8287d 8b4edfe 4f8287d 8b4edfe 4f8287d 8b4edfe 287fc3c 8b4edfe 287fc3c 8b4edfe 1ecd6cd 17bc18e 287fc3c b75e1d4 74ee273 d2129ef 287fc3c d7bc122 287fc3c b75e1d4 17bc18e 7e37608 17bc18e b992ea8 950f7c1 85ad987 a5876d5 | 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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 | import streamlit as st
import numpy as np
import pandas as pd
import os
import zipfile
import cv2
st.markdown("""
<style>
/* Set full-page width and remove Streamlit default padding */
.stApp {
background-color: #d0e8ff;
}
.main .block-container {
padding-top: 2rem;
padding-right: 2rem;
padding-left: 2rem;
padding-bottom: 2rem;
max-width: 100%;
}
</style>
""", unsafe_allow_html=True)
# Main Heading
st.markdown('<h2 style="text-align: center;"><span style="color:#003366;">Generate Augmented Images </span></h2>', unsafe_allow_html=True)
st.markdown("<br><br>", unsafe_allow_html=True)
#st.markdown('<h4 style="text-align: center;"><span style="color:#003366;">Upload the image </span></h4>', unsafe_allow_html=True)
# Upload image
uploaded_file = st.file_uploader("Upload an image...", type=["jpg", "jpeg", "png"])
image_count=st.number_input("Number of Images to be generated",1,50,placeholder="Type a number...")
st.write("Select the Affine Transformation to be applied:")
trans = st.checkbox("Translation")
rot = st.checkbox("Rotation")
scal = st.checkbox("Scaling")
shear=st.checkbox("Shearing")
crop=st.checkbox("Crop")
transformation=[]
btn=st.button("Preview Image")
#sub=st.button("Preview Image")
def clear_folder(folder_name="output_images"):
if os.path.exists(folder_name):
for file in os.listdir(folder_name):
file_path=os.path.join(folder_name,file)
if os.path.isfile(file_path):
os.remove(file_path)
else:
os.makedirs(folder_name)
def save_image_and_show(image, folder_name="output_images"):
if not os.path.exists(folder_name):
os.makedirs(folder_name)
image_name = f"image_{len(os.listdir(folder_name)) + 1}.jpg"
image_path = os.path.join(folder_name, image_name)
# Save the image
cv2.imwrite(image_path, image)
if btn:
# Showing the success message
st.success(f"Image saved to {image_path} (local path)")
# Show the image to the user (not the path)
st.image(image, caption="Saved Image", channels="BGR")
def zip_file(folder_name="output_images"):
zip_filename = f"{folder_name}.zip"
with zipfile.ZipFile(zip_filename, 'w') as zipf:
for root, dirs, files in os.walk(folder_name):
for file in files:
filepath = os.path.join(root, file)
arcname = os.path.relpath(filepath, folder_name)
zipf.write(filepath, arcname)
# Read zip for download
with open(zip_filename, "rb") as f:
st.download_button(
label="Download All Images as ZIP",
data=f,
file_name=zip_filename,
mime="application/zip"
)
def translation(img,n=5):
for i in range(n):
tx=np.random.randint(2,50)
ty=np.random.randint(1,50)
tm=np.array([[1,0,tx],[0,1,ty]],dtype=np.float32)
trans_img=cv2.warpAffine(img,tm,dsize=(img.shape[1],img.shape[0]),borderMode=cv2.BORDER_REFLECT)
#img_trans_rgb = cv2.cvtColor(trans_img, cv2.COLOR_BGR2RGB)
save_image_and_show(trans_img)
#st.image(img_trans_rgb, caption="Translation Applied", use_container_width=True)
def rotation(img,n=5):
for i in range(n):
angle=np.random.choice([30,45,90, 180, -90,-30,-45])
scale=np.round(np.random.uniform(1,1.6),2)
r_x=img.shape[0]//2
r_y=img.shape[1]//2
rm=cv2.getRotationMatrix2D((r_y,r_x),angle,scale)
r_img=cv2.warpAffine(img,rm,(img.shape[1],img.shape[0]))
#img_rot_rgb = cv2.cvtColor(r_img, cv2.COLOR_BGR2RGB)
save_image_and_show(r_img)
#st.image(img_rot_rgb, caption="Rotation Applied", use_container_width=True)
def scaling(img,n=5):
for i in range(n):
sx=np.random.uniform(1,2)
sy=np.random.uniform(1,2)
tx=np.random.randint(1,16)
ty=np.random.randint(1,12)
sm=np.array([[sx,0,tx],[0,sy,ty]],dtype=np.float32)
s_img=cv2.warpAffine(img,sm,(img.shape[1],img.shape[0]))
#img_scal_rgb=cv2.cvtColor(s_img,cv2.COLOR_BGR2RGB)
save_image_and_show(s_img)
#st.image(img_scal_rgb, caption="Scaling Applied", use_container_width=True)
def shearing(img,n=5):
for i in range(n):
sx=np.round(np.random.uniform(1,1.5),2)
shx=np.round(np.random.uniform(0,0.5))
tx=np.random.randint(1,10)
shy=np.round(np.random.uniform(0,0.5))
sy=np.round(np.random.uniform(1,1.2),2)
ty=np.random.randint(1,15)
shm=np.array([[sx,shx,tx],[shy,sy,ty]],dtype=np.float32)
img_shear=cv2.warpAffine(img,shm,(img.shape[1],img.shape[0]),borderMode=cv2.BORDER_REFLECT)
save_image_and_show(img_shear)
#img_shear_rgb=cv2.cvtColor(img_all,cv2.COLOR_BGR2RGB)
#st.image(img_shear_rgb, caption="Shearing Applied", use_container_width=True)
def cropping(img,n=5):
for i in range(n):
x_1=np.random.randint(20,60)
y_1=np.random.randint(110,160)
x_2=np.random.randint(20,60)
y_2=np.random.randint(110,160)
img_crop=img[x_1:y_1,x_2:y_2]
save_image_and_show(img_crop)
# img_crop_rgb=cv2.cvtColor(img_crop,cv2.COLOR_BGR2RGB)
# st.image(img_crop_rgb, caption="Shearing Applied", use_container_width=True)
# def save_image(image, folder_name="output_images"):
# if not os.path.exists(folder_name):
# os.makedirs(folder_name)
# image_name = f"image_{len(os.listdir(folder_name)) + 1}.jpg"
# image_path = os.path.join(folder_name, image_name)
# cv2.imwrite(image_path, image)
# st.success(f"Image saved to {image_path}")
if uploaded_file is not None:
# Converting the image into array
img_array=np.asarray(bytearray(uploaded_file.read()),dtype=np.uint8)
# Decode or converting the array back to image array
img=cv2.imdecode(img_array,cv2.IMREAD_COLOR)
# # Convert BGR to RGB as streamlit expects RGB colorspace
# img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
transformation = []
if trans:
transformation.append("Translation")
if rot:
transformation.append("Rotation")
if scal:
transformation.append("Scaling")
if shear:
transformation.append("Shearing")
if crop:
transformation.append("Crop")
if transformation:
clear_folder()
# per_trans = max(1,(image_count // len(transformation)))
# trans_count=[per_trans+1 if (per_trans * len(transformation))!= image_count else per_trans][0]
num_trans = len(transformation)
base = image_count // num_trans
remainder = image_count % num_trans
# Create a list like [base+1, base, base, ...] to ensure sum == image_count
counts = [base + 1 if i < remainder else base for i in range(num_trans)]
# if "Translation" in transformation:
# translation(img,trans_count)
# if "Rotation" in transformation:
# rotation(img, per_trans)
# if "Scaling" in transformation:
# scaling(img, per_trans)
# if "Shearing" in transformation:
# shearing(img, per_trans)
# if "Crop" in transformation:
# cropping(img, per_trans)
for t_name, t_count in zip(transformation, counts):
if t_name == "Translation":
translation(img, t_count)
elif t_name == "Rotation":
rotation(img, t_count)
elif t_name == "Scaling":
scaling(img, t_count)
elif t_name == "Shearing":
shearing(img, t_count)
elif t_name == "Crop":
cropping(img, t_count)
zip_file()
#st.download_button("Download File",img_rgb)
# # Displaying image in streamlit
# st.image(img_rgb, caption="Uploaded Image (OpenCV)", use_container_width=True)
|