-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrol_class.py
88 lines (73 loc) · 3.77 KB
/
control_class.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
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
82
83
84
85
86
87
88
from dependencies import *
class ControlClass:
def compute():
# set parameters
percent_frames = 3
k_size = 9
# set files and paths
videos_path = '/media/sergio/0C5EC1615EC14464/chimp_and_see/data/interim/videos/animals'
export_path = '/media/sergio/0C5EC1615EC14464/msc_data/images/animals_demo'
output_csv = "output_data_demo.csv"
data_csv = "video_subject_species.csv"
# get csv data
filenames_and_subject_ids = pd.read_csv(data_csv)
output_data = pd.read_csv(output_csv)
# get counters and files to iterate over
if len(output_data.image_id) > 1:
image_id = int(max(output_data.image_id)) + 1
file_id = int(max(output_data.file_id)) + 1
frame_id = int(max(output_data.frame_id)) + 1
else:
image_id = 1
file_id = 1
frame_id = 1
print(image_id, file_id, frame_id)
completed_files = output_data.filename.tolist()
all_filenames = filenames_and_subject_ids.filename.tolist()
remaining_files = list(set(all_filenames) - set(completed_files))
existing_vids = os.listdir(videos_path)
existing_vids = list(map(lambda x: x.split('.')[0], existing_vids)) # remove file extensions
# make a dataframe
data_store = []
batch_counter = 0
for filename in remaining_files:
blank = True
print(filename)
species = filenames_and_subject_ids[filenames_and_subject_ids['filename'].isin([filename])].species.tolist()[0]
print(species)
if species != "human":
subject_ids = filenames_and_subject_ids[filenames_and_subject_ids['filename'].isin([filename])].subject_id.tolist()
# 1. get frames
video_combiner = VideoCombiner(videos_path, subject_ids, percent_frames)
raw_frames = video_combiner.get_frames()
if len(raw_frames) > 0:
remove_outliers = RemoveOutliers(raw_frames, 0.25)
useable_frames = remove_outliers.get_frames()
# 2. process
image_processor = ImageProcessor(useable_frames, k_size)
boxes, _, _ = image_processor.analyse_frames()
len(sum(boxes, []))
if len(sum(boxes, [])) > 0:
blank = False
# 3. export images
image_exporter = ImageExporter(useable_frames, boxes, export_path, image_id, frame_id)
image_id, frame_id, image_and_frame_ids = image_exporter.export_images()
# 4. write to df
for image in image_and_frame_ids:
# print([file_id, filename, species, image[0], image[1], image[2][0], image[2][1], image[2][2], image[2][3]])
data_store.append([file_id, filename, species, image[0], image[1], image[2][0], image[2][1], image[2][2], image[2][3]])
batch_counter += len(image_and_frame_ids)
if blank == True:
data_store.append([file_id, filename, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan])
file_id += 1
# 5. write to csv
if batch_counter > 500:
with open(f"{output_csv}", 'a') as f:
writer = csv.writer(f)
for row in data_store:
writer.writerow(row)
f.close
# 6. clear data store/home/sergio/deep-learning/image_differencing/image_processing/dependencies.py
data_store = []
batch_counter = 0
ControlClass.compute()