View on GitHub

Classificador-de-Imagens-R

Basicamente um código usando Machine Learning para identificação e predição de imagens.

Classificador-de-Imagens-R

Basicamente um código usando Machine Learning para identificação e predição de imagens no R

images

Foram usadas apenas 8 imagens de bolinhos e 8 imagens de cachorros, o código importa as imagens (que no teste estavam 240x240).

library(imager)
Folder_CHR <- "G:/Photos/CHR"
files_CHR <- list.files(path = Folder_CHR, pattern = "*.jpg", full.names=TRUE)
all_im_CHR <- lapply(files_CHR, load.image )
plot(all_im_CHR[[1]])

library(imager)
Folder_BLH <- "G:/Photos/BLH"
files_BLH <- list.files(path = Folder_BLH, pattern = "*.jpg", full.names=TRUE)
all_im_BLH <- lapply(files_BLH, load.image )
plot(all_im_BLH[[1]])

Dá um down scale 90% (24x24) e um grayscale (deixa preto e branco).

for(i in 1:length(all_im_CHR)){
  all_im_CHR[[i]]=as.data.frame(grayscale(imresize(all_im_CHR[[i]],1/10)))
}
head(all_im_CHR[[1]])
plot(as.cimg(all_im_CHR[[1]]))

for(i in 1:length(all_im_BLH)){
  all_im_BLH[[i]]=as.data.frame(grayscale(imresize(all_im_BLH[[i]],1/10)))
}
head(all_im_BLH[[1]])
plot(as.cimg(all_im_BLH[[1]]))

E por fim usa Randon Florest para fazer o modelo de classificação.

sample <- createDataPartition(MIX$nome, p=0.85, list=FALSE)
MIX_train <- MIX[sample,]
str(MIX_train)
length(MIX_train[,1])
MIX_test <- MIX[-sample,]
str(MIX_test)
length(MIX_test[,1])
tune.grid <- expand.grid(mtry = 1:(ncol(MIX)-1))
fit.rf <- train(nome~., data=MIX_train, method="rf",tuneGrid =tune.grid,
                trControl=control)
MIX_prediction <- predict(fit.rf, MIX_test)
confusionMatrix(MIX_prediction, as.factor(MIX_test$nome))
          Reference
Prediction BLH CHR
       BLH 679 341
       CHR 401 739
                                         
               Accuracy : 0.6565         
                 95% CI : (0.636, 0.6765)
    No Information Rate : 0.5            
    P-Value [Acc > NIR] : < 2e-16  

Com o modelo pronto, ele consegue classificar se na imagem contem cachorro ou bolinho (mesmo para imagens fora do treinamento/teste) e diz uma porcentagem de acerto/erro.

TEST_IMG=load.image("G:/Photos/CHR.jpg")#ESCOLHA ESSA LINHA PARA TESTAR CACHORRO
MIX_prediction <- predict(fit.rf, TEST_IMG)
data.frame("CHR" = length(MIX_prediction[MIX_prediction=="CHR"])/length(MIX_prediction),"BLH" = length(MIX_prediction[MIX_prediction=="BLH"])/length(MIX_prediction))

        CHR       BLH
1 0.6188889 0.3811111
TEST_IMG=load.image("G:/Photos/BLH.jpg")#ESCOLHA ESSA LINHA PARA TESTAR BOLINHO
MIX_prediction <- predict(fit.rf, TEST_IMG)
data.frame("CHR" = length(MIX_prediction[MIX_prediction=="CHR"])/length(MIX_prediction),"BLH" = length(MIX_prediction[MIX_prediction=="BLH"])/length(MIX_prediction))

        CHR       BLH
1 0.3177778 0.6822222

As 16 imagens foram separadas para treino e teste do modelo e as 2 restantes são usadas para fazer o teste em tempo real. Devido ao número extremamente pequeno de imagens, as imagens de teste em tempo real foram escolhidas para melhor expressar os resultados do modelo.

O código pode ser usado para mais imagens e outros tipos de classificação (como humanos e não humanos, identificar pessoas diferentes, etc).