Commit b1450a8c authored by Alessandro dos Santos Ferreira's avatar Alessandro dos Santos Ferreira
Browse files

Gerando arquivo trainning.arff

parent 82c4240a
......@@ -48,7 +48,6 @@ Pacote de Visão Computacional do Inovisão.
- Python 2.7.6
- scikit-image
- Opencv 2.7
- python-weka-wrapper 0.3.5
- tk/tk-dev
### Windows
......@@ -93,65 +92,6 @@ Pacote de Visão Computacional do Inovisão.
$ sudo pip install cycler
```
## Como instalar o python-weka-wrapper
### Ubuntu
Primeiro você precisa compilar os código C/C+ e os módulos Python:
$ sudo apt-get install build-essential python-dev
Agora você pode instalar os vários pacotes que precisamos para instalar o python-weka-wrapper:
$ sudo apt-get install python-pip python-numpy
Os seguintes pacotes são opcionais mas necessários se você deseja uma representação gráfica:
$ sudo apt-get install python-imaging python-matplotlib python-pygraphviz
Instale OpenJDK para obter todos os cabeçalhos que javabridge compila:
$ sudo apt-get install default-jdk
No meu ubuntu 14.04 tive problemas com dependência, acabei instalando o java da oracle seguindo as orientações deste site: [instalando java da oracle](http://askubuntu.com/questions/521145/how-to-install-oracle-java-on-ubuntu-14-04)
Finalmente você pode usar pip para instalar os pacotes Python que não estão disponíveis no repositório:
$ sudo pip install javabridge
$ sudo pip install python-weka-wrapper
### Windows
Por favor note: você precisa certificar-se que os bits do seu ambiente é consistente. Isto é, se você instalar uma versão de Python 32-bit você deve instalar um JDK 32-bit e numpy 32-bit ( ou então todos eles devem ser 64-bit ).
Realize os seguintes passos:
Instale Python, esteja certo que você checou Add python.exe to path durante a instalação
Adicione os scripts Python eu sua variável de ambiente PATH, por exemplo, :\\Python27\\Scripts
Instale pip com os seguintes passos:
* baixe daqui https://bootstrap.pypa.io/get-pip.py
* instale usando python get-pip.py
Instale numpy
* baixe numpy 1.9.x MKL ( ou superior ) para Python 2.7 (cp27) e sua configuração de bit (32 ou 64 bit)
* instale o arquivo .whl usando pip: pip install numpy-X.Y.Z.whl
Instale .Net 4.0 (se já não estiver instalado)
Instale Windows SDK 7.1
Abra o prompt de comando do Windows SDK (não o prompt de comando convencional!) e instale javabridge e python-weka-wrapper
> set MSSdk=1
> set DISTUTILS_USE_SDK=1
> pip install javabridge
> pip install python-weka-wrapper
Agora você pode executar python-weka-wrapper usando o prompt de comando convencional também.
Se você deseja as funcionalidades gráficas você precisa instalar matplotlib também:
* baixe matplotlib para Python 2.7 (cp27) e sua configuração de bit (32 or 64 bit)
* instale o arquivo .whl usando pip: pip install matplotlib-X.Y.Z.whl
## Mais informações
- http://pythonhosted.org/python-weka-wrapper/install.html
- http://pythonhosted.org/python-weka-wrapper/troubleshooting.html
## Como instalar o tk/tk-dev
### Ubuntu
......
from extractor import Extractor
from feature_extraction import FeatureExtractor
from color_summarizer import ColorStats
from glcm import GLCM
from hog import HOG
from image_moments import ImageMoments
from lbp import LBP
__all__ = ["extractor",
"color_summarizer",
"glcm",
"hog",
"image_moments",
"lbp"]
......@@ -18,6 +21,7 @@ from util.config import Config
_extractor_list = OrderedDict( {
"color_summarizer" : Config("Color Statistics", True, bool, meta=ColorStats),
"hog" : Config("Histogram of Oriented Gradients", True, bool, meta=HOG),
"glcm" : Config("Gray-Level Co-Occurrence Matrix", True, bool, meta=GLCM),
"image_moments" : Config("Image Moments (Hu)", True, bool, meta=ImageMoments),
"lbp" : Config("Local Binary Patterns", True, bool, meta=LBP)
} )
......@@ -27,6 +31,7 @@ def get_extractor_config():
def set_extractor_config(configs):
_extractor_list["color_summarizer"] = Config.nvl_config(configs["color_summarizer"], _extractor_list["color_summarizer"])
_extractor_list["glcm"] = Config.nvl_config(configs["glcm"], _extractor_list["glcm"])
_extractor_list["hog"] = Config.nvl_config(configs["hog"], _extractor_list["hog"])
_extractor_list["image_moments"] = Config.nvl_config(configs["image_moments"], _extractor_list["image_moments"])
_extractor_list["lbp"] = Config.nvl_config(configs["lbp"], _extractor_list["lbp"])
......@@ -8,6 +8,7 @@
Author: Alessandro dos Santos Ferreira ( santosferreira.alessandro@gmail.com )
"""
import cv2
import numpy as np
from util.utils import ImageUtils
......@@ -16,26 +17,19 @@ from extractor import Extractor
class ColorStats(Extractor):
def __init__(self, image, image_bgr = None, image_hsv = None, image_cielab = None):
if image_bgr is None:
image_bgr = ImageUtils.image_to_bgr(image)
if image_hsv is None:
image_hsv = ImageUtils.image_to_hsv(image_bgr, bgr = True)
if image_cielab is None:
image_cielab = ImageUtils.image_to_cielab(image_bgr, bgr = True)
super(self.__class__, self).__init__(image=image, image_bgr=image_bgr, image_hsv=image_hsv, image_cielab=image_cielab)
def __init__(self):
pass
def run(self):
def run(self, image):
imagemHSV = cv2.cvtColor(self.imagem, cv2.COLOR_BGR2HSV)
imagemCIELab = cv2.cvtColor(self.imagem, cv2.COLOR_BGR2LAB)
b, g, r = cv2.split(self.imagem_bgr)
h, s, v = cv2.split(self.imagem_hsv)
ciel, ciea, cieb = cv2.split(self.imagem_cielab)
image_hsv = ImageUtils.image_to_hsv(image, bgr = True)
image_cielab = ImageUtils.image_to_cielab(image, bgr = True)
b, g, r = cv2.split(image)
h, s, v = cv2.split(image_hsv)
ciel, ciea, cieb = cv2.split(image_cielab)
labels = [
'cor_rmin', 'cor_rmax', 'cor_rmediamedia', 'cor_rdesvio',
'cor_gmin', 'cor_gmax', 'cor_gmedia', 'cor_gdesvio',
......@@ -47,8 +41,8 @@ class ColorStats(Extractor):
'cor_cieamin', 'cor_cieamax', 'cor_cieamedia', 'cor_cieadesvio',
'cor_ciebmin', 'cor_ciebmax', 'cor_ciebmedia', 'cor_ciebdesvio'
]
tipos = [Extractor.NUMERIC] * len(labels)
types = [Extractor.NUMERIC] * len(labels)
values = [
np.min(r), np.max(r), np.mean(r), np.std(r),
......
......@@ -17,29 +17,10 @@ class Extractor(object):
NUMERIC = 'numeric'
NOMINAL = 'nominal'
def __init__(self,
image = None,
image_bgr = None,
image_hsv = None,
image_cielab = None,
image_grayscale = None,
image_binary = None,
image_edge = None,
image_128x128 = None):
self.image = image
self.image_bgr = image_bgr
self.image_hsv = image_hsv
self.image_cielab = image_cielab
self.image_grayscale = image_grayscale
self.image_binary = image_binary
self.image_edge = image_edge
self.image_128x128 = image_128x128
@staticmethod
def get_name(self):
return self.__class__.__name__
@abstractmethod
def run(self):
pass
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
"""
Runs feature extraction algorithms.
Name: feature_extractor.py
Author: Alessandro dos Santos Ferreira ( santosferreira.alessandro@gmail.com )
"""
import io
import itertools
import os
from interface.interface import InterfaceException as IException
from util.file_utils import File
from util.utils import ImageUtils
from util.utils import TimeUtils
from extractor import Extractor
class FeatureExtractor(object):
def __init__(self, extractors):
self.extractors = extractors
def extract_all(self, dataset, output_file = None, classes = None, overwrite = True):
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 overwrite == False and os.path.isfile(output_file):
return output_file
classes = sorted(File.list_dirs(dataset)) if classes is None else classes
start_time = TimeUtils.get_time()
data = []
for cl in classes:
items = 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 len(data) == 0:
raise IException("There are no images in dataset: %s" % dataset)
self._save_output(File.get_filename(dataset), classes, labels, types, data, output_file)
end_time = TimeUtils.get_time()
return output_file, (end_time - start_time)
def _save_output(self, relation, classes, labels, types, data, output_file):
arff = open(output_file,'wb')
arff.write("%s %s\n\n" % ('@relation ', relation))
for label, t in zip(labels,types):
arff.write("%s %s %s\n" % ('@attribute', label, t))
arff.write("%s %s {%s}\n\n" % ('@attribute','classe',', '.join(classes)))
arff.write('@data\n\n')
for instance in data:
instance = map(str, instance)
line = ",".join(instance)
arff.write(line+"\n")
arff.close()
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
"""
Extract GLCM (Gray-Level Co-Occurrence Matrix) feature.
Name: glcm.py
Author: Alessandro dos Santos Ferreira ( santosferreira.alessandro@gmail.com )
"""
import numpy as np
from skimage import feature
from util.utils import ImageUtils
from extractor import Extractor
class GLCM(Extractor):
def __init__(self, glcm_levels = 256):
self.glcm_levels = glcm_levels
def run(self, image):
image_grayscale = ImageUtils.image_grayscale(image, bgr = True)
g = feature.greycomatrix(image_grayscale, [1, 2], [0, np.pi / 4, np.pi / 2], self.glcm_levels, normed=True, symmetric=True)
contrast = feature.greycoprops(g, 'contrast').tolist()
dissimilarity = feature.greycoprops(g, 'dissimilarity').tolist()
homogeneity = feature.greycoprops(g, 'homogeneity').tolist()
asm = feature.greycoprops(g, 'ASM').tolist()
energy = feature.greycoprops(g, 'energy').tolist()
correlation = feature.greycoprops(g, 'correlation').tolist()
labels = [
'glcm_cont_1_0', 'glcm_cont_1_45', 'glcm_cont_1_90', 'glcm_cont_2_0', 'glcm_cont_2_45', 'glcm_cont_2_90',
'glcm_diss_1_0', 'glcm_diss_1_45', 'glcm_diss_1_90', 'glcm_diss_2_0', 'glcm_diss_2_45', 'glcm_diss_2_90',
'glcm_homo_1_0', 'glcm_homo_1_45', 'glcm_homo_1_90', 'glcm_homo_2_0', 'glcm_homo_2_45', 'glcm_homo_2_90',
'glcm_asm_1_0', 'glcm_asm_1_45', 'glcm_asm_1_90', 'glcm_asm_2_0', 'glcm_asm_2_45', 'glcm_asm_2_90',
'glcm_ener_1_0', 'glcm_ener_1_45', 'glcm_ener_1_90', 'glcm_ener_2_0', 'glcm_ener_2_45', 'glcm_ener_2_90',
'glcm_corr_1_0', 'glcm_corr_1_45', 'glcm_corr_1_90', 'glcm_corr_2_0', 'glcm_corr_2_45', 'glcm_corr_2_90',
]
types = [Extractor.NUMERIC] * len(labels)
values = contrast[0] + contrast[1] + dissimilarity[0] + dissimilarity[1] + homogeneity[0] + \
homogeneity[1] + asm[0] + asm[1] + energy[0] + energy[1] + correlation[0] + correlation[1]
return labels, types, values
......@@ -16,20 +16,19 @@ from extractor import Extractor
class HOG(Extractor):
def __init__(self, image, image_128x128 = None):
if image_128x128 is None:
image_128x128 = ImageUtils.image_resize( ImageUtils.image_to_bgr(image), 128, 128)
super(self.__class__, self).__init__(image=image, image_128x128=image_128x128)
def __init__(self):
pass
def run(self):
def run(self, image):
labels = [m+n for m,n in zip(['hog_'] * len(valores),map(str,range(0,len(valores))))]
types = [Extractor.NUMERIC] * len(labels)
image_grayscale = ImageUtils.image_grayscale(image, bgr = True)
image_128x128 = ImageUtils.image_resize(image_grayscale, 128, 128)
values, _ = feature.hog(self.image_128x128, orientations=8, pixels_per_cell=(32, 32),
values, _ = feature.hog(image_128x128, orientations=8, pixels_per_cell=(32, 32),
cells_per_block=(1, 1), visualise=True)
labels = [m+n for m,n in zip(['hog_'] * len(values),map(str,range(0,len(values))))]
types = [Extractor.NUMERIC] * len(labels)
return labels, types, list(values)
......@@ -16,25 +16,23 @@ from extractor import Extractor
class ImageMoments(Extractor):
def __init__(self, image, image_grayscale = None):
if image_grayscale is None:
image_grayscale = ImageUtils.image_grayscale(image)
def __init__(self):
self._moments_order = [(0, 1), (1, 0), (1, 1), (0, 2), (2, 0)]
super(self.__class__, self).__init__(image=image, image_grayscale=image)
def run(self, image):
self._moments_order = [(0, 1), (1, 0), (1, 1), (0, 2), (2, 0)]
image_grayscale = ImageUtils.image_grayscale(image, bgr = True)
def run(self):
m = measure.moments(self.image_grayscale)
m = measure.moments(image_grayscale)
values_m = [m[p, q] for (p, q) in self._moments_order]
labels_m = [M+str(p)+str(q) for M,(p,q) in zip(['M_'] * len(self._moments_order), self._moments_order)]
row = m[0, 1] / m[0, 0]
col = m[1, 0] / m[0, 0]
mu = measure.moments_central(self.image_grayscale, row, col)
mu = measure.moments_central(image_grayscale, row, col)
values_mu = [mu[p, q] for (p, q) in self._moments_order]
labels_mu = [M+str(p)+str(q) for M,(p,q) in zip(['Mu_'] * len(self._moments_order), self._moments_order)]
......@@ -43,12 +41,11 @@ class ImageMoments(Extractor):
hu = measure.moments_hu(nu)
values_hu = list(hu)
labels_hu = [m+n for m,n in zip(['Hu_'] * len(valores_hu),map(str,range(0,len(valores_hu))))]
labels_hu = [m+n for m,n in zip(['Hu_'] * len(values_hu),map(str,range(0,len(values_hu))))]
values = values_m + values_mu + values_hu
labels = labels_m + labels_mu + labels_hu
types = [Extractor.NUMERIC] * len(labels)
values = values_m + values_mu + values_hu
return labels, types, values
......@@ -7,6 +7,7 @@
Name: lpb.py
Author: Alessandro dos Santos Ferreira ( santosferreira.alessandro@gmail.com )
"""
import numpy as np
from skimage import feature
from util.utils import ImageUtils
......@@ -16,22 +17,19 @@ from extractor import Extractor
class LBP(Extractor):
def __init__(self, image, image_grayscale = None, lbp_radius = 2, n_bins = 18):
if image_grayscale is None:
image_grayscale = ImageUtils.image_grayscale(image)
super(self.__class__, self).__init__(image=image, image_grayscale=image)
def __init__(self, lbp_radius = 2, n_bins = 18):
self.lbp_radius = lbp_radius
self.n_bins = n_bins
def run(self):
def run(self, image):
image_grayscale = ImageUtils.image_grayscale(image, bgr = True)
labels = [m+n for m,n in zip(['lbp_'] * len(valores),map(str,range(0,len(valores))))]
lbp = feature.local_binary_pattern(image_grayscale, 8 * self.lbp_radius, self.lbp_radius, 'uniform')
values, _ = np.histogram(lbp, normed=True, bins=self.n_bins, range=(0, self.n_bins))
labels = [m+n for m,n in zip(['lbp_'] * len(values),map(str,range(0,len(values))))]
types = [Extractor.NUMERIC] * len(labels)
lbp = feature.local_binary_pattern(self.image_grayscale, 8 * self.lbp_radius, self.lbp_radius, 'uniform')
valuess, _ = np.histogram(lbp, normed=True, bins=self.n_bins, range=(0, self.n_bins))
return labels, types, list(values)
......@@ -191,8 +191,7 @@ class TkInterface(Interface):
return configs
except:
self._conf_dialog.destroy()
#raise IException("Illegal values, please try again")
raise
raise IException("Illegal values, please try again")
def show_error(self, fmt, *args):
......@@ -228,9 +227,9 @@ class TkInterface(Interface):
self._log_exception( IException.format_exception() )
def _log_exception(self, message, warning = False):
if warning == True:
self.show_warning( message )
def _log_exception(self, message, dialog = False):
if dialog == True:
self.show_error( message )
elif IException.DEBUG == True:
self.append_log( message )
else:
......
......@@ -52,7 +52,7 @@ if __name__ == "__main__":
tk.add_menu("Feature Extraction")
tk.add_command("Select extractors", act.select_extractors, 'e')
tk.add_separator()
tk.add_command("Execute", act.func_not_available, 'F')
tk.add_command("Execute", act.run_extractors, 'F')
tk.add_menu("Classification")
tk.add_command("Configure", act.func_not_available)
......
......@@ -15,6 +15,7 @@ from interface.interface import InterfaceException as IException
import segmentation
import extraction
from extraction import FeatureExtractor
import util
from util.config import Config
......@@ -231,6 +232,9 @@ class Act(object):
self.extractors = [new_config[extractor].meta for extractor in new_config
if new_config[extractor].value == True ]
if len(self.extractors) == 0:
raise IException("Please select at least one extractor")
self.tk.append_log("\nConfig updated:\n%s",
'\n'.join(["%s: %s" % (new_config[extractor].label, "on" if new_config[extractor].value==True else "off")
......@@ -238,8 +242,20 @@ class Act(object):
extraction.set_extractor_config(new_config)
self.tk.dialogue_select(title, current_config, process_config)
def run_extractors(self):
self.tk.write_log("Running extractors on all images in %s", self.dataset)
fextractor = FeatureExtractor(self.extractors)
self.tk.append_log("%s", '\n'.join([extraction._extractor_list[extractor].label for extractor in extraction._extractor_list
if extraction._extractor_list[extractor].value == True ]))
output_file, run_time = fextractor.extract_all(self.dataset, "trainning")
self.tk.append_log("\nOutput file saved in %s", output_file)
self.tk.append_log("Time elapsed: %0.3f seconds", run_time)
def func_not_available(self):
self.tk.write_log("This functionality is not available right now.")
......@@ -29,12 +29,15 @@ class File(object):
return path
@staticmethod
def open_image(filepath):
def open_image(filepath, rgb = True):
image = cv2.imread(filepath)
if image is None:
raise IOError('Image not opened')
#image = np.flipud(image)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
if rgb == True:
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
#return img_as_float(image)
return image
......@@ -43,12 +46,12 @@ class File(object):
for root, dirs, files in os.walk(dataset):
for d in dirs:
filepath = dataset + '/' + d + '/' + filename + '_%05d' % idx + ext
filepath = File.make_path(dataset, d, filename + '_%05d' % idx + ext)
if(os.path.isfile(filepath)):
os.remove(filepath)
dir_path = dataset + '/' + directory
filepath = dir_path + '/' + filename + '_%05d' % idx + ext
dir_path = File.make_path(dataset, directory)
filepath = File.make_path(dir_path, filename + '_%05d' % idx + ext)
if not os.path.exists(dir_path):
os.makedirs(dir_path)
cv2.imwrite(filepath, cv2.cvtColor(image, cv2.COLOR_RGB2BGR))
......@@ -64,3 +67,7 @@ class File(object):
def create_dir(directory):
if not os.path.exists(directory):
os.makedirs(directory)
@staticmethod
def make_path(*dirs):
return '/'.join([ d for d in dirs])
......@@ -29,38 +29,38 @@ class ImageUtils(object):
@staticmethod
def image_to_hsv(image, bgr = False):
if bgr == False:
image = image_to_bgr(image)
image = ImageUtils.image_to_bgr(image)
return cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
@staticmethod
def image_to_cielab(image, bgr = False):
if bgr == False:
image = image_to_bgr(image)
image = ImageUtils.image_to_bgr(image)
return cv2.cvtColor(image, cv2.COLOR_BGR2LAB)
@staticmethod
def image_grayscale(image, bgr = False):
if bgr == False:
image = image_to_bgr(image)
image = ImageUtils.image_to_bgr(image)
return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
@staticmethod
def image_binary(image, bgr = False, grayscale = False):
if grayscale == False:
image = image_grayscale(image, bgr)
image = ImageUtils.image_grayscale(image, bgr)
_, image = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)