Skip to content

Commit

Permalink
minor test cleanups et al
Browse files Browse the repository at this point in the history
  • Loading branch information
rdp committed Nov 23, 2010
1 parent a252939 commit d353b5c
Show file tree
Hide file tree
Showing 14 changed files with 89 additions and 68 deletions.
5 changes: 2 additions & 3 deletions Rakefile
Expand Up @@ -8,13 +8,12 @@ Jeweler::Tasks.new do |s|
s.authors = ["Roger Pack"]
s.add_dependency 'sane', '>= 0.22.0'
s.add_dependency 'rdp-win32screenshot', '>= 0.0.7.3'
s.add_dependency 'mini_magick' # for ocr...
s.add_dependency 'mini_magick', '>= 3.1' # for ocr...
s.add_dependency 'jruby-win32ole'
s.add_dependency 'rdp-ruby-wmi'
s.add_dependency 'ffi'
s.add_dependency 'ffi' # mouse, etc.
s.add_development_dependency 'rspec' # prefer rspec 2 I guess...
s.add_development_dependency 'jeweler'
s.add_development_dependency 'rdp-rmagick'
s.add_development_dependency 'hitimes' # now jruby compat. yea!
s.extensions = ["ext/mkrf_conf.rb"]
end
Expand Down
5 changes: 5 additions & 0 deletions TODO
Expand Up @@ -62,6 +62,11 @@ instructions on how to do their own (private) or do one and submit it (public),

== random backlog ... note: just plow forward, to "grab" available ideas...except that for now, just what *I* plan on needing for myself (filters for what I need/want). ==

the overlayer "freeze frames" in vista

0:01:01.9 next will be at 0:01:02.0s (r [or q to quit]):
0:01:04.0 next will be at 0:01:06.5s (blanked) (r [or q to quit]): "warning--unable to track screen time for some reason" # *of course you can't track it now!*

byu.tv descriptor
youtube non full screen: work with all browsers
easier "here's how on the command line, BTW"
Expand Down
3 changes: 2 additions & 1 deletion bin/sensible-cinema
Expand Up @@ -25,8 +25,9 @@ module SensibleSwing
jlabel.set_bounds(44,44,136,14)
panel.add jlabel
b = JButton.new( "Play Hulu, Youtube, or a DVD (Tracking)" ).on_clicked {
Thread.new { go_sc } # needs to be in its own thread becuase it blocks,
# so "this thread" is the uni-swing thread, gets blocked, and the overlayer can't repaint
self.close
go_sc
}
button_width = 230
b.set_bounds(44,120,button_width,23)
Expand Down
9 changes: 5 additions & 4 deletions bin/sensible-cinema-cli
Expand Up @@ -10,10 +10,10 @@ for file in ['overlayer', 'keyboard_input', 'screen_tracker', 'mouse', 'file_cho
end


def go_sc(args=ARGV)
def go_sc(args)

$VERBOSE = 1 if args.delete('-v')
$TEST = 1 if args.delete('-t')
$DEBUG = 1 if args.delete('-d')
if args.delete('--clear-cache')
OCR.clear_cache!
puts 'cleared cache'
Expand Down Expand Up @@ -96,12 +96,13 @@ def go_sc(args=ARGV)
Blanker.shutdown # lodo move this and the 'q' key to within overlayer
OCR.serialize_cache_to_disk
}
key_input.handle_keystrokes_forever # when this method exits, we want to exit fully...
puts 'syntax from command line: ' + player_description + ' ' + scene_list
key_input.handle_keystrokes_forever # blocking...

end
end

if __FILE__ == $0
puts 'Welcome to Sensible Cinema...'
go_sc
go_sc ARGV
end
17 changes: 10 additions & 7 deletions lib/blanker.rb
Expand Up @@ -13,10 +13,10 @@ def self.startup
@fr.default_close_operation = JFrame::EXIT_ON_CLOSE
@fr.set_size(2000, 2000) # ltodo better size ?
cp = @fr.getContentPane
cp.setBackground(java.awt.Color.black);

