-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper_functions.R
140 lines (106 loc) · 3.39 KB
/
helper_functions.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
## Helper functions for image processing
# Image cropping function for 2x4 scanning grid
x_inc <- 753
y_inc <- 1050
num_rows <- 2
num_cols <- 4
num_cards <- num_rows*num_cols
y_positions <- c(rep(0, num_cols), rep(y_inc, num_cols))
cols <- seq(0, num_cols -1, by = 1)
locations <- tibble(x_inc = rep(x_inc, num_cards), y_inc = rep(y_inc, num_cards),
cols = rep(cols, num_rows), y_pos = y_positions)
crop_locations <- locations %>%
mutate(x_pos = x_inc*cols) %>%
str_glue_data("{x_inc}x{y_inc}+{x_pos}+{y_pos}")
crop <- function(file, crop_locations) {
map2(file, crop_locations, ~ image_crop(image_read(here::here("Scans", "Grids", "UpperDeck_1998", .x)), .y))
}
x_inc <- 176
y_inc <- 248
num_rows <- 2
num_cols <- 4
num_cards <- num_rows*num_cols
y_positions <- c(rep(0, num_cols), rep(y_inc, num_cols))
cols <- seq(0, num_cols -1, by = 1)
locations <- tibble(x_inc = rep(x_inc, num_cards), y_inc = rep(y_inc, num_cards),
cols = rep(cols, num_rows), y_pos = y_positions)
crop_locations <- locations %>%
mutate(x_pos = x_inc*cols) %>%
str_glue_data("{x_inc}x{y_inc}+{x_pos}+{y_pos}")
front_crop_smaller <- function(file, crop_locations) {
map2(file, crop_locations, ~ image_crop(image_read(here::here("Scans", "Grids", "UpperDeck_1998", "Smaller", .x)), .y))
}
#
get_table <- function(image) {
image %>%
image_scale("x2000") %>%
image_rotate(90) %>%
image_deskew() %>%
image_contrast() %>%
image_write(format = 'png', density = '300x300') %>%
tesseract::ocr(engine = eng_table) %>%
str_split("\n") %>%
as.data.frame(.) %>%
rename(info = 1)
}
get_description <- function(image) {
image %>%
image_scale("x2000") %>%
image_rotate(90) %>%
image_deskew() %>%
image_contrast() %>%
image_write(format = 'png', density = '300x300') %>%
tesseract::ocr(engine = eng_description) %>%
str_split("\n") %>%
as.data.frame(.) %>%
rename(info = 1)
}
get_metdata <- function(image) {
image %>%
image_rotate(90) %>%
image_crop("1050x150+0+0") %>%
#image_scale("x2000") %>%
image_deskew() %>%
image_contrast() %>%
image_negate() %>%
image_contrast() %>%
image_modulate(brightness = 120) %>%
#image_threshold("white", "20%")
image_write(format = 'png', density = '300x300') %>%
tesseract::ocr(engine = eng_description) %>%
str_split("\n") %>%
as.data.frame(.) %>%
rename(info = 1)
}
get_name <- function(image) {
image %>%
image_rotate(90) %>%
image_crop("1050x105+0+0") %>%
image_negate() %>%
image_threshold(type = "white", "10%") %>%
image_reducenoise() %>%
image_write(format = 'png', density = '300x300') %>%
tesseract::ocr(engine = eng_name)
}
get_position <- function(image) {
image_rotate(90) %>%
image_crop("1050x105+0+0") %>%
image_threshold(type = "white", "30%") %>%
image_write(format = 'png', density = '300x300') %>%
tesseract::ocr()
}
get_player_char <- function(image) {
image_rotate(90) %>%
image_crop("1050x105+0+0") %>%
image_threshold(type = "white", "30%") %>%
image_write(format = 'png', density = '300x300') %>%
tesseract::ocr()
}
get_position <- function(image) {
image %>%
image_rotate(90) %>%
image_crop("1050x140+0+0") %>%
image_threshold(type = "white", "30%") %>%
image_reducenoise() %>%
tesseract::ocr()
}