Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
inovisao
pynovisao
Commits
70b464e4
Commit
70b464e4
authored
Aug 21, 2018
by
Diego André Sant'Ana
🤞
Browse files
Adicionado threads
parent
d00ea206
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
60 additions
and
40 deletions
+60
-40
src/extraction/feature_extraction.py
src/extraction/feature_extraction.py
+60
-40
No files found.
src/extraction/feature_extraction.py
View file @
70b464e4
...
...
@@ -11,7 +11,7 @@
import
io
import
itertools
import
os
import
threading
from
interface.interface
import
InterfaceException
as
IException
from
util.file_utils
import
File
...
...
@@ -22,20 +22,28 @@ from extractor import Extractor
class
FeatureExtractor
(
object
):
"""Handle the feature extraction."""
def
__init__
(
self
,
extractors
):
"""Constructor.
Parameters
----------
extractor : list of Extractor
Initial set of active extractors.
"""
self
.
extractors
=
extractors
def
extract_all
(
self
,
dataset
,
output_file
=
None
,
dirs
=
None
,
overwrite
=
True
):
self
.
labels
=
[]
self
.
types
=
[]
self
.
data
=
[]
self
.
threads
=
[]
self
.
labels
=
[]
self
.
types
=
[]
"""Runs the feature extraction algorithms on all images of dataset.
Parameters
----------
dataset : string
...
...
@@ -46,12 +54,12 @@ class FeatureExtractor(object):
List of directories to be serched. If not informed search in all directories with images inside dataset.
overwrite : boolean, optional, default = True
If False check if already exists a file containing the features.
Returns
-------
out : tuple
out : tuple
Returns a tuple containing the name of output file and time spent in milliseconds.
Raises
------
IException 'Please select at least one extractor'
...
...
@@ -63,55 +71,67 @@ class FeatureExtractor(object):
"""
if
len
(
self
.
extractors
)
==
0
:
raise
IException
(
"Please select at least one extractor"
)
if
output_file
is
None
:
output_file
=
File
.
get_filename
(
dataset
)
output_file
=
File
.
make_path
(
dataset
,
output_file
+
'.arff'
)
# if already exists a output file and must not override, return current file
if
overwrite
==
False
and
os
.
path
.
isfile
(
output_file
):
return
output_file
,
0
start_time
=
TimeUtils
.
get_time
()
classes
=
sorted
(
File
.
list_dirs
(
dataset
))
dirs
=
classes
if
dirs
is
None
else
dirs
data
=
[]
# Runs the feature extraction for all classes inside the dataset
for
cl
in
dirs
:
items
=
sorted
(
os
.
listdir
(
File
.
make_path
(
dataset
,
cl
)))
print
(
"Processing class %s - %d itens"
%
(
cl
,
len
(
items
)))
for
item
in
items
:
if
item
.
startswith
(
'.'
):
continue
try
:
filepath
=
File
.
make_path
(
dataset
,
cl
,
item
)
image
=
File
.
open_image
(
filepath
,
rgb
=
False
)
except
:
raise
IException
(
"Image %s is possibly corrupt"
%
filepath
)
if
len
(
data
)
>
0
:
values
=
list
(
itertools
.
chain
.
from_iterable
(
zip
(
*
([
extractor
().
run
(
image
)
for
extractor
in
self
.
extractors
]))[
2
]
))
else
:
labels
,
types
,
values
=
[
list
(
itertools
.
chain
.
from_iterable
(
ret
))
for
ret
in
zip
(
*
([
extractor
().
run
(
image
)
for
extractor
in
self
.
extractors
]))
]
data
.
append
(
values
+
[
cl
if
cl
in
classes
else
classes
[
0
]])
if
len
(
data
)
==
0
:
# start job for each extractor
process
=
threading
.
Thread
(
target
=
self
.
job_extractor
,
args
=
[
dataset
,
cl
,
classes
])
process
.
start
()
self
.
threads
.
append
(
process
)
#wait jobs finish
for
process
in
self
.
threads
:
process
.
join
()
if
len
(
self
.
data
)
==
0
:
raise
IException
(
"There are no images in dataset: %s"
%
dataset
)
# Save the output file in ARFF format
self
.
_save_output
(
File
.
get_filename
(
dataset
),
classes
,
labels
,
types
,
data
,
output_file
)
self
.
_save_output
(
File
.
get_filename
(
dataset
),
classes
,
self
.
labels
,
self
.
types
,
self
.
data
,
output_file
)
end_time
=
TimeUtils
.
get_time
()
return
output_file
,
(
end_time
-
start_time
)
def
job_extractor
(
self
,
dataset
,
cl
,
classes
):
items
=
sorted
(
os
.
listdir
(
File
.
make_path
(
dataset
,
cl
)))
print
(
"Processing class %s - %d itens"
%
(
cl
,
len
(
items
)))
for
item
in
items
:
values
=
[]
if
item
.
startswith
(
'.'
):
continue
try
:
filepath
=
File
.
make_path
(
dataset
,
cl
,
item
)
image
=
File
.
open_image
(
filepath
,
rgb
=
False
)
except
:
raise
IException
(
"Image %s is possibly corrupt"
%
filepath
)
if
len
(
self
.
data
)
>
0
:
values
=
list
(
itertools
.
chain
.
from_iterable
(
zip
(
*
([
extractor
().
run
(
image
)
for
extractor
in
self
.
extractors
]))[
2
]
))
self
.
data
.
append
(
values
+
[
cl
if
cl
in
classes
else
classes
[
0
]])
else
:
self
.
labels
,
self
.
types
,
values
=
[
list
(
itertools
.
chain
.
from_iterable
(
ret
))
for
ret
in
zip
(
*
([
extractor
().
run
(
image
)
for
extractor
in
self
.
extractors
]))
]
self
.
data
.
append
(
values
+
[
cl
if
cl
in
classes
else
classes
[
0
]])
def
extract_one_file
(
self
,
dataset
,
image_path
,
output_file
=
None
):
"""Runs the feature extraction algorithms on specific image.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment