Commit 9f4009b4 authored by Diego André Sant'Ana's avatar Diego André Sant'Ana 🤞
Browse files

Alterou muita coisa para suportar o Python3- Correção bugs

parent c8a8ce6c
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding" addBOMForNewFiles="with NO BOM" />
</project>
\ No newline at end of file
...@@ -50,6 +50,20 @@ ...@@ -50,6 +50,20 @@
<option value="E501" /> <option value="E501" />
<option value="W29" /> <option value="W29" />
<option value="E501" /> <option value="E501" />
<option value="W29" />
<option value="E501" />
<option value="W29" />
<option value="E501" />
<option value="W29" />
<option value="E501" />
<option value="W29" />
<option value="E501" />
<option value="W29" />
<option value="E501" />
<option value="W29" />
<option value="E501" />
<option value="W29" />
<option value="E501" />
</list> </list>
</option> </option>
</inspection_tool> </inspection_tool>
......
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 2.7 (pynovisao)" project-jdk-type="Python SDK" />
</project>
\ No newline at end of file
This diff is collapsed.
opencv-python opencv-python==3.4.5.20
python-dateutil==2.5.0 python-dateutil==2.5.0
matplotlib==2.0.2 matplotlib==2.0.2
scipy scipy
...@@ -7,7 +7,7 @@ sympy ...@@ -7,7 +7,7 @@ sympy
nose nose
networkx networkx
scikit-image scikit-image
javabridge==1.0.18 javabridge==1.0.18
python-weka-wrapper python-weka-wrapper
cycler==0.10 cycler==0.10
cython cython
...@@ -20,3 +20,4 @@ opencv-contrib-python ...@@ -20,3 +20,4 @@ opencv-contrib-python
python-interface python-interface
tqdm tqdm
sklearn sklearn
pyxdg
...@@ -4,13 +4,13 @@ try: ...@@ -4,13 +4,13 @@ try:
from .weka_classifiers import WekaClassifiers from .weka_classifiers import WekaClassifiers
except Exception as e: except Exception as e:
WekaClassifiers = None WekaClassifiers = None
print e.message print(e)
try: try:
from .cnn_caffe import CNNCaffe from .cnn_caffe import CNNCaffe
except Exception as e: except Exception as e:
CNNCaffe = None CNNCaffe = None
print e.message print(e)
try: try:
from .syntactic import Syntactic from .syntactic import Syntactic
...@@ -21,14 +21,14 @@ try: ...@@ -21,14 +21,14 @@ try:
from .cnn_keras import CNNKeras from .cnn_keras import CNNKeras
except Exception as e: except Exception as e:
CNNKeras = None CNNKeras = None
print e.message print(e)
try: try:
from .segnet_keras import SEGNETKeras from .segnet_keras import SEGNETKeras
except Exception as e: except Exception as e:
SEGNETKeras = None SEGNETKeras = None
print e.message print(e)
__all__ = ["classifier", __all__ = ["classifier",
......
...@@ -40,6 +40,7 @@ from util.file_utils import File ...@@ -40,6 +40,7 @@ from util.file_utils import File
from util.utils import TimeUtils from util.utils import TimeUtils
logger = logging.getLogger('PIL') logger = logging.getLogger('PIL')
logger.setLevel(logging.WARNING) logger.setLevel(logging.WARNING)
...@@ -208,10 +209,10 @@ class CNNKeras(Classifier): ...@@ -208,10 +209,10 @@ class CNNKeras(Classifier):
new_file = os.path.splitext(file)[0] + ".png" new_file = os.path.splitext(file)[0] + ".png"
img.save(File.make_path(predict_directory, img.save(File.make_path(predict_directory,
'png', new_file), "PNG", quality=100) 'png', new_file), "PNG", quality=100)
except Exception, e: except Exception as e:
print e print(e)
else: else:
print File.make_path(predict_directory, file) print(File.make_path(predict_directory, file))
os.symlink(File.make_path(predict_directory, file), os.symlink(File.make_path(predict_directory, file),
File.make_path(predict_directory, 'png', file)) File.make_path(predict_directory, 'png', file))
...@@ -219,7 +220,7 @@ class CNNKeras(Classifier): ...@@ -219,7 +220,7 @@ class CNNKeras(Classifier):
classify_generator = classify_datagen.flow_from_directory( classify_generator = classify_datagen.flow_from_directory(
File.make_path(predict_directory, 'png'), File.make_path(predict_directory, 'png'),
taet_size=(IMG_HEIGHT, IMG_WIDTH), target_size=(IMG_HEIGHT, IMG_WIDTH),
batch_size=1, batch_size=1,
shuffle=False, shuffle=False,
class_mode=None) class_mode=None)
...@@ -232,8 +233,9 @@ class CNNKeras(Classifier): ...@@ -232,8 +233,9 @@ class CNNKeras(Classifier):
self.model = load_model(self.weight_path) self.model = load_model(self.weight_path)
path_classes = self.weight_path.replace( path_classes = self.weight_path.replace(
"_model.h5", "_classes.npy") "_model.h5", "_classes.npy")
print("Load Model H5:"+self.weight_path)
CLASS_NAMES = np.load(path_classes).item().keys() CLASS_NAMES = np.load(path_classes).item().keys()
except Exception, e: except Exception as e:
raise IException("Can't load the model in " + raise IException("Can't load the model in " +
self.weight_path + str(e)) self.weight_path + str(e))
...@@ -479,9 +481,9 @@ class CNNKeras(Classifier): ...@@ -479,9 +481,9 @@ class CNNKeras(Classifier):
img.save(File.make_path(dir_class_train, img.save(File.make_path(dir_class_train,
new_file), "PNG", quality=100) new_file), "PNG", quality=100)
else: else:
print 100*'-' print(100*'-')
print File.make_path(root, file) print(File.make_path(root, file))
print 100*'-' print(100*'-')
os.symlink(File.make_path(root, file), os.symlink(File.make_path(root, file),
File.make_path(dir_class_train, file)) File.make_path(dir_class_train, file))
......
...@@ -151,7 +151,7 @@ class SEGNETKeras(Classifier): ...@@ -151,7 +151,7 @@ class SEGNETKeras(Classifier):
K.clear_session() K.clear_session()
if self.weight_path is not None: if self.weight_path is not None:
self.model = load_model(self.weight_path) self.model = load_model(self.weight_path)
except Exception, e: except Exception as e:
raise IException("Can't load the model in " + raise IException("Can't load the model in " +
self.weight_path + str(e)) self.weight_path + str(e))
...@@ -340,13 +340,13 @@ class SEGNETKeras(Classifier): ...@@ -340,13 +340,13 @@ class SEGNETKeras(Classifier):
for file in files_test: for file in files_test:
if os.path.splitext(file)[-1] == ".tif": if os.path.splitext(file)[-1] == ".tif":
img = Image.open(File.make_path(dataset, file)) img = Image.open(File.make_path(dataset, file))
#img.thumbnail(img.size) #img.thumbnail(img.size)
new_file = os.path.splitext(file)[0]+".png" new_file = os.path.splitext(file)[0]+".png"
img.save(File.make_path(dataset, KERAS_DATASET_DIR_NAME, KERAS_DIR_TEST_NAME, 'images', new_file), "PNG", quality=100) img.save(File.make_path(dataset, KERAS_DATASET_DIR_NAME, KERAS_DIR_TEST_NAME, 'images', new_file), "PNG", quality=100)
img = Image.open(File.make_path(dataset, os.path.splitext(file)[0] + "_json", "label.png")) img = Image.open(File.make_path(dataset, os.path.splitext(file)[0] + "_json", "label.png"))
#img.thumbnail(img.size) #img.thumbnail(img.size)
img.save(File.make_path(dataset, KERAS_DATASET_DIR_NAME, KERAS_DIR_TEST_MASK_NAME, 'images', new_file), "PNG", quality=100) img.save(File.make_path(dataset, KERAS_DATASET_DIR_NAME, KERAS_DIR_TEST_MASK_NAME, 'images', new_file), "PNG", quality=100)
else: else:
os.symlink(File.make_path(dataset, file), File.make_path(dataset, KERAS_DATASET_DIR_NAME, KERAS_DIR_TEST_NAME, 'images', file)) os.symlink(File.make_path(dataset, file), File.make_path(dataset, KERAS_DATASET_DIR_NAME, KERAS_DIR_TEST_NAME, 'images', file))
os.symlink(File.make_path(dataset, os.path.splitext(file)[0] + "_json", "label.png"), File.make_path(dataset, KERAS_DATASET_DIR_NAME, KERAS_DIR_TEST_MASK_NAME, 'images', file)) os.symlink(File.make_path(dataset, os.path.splitext(file)[0] + "_json", "label.png"), File.make_path(dataset, KERAS_DATASET_DIR_NAME, KERAS_DIR_TEST_MASK_NAME, 'images', file))
......
...@@ -12,7 +12,7 @@ import numpy as np ...@@ -12,7 +12,7 @@ import numpy as np
from util.utils import ImageUtils from util.utils import ImageUtils
from extractor import Extractor from .extractor import Extractor
class ColorStats(Extractor): class ColorStats(Extractor):
"""Implements color feature extraction.""" """Implements color feature extraction."""
......
...@@ -12,13 +12,14 @@ ...@@ -12,13 +12,14 @@
""" """
try: try:
# for Python2 # for Python2
import Tkinter import Tkinter as Tk
import tkFileDialog import tkFileDialog
from Tkinter import *
except ImportError: except ImportError:
# for Python3 # for Python3
import tkinter import tkinter as Tk
import filedialog as tkFileDialog from tkinter import filedialog as tkFileDialog
from tkinter import *
import os import os
from os import listdir from os import listdir
from os.path import isfile, join from os.path import isfile, join
...@@ -26,8 +27,8 @@ import cv2 ...@@ -26,8 +27,8 @@ import cv2
import threading import threading
from Tkinter import * #
from util.utils import TimeUtils #from util.utils import TimeUtils
......
...@@ -11,7 +11,8 @@ ...@@ -11,7 +11,8 @@
import io import io
import itertools import itertools
import os import os
import multiprocessing import gc
import multiprocessing
from multiprocessing import Process, Manager from multiprocessing import Process, Manager
from interface.interface import InterfaceException as IException from interface.interface import InterfaceException as IException
...@@ -19,13 +20,14 @@ from util.file_utils import File ...@@ -19,13 +20,14 @@ from util.file_utils import File
from util.utils import ImageUtils from util.utils import ImageUtils
from util.utils import TimeUtils from util.utils import TimeUtils
import cv2 import cv2
from extractor import Extractor from .extractor import Extractor
from tqdm import tqdm from tqdm import tqdm
import sys import sys
if not sys.warnoptions: if not sys.warnoptions:
import warnings import warnings
warnings.simplefilter("ignore") warnings.simplefilter("ignore")
class FeatureExtractor(object): class FeatureExtractor(object):
"""Handle the feature extraction.""" """Handle the feature extraction."""
...@@ -96,11 +98,14 @@ class FeatureExtractor(object): ...@@ -96,11 +98,14 @@ class FeatureExtractor(object):
self.job_extractor(dataset, cl, classes) self.job_extractor(dataset, cl, classes)
self.print_console("Wait a moment, the threads are processing "+str(len(self.threads)) +" images, it may be delayed depending on the size or quantity of the images!") self.print_console("Wait a moment, the threads are processing "+str(len(self.threads)) +" images, it may be delayed depending on the size or quantity of the images!")
print("Number of Cores: "+str(multiprocessing.cpu_count()))
#Start threads #Start threads
with tqdm(total=len(self.threads)) as pbar: with tqdm(total=len(self.threads)) as pbar:
for t in self.threads: for t in self.threads:
t.start() t.start()
if(len(multiprocessing.active_children())>10):
t.join()
gc.collect()
pbar.update(1) pbar.update(1)
pbar.close() pbar.close()
...@@ -108,6 +113,7 @@ class FeatureExtractor(object): ...@@ -108,6 +113,7 @@ class FeatureExtractor(object):
with tqdm(total=len(self.threads)) as ppbar: with tqdm(total=len(self.threads)) as ppbar:
for t in self.threads: for t in self.threads:
t.join() t.join()
del t
ppbar.update(1) ppbar.update(1)
ppbar.close() ppbar.close()
self.print_console("The process was completed with "+str(len(self.threads))+" images!") self.print_console("The process was completed with "+str(len(self.threads))+" images!")
...@@ -132,6 +138,7 @@ class FeatureExtractor(object): ...@@ -132,6 +138,7 @@ class FeatureExtractor(object):
continue continue
#th = threading.Thread(target=self.sub_job_extractor,args=(item, dataset, cl, classes)) #th = threading.Thread(target=self.sub_job_extractor,args=(item, dataset, cl, classes))
th = multiprocessing.Process(target=self.sub_job_extractor,args=(item, dataset, cl, classes)) th = multiprocessing.Process(target=self.sub_job_extractor,args=(item, dataset, cl, classes))
self.threads.append(th) self.threads.append(th)
...@@ -146,11 +153,13 @@ class FeatureExtractor(object): ...@@ -146,11 +153,13 @@ class FeatureExtractor(object):
raise IException("Image %s is possibly corrupt" % filepath) raise IException("Image %s is possibly corrupt" % filepath)
if len(self.data) > 0: if len(self.data) > 0:
values = list( if sys.version_info >= (3, 0):
itertools.chain.from_iterable(zip(*([extractor().run(image) for extractor in self.extractors]))[2])) values = list(zip(*([extractor().run(image) for extractor in self.extractors])))
else:
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]]) self.data.append(values + [cl if cl in classes else classes[0]])
else: else:
labs, tys, values = [list(itertools.chain.from_iterable(ret)) labs, tys, values = [list(itertools.chain.from_iterable(ret))
for ret in for ret in
...@@ -158,7 +167,8 @@ class FeatureExtractor(object): ...@@ -158,7 +167,8 @@ class FeatureExtractor(object):
self.labels.append(labs) self.labels.append(labs)
self.types.append(tys) self.types.append(tys)
self.data.append(values + [cl if cl in classes else classes[0]]) self.data.append(values + [cl if cl in classes else classes[0]])
image=None
filepath=None
def extract_one_file(self, dataset, image_path, output_file=None): def extract_one_file(self, dataset, image_path, output_file=None):
"""Runs the feature extraction algorithms on specific image. """Runs the feature extraction algorithms on specific image.
......
...@@ -14,9 +14,10 @@ ...@@ -14,9 +14,10 @@
""" """
import numpy as np import numpy as np
#import cupy as np
from util.utils import ImageUtils from util.utils import ImageUtils
from extractor import Extractor from .extractor import Extractor
from scipy import ndimage as ndi from scipy import ndimage as ndi
from skimage import feature from skimage import feature
from skimage.filters import gabor_kernel from skimage.filters import gabor_kernel
......
...@@ -14,7 +14,7 @@ from skimage import feature ...@@ -14,7 +14,7 @@ from skimage import feature
from util.utils import ImageUtils from util.utils import ImageUtils
from extractor import Extractor from .extractor import Extractor
class GLCM(Extractor): class GLCM(Extractor):
"""Implements GLCM (Gray-Level Co-Occurrence Matrix) feature extraction.""" """Implements GLCM (Gray-Level Co-Occurrence Matrix) feature extraction."""
......
...@@ -18,7 +18,7 @@ from skimage import feature ...@@ -18,7 +18,7 @@ from skimage import feature
from util.utils import ImageUtils from util.utils import ImageUtils
from extractor import Extractor from .extractor import Extractor
class HOG(Extractor): class HOG(Extractor):
"""Implements HOG (Histogram of Oriented Gradient) feature extraction.""" """Implements HOG (Histogram of Oriented Gradient) feature extraction."""
......
...@@ -17,7 +17,7 @@ from util.utils import ImageUtils ...@@ -17,7 +17,7 @@ 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
class RawCentralMoments(Extractor): class RawCentralMoments(Extractor):
"""Calculate raw and central set of image moments.""" """Calculate raw and central set of image moments."""
...@@ -58,7 +58,7 @@ class RawCentralMoments(Extractor): ...@@ -58,7 +58,7 @@ class RawCentralMoments(Extractor):
values_mu = [mu[p, q] for (p, q) in self._moments_order] 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)] labels_mu = [M+str(p)+str(q) for M,(p,q) in zip(['Mu_'] * len(self._moments_order), self._moments_order)]
print(values_mu)
labels = labels_m + labels_mu labels = labels_m + labels_mu
types = [Extractor.NUMERIC] * len(labels) types = [Extractor.NUMERIC] * len(labels)
values = values_m + values_mu values = values_m + values_mu
...@@ -104,6 +104,7 @@ class HuMoments(Extractor): ...@@ -104,6 +104,7 @@ class HuMoments(Extractor):
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
values_hu= cv2.HuMoments(cv2.moments(image)).flatten() values_hu= cv2.HuMoments(cv2.moments(image)).flatten()
values_hu = list(values_hu) values_hu = list(values_hu)
values_hu= np.nan_to_num(values_hu) values_hu= np.nan_to_num(values_hu)
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
https://github.com/accord-net/framework/blob/a5a2ea8b59173dd4e695da8017ba06bc45fc6b51/Sources/Accord.Math/Geometry/KCurvature.cs""" https://github.com/accord-net/framework/blob/a5a2ea8b59173dd4e695da8017ba06bc45fc6b51/Sources/Accord.Math/Geometry/KCurvature.cs"""
import numpy as np import numpy as np
import math import math
from extractor import Extractor from .extractor import Extractor
import sys import sys
import cv2 import cv2
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
...@@ -65,7 +65,7 @@ class KCURVATURE(Extractor): ...@@ -65,7 +65,7 @@ class KCURVATURE(Extractor):
return list_angle,image return list_angle,image
""""https://dsp.stackexchange.com/questions/37281/improving-canny-edge-detection-and-contours-image-segmentation""" """"Source used https://dsp.stackexchange.com/questions/37281/improving-canny-edge-detection-and-contours-image-segmentation"""
def auto_canny(self,image, sigma=0.5): def auto_canny(self,image, sigma=0.5):
v = np.median(image) v = np.median(image)
lower = int(max(0, (1.0 - sigma) * v)) lower = int(max(0, (1.0 - sigma) * v))
...@@ -76,7 +76,7 @@ class KCURVATURE(Extractor): ...@@ -76,7 +76,7 @@ class KCURVATURE(Extractor):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.medianBlur(gray, 9) gray = cv2.medianBlur(gray, 9)
ret,gray = cv2.threshold( np.copy(gray),127,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C/cv2.THRESH_BINARY_INV) ret,gray = cv2.threshold( np.copy(gray),127,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C+cv2.THRESH_BINARY_INV)
#ret,gray = cv2.threshold( np.copy(gray),127,255,) #ret,gray = cv2.threshold( np.copy(gray),127,255,)
#kernel = np.ones((5,5),np.uint8) #kernel = np.ones((5,5),np.uint8)
...@@ -87,15 +87,21 @@ class KCURVATURE(Extractor): ...@@ -87,15 +87,21 @@ class KCURVATURE(Extractor):
#ret, thresh = cv2.threshold(np.copy(gray), 127, 255, 0) #ret, thresh = cv2.threshold(np.copy(gray), 127, 255, 0)
edges = self.auto_canny(gray, 1) edges = self.auto_canny(gray, 1)
if cv2.getVersionMajor() in [2, 4]:
img2,contours,hierarchy = cv2.findContours(edges ,cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # OpenCV 2, OpenCV 4 case
#print(contours) contour, hier = cv2.findContours(edges.copy(), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)
list,image=self.find_angle(contours,img2) #print(contours)
list,image=self.find_angle(contour,edges)
else:
# OpenCV 3 case
img2,contours,hierarchy = cv2.findContours(edges.copy() ,cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
#print(contours)
list,image=self.find_angle(contours,img2)
labels = [] labels = []
values = [] values = []
list_band=[] list_band=[]
for i in range(180/self.band): for i in range(int(180/self.band)):
list_band.append(0) list_band.append(0)
for j,angle in enumerate(list): for j,angle in enumerate(list):
...@@ -103,7 +109,7 @@ class KCURVATURE(Extractor): ...@@ -103,7 +109,7 @@ class KCURVATURE(Extractor):
#print(angle) #print(angle)
if(angle>=i*self.band and angle<((i+1)*self.band)): if(angle>=i*self.band and angle<((i+1)*self.band)):
list_band[i]=list_band[i]+1 list_band[i]=list_band[i]+1
elif (180==angle and 180/self.band==i): elif (180==angle and int(180/self.band)==i):
list_band[i]=list_band[i]+1 list_band[i]=list_band[i]+1
for i, c in enumerate(list_band): for i, c in enumerate(list_band):
......
...@@ -14,7 +14,7 @@ from skimage import feature ...@@ -14,7 +14,7 @@ from skimage import feature
from util.utils import ImageUtils from util.utils import ImageUtils
from extractor import Extractor