Vadzim Kashko commited on
Commit
63b863f
1 Parent(s): 3490722

feat: add script

Browse files
2d-masks-presentation-attack-detection.py ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import datasets
2
+ import pandas as pd
3
+
4
+ _CITATION = """\
5
+ @InProceedings{huggingface:dataset,
6
+ title = {selfie_and_video},
7
+ author = {TrainingDataPro},
8
+ year = {2023}
9
+ }
10
+ """
11
+
12
+ _DESCRIPTION = """\
13
+ 4000 people in this dataset. Each person took a selfie on a webcam,
14
+ took a selfie on a mobile phone. In addition, people recorded video from
15
+ the phone and from the webcam, on which they pronounced a given set of numbers.
16
+ Includes folders corresponding to people in the dataset. Each folder includes
17
+ 8 files (4 images and 4 videos).
18
+ """
19
+ _NAME = 'selfie_and_video'
20
+
21
+ _HOMEPAGE = f"https://huggingface.co/datasets/TrainingDataPro/{_NAME}"
22
+
23
+ _LICENSE = ""
24
+
25
+ _DATA = f"https://huggingface.co/datasets/TrainingDataPro/{_NAME}/resolve/main/data/"
26
+
27
+
28
+ class SelfieAndVideo(datasets.GeneratorBasedBuilder):
29
+ """Small sample of image-text pairs"""
30
+
31
+ def _info(self):
32
+ return datasets.DatasetInfo(
33
+ description=_DESCRIPTION,
34
+ features=datasets.Features({
35
+ 'photo_1': datasets.Image(),
36
+ 'photo_2': datasets.Image(),
37
+ 'video_3': datasets.Value('string'),
38
+ 'video_4': datasets.Value('string'),
39
+ 'photo_5': datasets.Image(),
40
+ 'photo_6': datasets.Image(),
41
+ 'video_7': datasets.Value('string'),
42
+ 'video_8': datasets.Value('string'),
43
+ 'set_id': datasets.Value('string'),
44
+ 'worker_id': datasets.Value('string'),
45
+ 'age': datasets.Value('int8'),
46
+ 'country': datasets.Value('string'),
47
+ 'gender': datasets.Value('string')
48
+ }),
49
+ supervised_keys=None,
50
+ homepage=_HOMEPAGE,
51
+ citation=_CITATION,
52
+ )
53
+
54
+ def _split_generators(self, dl_manager):
55
+ images = dl_manager.download(f"{_DATA}data.tar.gz")
56
+ annotations = dl_manager.download(f"{_DATA}{_NAME}.csv")
57
+ images = dl_manager.iter_archive(images)
58
+ return [
59
+ datasets.SplitGenerator(name=datasets.Split.TRAIN,
60
+ gen_kwargs={
61
+ "images": images,
62
+ 'annotations': annotations
63
+ }),
64
+ ]
65
+
66
+ def _generate_examples(self, images, annotations):
67
+ annotations_df = pd.read_csv(annotations, sep=';')
68
+ images_data = pd.DataFrame(columns=['Link', 'Bytes'])
69
+ for idx, (image_path, image) in enumerate(images):
70
+ if image_path.lower().endswith('.jpg'):
71
+ images_data.loc[idx] = {
72
+ 'Link': image_path,
73
+ 'Bytes': image.read()
74
+ }
75
+
76
+ annotations_df = pd.merge(annotations_df,
77
+ images_data,
78
+ on=['Link'],
79
+ how='left')
80
+
81
+ for idx, worker_id in enumerate(pd.unique(annotations_df['WorkerId'])):
82
+ annotation = annotations_df.loc[annotations_df['WorkerId'] ==
83
+ worker_id]
84
+ annotation = annotation.sort_values(['Link'])
85
+ data = {
86
+ (f'photo_{row[7][37]}' if row[7].lower().endswith('.jpg') else f'video_{row[7][37]}'):
87
+ ({
88
+ 'path': row[7],
89
+ 'bytes': row[8]
90
+ } if row[7].lower().endswith('.jpg') else row[7])
91
+ for row in annotation.itertuples()
92
+ }
93
+
94
+ age = annotation.loc[annotation['Link'].str.lower().str.endswith(
95
+ '1.jpg')]['Age'].values[0]
96
+ country = annotation.loc[annotation['Link'].str.lower().str.
97
+ endswith('1.jpg')]['Country'].values[0]
98
+ gender = annotation.loc[annotation['Link'].str.lower().str.
99
+ endswith('1.jpg')]['Gender'].values[0]
100
+ set_id = annotation.loc[annotation['Link'].str.lower().str.
101
+ endswith('1.jpg')]['SetId'].values[0]
102
+
103
+ data['worker_id'] = worker_id
104
+ data['age'] = age
105
+ data['country'] = country
106
+ data['gender'] = gender
107
+ data['set_id'] = set_id
108
+
109
+ yield idx, data