Advertisement
11eimilia11

a

Feb 20th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 14.13 KB | None | 0 0
  1. import pandas as pd
  2. from numpy import ndarray
  3. from sklearn import preprocessing
  4. from sklearn.neighbors import KNeighborsClassifier
  5. from scipy import stats
  6. from sklearn.naive_bayes import GaussianNB
  7. import statistics
  8. from sklearn.metrics import pairwise_distances_argmin_min
  9. import numpy as np
  10. from random import randint
  11. import matplotlib.pyplot as plt
  12. import cv2
  13. from sklearn.decomposition import PCA
  14. import glob
  15. from sklearn.metrics import pairwise_distances_argmin_min
  16. from sklearn.metrics import accuracy_score
  17. from operator import itemgetter
  18. import math
  19. import os
  20.  
  21. # Carregando fotos da pasta
  22. def loadFiles(path, array):
  23.  
  24.     for i in glob.glob(path):
  25.  
  26.         img = cv2.imread(i)
  27.         array.append(img)
  28.  
  29.     return array
  30.  
  31. # Função que faz o filtro cinza nas fotos
  32. def grayConversion(arrayphotos):
  33.  
  34.     size = len(arrayphotos)
  35.     for x in range (0,size):
  36.         arrayphotos[x] = cv2.cvtColor(arrayphotos[x], cv2.COLOR_BGR2GRAY)
  37.  
  38.     return arrayphotos
  39.  
  40. # Função que aplic o filtro blur nas fotos do array
  41. def blurConversion(arrayphotos ,val1, val2):
  42.  
  43.     for x in range(len(arrayphotos)):
  44.         arrayphotos[x] = cv2.GaussianBlur(arrayphotos[x],(val1,val1), val2)
  45.  
  46.     return arrayphotos
  47.  
  48. #Função que faz a binarização das fotos
  49. def binaryConversion(arrayphotos,threshold,val1):
  50.  
  51.     for x in range(len(arrayphotos)):
  52.         arrayphotos[x] = cv2.adaptiveThreshold(arrayphotos[x],threshold,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,val1,10)
  53.  
  54.     return arrayphotos
  55.  
  56. #Função que inverte as fotos binárias
  57. def invertConversion(arrayphotos):
  58.  
  59.     for x in range(len(arrayphotos)):
  60.         arrayphotos[x] = cv2.bitwise_not(arrayphotos[x])
  61.  
  62.     return arrayphotos
  63.  
  64.  
  65. # função de normalização dos dados usando o modelo de
  66. # normalização por reescala
  67.  
  68. def normalizar(dados):
  69.     x = dados.values
  70.     min_max_scaler = preprocessing.MinMaxScaler()
  71.     x_scaled = min_max_scaler.fit_transform(x)
  72.     dados_norm = pd.DataFrame(x_scaled)
  73.     return dados_norm
  74.  
  75.  
  76. def geneticlabels(dataframe,centers):
  77.     return pairwise_distances_argmin_min(dataframe,centers,metric='minkowski')
  78.  
  79. def find_clusters(X, n_clusters, rng, max_it):
  80.  
  81.     i = rng.permutation(X.shape[0])[:n_clusters]
  82.     centers = X[i]
  83.  
  84.     max_iterator = 0
  85.     distances = []
  86.     while True:
  87.  
  88.         labels,distance = pairwise_distances_argmin_min(X,centers,metric='minkowski')
  89.         distances.append(distance)
  90.  
  91.         new_centers = np.array([X[labels == i].mean(0)
  92.                                 for i in range(n_clusters)])
  93.  
  94.  
  95.         if np.all(centers == new_centers) or max_iterator > max_it:
  96.             break
  97.         centers = new_centers
  98.         max_iterator += 1
  99.  
  100.     return centers, labels, distances
  101.  
  102. def accuracy_majority_vote(base, predict_labels, real_labels, n_clusters):
  103.  
  104.     classes = real_labels.unique()
  105.  
  106.     majority = []
  107.     groups = []
  108.     k = 0
  109.     for i in range(n_clusters):
  110.         group = []
  111.         for a in range(len(base)):
  112.             if predict_labels[a] == i:
  113.                 group.append(real_labels[a])
  114.         groups.append(group)
  115.         majority.append(initialize_dic_majority(classes))
  116.         for real_label in group:
  117.             majority[k][real_label] += 1
  118.         k += 1
  119.  
  120.     label_groups = []
  121.     for m in majority:
  122.         label_groups.append(max(m.items(), key=itemgetter(1))[0])
  123.  
  124.     pred_labels = []
  125.     true_labels = []
  126.     for g in range(len(groups)):
  127.         pred_labels = pred_labels + ([label_groups[g]]*len(groups[g]))
  128.         true_labels = true_labels + [a for a in groups[g]]
  129.  
  130.     return accuracy_score(pred_labels, true_labels)
  131.  
  132.  
  133. def find_clustersGENETIC(X, n_clusters, max_it, array):
  134.  
  135.     centers = array
  136.  
  137.     max_iterator = 0
  138.     distances = []
  139.     while True:
  140.  
  141.         labels,distance = pairwise_distances_argmin_min(X,centers,metric='minkowski')
  142.         distances.append(distance)
  143.  
  144.         new_centers = np.array([X[labels == i].mean(0)
  145.                                 for i in range(n_clusters)])
  146.  
  147.  
  148.         if np.all(centers == new_centers) or max_iterator > max_it:
  149.             break
  150.         centers = new_centers
  151.         max_iterator += 1
  152.  
  153.     return centers, labels, distances
  154.  
  155.  
  156. def WCSSgenetic(x, population):
  157.  
  158.     arrayint = []
  159.     for a in x:
  160.         arrayint.append(int(a))
  161.  
  162.     print(arrayint)
  163.     soma = 0
  164.     for b in arrayint:
  165.         labels, distances = pairwise_distances_argmin_min(population[b],population, metric='minkowski')
  166.         for x in distances:
  167.             soma += x**2
  168.     return soma
  169.  
  170.  
  171. def generatepopulation(X,numberpopu, K, rng):
  172.  
  173.     population = []
  174.  
  175.     for x in range(numberpopu):
  176.         first = rng.permutation(X.shape[0])[:K]
  177.         print(first)
  178.         population.append(np.concatenate(X[first]))
  179.  
  180.     return population
  181.  
  182.  
  183. def find_clustersgenetic(X, n_clusters, max_it, array):
  184.  
  185.     centers = array
  186.  
  187.     max_iterator = 0
  188.     distances = []
  189.     while True:
  190.  
  191.         labels,distance = pairwise_distances_argmin_min(X,centers,metric='minkowski')
  192.         distances.append(distance)
  193.  
  194.         new_centers = np.array([X[labels == i].mean(0)
  195.                                 for i in range(n_clusters)])
  196.  
  197.         if np.all(centers == new_centers) or max_iterator > max_it:
  198.             break
  199.         centers = new_centers
  200.         max_iterator += 1
  201.  
  202.     return centers, labels, distances
  203.  
  204.  
  205. #FUNÇÃO DE NORMALIZAÇÃO
  206.  
  207. def imgtoarray(arrayphotos):
  208.  
  209.     size = len(arrayphotos)
  210.     for x in range (0,size):
  211.         arrayphotos[x] = np.array(arrayphotos[x] , dtype=float)
  212.  
  213.     return arrayphotos
  214.  
  215. def gethumoments(arrayphotos):
  216.  
  217.     for x in range(len(arrayphotos)):
  218.  
  219.         arrayphotos[x] = cv2.HuMoments(cv2.moments(arrayphotos[x]), True).flatten()
  220.  
  221.     return arrayphotos
  222.  
  223. def WCSS2(distance):
  224.     soma = 0
  225.     for x in distance:
  226.         soma += x ** 2
  227.  
  228.     return soma
  229.  
  230.  
  231. def initialize_dic_majority(classes):
  232.     majority = {}
  233.     for c in classes:
  234.         majority[c] = 0
  235.  
  236.     return majority
  237.  
  238.  
  239. def extratorCaracteristicas(arrayimgs):
  240.  
  241.     squarescarac = []
  242.  
  243.  
  244.     for x in arrayimgs:
  245.  
  246.         aux = []
  247.  
  248.         im2, countours, hierachy = cv2.findContours(x, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
  249.  
  250.         if len(countours) == 0:
  251.  
  252.             for b in range(0,45):
  253.  
  254.                 aux.append(0)
  255.  
  256.         elif len(countours) > 0:
  257.  
  258.             momentum = cv2.moments(x)
  259.             momentum = list(dict.values(momentum))  # momentos
  260.             for i in momentum:
  261.                 aux.append(i)
  262.  
  263.             moments = cv2.HuMoments(momentum, True).flatten()  # Hu moments
  264.  
  265.             for i in moments:
  266.                 aux.append(i)
  267.  
  268.             area = cv2.contourArea(countours[0])  # area
  269.             aux.append(area)
  270.  
  271.             peri = cv2.arcLength(countours[0], True)  # perimetro
  272.             aux.append(peri)
  273.  
  274.             if peri > 0:
  275.                 compactness = (4 * math.pi * area) / (peri ** 2)  # compacidade
  276.                 aux.append(compactness)
  277.             elif peri == 0:
  278.                 aux.append(0)
  279.  
  280.             aproxx = cv2.approxPolyDP(countours[0], 0.04 * peri, True)  # vertices
  281.             vertc = len(aproxx)
  282.             aux.append(vertc)
  283.  
  284.             histogram = get_histogram(aproxx)  # Frequencia de angulos
  285.  
  286.             for h in histogram:
  287.                 aux.append(h)
  288.  
  289.         squarescarac.append(aux)
  290.  
  291.  
  292.     return squarescarac
  293.  
  294. def niveisCinza(image):
  295.  
  296.     gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
  297.  
  298.     return gray
  299.  
  300.  
  301. def filtroGaussiano(image):
  302.  
  303.     gaussianb = cv2.GaussianBlur(image,(5,5),0)
  304.  
  305.     return gaussianb
  306.  
  307. def turnToBinary(image):
  308.  
  309.     binary = cv2.adaptiveThreshold(image,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,31,10)
  310.  
  311.     return binary
  312.  
  313. def inverterCores(image):
  314.  
  315.     invert = cv2.bitwise_not(image)
  316.  
  317.     return invert
  318.  
  319. def resizeImages(images,width,height):
  320.  
  321.     i = len(images)
  322.     for x in range(0,i):
  323.  
  324.         images[x] = cv2.resize(images[x],(width,height))
  325.  
  326.     return images
  327.  
  328.  
  329. def angle3pt(a, b, c):
  330.     """Counterclockwise angle in degrees by turning from a to c around b
  331.        Returns a float between 0.0 and 360.0"""
  332.     ang = math.degrees(
  333.         math.atan2(c[1] - b[1], c[0] - b[0]) - math.atan2(a[1] - b[1], a[0] - b[0]))
  334.  
  335.     return ang + 360 if ang < 0 else ang
  336.  
  337. def get_vertices(imagem):
  338.  
  339.     im2, countours, hierachy = cv2.findContours(imagem, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
  340.  
  341.     peri = cv2.arcLength(countours[0], True)  # perimetro
  342.  
  343.     aproxx = cv2.approxPolyDP(countours[0], 0.04 * peri, True)  # vertices
  344.  
  345.     return aproxx
  346.  
  347. def get_angle(p0, p1, p2):
  348.  
  349.     v0 = np.array(p0) - np.array(p1)
  350.     v1 = np.array(p2) - np.array(p1)
  351.  
  352.     angle = np.math.atan2(np.linalg.det([v0, v1]), np.dot(v0, v1))
  353.     return np.degrees(angle)
  354.  
  355.  
  356. def get_histogram(aproxx):
  357.     zero = []
  358.     zero.append(0)
  359.     zero.append(0)
  360.     zero.append(0)
  361.     zero.append(0)
  362.     zero.append(0)
  363.     zero.append(0)
  364.     zero.append(0)
  365.     zero.append(0)
  366.     zero.append(0)
  367.     zero.append(0)
  368.     zero.append(0)
  369.     #transformando em um array bidimensional
  370.     retornopollyDP = np.reshape(aproxx, (aproxx.shape[0], (aproxx.shape[1] * aproxx.shape[2])))
  371.  
  372.     #checando se não é uma linha
  373.     if (len(retornopollyDP) < 3):
  374.  
  375.         return zero
  376.  
  377.     arraysdeangulo = []
  378.  
  379.     for x in range(len(retornopollyDP)):
  380.         # caso especial : quando a primeira posição do array for o angulo central
  381.         if x == 0:
  382.  
  383.             bla3 = get_angle(retornopollyDP[x + 1], retornopollyDP[x], retornopollyDP[len(retornopollyDP) - 1])
  384.             arraysdeangulo.append(math.fabs(bla3))
  385.  
  386.         # caso especial : quando a última posição do array for o ângulo central
  387.         if x == len(retornopollyDP) - 1:
  388.  
  389.             bla4 = get_angle(retornopollyDP[x - 1], retornopollyDP[x], retornopollyDP[0])
  390.             arraysdeangulo.append(math.fabs(bla4))
  391.  
  392.         if x > 0 and x < len(retornopollyDP) - 1:
  393.  
  394.             bla5 = get_angle(retornopollyDP[x + 1], retornopollyDP[x], retornopollyDP[x - 1])
  395.             arraysdeangulo.append(math.fabs(bla5))
  396.  
  397.     hist, bins = np.histogram(arraysdeangulo, bins=[15, 30, 45, 60, 75, 90, 105, 120, 135, 150, 165, 180])
  398.  
  399.     return hist
  400.  
  401.  
  402. # função que calcula os valores de média, moda , mediana e desvio padrão
  403. # para os vetores de acerto de um algoritmo, recebe como parâmetro o vetor
  404. # de acerto e o nome do algoritmo
  405.  
  406. def tendencia_central (nomeAlgoritmo, vetorAcerto, vetorTempo):
  407.     print('________________________________________________\n')
  408.     print(nomeAlgoritmo)
  409.     print('Tempo Média = ', np.mean(vetorTempo))
  410.     print('Tempo Desvio padrão = ', statistics.pstdev(vetorTempo))
  411.     print('Tempo Moda = ', stats.mode(vetorTempo, axis=None))
  412.     print('Tempo Mediana =', np.median(vetorTempo))
  413.     print('----------------------------------------------')
  414.     print('Acurácia Média = ', np.mean(vetorAcerto))
  415.     print('Acurácia Desvio padrão = ', statistics.pstdev(vetorAcerto))
  416.     print('Acurácia Moda = ', stats.mode(vetorAcerto, axis=None))
  417.     print('Acurácia Mediana = ', np.median(vetorAcerto))
  418.  
  419.     print('________________________________________________\n')
  420.  
  421. # funcão que seleciona num quantidade de imagens aleatoriamente
  422. # em um vetor e retorna um vetor auxiliar apenas com as imagens
  423. # selecionadas
  424.  
  425.  
  426. def seleciona_imagens (arrayImgs, num):
  427.     tamanho = len(arrayImgs)
  428.     selecionadas = []
  429.     nao_selecionadas = []
  430.     aux = []
  431.     var = 0
  432.  
  433.     for x in range(0, num):
  434.  
  435.         if x == 0:
  436.  
  437.             var = randint(0, tamanho - 1)
  438.             aux.append(var)
  439.             selecionadas.append(arrayImgs[var])
  440.  
  441.         elif x > 0:
  442.  
  443.             var = randint(0, tamanho - 1)
  444.  
  445.             while np.isin(var ,aux):
  446.  
  447.                 var = randint(0, tamanho - 1)
  448.  
  449.             selecionadas.append(arrayImgs[var])
  450.             aux.append(var)
  451.  
  452.     for x in range(0, tamanho):
  453.         if np.isin(x,aux):
  454.             continue
  455.         else:
  456.             nao_selecionadas.append(arrayImgs[x])
  457.  
  458.     return selecionadas, nao_selecionadas
  459.  
  460.  
  461. def is_similar(image1, image2):
  462.  
  463.     retorno = image1.shape == image2.shape and not(np.bitwise_xor(image1,image2).any())
  464.  
  465.     return retorno
  466.  
  467.  
  468. def create_folder(path):
  469.     x = 0
  470.     try:
  471.         os.mkdir(path)
  472.     except OSError:
  473.        x = -1
  474.     else:
  475.         x = 1
  476.     return x
  477.  
  478.  
  479. def save_images(images, path):
  480.     cont = 0
  481.  
  482.     for i in images:
  483.         y = str(cont)
  484.         cv2.imwrite(path + y + '.jpg', i)
  485.         cont += 1
  486.  
  487.     return 0
  488.  
  489. # checa se o elemento escolhido já existe no array aux para
  490. # evitar elementos duplicados
  491.  
  492. # var = randint(0, tamanho)
  493. #
  494. # for i in range(0, len(aux)):
  495. #     if is_similar(arrayImgs[var], aux[i]):
  496. #         duplicate = 1
  497. #
  498. # while duplicate == 1:
  499.  
  500.  
  501. def delete_folder (path):
  502.     x = 0
  503.     try:
  504.         os.rmdir(path)
  505.     except OSError:
  506.         x = -1
  507.     else:
  508.         x = 1
  509.     return x
  510.  
  511. def create_population(size_population, size_chromossome):
  512.     population = []
  513.  
  514.     for y in range(0, size_population):
  515.         aux = []
  516.         for i in range(0, size_chromossome):
  517.             x = randint(0, 200)
  518.             if x % 2 == 0:
  519.                 aux.append(0)
  520.             elif x % 2 != 0:
  521.                 aux.append(1)
  522.         population.append(aux)
  523.  
  524.     return population
  525.  
  526. def decode_chromossome(chromossome):
  527.     selected_carac = []
  528.     selected_carac = pd.DataFrame(selected_carac)
  529.     aux = []
  530.     images = []
  531.     if chromossome[0] == 0 and chromossome[1] == 0:
  532.         images = loadFiles('C:/Users/Auricelia/Desktop/DataSetsML/Images_16_s.csv',images)
  533.     elif chromossome[0] == 0 and chromossome[1] == 1:
  534.         images = loadFiles('C:/Users/Auricelia/Desktop/DataSetsML/Images_32_s.csv',images)
  535.     elif chromossome[0] == 1 and chromossome[1] == 0:
  536.         images = loadFiles('C:/Users/Auricelia/Desktop/DataSetsML/Images_64_s.csv',images)
  537.     elif chromossome[0] == 1 and chromossome[1] == 1:
  538.         images = loadFiles('C:/Users/Auricelia/Desktop/DataSetsML/Images_128_s.csv',images)
  539.  
  540.     for c in range(2,len(chromossome)):
  541.         if chromossome[c] == 1:
  542.             selected_carac[]
  543.         elif chromossome[c] == 0:
  544.             continue
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement