Ashot Vardanian
commited on
Commit
•
854043f
1
Parent(s):
21a709d
Add: Script to generate data URIs in `.txt`
Browse files
main.py
CHANGED
@@ -1,6 +1,8 @@
|
|
1 |
#!/usr/bin/env python3
|
2 |
-
from os import
|
3 |
from os.path import isfile, join, exists
|
|
|
|
|
4 |
|
5 |
import pandas as pd
|
6 |
import numpy as np
|
@@ -25,6 +27,17 @@ def is_image(path: PathLike) -> bool:
|
|
25 |
return False
|
26 |
|
27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
def trim_extension(filename: str) -> str:
|
29 |
return filename.rsplit('.', 1)[0]
|
30 |
|
@@ -32,7 +45,7 @@ def trim_extension(filename: str) -> str:
|
|
32 |
names = sorted(f for f in listdir('images') if is_image(join('images', f)))
|
33 |
names = [trim_extension(f) for f in names]
|
34 |
|
35 |
-
table = pd.read_table('images.tsv') if
|
36 |
'images.tsv') else pd.read_table('images.csv')
|
37 |
table = table[table['photo_id'].isin(names)]
|
38 |
table = table.sort_values('photo_id')
|
@@ -42,6 +55,7 @@ table.to_csv('images.csv', index=False)
|
|
42 |
names = list(set(table['photo_id']).intersection(names))
|
43 |
names_to_delete = [f for f in listdir(
|
44 |
'images') if trim_extension(f) not in names]
|
|
|
45 |
|
46 |
if len(names_to_delete) > 0:
|
47 |
print(f'Plans to delete: {len(names_to_delete)} images without metadata')
|
@@ -52,7 +66,7 @@ if not exists('images.fbin'):
|
|
52 |
model = get_model('unum-cloud/uform-vl-english')
|
53 |
vectors = []
|
54 |
|
55 |
-
for name in tqdm(
|
56 |
image = Image.open(join('images', name + '.jpg'))
|
57 |
image_data = model.preprocess_image(image)
|
58 |
image_embedding = model.encode_image(image_data).detach().numpy()
|
@@ -61,6 +75,17 @@ if not exists('images.fbin'):
|
|
61 |
image_mat = np.vstack(vectors)
|
62 |
save_matrix(image_mat, 'images.fbin')
|
63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
if not exists('images.usearch'):
|
65 |
image_mat = load_matrix('images.fbin')
|
66 |
count = image_mat.shape[0]
|
|
|
1 |
#!/usr/bin/env python3
|
2 |
+
from os import PathLike, listdir, remove
|
3 |
from os.path import isfile, join, exists
|
4 |
+
from mimetypes import guess_type
|
5 |
+
from base64 import b64encode
|
6 |
|
7 |
import pandas as pd
|
8 |
import numpy as np
|
|
|
27 |
return False
|
28 |
|
29 |
|
30 |
+
def image_to_data(path: PathLike) -> str:
|
31 |
+
"""Convert a file (specified by a path) into a data URI."""
|
32 |
+
if not exists(path):
|
33 |
+
raise FileNotFoundError
|
34 |
+
mime, _ = guess_type(path)
|
35 |
+
with open(path, 'rb') as fp:
|
36 |
+
data = fp.read()
|
37 |
+
data64 = b64encode(data).decode('utf-8')
|
38 |
+
return f'data:{mime}/jpg;base64,{data64}'
|
39 |
+
|
40 |
+
|
41 |
def trim_extension(filename: str) -> str:
|
42 |
return filename.rsplit('.', 1)[0]
|
43 |
|
|
|
45 |
names = sorted(f for f in listdir('images') if is_image(join('images', f)))
|
46 |
names = [trim_extension(f) for f in names]
|
47 |
|
48 |
+
table = pd.read_table('images.tsv') if exists(
|
49 |
'images.tsv') else pd.read_table('images.csv')
|
50 |
table = table[table['photo_id'].isin(names)]
|
51 |
table = table.sort_values('photo_id')
|
|
|
55 |
names = list(set(table['photo_id']).intersection(names))
|
56 |
names_to_delete = [f for f in listdir(
|
57 |
'images') if trim_extension(f) not in names]
|
58 |
+
names = list(table['photo_id'])
|
59 |
|
60 |
if len(names_to_delete) > 0:
|
61 |
print(f'Plans to delete: {len(names_to_delete)} images without metadata')
|
|
|
66 |
model = get_model('unum-cloud/uform-vl-english')
|
67 |
vectors = []
|
68 |
|
69 |
+
for name in tqdm(names, desc='Vectorizing images'):
|
70 |
image = Image.open(join('images', name + '.jpg'))
|
71 |
image_data = model.preprocess_image(image)
|
72 |
image_embedding = model.encode_image(image_data).detach().numpy()
|
|
|
75 |
image_mat = np.vstack(vectors)
|
76 |
save_matrix(image_mat, 'images.fbin')
|
77 |
|
78 |
+
if not exists('images.txt'):
|
79 |
+
|
80 |
+
datas = []
|
81 |
+
for name in tqdm(names, desc='Encoding images'):
|
82 |
+
data = image_to_data(join('images', name + '.jpg'))
|
83 |
+
datas.append(data)
|
84 |
+
|
85 |
+
with open('images.txt', 'w') as f:
|
86 |
+
f.write('\n'.join(datas))
|
87 |
+
|
88 |
+
|
89 |
if not exists('images.usearch'):
|
90 |
image_mat = load_matrix('images.fbin')
|
91 |
count = image_mat.shape[0]
|