---
title: "Image training"
author: "Duncan Golicher"
date: "31/01/2020"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, message = FALSE,warning = FALSE)

```


```{r}
library(Rcpp)
cppFunction('void redir(){FILE* F=freopen("capture.txt","w+",stdout);}')
```


```{r}
#devtools::install_github("bnosac/image", subdir = "image.darknet", build_vignettes = TRUE)
```

```{r}
library(image.darknet)
yolo_tiny_voc <- image_darknet_model(type = 'detect', 
 model = 'tiny-yolo-voc.cfg', 
 weights = system.file(package='image.darknet', 'models', 'tiny-yolo-voc.weights'), 
 labels = system.file(package='image.darknet', 'include', 'darknet', 'data', 'voc.names'))



```





```{r}

path<-"/home/rstudio/webpages/examples/yolo/video"

a<-c(dir(path=path,pattern="png"),dir(path=path,pattern="jpg"))

f<-function(x){

  fl<-paste(path,x,sep="/")

  pred <- image_darknet_detect(file =fl, 
                          object = yolo_tiny_voc,
                          threshold = 0.19)
  system(sprintf("mv predictions.png pred/%s",x))
  pred
}

redir(); 
d<-lapply(a, f)

```

```{r}
library(dplyr)
library(tidyr)
d<-data.frame(txt=unlist(readLines("capture.txt")))
d %>% filter(!grepl("Boxes",txt))->d
d %>% filter(!grepl("pandoc",txt))->d
d %>% filter(!grepl("unnamed",txt))->d
d$isfile<-grepl("/home",d$txt)
d$txt<-gsub("/home/rstudio/webpages/examples/yolo/","",d$txt)
d$file<-ifelse(d$isfile,d$txt,NA)
d$object<-ifelse(!d$isfile,d$txt,NA)
tidyr::fill(d, "file") ->d
d<-na.omit(d)[,3:4]
d %>% separate(file, into=c("file","time"),sep=":")->d
d %>% separate(object, into=c("object","prob"),sep=":")->d
d %>% filter(!is.na(prob))->d
aqm::dt(d)
```

## `r a[[1]]`

![](`r sprintf("pred/%s",a[[1]])`)

# `r a[[2]]`

![](`r sprintf("pred/%s",a[[2]])`)

# `r a[[3]]`

![](`r sprintf("pred/%s",a[[3]])`)

# `r a[[4]]`

![](`r sprintf("pred/%s",a[[4]])`)


# `r a[[5]]`

![](`r sprintf("pred/%s",a[[5]])`)


# `r a[[6]]`

![](`r sprintf("pred/%s",a[[6]])`)


# `r a[[7]]`

![](`r sprintf("pred/%s",a[[7]])`)

# `r a[[8]]`

![](`r sprintf("pred/%s",a[[8]])`)




