CoolFace
Apppublic

Goyamproject/React_native_app

sourceHugging Faceopenrailupdated 6mo agoView on Hugging Face
1likes
verify.py105 linesDownload Raw Back to root
1import os
2import cv2
3import random
4import numpy as np
5from tqdm import tqdm
6
7
8DATASET_DIR = r"C:\Users\charu\Desktop\all new\40000\goyam_v2_dataset"
9
10
11IMG_DIR = os.path.join(DATASET_DIR, "images", "train")
12LBL_DIR = os.path.join(DATASET_DIR, "labels", "train")
13
14
15OUTPUT_DIR = os.path.join(DATASET_DIR, "visual_check")
16
17NUM_SAMPLES = 100 
18
19
20COLORS = {
21    0: (0, 0, 255),   
22    1: (255, 0, 0),   
23    2: (0, 255, 0)    
24}
25
26
27def verify_labels():
28    os.makedirs(OUTPUT_DIR, exist_ok=True)
29    
30  
31    valid_exts = ('.jpg', '.jpeg', '.png')
32    all_images = [f for f in os.listdir(IMG_DIR) if f.lower().endswith(valid_exts)]
33    
34    if not all_images:
35        print(f"No images found in {IMG_DIR}")
36        return
37
38 
39    if NUM_SAMPLES > 0 and len(all_images) > NUM_SAMPLES:
40        print(f"Randomly selecting {NUM_SAMPLES} images for spot-checking...")
41        images_to_check = random.sample(all_images, NUM_SAMPLES)
42    else:
43        print(f"๐Ÿ” Checking ALL {len(all_images)} images...")
44        images_to_check = all_images
45
46  
47    for filename in tqdm(images_to_check, desc="Drawing Polygons"):
48        img_path = os.path.join(IMG_DIR, filename)
49        lbl_path = os.path.join(LBL_DIR, os.path.splitext(filename)[0] + ".txt")
50 
51        img = cv2.imread(img_path)
52        if img is None:
53            continue
54            
55        h, w = img.shape[:2]
56        
57  
58        overlay = img.copy()
59        
60
61        if os.path.exists(lbl_path):
62            with open(lbl_path, "r") as file:
63                lines = file.readlines()
64                
65            for line in lines:
66                parts = line.strip().split()
67                if len(parts) < 5: 
68                    continue 
69                
70                class_id = int(parts[0])
71                color = COLORS.get(class_id, (0, 255, 255)) 
72                
73        
74                coords = [float(x) for x in parts[1:]]
75                
76           
77                points = np.array(coords).reshape(-1, 2)
78                
79  
80                points[:, 0] = points[:, 0] * w
81                points[:, 1] = points[:, 1] * h
82                
83              
84                points = np.int32(points)
85                
86          
87                cv2.fillPoly(overlay, [points], color)
88                
89        
90                cv2.polylines(img, [points], isClosed=True, color=color, thickness=2)
91                
92  
93                cv2.putText(img, f"Class {class_id}", (points[0][0], points[0][1] - 5), 
94                            cv2.FONT_HERSHEY_SIMPLEX, 0.6, color, 2)
95
96    
97        cv2.addWeighted(overlay, 0.4, img, 0.6, 0, img)
98      
99        save_path = os.path.join(OUTPUT_DIR, filename)
100        cv2.imwrite(save_path, img)
101
102    print(f"\n Done! ")
103
104if __name__ == "__main__":
105    verify_labels()