Commit d101fffc authored by Gilberto Astolfi's avatar Gilberto Astolfi
Browse files

Merge branch 'desempenho_extrator_kcurvatura_e_extrator_de_frame'

parents 71a73cba 9249fb89
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="PyPep8Inspection" enabled="true" level="WEAK WARNING" enabled_by_default="true">
<option name="ignoredErrors">
<list>
<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" />
<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" />
<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>
</profile>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/pynovisao.iml" filepath="$PROJECT_DIR$/.idea/pynovisao.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="jdk" jdkName="Python 2.7 (pynovisao)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
<component name="TestRunnerService">
<option name="PROJECT_TEST_RUNNER" value="Unittests" />
</component>
</module>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
This diff is collapsed.
......@@ -17,7 +17,8 @@ fi
echo "[PYNOVISAO INSTALLER] Updating apt-get..."
sudo apt-get -qq update
echo "[PYNOVISAO INSTALLER] Installing prerequisites..."
sudo apt-get -qq -y install libfreetype6-dev tk tk-dev python-pip default-jre default-jdk weka weka-doc python-tk
sudo apt-get -qq -y install libfreetype6-dev tk tk-dev python-pip default-jre default-jdk weka weka-doc python-tk python-matplotlib python-opencv
echo "[PYNOVISAO INSTALLER] Upgrading pip..."
pip install --upgrade pip --quiet
......
opencv-python
python-dateutil==2.5.0
matplotlib==2.0.2
scipy
pandas
......@@ -8,7 +9,7 @@ networkx
scikit-image
javabridge
python-weka-wrapper
cycler
cycler==0.10
cython
h5py
scikit-learn
......@@ -17,3 +18,4 @@ statistics
pandas_ml
pyxdg
opencv-contrib-python
python-interface
......@@ -6,6 +6,8 @@ from .hog import HOG
from .image_moments import RawCentralMoments, HuMoments
from .lbp import LBP
from .gabor import GABOR
from .kcurvature import KCURVATURE
__all__ = ["extractor",
"color_summarizer",
......@@ -13,7 +15,8 @@ __all__ = ["extractor",
"hog",
"image_moments",
"lbp",
"gabor"]
"gabor",
"kcurvature"]
from collections import OrderedDict
......@@ -27,7 +30,8 @@ _extractor_list = OrderedDict( [
["hu_moments", Config("Hu Image Moments", True, bool, meta=HuMoments)],
["rc_moments", Config("Image Moments (Raw/Central)", True, bool, meta=RawCentralMoments)],
["lbp", Config("Local Binary Patterns", True, bool, meta=LBP)],
["gabor", Config("Gabor Filter Bank", True, bool, meta=GABOR)]
["gabor", Config("Gabor Filter Bank", True, bool, meta=GABOR)],
["kcurvature", Config("K-Curvature Angles", True, bool, meta=KCURVATURE)]
] )
def get_extractor_config():
......@@ -41,3 +45,4 @@ def set_extractor_config(configs):
_extractor_list["rc_moments"] = Config.nvl_config(configs["rc_moments"], _extractor_list["rc_moments"])
_extractor_list["lbp"] = Config.nvl_config(configs["lbp"], _extractor_list["lbp"])
_extractor_list["gabor"] = Config.nvl_config(configs["gabor"], _extractor_list["gabor"])
_extractor_list["kcurvature"] = Config.nvl_config(configs["kcurvature"], _extractor_list["kcurvature"])
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Extract frames from video
Selecter folder where stay videos, before select folder where extract frames.
Name: extractor_frame_video.py
Author: Diego Andre Sant Ana ( diego.santana@ifms.edu.br )
"""
try:
# for Python2
import Tkinter
import tkFileDialog
except ImportError:
# for Python3
import tkinter
import filedialog as tkFileDialog
import os
from os import listdir
from os.path import isfile, join
import cv2
import threading
from Tkinter import *
from util.utils import TimeUtils
class ExtractFM(object):
def __init__(self):
pass
#Method Run, you can passa tkParent to manipulate objet to tk parent, this code use to console to print process extract frame movies.
def run(self,tkParent):
self.tk=tkParent
self.folder_a=None
self.folder_b=None
#window
self.window = Tk()
self.window.mainloop()
self.window.title("Extract frames from videos")
self.window.attributes('-zoomed', False)
self.window.geometry("450x250+300+300")
self.frame = Frame(self.window )
self.frame.pack(fill=X, expand=False)
#find a folder with videos
self.folder_source = Button(self.frame, text="Select source folder", padx=5, pady=5, command = self.select_folder_source)
self.folder_source.pack(padx=5, pady=5, expand=True, fill=X)
#ToolTip(self.folder_source, "Select folder with videos from extract frames!")
self.label_a =Label(self.frame, text="Folder no selected", padx=5, pady=5)
self.label_a.pack(padx=5, pady=5, expand=True, fill=X)
self.folder_export = Button(self.frame, text="Select destination folder", padx=5, pady=5, command = self.select_folder_export)
self.folder_export.pack(padx=5, pady=5, expand=True, fill=X)
#ToolTip(self.folder_export , "Select folder to save frames!")
self.label_b =Label(self.frame, text="Folder no selected", padx=5, pady=5)
self.label_b.pack(padx=5, pady=5, expand=True, fill=X)
self.label_c =Label(self.frame, text="Enter in how many frames a frame will be extracted ", padx=5, pady=5)
self.label_c.pack(padx=5, pady=5, expand=True, fill=X)
#number de frame by extract one picture
self.val_frame=IntVar()
self.val_frame.set(1)
self.spinbox = Spinbox(self.frame, from_=1.0, to=100.0)
self.spinbox.configure(activebackground="#f9f9f9")
self.spinbox.configure(background="white")
self.spinbox.configure(buttonbackground="wheat")
self.spinbox.configure(disabledforeground="#b8a786")
self.spinbox.configure(from_="1.0")
self.spinbox.configure(highlightbackground="black")
self.spinbox.configure(selectbackground="#c4c4c4")
self.spinbox.configure(textvariable=self.val_frame)
self.spinbox.configure(to="100.0")
self.spinbox.pack(padx=5, pady=5, expand=True, fill=X)
#ToolTip(self.spinbox , "Select between 1 an 100 to extract frame each the number selected!Example: Select 30 to extract 1 frame each 30 fps!")
self.buttonOpen = Button(self.frame, text="Run Extract", padx=5, pady=5, command = self.export_frame)
self.buttonOpen.pack(padx=5, pady=5, expand=True, fill=X)
# Open the GUI
self.window.mainloop()
def extract_frame(self, file,source,n_frame):
newDir= self.folder_b+"/"+ file.split(".")[-2]
print(newDir)
print(source)
try:
os.mkdir(newDir)
print("Create Folder:"+newDir)
except OSError:
print("Folder exists:"+newDir)
cap = cv2.VideoCapture(source)
counter_frame = 0
last_extract=0
print(n_frame)
while(True):
print(counter_frame)
ret, img= cap.read()
if(ret == False):
return
counter_frame+=1
print(str(n_frame+last_extract==counter_frame))
if( n_frame+last_extract==counter_frame):
cv2.imwrite((newDir+"/"+str(counter_frame)+".png").encode('utf-8').strip(), img)
last_extract+=n_frame
cap.release()
def export_frame(self):
if self.folder_a is None:
return
if self.folder_b is None:
return
if self.val_frame is None:
return
if self.val_frame==0:
return
dir=self.folder_a
listaArquivo = [f for f in listdir(dir) if isfile(join(dir, f))]
self.tk.write_log("Init process:"+ str(listaArquivo))
lista_thread=[]
#start_time = TimeUtils.get_time()
for r, d, f in os.walk(dir):
for file in f:
ext=file.split(".")[-1]
if ext =="avi" or "mp4"==ext :
t=threading.Thread(target=self.extract_frame,args=(file, os.path.join(r, file),self.val_frame.get()))
t.start()
lista_thread.append(t)
self.tk.append_log("Threads running:"+str(threading.activeCount()))
for t in (lista_thread):
t.join()
#end_time = TimeUtils.get_time()
self.tk.append_log("Finish process:"+str(end_time - start_time))
def select_folder_source(self):
self.folder_a=None
options = {
'title': 'Auto select all videos from folder(AVI or MP4)',
'filetypes': (("File MP4", '*.mp4'), ('File AVI', '*.avi'))
}
filename = tkFileDialog.askdirectory()
if(filename != ''):
self.folder_a = filename
self.label_a.configure(text=self.folder_a )
self.window.update_idletasks()
def select_folder_export(self):
self.folder_b=None
options = {
'title': 'Select folder to export frames(PNG)',
'filetypes': (('File PNG', '*.png'))
}
filename = tkFileDialog.askdirectory()#askopenfilename(**options)
if(filename != ''):
self.folder_b = filename
self.label_b.configure(text=self.folder_b )
self.window.update_idletasks()
#tela=ExtractFM().run()
......@@ -11,31 +11,44 @@
import io
import itertools
import os
import threading
from interface.interface import InterfaceException as IException
from util.file_utils import File
from util.utils import ImageUtils
from util.utils import TimeUtils
import cv2
from extractor import Extractor
from tqdm import tqdm
import sys
class FeatureExtractor(object):
"""Handle the feature extraction."""
def __init__(self, extractors):
def __init__(self, extractors, tkParent=None):
"""Constructor.
Parameters
----------
extractor : list of Extractor
Initial set of active extractors.
"""
self.extractors = extractors
def extract_all(self, dataset, output_file = None, dirs = None, overwrite = True):
self.tkParent=tkParent
def extract_all(self, dataset, output_file=None, dirs=None, overwrite=True):
self.labels = []
self.types = []
self.data = []
self.threads = []
self.labels = []
self.types = []
"""Runs the feature extraction algorithms on all images of dataset.
Parameters
----------
dataset : string
......@@ -46,12 +59,12 @@ class FeatureExtractor(object):
List of directories to be serched. If not informed search in all directories with images inside dataset.
overwrite : boolean, optional, default = True
If False check if already exists a file containing the features.
Returns
-------
out : tuple
out : tuple
Returns a tuple containing the name of output file and time spent in milliseconds.
Raises
------
IException 'Please select at least one extractor'
......@@ -63,59 +76,90 @@ class FeatureExtractor(object):
"""
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 already exists a output file and must not override, return current file
if overwrite == False and os.path.isfile(output_file):
return output_file, 0
start_time = TimeUtils.get_time()
classes = sorted(File.list_dirs(dataset))
dirs = classes if dirs is None else dirs
data = []
# Runs the feature extraction for all classes inside the dataset
for cl in dirs:
items = sorted(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 cl in classes else classes[0]])
if len(data) == 0:
for cl in dirs:
# start job for each extractor
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!")
#Start threads
with tqdm(total=len(self.threads)) as pbar:
for t in self.threads:
t.start()
pbar.close()
self.print_console("Waiting for workers to finish extracting attributes from images!")
with tqdm(total=len(self.threads)) as ppbar:
for t in self.threads:
t.join()
ppbar.update(1)
ppbar.close()
self.print_console("The process was completed with "+str(len(self.threads))+" images!")
if len(self.data) == 0:
raise IException("There are no images in dataset: %s" % dataset)
# Save the output file in ARFF format
self._save_output(File.get_filename(dataset), classes, labels, types, data, output_file)
# self._save_output(File.get_filename(dataset), classes, self.labels, self.types, self.data, output_file)
self._save_output(File.get_filename(dataset), classes, self.labels, self.types, self.data, output_file)
end_time = TimeUtils.get_time()
return output_file, (end_time - start_time)
# create one thread for folder
def job_extractor(self, dataset, cl, classes):
items = sorted(os.listdir(File.make_path(dataset, cl)))
self.print_console("Processing class %s - %d itens" % (cl, len(items)))
def extract_one_file(self, dataset, image_path, output_file = None):
for item in items :
if item.startswith('.'):
continue
th = threading.Thread(target=self.sub_job_extractor,args=(item, dataset, cl, classes))
self.threads.append(th)
# create one thread each image for use extractor
def sub_job_extractor(self, item, dataset, cl, classes):
try:
filepath = File.make_path(dataset, cl, item)
image = cv2.imread(filepath)
#image = self.equalize_size_image(image)
except:
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]))
self.data.append(values + [cl if cl in classes else classes[0]])
else:
self.labels, self.types, values = [list(itertools.chain.from_iterable(ret))
for ret in
zip(*(extractor().run(image) for extractor in self.extractors))]
self.data.append(values + [cl if cl in classes else classes[0]])
def extract_one_file(self, dataset, image_path, output_file=None):
"""Runs the feature extraction algorithms on specific image.
Parameters
----------
dataset : string
......@@ -124,12 +168,12 @@ class FeatureExtractor(object):
Path to image.
output_file : string, optional, default = None
Name of output file continaing the features. If not informed is considered the name of dataset.
Returns
-------
out : tuple
out : tuple
Returns a tuple containing the name of output file and time spent in milliseconds.
Raises
------
IException 'Please select at least one extractor'
......@@ -139,30 +183,29 @@ class FeatureExtractor(object):
"""
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')
classes = sorted(File.list_dirs(dataset))
start_time = TimeUtils.get_time()
try:
image = File.open_image(image_path, rgb = False )
image = File.open_image(image_path, rgb=False)
except:
raise IException("Image %s is possibly corrupt" % filepath)
labels, types, values = [ list(itertools.chain.from_iterable(ret))
for ret in zip(*([extractor().run(image) for extractor in self.extractors])) ]
labels, types, values = [list(itertools.chain.from_iterable(ret))
for ret in zip(*([extractor().run(image) for extractor in self.extractors]))]
self._save_output(File.get_filename(dataset), classes, labels, types, [values + [classes[0]]], 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):
"""Save output file in ARFF format.
......@@ -181,21 +224,55 @@ class FeatureExtractor(object):
output_file : string
Path to output file.
"""
arff = open(output_file,'wb')
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)))