Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library(stats19)
- library(tidyverse)
- library(MASS)
- library(pixmap)
- calibra <- function(dataset = grep("normal|happy|sad|sleepy|surprised|wink",
- list.files("data_atividade2/"), value=TRUE)) {
- set.seed(202227)
- data <- matrix(ncol = 20691, nrow = 90)
- c <- 1
- for(i in dataset){#monta matriz do poligono geral de um rosto das pessoas
- path <- paste("data_atividade2", i, sep="/")
- img <- read.pnm(file=path, cellres=1)
- vetor_rosto <- as.numeric(img@grey[40:210, 40:160])
- data[c,] <- vetor_rosto
- c <- c + 1
- }
- data <- as.data.frame(data)
- data$resp <- as.factor(rep(c("happy", "normal", "sad", "sleepy", "surprised","wink"), 15))
- id <- c(1:nrow(data))
- id_train <- sample(id, 0.8*nrow(data))
- train <- data %>% slice(id_train)
- test <- data %>% slice(-id_train)
- pc <- prcomp(train[-20692], scale=TRUE, retx=FALSE)
- train.pc <- as.data.frame(predict(pc, train))
- test.pc <- as.data.frame(predict(pc, test))
- var.pc.prop <- (pc$sdev^2)/sum(pc$sdev^2)
- cum.var <- cumsum(var.pc.prop)
- ncomp <- which.max(cum.var > 0.95)
- train.pc <- train.pc[1:ncomp]
- train.pc$resp <- train$resp
- test.pc <- test.pc[1:ncomp]
- test.pc$resp <- test$resp
- lda.fit <- lda(resp ~ ., data = train.pc)
- final <- list(lda.fit, pc, ncomp)
- return(final)
- }
- prediz <- function(modelo=calibra()[[1]], teste){
- img <- matrix(ncol=20691)
- vetor_rosto <- as.numeric(teste[40:210, 40:160])
- img <- rbind(img, vetor_rosto) %>% as.data.frame()
- img <- img[2,]
- teste_proc <- as.data.frame(predict(calibra()[[2]], img))
- teste_proc <- teste_proc[1:calibra()[[3]]]
- teste_pred <- predict(modelo, teste_proc)
- return(teste_pred$class)
- #teste.pc <- as.data.frame(predict(calibra()[[2]], teste))
- #teste.pc <- teste.pc[1:calibra()[[3]]]
- #teste.pred <- predict(modelo, teste.pc)
- #return(teste.pred$class)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement