Commit d5869921 authored by Diego André Sant'Ana's avatar Diego André Sant'Ana 🤞

Ajustes para o python 2 ainda falta resolver para python3

parent fb022b34
...@@ -24,7 +24,7 @@ import cv2 ...@@ -24,7 +24,7 @@ import cv2
from .extractor import Extractor from .extractor import Extractor
from tqdm import tqdm from tqdm import tqdm
import sys import sys
from sys import version_info
if not sys.warnoptions: if not sys.warnoptions:
import warnings import warnings
warnings.simplefilter("ignore") warnings.simplefilter("ignore")
...@@ -256,23 +256,41 @@ class FeatureExtractor(object): ...@@ -256,23 +256,41 @@ class FeatureExtractor(object):
output_file : string output_file : string
Path to output file. Path to output file.
""" """
if version_info[0] >= 3:
arff = open(output_file, 'wb')
arff.write(bytes(str("%s %s\n\n" % ('@relation', relation)), 'utf-8'))
arff = open(output_file, 'wb') for label, t in zip(labels, types):
arff.write(bytes(str("%s %s\n\n" % ('@relation', relation)), 'utf-8')) arff.write(bytes(str("%s %s %s\n" % ('@attribute', label, t)), 'utf-8'))
arff.write(bytes(str("%s %s {%s}\n\n" % ('@attribute', 'classe', ', '.join(classes))), 'utf-8'))
for label, t in zip(labels, types): arff.write(bytes(str('@data\n\n'), 'utf-8'))
arff.write(bytes(str("%s %s %s\n" % ('@attribute', label, t)), 'utf-8'))
for instance in data:
arff.write(bytes(str("%s %s {%s}\n\n" % ('@attribute', 'classe', ', '.join(classes))), 'utf-8')) instance = map(str, instance)
line = ",".join(instance)
arff.write(bytes(str(line + "\n"), 'utf-8'))
arff.close()
else:
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(bytes(str('@data\n\n'), 'utf-8')) arff.write('@data\n\n')
for instance in data: for instance in data:
instance = map(str, instance) instance = map(str, instance)
line = ",".join(instance) line = ",".join(instance)
arff.write(bytes(str(line + "\n"), 'utf-8')) arff.write(line + "\n")
arff.close() arff.close()
#method to equalize size of images #method to equalize size of images
def equalize_size_image(self, image): def equalize_size_image(self, image):
......
...@@ -17,7 +17,8 @@ from util.utils import ImageUtils ...@@ -17,7 +17,8 @@ from util.utils import ImageUtils
from skimage.measure import regionprops, moments, moments_central from skimage.measure import regionprops, moments, moments_central
from skimage.morphology import label from skimage.morphology import label
import numpy as np import numpy as np
from .extractor import Extractor from extractor import Extractor
import math
class RawCentralMoments(Extractor): class RawCentralMoments(Extractor):
"""Calculate raw and central set of image moments.""" """Calculate raw and central set of image moments."""
...@@ -29,41 +30,44 @@ class RawCentralMoments(Extractor): ...@@ -29,41 +30,44 @@ class RawCentralMoments(Extractor):
def run(self, image): def run(self, image):
"""Calculate raw and central set of image moments of order 1 and 2. """Calculate raw and central set of image moments of order 1 and 2.
Parameters Parameters
---------- ----------
image : opencv image image : opencv image
Image to be analyzed. image to be analyzed.
Returns Returns
------- -------
features : tuple features : tuple
Returns a tuple containing a list of labels, type and values for each feature extracted. Returns a tuple containing a list of labels, type and values for each feature extracted.
""" """
#raw_moments = moments(image) #raw_moments = moments(image)
image_binary = ImageUtils.image_binary(image, bgr = True)
image_binary = ImageUtils.image_binary(image, bgr = True)
m = measure.moments(image_binary)
m = measure.moments(image_binary) m=np.nan_to_num(m)
values_m = [m[p, q] for (p, q) in self._moments_order] 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)]
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] row = m[0, 1] / m[0, 0]
col = m[1, 0] / m[0, 0] col = m[1, 0] / m[0, 0]
mu = measure.moments_central(image_binary, center=(row, col), order=3) mu = measure.moments_central(image_binary, center=(row, col), order=3)
mu=np.nan_to_num(mu)
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)] values_mu = [mu[p, q] for (p, q) in self._moments_order]
#print(values_mu)
labels = labels_m + labels_mu
types = [Extractor.NUMERIC] * len(labels) labels_mu = [M+str(p)+str(q) for M,(p,q) in zip(['Mu_'] * len(self._moments_order), self._moments_order)]
values = values_m + values_mu
labels = labels_m + labels_mu
return labels, types, values types = [Extractor.NUMERIC] * len(labels)
values = values_m + values_mu
return labels, types, values
class HuMoments(Extractor): class HuMoments(Extractor):
...@@ -74,47 +78,44 @@ class HuMoments(Extractor): ...@@ -74,47 +78,44 @@ class HuMoments(Extractor):
def run(self, image): def run(self, image):
"""Calculate Hu's set set of image moments. """Calculate Hu's set set of image moments.
Parameters Parameters
---------- ----------
image : opencv image image : opencv image
Image to be analyzed. Image to be analyzed.
Returns
Returns -------
------- features : tuple
features : tuple Returns a tuple containing a list of labels, type and values for each feature extracted.
Returns a tuple containing a list of labels, type and values for each feature extracted. """
""" """image_binary = ImageUtils.image_binary(image, bgr = True)
"""image_binary = ImageUtils.image_binary(image, bgr = True) m = measure.moments(image_binary)
m = measure.moments(image_binary)
row = m[0, 1] / m[0, 0]
col = m[1, 0] / m[0, 0]
mu = measure.moments_central(image_binary, row, col) row = m[0, 1] / m[0, 0]
col = m[1, 0] / m[0, 0]
nu = measure.moments_normalized(mu)
hu = measure.moments_hu(nu)
values_hu = list(hu)""" mu = measure.moments_central(image_binary, row, col)
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) nu = measure.moments_normalized(mu)
hu = measure.moments_hu(nu)
values_hu= cv2.HuMoments(cv2.moments(image)).flatten() values_hu = list(hu)"""
values_hu= np.nan_to_num(values_hu) image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
values_hu = list(values_hu) values_hu= cv2.HuMoments(cv2.moments(image)).flatten()
labels_hu = [m+n for m,n in zip(['Hu_'] * len(values_hu),map(str,range(0,len(values_hu))))]
values_hu= np.nan_to_num(values_hu)
labels = labels_hu values_hu = list(values_hu)
types = [Extractor.NUMERIC] * len(labels) labels_hu = [m+n for m,n in zip(['Hu_'] * len(values_hu),map(str,range(0,len(values_hu))))]
values = values_hu
labels = labels_hu
types = [Extractor.NUMERIC] * len(labels)
values = values_hu
return labels, types, values return labels, types, values
...@@ -17,7 +17,7 @@ from interface.interface import InterfaceException as IException ...@@ -17,7 +17,7 @@ from interface.interface import InterfaceException as IException
from PIL import Image from PIL import Image
from pascal_voc_writer import Writer as wr from pascal_voc_writer import Writer as wr
import shutil import shutil
from tkinter import * #from tkinter import *
import segmentation import segmentation
import extraction import extraction
from extraction import FeatureExtractor from extraction import FeatureExtractor
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment