Spaces:
Sleeping
Sleeping
Update Image Augmentation.py
Browse files- Image Augmentation.py +100 -1
Image Augmentation.py
CHANGED
|
@@ -1,2 +1,101 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
from io import BytesIO
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import base64
|
| 7 |
+
from fpdf import FPDF
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def rotate_image(image, angle):
|
| 11 |
+
(h, w) = image.shape[:2]
|
| 12 |
+
center = (w // 2, h // 2)
|
| 13 |
+
rotation_matrix = cv2.getRotationMatrix2D(center, angle, 1.0)
|
| 14 |
+
rotated_image = cv2.warpAffine(image, rotation_matrix, (w, h))
|
| 15 |
+
return rotated_image
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def add_noise(image):
|
| 19 |
+
s_vs_p = 0.5
|
| 20 |
+
amount = 0.02
|
| 21 |
+
noisy_image = image.copy()
|
| 22 |
+
num_salt = np.ceil(amount * image.size * s_vs_p)
|
| 23 |
+
num_pepper = np.ceil(amount * image.size * (1.0 - s_vs_p))
|
| 24 |
+
coords = [np.random.randint(0, i - 1, int(num_salt)) for i in image.shape[:2]]
|
| 25 |
+
noisy_image[coords[0], coords[1], :] = 255
|
| 26 |
+
coords = [np.random.randint(0, i - 1, int(num_pepper)) for i in image.shape[:2]]
|
| 27 |
+
noisy_image[coords[0], coords[1], :] = 0
|
| 28 |
+
return noisy_image
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def blur_image(image, kernel_size):
|
| 32 |
+
blurred_image = cv2.GaussianBlur(image, (kernel_size, kernel_size), 0)
|
| 33 |
+
return blurred_image
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def translate_image(image, tx, ty):
|
| 37 |
+
translation_matrix = np.float32([[1, 0, tx], [0, 1, ty]])
|
| 38 |
+
height, width = image.shape[:2]
|
| 39 |
+
translated_image = cv2.warpAffine(image, translation_matrix, (width, height))
|
| 40 |
+
return translated_image
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def download_image(image, file_format):
|
| 44 |
+
pil_image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
|
| 45 |
+
buffer = BytesIO()
|
| 46 |
+
pil_image.save(buffer, format=file_format.upper())
|
| 47 |
+
buffer.seek(0)
|
| 48 |
+
b64 = base64.b64encode(buffer.read()).decode()
|
| 49 |
+
href = f'<a href="data:file/{file_format};base64,{b64}" download="augmented_image.{file_format}">Download {file_format.upper()} File</a>'
|
| 50 |
+
return href
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def download_pdf(image):
|
| 54 |
+
pdf = FPDF()
|
| 55 |
+
pdf.add_page()
|
| 56 |
+
pil_image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
|
| 57 |
+
buffer = BytesIO()
|
| 58 |
+
pil_image.save(buffer, format="JPEG")
|
| 59 |
+
buffer.seek(0)
|
| 60 |
+
pdf.image(buffer, x=10, y=10, w=190)
|
| 61 |
+
pdf_output = BytesIO()
|
| 62 |
+
pdf.output(pdf_output, "F")
|
| 63 |
+
pdf_output.seek(0)
|
| 64 |
+
b64 = base64.b64encode(pdf_output.read()).decode()
|
| 65 |
+
href = f'<a href="data:application/octet-stream;base64,{b64}" download="augmented_image.pdf">Download PDF</a>'
|
| 66 |
+
return href
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
st.title("Image Augmentation Tool")
|
| 70 |
+
|
| 71 |
+
# File upload
|
| 72 |
+
uploaded_file = st.file_uploader("Upload an Image", type=["jpg", "png", "jpeg"])
|
| 73 |
+
if uploaded_file:
|
| 74 |
+
image = Image.open(uploaded_file)
|
| 75 |
+
image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
| 76 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 77 |
+
|
| 78 |
+
st.write("### Select Transformations to Apply:")
|
| 79 |
+
rotate = st.checkbox("Rotate (45 degrees)")
|
| 80 |
+
noise = st.checkbox("Add Noise")
|
| 81 |
+
blur = st.checkbox("Blur (5x5 Kernel)")
|
| 82 |
+
translate = st.checkbox("Translate (50 pixels right and down)")
|
| 83 |
+
|
| 84 |
+
if st.button("Apply Transformations"):
|
| 85 |
+
transformed_image = image.copy()
|
| 86 |
+
|
| 87 |
+
if rotate:
|
| 88 |
+
transformed_image = rotate_image(transformed_image, 45)
|
| 89 |
+
if noise:
|
| 90 |
+
transformed_image = add_noise(transformed_image)
|
| 91 |
+
if blur:
|
| 92 |
+
transformed_image = blur_image(transformed_image, 5)
|
| 93 |
+
if translate:
|
| 94 |
+
transformed_image = translate_image(transformed_image, 50, 50)
|
| 95 |
+
|
| 96 |
+
st.image(cv2.cvtColor(transformed_image, cv2.COLOR_BGR2RGB), caption="Transformed Image", use_column_width=True)
|
| 97 |
+
|
| 98 |
+
st.markdown("### Download Transformed Image:")
|
| 99 |
+
st.markdown(download_image(transformed_image, "jpg"), unsafe_allow_html=True)
|
| 100 |
+
st.markdown(download_image(transformed_image, "png"), unsafe_allow_html=True)
|
| 101 |
+
st.markdown(download_pdf(transformed_image), unsafe_allow_html=True)
|