cp.setBackground(java.awt.Color.black);

@label = JLabel.new
@label.set_text 'blanked'
@fr.add(@label)
@label.setForeground(java.awt.Color.white);
@label.repaint
Expand All @@ -25,24 +25,27 @@ def self.startup
@fr.set_resizable(false)
@fr.set_visible(true) # have to do this once, to ever see the thing
# lodo does this really speed things up to pre-create it? that icon is a bit ugly...
@fr.repaint
unblank_full_screen! # and hide it to start
end

def self.blank_full_screen! seconds
# somewhat hacky work around for doze: http://www.experts-exchange.com/Programming/Languages/Java/Q_22977145.html
@fr.setAlwaysOnTop(false)
@fr.setAlwaysOnTop(true)
@fr.set_location(0,0)
@fr.repaint # early paint, just in case that helps it pop up faster :)
if seconds
@label.set_text " #{seconds} s"
else
@label.set_text " Blank section"
end
# somewhat hacky work around for doze: http://www.experts-exchange.com/Programming/Languages/Java/Q_22977145.html
@fr.setAlwaysOnTop(false)
@fr.setAlwaysOnTop(true)
@fr.set_location(0,0)
end

def self.unblank_full_screen!
# off screen...
# move it off screen...
@fr.set_location(-2100, -2100)
@fr.repaint 0
end

def self.shutdown
Expand Down
5 changes: 2 additions & 3 deletions lib/ocr.rb
Expand Up @@ -26,7 +26,7 @@ def identify_digit memory_bitmap, options = {}
return ":"
end
end
image = MiniMagick::Image.from_blob(memory_bitmap)
image = MiniMagick::Image.read(memory_bitmap)
# any operation on image is expensive, requires convert.exe in path...
if options[:should_invert]
# hulu wants negate
Expand Down Expand Up @@ -74,8 +74,7 @@ def serialize_cache_to_disk
def unserialize_cache_from_disk
if File.exist? CACHE_FILE
CACHE.merge!(Marshal.load(File.binread(CACHE_FILE)))
end

end
end

extend self
Expand Down
2 changes: 1 addition & 1 deletion lib/overlayer.rb
Expand Up @@ -192,7 +192,7 @@ def status
end
end
check_reload_yaml
time + state + "(r [or q to quit]): "
time + state + "(r [or ctrl+c to quit]): "
end

def keyboard_input char
Expand Down
81 changes: 46 additions & 35 deletions lib/screen_tracker.rb
Expand Up @@ -39,7 +39,7 @@ def initialize name_or_regex,x,y,width,height,use_class_name=nil,digits=nil,call
raise 'poor height or wrong window'
end
@digits = digits
@displayed_warning = false
@previously_displayed_warning = false
@dump_digit_count = 1
pps 'using x',@x, 'from x', x, 'y', @y, 'from y', y,'x2',@x2,'y2',@y2,'digits', @digits if $VERBOSE
end
Expand Down Expand Up @@ -82,18 +82,20 @@ def get_full_bmp
def dump_bmp filename = 'dump.bmp'
File.binwrite filename, get_bmp
File.binwrite 'all.' + filename, get_full_bmp
dump_digits get_digits_as_bitmaps if @digits
dump_digits(get_digits_as_bitmaps, 'dump_bmp') if @digits
end

def dump_digits digits
for type, bitmap in get_digits_as_bitmaps
File.binwrite type.to_s + '.' + @dump_digit_count.to_s + '.bmp', bitmap
end
print 'debug dumped digits that Im about to parse:', @dump_digit_count, "\n"
@dump_digit_count += 1
def dump_digits digits, message
p "#{message} dumping digits to dump no: #{@dump_digit_count} #{Time.now.to_f}"
for type, bitmap in digits
File.binwrite type.to_s + '.' + @dump_digit_count.to_s + '.bmp', bitmap
end
File.binwrite @dump_digit_count.to_s + '.mrsh', Marshal.dump(digits)
@dump_digit_count += 1
end

