shubham680 commited on
Commit
b75e1d4
·
verified ·
1 Parent(s): b41fa7c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -25
app.py CHANGED
@@ -62,6 +62,15 @@ crop=st.checkbox("Crop")
62
  transformation=[]
63
  btn=st.button("Preview Image")
64
 
 
 
 
 
 
 
 
 
 
65
  def save_image_and_show(image, folder_name="output_images"):
66
  if not os.path.exists(folder_name):
67
  os.makedirs(folder_name)
@@ -73,7 +82,7 @@ def save_image_and_show(image, folder_name="output_images"):
73
  cv2.imwrite(image_path, image)
74
 
75
 
76
- # Show success message
77
  st.success(f"Image saved to {image_path} (local path)")
78
  # Show the image to the user (not the path)
79
  st.image(image, caption="Saved Image", channels="BGR")
@@ -102,8 +111,8 @@ def zip_file(folder_name="output_images"):
102
 
103
  def translation(img,n=5):
104
  for i in range(n):
105
- tx=np.random.randint(2,50)
106
- ty=np.random.randint(1,45)
107
  tm=np.array([[1,0,tx],[0,1,ty]],dtype=np.float32)
108
  trans_img=cv2.warpAffine(img,tm,dsize=(img.shape[1],img.shape[0]),borderMode=cv2.BORDER_REFLECT)
109
  #img_trans_rgb = cv2.cvtColor(trans_img, cv2.COLOR_BGR2RGB)
@@ -112,7 +121,6 @@ def translation(img,n=5):
112
 
113
  def rotation(img,n=5):
114
  for i in range(n):
115
- #center = (width//2, height//2)
116
  angle=np.random.choice([90, 180, -90])
117
  scale=np.round(np.random.uniform(1,1.6),2)
118
  r_x=img.shape[0]//2
@@ -144,8 +152,8 @@ def shearing(img,n=5):
144
  sy=np.round(np.random.uniform(1,1.2),2)
145
  ty=np.random.randint(1,20)
146
  shm=np.array([[sx,shx,tx],[shy,sy,ty]],dtype=np.float32)
147
- img_all=cv2.warpAffine(img,shm,(img.shape[1],img.shape[0]),borderMode=cv2.BORDER_REFLECT)
148
- save_image_and_show(img_all)
149
  #img_shear_rgb=cv2.cvtColor(img_all,cv2.COLOR_BGR2RGB)
150
  #st.image(img_shear_rgb, caption="Shearing Applied", use_container_width=True)
151
 
@@ -178,6 +186,7 @@ if uploaded_file is not None:
178
  img=cv2.imdecode(img_array,cv2.IMREAD_COLOR)
179
  # # Convert BGR to RGB as streamlit expects RGB colorspace
180
  # img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
 
181
  transformation = []
182
 
183
  if trans:
@@ -193,27 +202,22 @@ if uploaded_file is not None:
193
 
194
 
195
 
 
 
 
196
 
 
 
 
 
 
 
 
 
 
 
197
 
198
-
199
-
200
- if "Translation" in transformation and btn:
201
- translation(img,round(image_count/len(transformation)))
202
-
203
- if "Rotation" in transformation and btn:
204
- rotation(img,round(image_count/len(transformation)))
205
- #transformation.append("Rotation")
206
- if "Scaling" in transformation and btn:
207
- scaling(img,round(image_count/len(transformation)))
208
- #transformation.append("Scaling")
209
- if "Shearing" in transformation and btn:
210
- shearing(img,round(image_count/len(transformation)))
211
- #transformation.append("Shearing")
212
- if "Crop" in transformation:
213
- cropping(img,round(image_count/len(transformation)))
214
- #transformation.append("Crop")
215
-
216
- zip_file()
217
 
218
  #st.download_button("Download File",img_rgb)
219
 
 
62
  transformation=[]
63
  btn=st.button("Preview Image")
64
 
65
+ def clear_folder(folder_name="output_images"):
66
+ if os.path.exists(folder_name):
67
+ for file in os.listdir(folder_name):
68
+ file_path=os.path.join(folder_name,file)
69
+ if os.path.isfile(file_path):
70
+ os.remove(file_path)
71
+ else:
72
+ os.makedirs(folder_name)
73
+
74
  def save_image_and_show(image, folder_name="output_images"):
75
  if not os.path.exists(folder_name):
76
  os.makedirs(folder_name)
 
82
  cv2.imwrite(image_path, image)
83
 
84
 
85
+ # Showing the success message
86
  st.success(f"Image saved to {image_path} (local path)")
87
  # Show the image to the user (not the path)
88
  st.image(image, caption="Saved Image", channels="BGR")
 
111
 
112
  def translation(img,n=5):
113
  for i in range(n):
114
+ tx=np.random.randint(2,45)
115
+ ty=np.random.randint(1,40)
116
  tm=np.array([[1,0,tx],[0,1,ty]],dtype=np.float32)
117
  trans_img=cv2.warpAffine(img,tm,dsize=(img.shape[1],img.shape[0]),borderMode=cv2.BORDER_REFLECT)
118
  #img_trans_rgb = cv2.cvtColor(trans_img, cv2.COLOR_BGR2RGB)
 
121
 
122
  def rotation(img,n=5):
123
  for i in range(n):
 
124
  angle=np.random.choice([90, 180, -90])
125
  scale=np.round(np.random.uniform(1,1.6),2)
126
  r_x=img.shape[0]//2
 
152
  sy=np.round(np.random.uniform(1,1.2),2)
153
  ty=np.random.randint(1,20)
154
  shm=np.array([[sx,shx,tx],[shy,sy,ty]],dtype=np.float32)
155
+ img_shear=cv2.warpAffine(img,shm,(img.shape[1],img.shape[0]),borderMode=cv2.BORDER_REFLECT)
156
+ save_image_and_show(img_shear)
157
  #img_shear_rgb=cv2.cvtColor(img_all,cv2.COLOR_BGR2RGB)
158
  #st.image(img_shear_rgb, caption="Shearing Applied", use_container_width=True)
159
 
 
186
  img=cv2.imdecode(img_array,cv2.IMREAD_COLOR)
187
  # # Convert BGR to RGB as streamlit expects RGB colorspace
188
  # img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
189
+
190
  transformation = []
191
 
192
  if trans:
 
202
 
203
 
204
 
205
+ if transformation:
206
+ clear_output_folder()
207
+ per_trans = max(1, round(image_count / len(transformation)))
208
 
209
+ if "Translation" in transformation:
210
+ translation(img, per_trans)
211
+ if "Rotation" in transformation:
212
+ rotation(img, per_trans)
213
+ if "Scaling" in transformation:
214
+ scaling(img, per_trans)
215
+ if "Shearing" in transformation:
216
+ shearing(img, per_trans)
217
+ if "Crop" in transformation:
218
+ cropping(img, per_trans)
219
 
220
+ zip_file()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
221
 
222
  #st.download_button("Download File",img_rgb)
223