Commit f92e6f7f authored by João Porto's avatar João Porto
Browse files

implementacao de threads na geracao de anotacoes em xml

parent b7693df1
......@@ -22,3 +22,4 @@ tqdm
sklearn
pyxdg
pascal-voc-writer
psutil
......@@ -25,9 +25,9 @@ class Utils(object):
"""Utilities functionalities from Tkinter."""
image_dir = '../data/'
image_file = 'demo.jpg'
image_file = 'inovisao.png'
default_directory = '../data/demo'
default_directory = '../data/demo/images'
@staticmethod
def ask_image_name(title = 'Open a image'):
......
......@@ -38,6 +38,7 @@ import multiprocessing
from multiprocessing import Process, Manager
import threading as th
from tqdm import tqdm
import psutil
class Act(object):
"""Store all actions of Pynovisao."""
......@@ -764,9 +765,7 @@ class Act(object):
def about(self):
self.tk.show_info("Pynovisao\n\nVersion 1.0.0\n\nAuthors:\nAdair da Silva Oliveira Junior\nAlessandro dos Santos Ferreira\nDiego Andre Sant Ana\nDiogo Nunes Goncalves\nEverton Castelao Tetila\nFelipe Silveira\nGabriel Kirsten Menezes\nGilberto Astolfi\nHemerson Pistori\nNicolas Alessandro de Souza Belete")
self.tk.show_info("Pynovisao\n\nVersion 1.1.0\n\nAuthors:\nAdair da Silva Oliveira Junior\nAlessandro dos Santos Ferreira\nDiego Andre Sant Ana\nDiogo Nunes Goncalves\nEverton Castelao Tetila\nFelipe Silveira\nGabriel Kirsten Menezes\nGilberto Astolfi\nHemerson Pistori\nNicolas Alessandro de Souza Belete\nFabio Prestes Cesar Rezende\nJoao Vitor de Andrade Porto")
def func_not_available(self):
"""Use this method to bind menu options not available."""
self.tk.write_log("This functionality is not available right now.")
......@@ -1095,95 +1094,134 @@ class Act(object):
shutil.move(os.getcwd()+"/{}.xml".format(self._image_name),current_path+"/{}.xml".format(self._image_name))
def run_xml_converter(self):
self.tk.write_log("Started mass annotation process, it may take a while...")
def moveXML(file): #Move the xml files into the 'data/XML' folder
'''
File: string
Name of the original image.
'''
current_path=os.getcwd()
current_path=current_path[-1::-1]
current_path=current_path[3::1]
current_path=current_path[-1::-1]
current_path=current_path+"data/XML"
if(not os.path.exists(current_path)): #Should data/XML not exist, create it
os.makedirs(current_path)
shutil.move(os.getcwd()+"/{}.xml".format(file),current_path+"/{}.xml".format(file))
def arrasteSeg(imgog, imgseg, LinhaOg, ColunaOg, LinhaSeg, ColunaSeg):
#arraste do superpixel sobre a imagem original
'''
imgog: numpy array
Array of the original image.
imseg: numpy array
Array of the segment being searched for.
LinhaOg: int
Current position in the imgog (X axis).
LinhaSeg: int
Current position in the imseg (X axis).
ColunaOg: int
Current position in the imgog (Y axis).
ColunaSeg: int
Current position in the imseg (Y axis).
'''
#Drags the segment over the original image
l = 0
while(l <= (LinhaOg - LinhaSeg)):
c = 0
while(c <= (ColunaOg - ColunaSeg)):
i = 0
igual = True
#verificacao pixel a pixel de igualdade entre superpixel e imagem original
#Verifies the equality between the segment and the current area of the original image
while(igual and i < LinhaSeg):
j = 0
while(igual and j < ColunaSeg):
if(imgog[l+i,c+j,0] != imgseg[i,j,0] or imgog[l+i,c+j,1] != imgseg[i,j,1] or imgog[l+i,c+j,2] != imgseg[i,j,2]): ##(-i)
if(imgseg[i,j,0] != 0 and imgseg[i,j,1] != 0 and imgseg[i,j,2] != 0): #verificacao de nao igualdade para pixels nao irrelevantes
igual = False
if(imgog[l+i,c+j,0] != imgseg[i,j,0] or imgog[l+i,c+j,1] != imgseg[i,j,1] or imgog[l+i,c+j,2] != imgseg[i,j,2]):
if(imgseg[i,j,0] != 0 and imgseg[i,j,1] != 0 and imgseg[i,j,2] != 0): #Verification of black/transparent pixels
igual = False #Found the segment's position, saves it's coordinates and gets out of the loops.
i = LinhaOg+1
j = i
j = j + 5
j = j + 5 #Segment still in comparision, compares every 5 pixels with the original image (20% similarity)
i = i + 5
if(igual):
return l, c, i, j, igual
c = c + 1
c = c + 1 #Segment not found, search the next pixel.
l = l + 1
def achaImg(pathS, pathIO):
def achaSeg(self, pathS, file, pog, arquivos):
'''
pathS: string
Filepath to the segments folder.
file: string
Name of the original image.
Pog: string
Filepath to original image (file).
arquivos: string array
Segments alredy found in a image.
'''
#Reads all segment files in the given folder (pathS)
tottime = 0
arquivos = []
#verificar arquivos em todas as pastas partindo de um diretório raiz
for pog, _, files in os.walk(os.path.abspath(pathIO)):
if (not pog.endswith('demo') and not pog.endswith('XML')):
#acha os arquivos de imagem na pasta de segmentos
for file in files:
if(file.endswith('.jpeg') or file.endswith('.tif') or file.endswith('.jpg') or file.endswith('.png')):##(-f)
#leitura dos arquivos de imagem na pasta original
arquivo=os.path.join(pog, file)
imgog=cv2.imread(arquivo,3)
self.tk.add_image(imgog, file)
imgog=cv2.imread(arquivo,-1)
LinhaOg,ColunaOg,_=imgog.shape
# Writer(path, width, height) para iniciar o geramento do xml
self.tk.write_log(" Creating XML file for image %s", file)
# Writer(path, width, height) iniciates the creation of the xml file
writer = wr(pog, LinhaOg, ColunaOg)
for pseg, _,filesIO in os.walk(os.path.abspath(pathS)):
pasta=pseg#pega o nome da pasta (classe do segmento)
pasta=pseg#Obtains the name of the folder (segment's class)
pasta = pasta[-1::-1]
pasta = pasta[:pasta.index('/')]
pasta = pasta[-1::-1]
for fileIO in filesIO:
if(fileIO.endswith('.jpeg') or fileIO.endswith('.tif') or fileIO.endswith('.jpg') or fileIO.endswith('.png')):##(-f)
if (fileIO.startswith(file)): #verifica se eh o segmento desta imagem ou nao.
#leitura dos segmentos em seus diretorios
if(fileIO.endswith('.jpeg') or fileIO.endswith('.tif') or fileIO.endswith('.jpg') or fileIO.endswith('.png')):
if (fileIO.startswith(file)): #Verifies if the segment belongs to the original image 'file' according to Pynovisao standards.
#Reads the segment files using cv2, one by one.
arquivo=os.path.join(pseg,fileIO)
imgseg=cv2.imread(arquivo,3)
imgseg=cv2.imread(arquivo,-1)
LinhaSeg,ColunaSeg,_=imgseg.shape
l=0
#arraste do superpixel sobre a imagem original
#Drags the segment over file.
starttime = time.time()
l,c,i,j,igual=arrasteSeg(imgog, imgseg, LinhaOg, ColunaOg, LinhaSeg, ColunaSeg)
if(igual): #Se o segmento esta igual a sua posicao no original
if(igual): #Should the segment find itself over it's original position in the image.
igual=False
#tudo isso pra terminar o loop desse segmento
if not(fileIO in arquivos): #Se o segmento atual ja foi visto
#Changes the boolean's value in order to stop the current search for this segment.
if not(fileIO in arquivos): #If the segment hasn't been found previously.
# ::addObject(name, xmin, ymin, xmax, ymax)
self.tk.append_log("\n Annotation of %s found", fileIO)
tottime = tottime + time.time() - starttime
arquivos.append(fileIO) #Adiciona nos segmentos ja vistos
writer.addObject(pasta, c, l, c + ColunaSeg, l + LinhaSeg) #adiciona o objeto no xml
arquivos.append(fileIO) #Adds the current segment to the list of found segments.
writer.addObject(pasta, c, l, c + ColunaSeg, l + LinhaSeg) #Adds xml object.
l=LinhaOg+1
c=ColunaOg+1
#save(path) termina o salvamento do xml
self.tk.append_log("\n Saving .XML")
#save(path) finishes the process of xml creation for this image/file.
self.tk.append_log("\n Saving .XML of {}".format(file))
self.tk.append_log(" Time elapsed: %0.3f seconds", tottime)
writer.save('{}.{}'.format(file,'xml'))
#gera o xml com as medidas
current_path=os.getcwd()
current_path=current_path[-1::-1]
current_path=current_path[3::1]
current_path=current_path[-1::-1]
current_path=current_path+"data/XML"
if(not os.path.exists(current_path)):
os.makedirs(current_path)
shutil.move(os.getcwd()+"/{}.xml".format(file),current_path+"/{}.xml".format(file))
img = np.zeros([100,100,3],dtype=np.uint8)
img.fill(255)
self.tk.add_image(img, "end")
self.tk.write_log("Process finished")
self.tk.write_log("Started mass annotation process, it may take a while...")
achaImg(self._seg_folder,self._img_folder)
moveXML(file)
def config_xml(self):
def achaImg(self, pathS, pathIO):
'''
pathS: string
Filepath to the segments folder.
pathIO: string
Filepath to the Original Images folder.
'''
arquivos = [] #Segments alredy found, starts empty
thread = [] #Threads created, starts empty.
#Verifies all original images (files) under the given folder (pathIO)
for pog, _, files in os.walk(os.path.abspath(pathIO)):
#Creates threads with one file each.
for file in files:
if(file.endswith('.jpeg') or file.endswith('.tif') or file.endswith('.jpg') or file.endswith('.png')):##(-f)
t=th.Thread(target = achaSeg, args=(self, pathS, file, pog, arquivos))
thread.append(t)
for td in thread:
#Starts thread, and halts further thread iniciation until a safe RAM level is achieved.
ramused=psutil.virtual_memory()[1]/1000000000
td.start()
if(ramused <= 1.5): #Joins current threads should current free RAM levels be under 1.5Gb.
td.join()
achaImg(self, self._seg_folder,self._img_folder) #Starts the process of identification of segments in files
def config_xml(self): #Configures the directories for Segments and Original Images.
config_xml = Toplevel()
current_path=os.getcwd()
current_path=current_path[-1::-1]
......
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