Spaces:
Sleeping
Sleeping
| import os | |
| import cv2 | |
| import random | |
| import numpy as np | |
| from tqdm import tqdm | |
| DATASET_DIR = r"C:\Users\charu\Desktop\all new\40000\goyam_v2_dataset" | |
| IMG_DIR = os.path.join(DATASET_DIR, "images", "train") | |
| LBL_DIR = os.path.join(DATASET_DIR, "labels", "train") | |
| OUTPUT_DIR = os.path.join(DATASET_DIR, "visual_check") | |
| NUM_SAMPLES = 100 | |
| COLORS = { | |
| 0: (0, 0, 255), | |
| 1: (255, 0, 0), | |
| 2: (0, 255, 0) | |
| } | |
| def verify_labels(): | |
| os.makedirs(OUTPUT_DIR, exist_ok=True) | |
| valid_exts = ('.jpg', '.jpeg', '.png') | |
| all_images = [f for f in os.listdir(IMG_DIR) if f.lower().endswith(valid_exts)] | |
| if not all_images: | |
| print(f"No images found in {IMG_DIR}") | |
| return | |
| if NUM_SAMPLES > 0 and len(all_images) > NUM_SAMPLES: | |
| print(f"Randomly selecting {NUM_SAMPLES} images for spot-checking...") | |
| images_to_check = random.sample(all_images, NUM_SAMPLES) | |
| else: | |
| print(f"๐ Checking ALL {len(all_images)} images...") | |
| images_to_check = all_images | |
| for filename in tqdm(images_to_check, desc="Drawing Polygons"): | |
| img_path = os.path.join(IMG_DIR, filename) | |
| lbl_path = os.path.join(LBL_DIR, os.path.splitext(filename)[0] + ".txt") | |
| img = cv2.imread(img_path) | |
| if img is None: | |
| continue | |
| h, w = img.shape[:2] | |
| overlay = img.copy() | |
| if os.path.exists(lbl_path): | |
| with open(lbl_path, "r") as file: | |
| lines = file.readlines() | |
| for line in lines: | |
| parts = line.strip().split() | |
| if len(parts) < 5: | |
| continue | |
| class_id = int(parts[0]) | |
| color = COLORS.get(class_id, (0, 255, 255)) | |
| coords = [float(x) for x in parts[1:]] | |
| points = np.array(coords).reshape(-1, 2) | |
| points[:, 0] = points[:, 0] * w | |
| points[:, 1] = points[:, 1] * h | |
| points = np.int32(points) | |
| cv2.fillPoly(overlay, [points], color) | |
| cv2.polylines(img, [points], isClosed=True, color=color, thickness=2) | |
| cv2.putText(img, f"Class {class_id}", (points[0][0], points[0][1] - 5), | |
| cv2.FONT_HERSHEY_SIMPLEX, 0.6, color, 2) | |
| cv2.addWeighted(overlay, 0.4, img, 0.6, 0, img) | |
| save_path = os.path.join(OUTPUT_DIR, filename) | |
| cv2.imwrite(save_path, img) | |
| print(f"\n Done! ") | |
| if __name__ == "__main__": | |
| verify_labels() |