Rudra Rahul Chothe
commited on
Update src/preprocessing.py
Browse files- src/preprocessing.py +44 -62
src/preprocessing.py
CHANGED
@@ -1,62 +1,44 @@
|
|
1 |
-
import os
|
2 |
-
import pickle
|
3 |
-
from .feature_extractor import FeatureExtractor
|
4 |
-
import time
|
5 |
-
from tqdm import tqdm
|
6 |
-
|
7 |
-
def precompute_embeddings(
|
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 |
-
except Exception as e:
|
47 |
-
print(f"\nError processing {filename}: {e}")
|
48 |
-
|
49 |
-
# Save embeddings and paths
|
50 |
-
with open(output_path, 'wb') as f:
|
51 |
-
pickle.dump({'embeddings': embeddings, 'image_paths': image_paths}, f)
|
52 |
-
|
53 |
-
total_time = time.time() - start_time
|
54 |
-
print(f"\nProcessing complete!")
|
55 |
-
print(f"Total time taken: {total_time//60:.0f} minutes and {total_time%60:.0f} seconds")
|
56 |
-
print(f"Successfully processed {len(embeddings)}/{total_images} images")
|
57 |
-
print(f"Embeddings saved to {output_path}")
|
58 |
-
|
59 |
-
return embeddings, image_paths
|
60 |
-
|
61 |
-
if __name__ == "__main__":
|
62 |
-
precompute_embeddings()
|
|
|
1 |
+
import os
|
2 |
+
import pickle
|
3 |
+
from .feature_extractor import FeatureExtractor
|
4 |
+
import time
|
5 |
+
from tqdm import tqdm
|
6 |
+
|
7 |
+
def precompute_embeddings():
|
8 |
+
# Use absolute paths for Hugging Face Spaces
|
9 |
+
base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
10 |
+
image_dir = os.path.join(base_dir, 'data', 'images')
|
11 |
+
output_path = os.path.join(base_dir, 'data', 'embeddings.pkl')
|
12 |
+
|
13 |
+
# Create directories if they don't exist
|
14 |
+
os.makedirs(image_dir, exist_ok=True)
|
15 |
+
os.makedirs(os.path.dirname(output_path), exist_ok=True)
|
16 |
+
|
17 |
+
# Rest of your existing code...
|
18 |
+
extractor = FeatureExtractor()
|
19 |
+
embeddings = []
|
20 |
+
image_paths = []
|
21 |
+
|
22 |
+
valid_images = [f for f in os.listdir(image_dir)
|
23 |
+
if f.lower().endswith(('.png', '.jpg', '.jpeg'))]
|
24 |
+
total_images = len(valid_images)
|
25 |
+
|
26 |
+
print(f"\nFound {total_images} images to process")
|
27 |
+
|
28 |
+
start_time = time.time()
|
29 |
+
for idx, filename in enumerate(tqdm(valid_images, desc="Processing images")):
|
30 |
+
img_path = os.path.join(image_dir, filename)
|
31 |
+
try:
|
32 |
+
embedding = extractor.extract_features(img_path)
|
33 |
+
embeddings.append(embedding)
|
34 |
+
image_paths.append(img_path)
|
35 |
+
except Exception as e:
|
36 |
+
print(f"\nError processing {filename}: {e}")
|
37 |
+
|
38 |
+
with open(output_path, 'wb') as f:
|
39 |
+
pickle.dump({'embeddings': embeddings, 'image_paths': image_paths}, f)
|
40 |
+
|
41 |
+
print(f"\nProcessing complete!")
|
42 |
+
print(f"Successfully processed {len(embeddings)}/{total_images} images")
|
43 |
+
|
44 |
+
return embeddings, image_paths
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|