Spaces:
Sleeping
Sleeping
Update app.py
Browse files4 unmodified lines5678910114 unmodified lines161718192021222317 unmodified lines414243444546474849505152535455565758597 unmodified lines6768697071727374757677787980818283848586878889909192939495969798991001011 unmodified line1031041051061071081091101111121131141154 unmodified linesimport iost.set_page_config(layout="wide")st.title("Image Augmentation Tool ")# Set max file size limit in bytes (e.g., 5MB)MAX_FILE_SIZE = 5 * 1024 * 1024 # 5MB4 unmodified linesaccept_multiple_files=True)# Function to apply all transformationsdef apply_all_transformations(img_array):results = []gray = cv2.cvtColor(img_array, cv2.COLOR_RGB2GRAY)17 unmodified linestranslated = cv2.warpAffine(img_array, trans_M, (cols, rows))results.append(("Translation", translated))h, w = img_array.shape[:2]x_start = w // 4x_end = 3 * x_starty_start = h // 4y_end = 3 * y_startcropped = img_array[y_start:y_end, x_start:x_end]results.append(("Crop (center 50%)", cropped, (x_start, x_end, y_start, y_end)))return resultsif uploaded_files:for uploaded_file in uploaded_files:# Check file size limitif uploaded_file.size > MAX_FILE_SIZE:st.error(f"❌ File {uploaded_file.name} is too large. Max size allowed is 5MB.")continue7 unmodified lines# Show original imagest.image(image, caption="Original Image", use_container_width=True)# Apply all transformationstransformations = apply_all_transformations(img_array)st.markdown("### 🧪 Transformed Versions")# Create columns dynamically (3 per row)cols = st.columns(3)# Find crop coordinates from transformations to display in sidebarcrop_coords = Nonefor item in transformations:if item[0].startswith("Crop"):crop_coords = item[2]break# Show crop coordinates in sidebarif crop_coords:x_start, x_end, y_start, y_end = crop_coordswith st.sidebar:st.markdown("### Crop Coordinates:")st.write(f"x_start: {x_start}")st.write(f"x_end: {x_end}")st.write(f"y_start: {y_start}")st.write(f"y_end: {y_end}")for idx, item in enumerate(transformations):name = item[0]transformed = item[1]# coords not needed here since shown in sidebarcol = cols[idx % 3]with col:1 unmodified lineif len(transformed.shape) == 2:st.image(transformed, caption=name, use_container_width=True, channels="GRAY")# Convert grayscale to RGB for savingsave_img = cv2.cvtColor(transformed, cv2.COLOR_GRAY2RGB)else:st.image(transformed, caption=name, use_container_width=True)save_img = transformed# Save transformed image to bytesresult_img = Image.fromarray(save_img)buf = io.BytesIO()result_img.save(buf, format="PNG")4 unmodified lines5678910114 unmodified lines1617181920212217 unmodified lines40414243444546474849505152535455565758597 unmodified lines676869707172737475767778798081828384858687888990911 unmodified line939495969798991001011021031041051061074 unmodified linesimport iost.set_page_config(layout="wide")st.title("Image Augmentation Tool")# Set max file size limit in bytes (e.g., 5MB)MAX_FILE_SIZE = 5 * 1024 * 1024 # 5MB4 unmodified linesaccept_multiple_files=True)def apply_all_transformations(img_array, x_start, x_end, y_start, y_end):results = []gray = cv2.cvtColor(img_array, cv2.COLOR_RGB2GRAY)17 unmodified linestranslated = cv2.warpAffine(img_array, trans_M, (cols, rows))results.append(("Translation", translated))# Crop with passed coordinates - ensure they are within image boundsh, w = img_array.shape[:2]x_start = max(0, min(x_start, w-1))x_end = max(1, min(x_end, w))y_start = max(0, min(y_start, h-1))y_end = max(1, min(y_end, h))cropped = img_array[y_start:y_end, x_start:x_end]results.append(("Crop (custom)", cropped, (x_start, x_end, y_start, y_end)))return resultsif uploaded_files:for uploaded_file in uploaded_files:if uploaded_file.size > MAX_FILE_SIZE:st.error(f"❌ File {uploaded_file.name} is too large. Max size allowed is 5MB.")continue7 unmodified lines# Show original imagest.image(image, caption="Original Image", use_container_width=True)h, w = img_array.shape[:2]# Sidebar inputs for crop coordinates with defaults to center 50%st.sidebar.markdown("## Crop Coordinates (Adjust to Crop)")x_start = st.sidebar.number_input("x_start", min_value=0, max_value=w-1, value=w // 4, step=1)x_end = st.sidebar.number_input("x_end", min_value=x_start+1, max_value=w, value=3 * (w // 4), step=1)y_start = st.sidebar.number_input("y_start", min_value=0, max_value=h-1, value=h // 4, step=1)y_end = st.sidebar.number_input("y_end", min_value=y_start+1, max_value=h, value=3 * (h // 4), step=1)# Apply all transformations with user crop coordinatestransformations = apply_all_transformations(img_array, x_start, x_end, y_start, y_end)st.markdown("### 🧪 Transformed Versions")cols = st.columns(3)for idx, item in enumerate(transformations):name = item[0]transformed = item[1]coords = item[2] if len(item) > 2 else Nonecol = cols[idx % 3]with col:1 unmodified lineif len(transformed.shape) == 2:st.image(transformed, caption=name, use_container_width=True, channels="GRAY")save_img = cv2.cvtColor(transformed, cv2.COLOR_GRAY2RGB)else:st.image(transformed, caption=name, use_container_width=True)save_img = transformedif coords:x_s, x_e, y_s, y_e = coordsst.markdown(f"Crop Coordinates:\n- x_start: {x_s}\n- x_end: {x_e}\n- y_start: {y_s}\n- y_end: {y_e}")result_img = Image.fromarray(save_img)buf = io.BytesIO()result_img.save(buf, format="PNG")
app.py+25-33