-
Notifications
You must be signed in to change notification settings - Fork 0
/
do_extract_features.py
110 lines (88 loc) · 3.72 KB
/
do_extract_features.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
"""Extracts three types of features from standardized square images:
(1) Wing texture: Gabor transformation features
(2) Wing color: Regional intensities of pixels in chromatic coords
(3) Wing shape: coefficients from morphometric analysis
Features are saved to a csv file.
"""
### Setup ======================================================================
print ('\nSetting up ...')
import os#, sys
from datetime import datetime
import pandas as pd
import numpy as np
import cv2
import autoID
# Read config file
cfg = autoID.utils.read_config('config.yaml')
# Initiate log file
#try: log_extract = pd.read_csv(log_extract_path,header=0,index_col=0) # load log
#except IOError: log_extract = pd.DataFrame(columns=['Error']) # create new log
# Import metadata
md = pd.read_csv(cfg['metadata_path'],header=0,index_col=None,encoding='utf-8')
# Import previously-extracted features
try: fd = pd.read_csv(cfg['features_path'],header=0,index_col=0)
except IOError:
columns = np.arange(1,663+1).astype(str)
fd = pd.DataFrame(columns=columns)
### Look for images to extract from ============================================
print ('Finding images from which to extract features ...')
# Get square filenames
square_files = [f for f in os.listdir(cfg['squares_path']) if f.endswith('.tif')]
# Which images need extracting?
image_cue = list(set(square_files) - set(fd.index.values))
print (' - Found {} images to process'.format(len(image_cue)))
### Extract features from images ===============================================
print ('Extracting ...')
t0 = datetime.now()
# Extract features from them & add them to extracted_features
yep = 0; nope = 0 #succes/fail counters
for i,fn in enumerate(image_cue):
# Print status update for every 10th image
if (i+1)%10 == 0:
print (' - Preprocessing image {} of {}. {} successes, {} failures so far ...'\
.format(i+1,len(image_cue),yep,nope))
# TODO: make this a function
# Load image
try:
square = cv2.imread(os.path.join(cfg['squares_path'],fn))[:,:,::-1]
except:
#appendRowIf(log_extract,fn,'Error loading image.')
nope+=1; continue
# Extract features
try:
morphCoeffs = autoID.extraction.morphometric_sample(square) # 0.506225 s
except RuntimeError:
#appendRowIf(log_extract,fn,"Error extracting 'morph' features: {}".format(os.environ['error_message']))
nope+=1; continue
except:
#appendRowIf(log_extract,fn,"Error extracting 'morph' features.")
nope+=1; continue
try:
chromCoeffs = autoID.extraction.chrom_sample(square) # 0.17824 s
except:
#appendRowIf(log_extract,fn,"Error extracting 'chrom' features.")
nope+=1; continue
try:
gaborCoeffs = map(lambda x: autoID.extraction.gabor_sample(square,x,3),[8,4,2,1])
gaborCoeffs = np.concatenate(gaborCoeffs) # 1.725601 s
except:
#appendRowIf(log_extract,fn,"Error extracting 'gabor' features.")
nope+=1; continue
# Concatenate features
features = []
features.extend(list(morphCoeffs))
features.extend(list(chromCoeffs))
features.extend(list(gaborCoeffs))
# Append features to dataframe
fd.loc[fn] = features
yep+=1
time = datetime.now() - t0
print (' - Extraction done: {} successes, {} failures. Took {}'.format(yep,nope,time))
### Save out features & clean up ===============================================
print ('Saving features ...')
fd.to_csv(cfg['features_path'],header=True,index=True,index_label='img_filename')
print ('Done. Clean metadata save to: {}'.format(cfg['features_path']))
# Save log file
print ('Finishing up ...')
#log_extract.to_csv(log_extract_path,header=True,index=True,index_label='Filename')
print ('Done.')