DIGIT_TYPES = [:hours, :minute_tens, :minute_ones, :second_tens, :second_ones]

# returns like {:hours => nil, :minutes_tens => raw_bmp, ...
def get_digits_as_bitmaps
# @digits are like {:hours => [100,5], :minute_tens => [x, width], :minute_ones, :second_tens, :second_ones}
Expand All @@ -115,7 +117,6 @@ def get_relative_coords
[@x,@y,@x2,@y2]
end

# split out for unit testing purposes
def identify_digit bitmap
OCR.identify_digit(bitmap, @digits)
end
Expand All @@ -132,66 +133,76 @@ def wait_till_next_change
if current != original
if @digits
got = attempt_to_get_time_from_screen time_before_scan
if @displayed_warning && got
if @previously_displayed_warning && got
# reassure user :)
p 'tracking it successfully again'
@displayed_warning = false
@previously_displayed_warning = false
end
return got
else
puts 'screen time change only detected... [unexpected]'
puts 'screen time change only detected... [unexpected]' # unit tests do this still
return
end
else
# no screen change detected ...
sleep 0.02
if(Time.now - time_since_last_screen_change > 2)
if(Time.now - time_since_last_screen_change > 2.0)
# display a warning
p 'warning--unable to track screen time for some reason'
@displayed_warning = true
time_since_last_screen_change = Time.now
# also reget window hwnd, just in case that's the problem...(can be with VLC)
p 'warning--unable to track screen time for some reason [perhaps screen obscured or it\'s not playing yet?]'
@previously_displayed_warning = true
time_since_last_screen_change = Time.now
# also reget window hwnd, just in case that's the problem...(can be with VLC moving from title to title)
get_hwnd
end
end
}
end

def attempt_to_get_time_from_screen start
def attempt_to_get_time_from_screen start_time
out = {}
start = get_digits_as_bitmaps
dump_digits(start, 'started_as')
# force it to have two matching snapshots in a row, to avoid race conditions grabbing the digits...
previous = nil # 0.08s [!] not too accurate...ltodo
until previous == (temp = get_digits_as_bitmaps)
previous = temp
sleep 0.05 # allow youtube to update (sigh) lodo just for utube
previous = nil
current = start
until previous == (current)
previous = current
sleep 0.25 # allow youtube to update (sigh) lodo just for utube
current = get_digits_as_bitmaps
p previous == current
dump_digits(current, 'current is')
# lodo it should probably poll *before* calling this, not here...maybe?
end
assert previous == temp
digits = temp
assert previous == current
digits = current = previous

dump_digits(digits) if $DEBUG
if $DEBUG
dump_digits(digits, 'using digits')
end
DIGIT_TYPES.each{|type|
if digits[type]
digit = identify_digit(digits[type])
unless digit
if $DEBUG || $VERBOSE
bitmap = digits[type]
# unable to identify a digit?
if $DEBUG || $VERBOSE && (type != :hours)
@a ||= 1
@a += 1
@already_wrote ||= {}
unless @already_wrote[digits[type]]
p 'unable to identify capture!' + type.to_s + @a.to_s + ' capture no:' + @dump_digit_count.to_s
File.binwrite("bad_digit#{@a}#{type}.bmp", digits[type]) unless type == :hours
@already_wrote[digits[type]] = true
unless @already_wrote[bitmap]
p 'unable to identify capture!' + type.to_s + @a.to_s + ' dump:' + @dump_digit_count.to_s
File.binwrite("bad_digit#{@a}#{type}.bmp", bitmap)
@already_wrote[bitmap] = true
end
end
if type == :hours
digit = 0 # this one can fail and that's ok in VLC bottom right
else
# early failure return
return
# early (failure) return
return nil
end
else
p " got digit #{type} as #{digit} which was captured as #{@dump_digit_count} " if $DEBUG
p " got digit #{type} OCR as #{digit} which was captured to dump #{@dump_digit_count - 1} #{Time.now_f}" if $DEBUG
end
out[type] = digit
else
Expand All @@ -200,8 +211,8 @@ def attempt_to_get_time_from_screen start
end
}
out = "%d:%d%d:%d%d" % DIGIT_TYPES.map{ |type| out[type] }
puts '', 'got new screen time ' + out + " tracking delta:" + (Time.now - start).to_s if $VERBOSE
return out, Time.now-start
puts '', 'got new screen time ' + out + " tracking delta:" + (Time.now - start_time).to_s if $VERBOSE
return out, Time.now-start_time
end

