Skip to content

Instantly share code, notes, and snippets.

@k12ish
Last active December 23, 2021 08:11
Embed
What would you like to do?
Select images from slideshow with maximum amount of text.
### 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
@k12ish
Copy link
Author

k12ish commented Jan 18, 2021

Given an video file, this program will select images that contain the maximum amount of text.

Use this program if:

  • the lessons is of a slideshow format
  • and the lecturer transcribes notes while teaching.

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".

Edginess vs Time

For this file, 6 drops in "Edginess" are observed, so six images are saved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment