Last active
December 23, 2021 08:11
-
-
Save k12ish/585efefd50bda511d0509e55c20a3cb7 to your computer and use it in GitHub Desktop.
Select images from slideshow with maximum amount of text.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### A Pluto.jl notebook ### | |
# v0.12.18 | |
using Markdown | |
using InteractiveUtils | |
# ╔═╡ 04587fae-597f-11eb-03db-41e2e288cabc | |
using VideoIO, ImageFiltering, Images, Plots | |
# ╔═╡ a522894e-59b5-11eb-147f-71052ad6df32 | |
DIRECTORY = "/path/to/videos" | |
# ╔═╡ 92e33fe8-5975-11eb-041f-a3a5ca27edf9 | |
function get_file_list(directory) | |
list = Tuple{String, String}[] | |
for (root, dirs, files) in walkdir(directory) | |
for file in files | |
push!(list, (root, file)) | |
end | |
end | |
return list | |
end | |
# ╔═╡ d9cab476-59b5-11eb-0476-57ccece8c363 | |
FILE_LIST = get_file_list(DIRECTORY) | |
# ╔═╡ fa4beaca-5986-11eb-0621-1b06369dad7a | |
function value_of(img) | |
return sum(abs.(imfilter(Gray.(img), Kernel.sobel()))) | |
end | |
# ╔═╡ ede92886-597e-11eb-098f-e94e01d00dfa | |
function process_video(folder_and_name::Tuple{String, String}) | |
best_images = [] | |
filename = joinpath(folder_and_name...) | |
vid = VideoIO.openvideo(filename) | |
prev_img = VideoIO.read(vid) | |
prev_value = value_of(prev_img) | |
while !eof(vid) | |
img = VideoIO.read(vid) | |
value = value_of(img) | |
if value < prev_value * 0.99 | |
push!(best_images, prev_img) | |
end | |
prev_img, prev_value = img, value | |
skipframes(vid, 60, throwEOF=false) | |
end | |
path = joinpath(DIRECTORY, split(folder_and_name[2], '.')[1]) | |
for (index, img) in enumerate(best_images) | |
save("$path IMG$index .png", img) | |
end | |
return best_images | |
end | |
# ╔═╡ 17082ba4-59b6-11eb-211d-c997e6bd1deb | |
process_video.(FILE_LIST) | |
# ╔═╡ Cell order: | |
# ╠═04587fae-597f-11eb-03db-41e2e288cabc | |
# ╠═a522894e-59b5-11eb-147f-71052ad6df32 | |
# ╠═92e33fe8-5975-11eb-041f-a3a5ca27edf9 | |
# ╠═d9cab476-59b5-11eb-0476-57ccece8c363 | |
# ╠═ede92886-597e-11eb-098f-e94e01d00dfa | |
# ╠═17082ba4-59b6-11eb-211d-c997e6bd1deb | |
# ╠═fa4beaca-5986-11eb-0621-1b06369dad7a |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Given an video file, this program will select images that contain the maximum amount of text.
Use this program if:
How it works
The program applies a sobel filter to frames in the video, thus highlighting the edges in an image. The "edginess" of an image is a reasonable metric for the amount of text on an image, hence the program saves images that precede large drops in "edginess".
For this file, 6 drops in "Edginess" are observed, so six images are saved.