-
Notifications
You must be signed in to change notification settings - Fork 1
/
trappeR.R
158 lines (100 loc) · 3.63 KB
/
trappeR.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# Camera TrappeR - Camera Trap Image Annotation Program for R
# Version 1.1
# Maxwell J. Farrell
# Works for Medium and High resolution photos from Bushnell Aggressor 2015 Model
rm(list=ls())
# Sourcing function library
source("trappeR_lib.R")
annotate <- function(){
############
# SET UP
############
# Choose folder with photos to work on
# Interactive folder choice varies with OS
if (Sys.info()[1]=="Windows"){
path <- choose.dir()
} else {
require(tcltk2)
path <- tk_choose.dir()
}
setwd(path)
# List files in photo directory
photo.list <- list.files(path, pattern="JPG$")
# if a previous csv is in the folder, start annotating from where session left off
if (any(grepl("csv", list.files(path)))){
csv <- list.files(path, pattern=".csv$")
annotated_photos <- read.csv(csv, as.is=TRUE)
last_photo <- tail(sort(annotated_photos$filename), 1)
index <- which(photo.list%in%last_photo)+1
} else {index <- 1}
# Pulling file path data:
folders <- strsplit(getwd(), "/")
nfolders <- length(folders[[1]])
site <- as.character(folders[[1]][nfolders-3])
date_range <- as.character(folders[[1]][nfolders-2])
camera <- as.character(folders[[1]][nfolders-1])
folder_num <- as.character(folders[[1]][nfolders])
#############################
# Annotate photos one by one
#############################
for (k in index:length(photo.list)){
photo <- photo.list[k]
print(paste("Photo",k, sep=" "))
# Plot photo
jpg <- readJPEG(photo, native=F)
plot_jpeg(jpg)
# get time & date from EXIF data
exif <- exifExtract(photo)
# Creating a data frame for meta data
meta <- data.frame( site=(site), date_range=(date_range),
camera=(camera), folder=(folder_num),
filename=(photo), date=(exif[1]),
time=(exif[2]), stringsAsFactors=FALSE)
# Data for Motion/Timelapse and Temperature Displayed in Photo
# Can automate detection if image is [2448 * 3264] OR [3312 * 4416]
if (dim(jpg)[1]==2448 | dim(jpg)[1]==3312){
motion <- MT_Recognition(jpg)
temp_F <- Temp_Recognition(jpg)
} else {
motion <- intMotion()
temp_F <- intTemp()
}
# How Many Species are there? (essentially how many rows will be in the dataframe)
n_species <- intSpecies()
n_speciesREAL <- n_species
if (n_species==0) n_species <- 1
# Creating a data frame for user input data
input <- data.frame(motion=integer(n_species), temp_F=integer(n_species),
species=character(n_species), n_present=integer(n_species),
n_waterhole=integer(n_species), n_contact=integer(n_species),
notes=character(n_species), stringsAsFactors=FALSE)
# Inputing Data for Motion/Timelapse and Temperature in data frame
input$motion <- motion
input$temp_F <- temp_F
# Populating the Data Frame with Species Data
if (n_speciesREAL==0){
input$species <- NA
input$n_present <- NA
input$n_waterhole <- NA
input$n_contact <- NA
} else {
for (x in 1:n_species){
input$species[x] <- nameSpecies(x)
input$n_present[x] <- intPresent(x, input)
input$n_waterhole[x] <- intWaterhole(x, input)
input$n_contact[x] <- intContact(x, input)
}
}
# Additional Notes
input$notes <- charNotes()
# Saving new data
new_annotation <- saveInput(meta, input, n_species)
if (exists("annotated_photos")) {
annotated_photos <- rbind(annotated_photos, new_annotation)
} else { annotated_photos <- new_annotation}
write.csv(annotated_photos, file=paste0(paste(site,date_range,camera,folder_num, sep="_"), ".csv"), row.names=FALSE)
# dev.off()
}
}
annotate()