This repository has been archived by the owner on Mar 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
dupe.rb
70 lines (58 loc) · 1.9 KB
/
dupe.rb
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
require 'find'
require 'mimemagic'
require 'digest/md5'
require 'set'
require 'childprocess'
require 'fileutils'
require 'json'
include FileUtils
TEST_FRAMES = 100
START_TIME = "00:00:20"
SEARCH_DIR = File.expand_path("video/")
TMP_DIR = File.expand_path("tmp/")
TILES_DIR = File.expand_path("tiles/")
# Create directories if they don't exist
mkdir SEARCH_DIR if ! File.directory?(SEARCH_DIR)
mkdir TMP_DIR if ! File.directory?(TMP_DIR)
mkdir TILES_DIR if ! File.directory?(TILES_DIR)
# Helper to run command silently and raise exception if didn't run correctly
def quietrun(cmd)
#print cmd.join(" ")
process = ChildProcess.build(*cmd)
process.start
begin
process.poll_for_exit(60)
rescue ChildProcess::TimeoutError
end
return process.exit_code
end
Dir.chdir(TMP_DIR) do
Find.find(SEARCH_DIR) do |filename|
# Skip if not a file or not video
next if ! File.file?(filename)
filemime = MimeMagic.by_path(filename)
next if ! filemime || ! filemime.video?
puts "*** Analysing: %s" % filename
# Get some frames with mplayer
print "\tMplayer "
if quietrun(['mplayer', '-nosound', '-vo', 'jpeg', '-fps', '10', '-vf', 'scale=320:240', '-ss', START_TIME, '-frames', TEST_FRAMES.to_s, filename]) != 0
print "...failed, skipping...\n"
next
end
# Sanity check
if %x(ls *.jpg).split("\n").length < 1
puts "\n\tUnable to create images, skipping..."
next
end
scores = []
print "|| ImageMagick tiles "
Find.find(TMP_DIR) do |frame|
quietrun(['convert', '-colorspace', 'gray', '-crop', '80x120', frame, '../tiles/tile%03d.png'])
out = `python ../centroid/centroid.py '#{TILES_DIR}/tile*.png'`
scores.push(JSON.parse(out.strip))
end
File.open(filename+".json", 'w+') { |file| file.write(scores.to_json) }
# Delete the images created by mogrify and mplayer
rm(%x(ls *.jpg).split("\n"))
end
end