def process_forever_in_thread
Expand Down
14 changes: 7 additions & 7 deletions spec/blanker.spec.rb
Expand Up @@ -12,19 +12,19 @@
end

it "should be able to blank then unblank" do
Blanker.blank_full_screen! "2.000"
sleep 2
Blanker.blank_full_screen! "blank then unblank"
sleep 0.5
Blanker.unblank_full_screen!
end

it "should be able to blank several times" do
it "should be able to blank then unblank several times" do
3.times {
Blanker.blank_full_screen! ''
Blanker.blank_full_screen! 'unblank several times'
Blanker.unblank_full_screen!
}
end

it "should be able to unblank several times I suppose" do
it "should be able to unblank several times in a row I suppose" do
3.times {
Blanker.unblank_full_screen!
}
Expand All @@ -34,9 +34,9 @@

it "should be able to blank certain coords"

it "should have a background color optionally"
it "should have a background color specificable"

it "should have a picture optionally"
it "should have a picture overlay optionally"

end

Expand Down
2 changes: 1 addition & 1 deletion spec/convert_image.rb
Expand Up @@ -3,6 +3,6 @@
require 'RMagick'

to = ARGV[1] || 'pnm'
img = Magick::Image.from_blob(File.binread ARGV[0])
img = Magick::Image.read(File.binread ARGV[0])
png = img[0].to_blob {self.format = to } # TIFF
File.open("picture10." + to, "wb") {|file| file.puts png}
Binary file added spec/images/youtube_light_2_9.bmp
Binary file not shown.
2 changes: 1 addition & 1 deletion spec/overlayer.spec.rb
Expand Up @@ -133,7 +133,7 @@ def start_bad
end

it 'should have key list output on screen' do
@o.status.should include("q to quit")
@o.status.should include("ctrl+c to quit")
end

it 'should allow for yaml input and parse it appropo' do
Expand Down
8 changes: 5 additions & 3 deletions spec/screen_tracker.spec.rb
Expand Up @@ -241,16 +241,17 @@ def start_vlc
elsif time_through == 2
{:minute_tens=>black}
else
# just return the same thing
times_read += 1
{:minute_tens=>four,:second_tens => four, :second_ones => four, :minute_ones => four,
:hours => four}
end
end

# these assert the same thing
@a.attempt_to_get_time_from_screen(Time.now)[0].should == "4:44:44"
got = @a.attempt_to_get_time_from_screen(Time.now)
time_through.should == 4
times_read.should == 2

got[0].should == "4:44:44"
end

context "with an OCR that can change from hour to minutes during ads" do
Expand All @@ -277,6 +278,7 @@ def kill_vlc
Process.kill 9, $pid1 # MRI...sigh.
FileUtils.rm_rf Dir['*.bmp']
$pid1 = nil
sleep 1
end

after(:all) do
Expand Down
4 changes: 2 additions & 2 deletions zamples/scene_lists/youtube/gummy_bear_song_youtube.txt
Expand Up @@ -2,8 +2,8 @@ mutes:
# none
blank_outs:
"0:01:02" : "0:01:06.5" # bum waggin'
0:02:17.5 : 0:02:19
0:02:17.5 : 0:02:19 # same

title: The Gummy Bear Song - Long English Version (probably works with other languages')
title: The Gummy Bear Song - Long English Version (probably works with other languages' Gummy Bear songs, too)
source: youtube
url: http://www.youtube.com/watch?v=astISOttCQ0

0 comments on commit d353b5c

Please sign in to comment.