File size: 2,876 Bytes
1028208
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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()