-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMouseDataLoader.py
More file actions
81 lines (65 loc) · 2.75 KB
/
MouseDataLoader.py
File metadata and controls
81 lines (65 loc) · 2.75 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import json
import os
import glob
from pathlib import Path
class MousePainDataLoader:
def __init__(self):
"""
Class to load and process mouse pain detection datasets.
"""
self.DATA_PATHS = {
'face': ('data/face_detection_data.json', 'Face_detection/'),
'eyes': ('data/eyes_detection_data.json', 'Eyes_detection/')
}
def load_json_data(self, json_path):
if not os.path.exists(json_path):
print(f"File doesn't exist: {json_path}")
return None
try:
with open(json_path, 'r', encoding='utf-8') as f:
data = json.load(f)
print(f"File loaded: {json_path}")
return data
except Exception as e:
print(f"File couldn't be loaded: {e}")
return None
def extract_dataset_info(self, dataset_type='eyes'):
"""Get dataset information for a specific type (eyes or face)"""
if dataset_type not in self.DATA_PATHS:
print(f"Unknown dataset type: {dataset_type}")
return None
json_path, images_folder = self.DATA_PATHS[dataset_type]
data = self.load_json_data(json_path)
if data is None:
return None
samples = []
for image_filename, annotations in data.items():
image_path = os.path.join(images_folder, image_filename)
if not os.path.exists(image_path):
print(f"Image is missing: {image_path}")
continue
for annotation in annotations:
sample = {
'image_path': image_path,
'image_filename': image_filename,
'animal_number': annotation.get('AnimalNumber', '1'),
'bbox': annotation['Boundingbox']
}
# only for eyes dataset (GrimmaceScale)
if dataset_type == 'eyes' and 'GrimaceScale' in annotation:
sample['pain_level'] = int(annotation['GrimaceScale'])
else:
sample['pain_level'] = None
samples.append(sample)
print(f"Found {len(samples)} samples in the {dataset_type}")
return samples
def get_all_samples(self):
"""Get all samples (images) from both datasets"""
all_samples = []
for dataset_type in ['eyes', 'face']:
samples = self.extract_dataset_info(dataset_type)
if samples:
for sample in samples:
sample['dataset_type'] = dataset_type
all_samples.extend(samples)
return all_samples