-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcreate_image_hash.py
34 lines (25 loc) · 915 Bytes
/
create_image_hash.py
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
import os
import collections
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from PIL import Image
import imagehash
from tqdm import tqdm
def calc_image_hash(img_path):
with Image.open(img_path) as img:
img_hash = imagehash.dhash(img)
return img_hash
def main():
df = pd.read_csv('inputs/train.csv')
test_df = pd.read_csv('inputs/sample_submission.csv')
df['hash'] = np.nan
for i in tqdm(range(len(df))):
df.loc[i, 'hash'] = calc_image_hash('inputs/train_images/' + df.loc[i, 'ImageId'] + '.jpg')
test_df['hash'] = np.nan
for i in tqdm(range(len(test_df))):
test_df.loc[i, 'hash'] = calc_image_hash('inputs/test_images/' + test_df.loc[i, 'ImageId'] + '.jpg')
df.to_csv('processed/train_image_hash.csv', index=False)
test_df.to_csv('processed/test_image_hash.csv', index=False)
if __name__ == '__main__':
main()