SHARE
TWEET

Untitled

a guest Oct 13th, 2019 89 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. library(pixmap)
  2. library(MASS)
  3.  
  4. #só pra printar
  5. library(lattice)
  6. x <- read.pnm(file = "data_atividade2/subject07.normal.pgm", cellres = 1)
  7. plot(x)
  8. polygon(c(45,145,145,45), c(20,20,200,200), border = "Red") #formato do rosto
  9.  
  10. teste <- (x@grey[40:200,40:150])
  11. levelplot(rotate(teste))
  12. rotate <- function(x) t(apply(x, 2, rev))
  13.  
  14.  
  15. data <- matrix(ncol=20691)
  16. for(i in grep("normal|happy|sad|sleepy|surprised|wink", list.files("data_atividade2/"), value = TRUE)){
  17.   path <- paste("data_atividade2", i, sep="/")
  18.   img <- read.pnm(file=path, cellres=1)
  19.   vetor_rosto <- as.numeric(img@grey[40:210, 40:160])
  20.   data <- rbind(data, vetor_rosto)
  21. }
  22. data <- data[-1,]
  23. data <- as.data.frame(data)
  24.  
  25. datainv<- data[,order(ncol(data):1)]
  26.  
  27. data <- as.data.frame(rbind(data,datainv))
  28. data$resp <- as.factor(rep(c("happy", "normal", "sad", "sleepy", "surprised","wink"), 30))
  29.  
  30. set.seed(188288)
  31. fracTraining   <- 0.80
  32. ssTraining   <- floor(fracTraining   * nrow(data))
  33. indexTraining    <- sort(sample(seq_len(nrow(data)), size=ssTraining))
  34. dataTr   <- data[indexTraining, ]
  35. dataTest <- data[-indexTraining, ]
  36.  
  37. #pca
  38. pc <- prcomp(dataTr[-20692], scale=TRUE, retx=FALSE)
  39. summary(pc)
  40. ##35 componentes > 95% variação
  41. ##48 com banco espelhado
  42.  
  43. cumVar <- cumsum(pc$sdev^2/sum(pc$sdev^2))
  44. nCompIdeal <<- max(2, which.max(cumVar >= 0.95))
  45.  
  46.  
  47. dataPcaTr <- as.data.frame(predict(pc, dataTr))[1:nCompIdeal]
  48. dataPcaTr$labels <- dataTr$resp
  49. dataPcaTest <- as.data.frame(predict(pc, dataTest))[1:nCompIdeal]
  50. dataPcaTest$labels <- dataTest$resp
  51.  
  52.  
  53. fitLDA <- lda(formula = labels ~ . , data=dataPcaTr)
  54. predLDA_Tr <- predict(fitLDA, dataPcaTr)
  55. (conf <- confusionMatrix(predLDA_Tr$class, dataPcaTr$labels))
  56. predLDA_Te <- predict(fitLDA, dataPcaTest)
  57. (conf <- confusionMatrix(predLDA_Te$class, dataPcaTest$labels))
  58.  
  59. x <- read.pnm(file = "data_atividade2/subjectmarlin.pgm", cellres = 1)
  60. teste <- as.data.frame(rbind(as.numeric(img@grey[40:210, 40:160])))
  61. testePca <- as.data.frame(predict(pc, teste))[1:nCompIdeal]
  62. predict(fitLDA, testePca)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top