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

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 @@
<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" />
<option value="W29" />
<option value="E501" />
</list>
</option>
</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
matplotlib==2.0.2
scipy
......@@ -7,7 +7,7 @@ sympy
nose
networkx
scikit-image
javabridge==1.0.18
javabridge==1.0.18
python-weka-wrapper
cycler==0.10
cython
......@@ -20,3 +20,4 @@ opencv-contrib-python
python-interface
tqdm
sklearn
pyxdg
......@@ -4,13 +4,13 @@ try:
from .weka_classifiers import WekaClassifiers
except Exception as e:
WekaClassifiers = None
print e.message
print(e)
try:
from .cnn_caffe import CNNCaffe
except Exception as e:
CNNCaffe = None
print e.message
print(e)
try:
from .syntactic import Syntactic
......@@ -21,14 +21,14 @@ try:
from .cnn_keras import CNNKeras
except Exception as e:
CNNKeras = None
print e.message
print(e)
try:
from .segnet_keras import SEGNETKeras
except Exception as e:
SEGNETKeras = None
print e.message
print(e)
__all__ = ["classifier",
......
......@@ -40,6 +40,7 @@ from util.file_utils import File
from util.utils import TimeUtils
logger = logging.getLogger('PIL')
logger.setLevel(logging.WARNING)
......@@ -208,10 +209,10 @@ class CNNKeras(Classifier):
new_file = os.path.splitext(file)[0] + ".png"
img.save(File.make_path(predict_directory,
'png', new_file), "PNG", quality=100)
except Exception, e:
print e
except Exception as e:
print(e)
else:
print File.make_path(predict_directory, file)
print(File.make_path(predict_directory, file))
os.symlink(File.make_path(predict_directory, file),
File.make_path(predict_directory, 'png', file))
......@@ -219,7 +220,7 @@ class CNNKeras(Classifier):
classify_generator = classify_datagen.flow_from_directory(
File.make_path(predict_directory, 'png'),
taet_size=(IMG_HEIGHT, IMG_WIDTH),
target_size=(IMG_HEIGHT, IMG_WIDTH),
batch_size=1,
shuffle=False,
class_mode=None)
......@@ -232,8 +233,9 @@ class CNNKeras(Classifier):
self.model = load_model(self.weight_path)
path_classes = self.weight_path.replace(
"_model.h5", "_classes.npy")
print("Load Model H5:"+self.weight_path)
CLASS_NAMES = np.load(path_classes).item().keys()
except Exception, e:
except Exception as e:
raise IException("Can't load the model in " +
self.weight_path + str(e))
......@@ -479,9 +481,9 @@ class CNNKeras(Classifier):
img.save(File.make_path(dir_class_train,
new_file), "PNG", quality=100)
else:
print 100*'-'
print File.make_path(root, file)
print 100*'-'
print(100*'-')
print(File.make_path(root, file))
print(100*'-')
os.symlink(File.make_path(root, file),
File.make_path(dir_class_train, file))
......
......@@ -151,7 +151,7 @@ class SEGNETKeras(Classifier):
K.clear_session()
if self.weight_path is not None:
self.model = load_model(self.weight_path)
except Exception, e:
except Exception as e:
raise IException("Can't load the model in " +
self.weight_path + str(e))
......@@ -340,13 +340,13 @@ class SEGNETKeras(Classifier):
for file in files_test:
if os.path.splitext(file)[-1] == ".tif":
img = Image.open(File.make_path(dataset, file))
#img.thumbnail(img.size)
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 = Image.open(File.make_path(dataset, os.path.splitext(file)[0] + "_json", "label.png"))
#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 = Image.open(File.make_path(dataset, file))
#img.thumbnail(img.size)
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 = Image.open(File.make_path(dataset, os.path.splitext(file)[0] + "_json", "label.png"))
#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)
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, 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
from util.utils import ImageUtils
from extractor import Extractor
from .extractor import Extractor
class ColorStats(Extractor):
"""Implements color feature extraction."""
......
......@@ -12,13 +12,14 @@
"""
try:
# for Python2
import Tkinter
import Tkinter as Tk
import tkFileDialog
from Tkinter import *
except ImportError:
# for Python3
import tkinter
import filedialog as tkFileDialog
import tkinter as Tk
from tkinter import filedialog as tkFileDialog
from tkinter import *
import os
from os import listdir
from os.path import isfile, join
......@@ -26,8 +27,8 @@ import cv2
import threading
from Tkinter import *
from util.utils import TimeUtils
#
#from util.utils import TimeUtils
......
......@@ -11,7 +11,8 @@
import io
import itertools
import os
import multiprocessing
import gc
import multiprocessing
from multiprocessing import Process, Manager
from interface.interface import InterfaceException as IException
......@@ -19,13 +20,14 @@ from util.file_utils import File
from util.utils import ImageUtils
from util.utils import TimeUtils
import cv2
from extractor import Extractor
from .extractor import Extractor
from tqdm import tqdm
import sys
if not sys.warnoptions:
import warnings
warnings.simplefilter("ignore")
class FeatureExtractor(object):
"""Handle the feature extraction."""
......@@ -96,11 +98,14 @@ class FeatureExtractor(object):
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!")
print("Number of Cores: "+str(multiprocessing.cpu_count()))
#Start threads
with tqdm(total=len(self.threads)) as pbar:
for t in self.threads:
t.start()
if(len(multiprocessing.active_children())>10):
t.join()
gc.collect()
pbar.update(1)
pbar.close()
......@@ -108,6 +113,7 @@ class FeatureExtractor(object):
with tqdm(total=len(self.threads)) as ppbar:
for t in self.threads:
t.join()
del t
ppbar.update(1)
ppbar.close()
self.print_console("The process was completed with "+str(len(self.threads))+" images!")
......@@ -132,6 +138,7 @@ class FeatureExtractor(object):
continue
#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))
self.threads.append(th)
......@@ -146,11 +153,13 @@ class FeatureExtractor(object):
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]))
if sys.version_info >= (3, 0):
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]])
else:
labs, tys, values = [list(itertools.chain.from_iterable(ret))
for ret in
......@@ -158,7 +167,8 @@ class FeatureExtractor(object):
self.labels.append(labs)
self.types.append(tys)
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):
"""Runs the feature extraction algorithms on specific image.
......
......@@ -14,9 +14,10 @@
"""
import numpy as np
#import cupy as np
from util.utils import ImageUtils
from extractor import Extractor
from .extractor import Extractor
from scipy import ndimage as ndi
from skimage import feature
from skimage.filters import gabor_kernel
......
......@@ -14,7 +14,7 @@ from skimage import feature
from util.utils import ImageUtils
from extractor import Extractor
from .extractor import Extractor
class GLCM(Extractor):
"""Implements GLCM (Gray-Level Co-Occurrence Matrix) feature extraction."""
......
......@@ -18,7 +18,7 @@ from skimage import feature
from util.utils import ImageUtils
from extractor import Extractor
from .extractor import Extractor
class HOG(Extractor):
"""Implements HOG (Histogram of Oriented Gradient) feature extraction."""
......
......@@ -17,7 +17,7 @@ from util.utils import ImageUtils
from skimage.measure import regionprops, moments, moments_central
from skimage.morphology import label
import numpy as np
from extractor import Extractor
from .extractor import Extractor
class RawCentralMoments(Extractor):
"""Calculate raw and central set of image moments."""
......@@ -58,7 +58,7 @@ class RawCentralMoments(Extractor):
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)]
print(values_mu)
labels = labels_m + labels_mu
types = [Extractor.NUMERIC] * len(labels)
values = values_m + values_mu
......@@ -104,6 +104,7 @@ class HuMoments(Extractor):
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
values_hu= cv2.HuMoments(cv2.moments(image)).flatten()
values_hu = list(values_hu)
values_hu= np.nan_to_num(values_hu)
......
......@@ -3,7 +3,7 @@
https://github.com/accord-net/framework/blob/a5a2ea8b59173dd4e695da8017ba06bc45fc6b51/Sources/Accord.Math/Geometry/KCurvature.cs"""
import numpy as np
import math
from extractor import Extractor
from .extractor import Extractor
import sys
import cv2
import matplotlib.pyplot as plt
......@@ -65,7 +65,7 @@ class KCURVATURE(Extractor):
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):
v = np.median(image)
lower = int(max(0, (1.0 - sigma) * v))
......@@ -76,7 +76,7 @@ class KCURVATURE(Extractor):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
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,)
#kernel = np.ones((5,5),np.uint8)
......@@ -87,15 +87,21 @@ class KCURVATURE(Extractor):
#ret, thresh = cv2.threshold(np.copy(gray), 127, 255, 0)
edges = self.auto_canny(gray, 1)
img2,contours,hierarchy = cv2.findContours(edges ,cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
#print(contours)
list,image=self.find_angle(contours,img2)
if cv2.getVersionMajor() in [2, 4]:
# OpenCV 2, OpenCV 4 case
contour, hier = cv2.findContours(edges.copy(), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)
#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 = []
values = []
list_band=[]
for i in range(180/self.band):
for i in range(int(180/self.band)):
list_band.append(0)
for j,angle in enumerate(list):
......@@ -103,7 +109,7 @@ class KCURVATURE(Extractor):
#print(angle)
if(angle>=i*self.band and angle<((i+1)*self.band)):
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
for i, c in enumerate(list_band):
......
......@@ -14,7 +14,7 @@ from skimage import feature
from util.utils import ImageUtils
from extractor import Extractor
from .extractor import Extractor
class LBP(Extractor):
"""Implements LBP (Local Binary Patterns) feature extraction."""
......
......@@ -45,3 +45,4 @@ class InterfaceException(Exception):
return "Unexpected error:\n%s" % traceback.format_exc().replace('%', '%%')
else:
return "Unexpected error\n"
......@@ -16,9 +16,13 @@ else:
import matplotlib
matplotlib.use('TkAgg')
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib import pylab as plt
from matplotlib import pylab as plt
import sys
if sys.version_info[0] < 3:
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
else:
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
class Image(object):
"""Class to manipulate structured graphics interface."""
......@@ -86,7 +90,11 @@ class Image(object):
# a tk.DrawingArea
self._fig.tight_layout()
self._canvas = FigureCanvasTkAgg(self._fig, master=self.parent)
self._canvas.show()
if sys.version_info[0] < 3:
self._canvas.show()
else:
self._canvas.draw()
self._canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
if onclick is not None:
......@@ -146,10 +154,16 @@ class Image(object):
"""Show the matplotlib image toolbar.
"""
if self._toolbar is None and self._canvas is not None:
self._toolbar = NavigationToolbar2TkAgg(self._canvas, self.parent)
if sys.version_info[0] < 3:
self._toolbar = NavigationToolbar2TkAgg(self._canvas, self.parent)
else:
self._toolbar = NavigationToolbar2Tk(self._canvas, self.parent)
self._toolbar.configure(background='white', borderwidth=0)
for child in self._toolbar.winfo_children():
child.configure(background='white', foreground='black')
if sys.version_info[0] < 3:
child.configure(background='white', foreground='black')
else:
child.configure(background='white')
#self._toolbar.update()
self._toolbar.pack(side=Tk.TOP, fill=Tk.X, expand=False)
......
......@@ -16,7 +16,7 @@ else:
from collections import OrderedDict
from tk_config import TkConfig
from .tk_config import TkConfig
class SimpleDialog(Tk.Toplevel):
"""Basic Dialogue class."""
......
......@@ -14,7 +14,7 @@ if sys.version_info[0] < 3:
else:
import tkinter as Tk
from tk_utils import Utils
from .tk_utils import Utils
from util.x11_colors import X11Colors
class CustomGrid(Tk.Frame):
......
......@@ -14,7 +14,7 @@ if sys.version_info[0] < 3:
else:
import tkinter as Tk
from tk_itemmenu import *
from .tk_itemmenu import *
class Menu(object):
"""Class for GUI menus."""
......
......@@ -11,10 +11,12 @@
import sys
if sys.version_info[0] < 3:
import Tkinter as Tk
import thread
else:
import tkinter as Tk
import _thread as thread
import thread
class Popup(object):
"""Non-blocking popup class."""
......
......@@ -8,8 +8,18 @@
Author: Alessandro dos Santos Ferreira ( santosferreira.alessandro@gmail.com )
"""
import os
import tkMessageBox, Tkconstants, tkFileDialog
import tkColorChooser
import sys
if sys.version_info[0] < 3:
import tkMessageBox, Tkconstants, tkFileDialog
import tkColorChooser
else:
import tkinter.messagebox as tkMessageBox
import tkinter.constants as Tkconstants
from tkinter import filedialog as tkFileDialog
from tkinter import colorchooser as tkColorChooser
class Utils(object):
"""Utilities functionalities from Tkinter."""
......
......@@ -9,30 +9,52 @@
"""
import sys
try:
if sys.version_info < (3, 0):
# for Python2
import Tkinter as Tk
import tkMessageBox
import tk as tk_local
from interface import Interface, InterfaceException as IException
except ImportError:
else:
# for Python3
import tkinter as Tk
import tkinter.messagebox as tkMenssageBox
import tkinter as tk_local
import tkinter.messagebox as tkMessageBox
from .tk import tk_utils,tk_popup,tk_customframe,tk_canvas,tk_logger,tk_customdialog,tk_menu
class TkLocal:
def __init__(self,Utils, Menu,Image,Log,Pop, CustomGrid,SelectDialog,ChooseOneDialog,ConfigDialog):
self.Utils=Utils
self.Menu=Menu
self.Image=Image
self.Log=Log
self.Pop=Pop
self.CustomGrid=CustomGrid
self.SelectDialog=SelectDialog
self.ChooseOneDialog=ChooseOneDialog
self.ConfigDialog=ConfigDialog
tk_local=TkLocal
tk_local.Utils=tk_utils.Utils
tk_local.Menu=tk_menu.Menu
tk_local.Image=tk_canvas.Image
tk_local.Log=tk_logger.Log
tk_local.Pop=tk_popup.Popup
tk_local.CustomGrid=tk_customframe.CustomGrid
tk_local.SelectDialog=tk_customdialog.SelectDialog
tk_local.ChooseOneDialog=tk_customdialog.ChooseOneDialog
tk_local.ConfigDialog=tk_customdialog.ConfigDialog
from interface import Interface, InterfaceException as IException
class TkInterface(Interface):
"""Implements graphical interface and functionalities using Tkinter."""
MAX_CLASSES = 200
try:
if sys.version_info < (3, 0):
utils = tk_local.Utils
else:
utils = tk_local.Utils
except ImportError:
utils = tk_local.utils
def __init__(self, title):
"""Constructor.
......
......@@ -5,6 +5,7 @@
Name: pynovisao.py
Author: Alessandro dos Santos Ferreira ( santosferreira.alessandro@gmail.com )
"""
import sys
import gc
from collections import OrderedDict
import numpy as np
......@@ -181,6 +182,7 @@ class Act(object):
"""Open a new weight."""
self.weight_path = self.tk.utils.ask_weight_name()
self.classifier.weight_path = self.weight_path
print(self.weight_path)
def restore_image(self):
"""Refresh the image and clean the segmentation.
......@@ -1009,12 +1011,15 @@ class Act(object):
rmtree(matrix_path)
except Exception as e:
pass
mkdir(matrix_path, 0777)
mkdir(matrix_path,0o777)
for class_ in class_names:
real=matrix_path+human+class_+'/'
mkdir(real, 0777)
mkdir(real, 0o777)
for _class in class_names:
mkdir(real+computer+_class, 0777)
mkdir(real+computer+_class,0o777)
header_output = 'Starting Graphical Confusion Matrix\n\n'
......
......@@ -15,8 +15,8 @@ from collections import OrderedDict
from util.config import Config
from segmenter import Segmenter
from skimage_segmenter import SkimageSegmenter
from .segmenter import Segmenter
from .skimage_segmenter import SkimageSegmenter
class Felzenszwalb(Segmenter, SkimageSegmenter):
"""Run Felzenszwalb's method segmentation."""
......
......@@ -15,8 +15,8 @@ from collections import OrderedDict
from util.config import Config
from segmenter import Segmenter
from skimage_segmenter import SkimageSegmenter
from .segmenter import Segmenter
from .skimage_segmenter import SkimageSegmenter
class Quickshift(Segmenter, SkimageSegmenter):
"""Run Quickshift segmentation."""
......
......@@ -17,8 +17,8 @@ from util.config import Config
from util.utils import TimeUtils
from util.x11_colors import X11Colors
from segmenter import Segmenter
from skimage_segmenter import SkimageSegmenter
from .segmenter import Segmenter
from .skimage_segmenter import SkimageSegmenter
class Slic(Segmenter, SkimageSegmenter):
"""Run SLIC (Simple Linear Iterative Clustering) segmentation."""
......
......@@ -7,9 +7,8 @@
Name: config.py
Author: Alessandro dos Santos Ferreira ( santosferreira.alessandro@gmail.com )