Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -35,7 +35,7 @@ REPO_ID = "Pavan2k4/Building_area"
|
|
| 35 |
REPO_TYPE = "space"
|
| 36 |
|
| 37 |
# Define base directory for Hugging Face Spaces
|
| 38 |
-
BASE_DIR = ""
|
| 39 |
|
| 40 |
# Define subdirectories
|
| 41 |
UPLOAD_DIR = os.path.join(BASE_DIR, "uploaded_images")
|
|
@@ -44,6 +44,51 @@ PATCHES_DIR = os.path.join(BASE_DIR, "patches")
|
|
| 44 |
PRED_PATCHES_DIR = os.path.join(BASE_DIR, "pred_patches")
|
| 45 |
CSV_LOG_PATH = os.path.join(BASE_DIR, "image_log.csv")
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
# Create directories
|
| 48 |
for directory in [UPLOAD_DIR, MASK_DIR, PATCHES_DIR, PRED_PATCHES_DIR]:
|
| 49 |
os.makedirs(directory, exist_ok=True)
|
|
|
|
| 35 |
REPO_TYPE = "space"
|
| 36 |
|
| 37 |
# Define base directory for Hugging Face Spaces
|
| 38 |
+
BASE_DIR = "DATA/"
|
| 39 |
|
| 40 |
# Define subdirectories
|
| 41 |
UPLOAD_DIR = os.path.join(BASE_DIR, "uploaded_images")
|
|
|
|
| 44 |
PRED_PATCHES_DIR = os.path.join(BASE_DIR, "pred_patches")
|
| 45 |
CSV_LOG_PATH = os.path.join(BASE_DIR, "image_log.csv")
|
| 46 |
|
| 47 |
+
|
| 48 |
+
def split(image, destination = PATCHES_DIR, patch_size = 256):
|
| 49 |
+
img = cv2.imread(image)
|
| 50 |
+
h,w,_ = img.shape
|
| 51 |
+
for y in range(0, h, patch_size):
|
| 52 |
+
for x in range(0, w, patch_size):
|
| 53 |
+
patch = img[y:y+patch_size, x:x+patch_size]
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
patch_filename = f"patch_{y}_{x}.png"
|
| 57 |
+
patch_path = os.path.join(destination, patch_filename)
|
| 58 |
+
cv2.imwrite(patch_path, patch)
|
| 59 |
+
|
| 60 |
+
def merge(patch_folder , dest_image = 'out.png', image_shape = None):
|
| 61 |
+
merged = np.zeros(image_shape[:-1] + (3,), dtype=np.uint8)
|
| 62 |
+
for filename in os.listdir(patch_folder):
|
| 63 |
+
if filename.endswith(".png"):
|
| 64 |
+
patch_path = os.path.join(patch_folder, filename)
|
| 65 |
+
patch = cv2.imread(patch_path)
|
| 66 |
+
patch_height, patch_width, _ = patch.shape
|
| 67 |
+
|
| 68 |
+
# Extract patch coordinates from filename
|
| 69 |
+
parts = filename.split("_")
|
| 70 |
+
x, y = None, None
|
| 71 |
+
for part in parts:
|
| 72 |
+
if part.endswith(".png"):
|
| 73 |
+
x = int(part.split(".")[0])
|
| 74 |
+
elif part.isdigit():
|
| 75 |
+
y = int(part)
|
| 76 |
+
if x is None or y is None:
|
| 77 |
+
raise ValueError(f"Invalid filename: {filename}")
|
| 78 |
+
|
| 79 |
+
# Check if patch fits within image boundaries
|
| 80 |
+
if x + patch_width > image_shape[1] or y + patch_height > image_shape[0]:
|
| 81 |
+
# Adjust patch position to fit within image boundaries
|
| 82 |
+
if x + patch_width > image_shape[1]:
|
| 83 |
+
x = image_shape[1] - patch_width
|
| 84 |
+
if y + patch_height > image_shape[0]:
|
| 85 |
+
y = image_shape[0] - patch_height
|
| 86 |
+
|
| 87 |
+
# Merge patch into the main image
|
| 88 |
+
merged[y:y+patch_height, x:x+patch_width, :] = patch
|
| 89 |
+
|
| 90 |
+
cv2.imwrite(dest_image, merged)
|
| 91 |
+
|
| 92 |
# Create directories
|
| 93 |
for directory in [UPLOAD_DIR, MASK_DIR, PATCHES_DIR, PRED_PATCHES_DIR]:
|
| 94 |
os.makedirs(directory, exist_ok=True)
|