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 @@
<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
......@@ -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))
......
......@@ -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,6 +11,7 @@
import io
import itertools
import os
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,8 +153,10 @@ 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]])
......@@ -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)
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)
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:
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():
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."""
......
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