comparing surf features between many images - comparison

I am comparing many images based on surf features. I am able to compare only 2 images, but I need to do it for multiple images in two folders; can someone help me out?
functionname='OpenSurf.m';
functiondir=which(functionname);
functiondir=functiondir(1:end-length(functionname));
addpath([functiondir '/SubFunctions'])
defaultoptions=struct('tresh',0.0002,'octaves',5,'init_sample',2,'upright',false,'extended',false,'verbose',false);
if(~exist('Options','var')),
Options=defaultoptions;
else
tags = fieldnames(defaultoptions);
for i=1:length(tags)
if(~isfield(Options,tags{i})), Options.(tags{i})=defaultoptions.(tags{i}); end
end
if(length(tags)~=length(fieldnames(Options))),
warning('register_volumes:unknownoption','unknown options found');
end
end
iimg=IntegralImage_IntegralImage(img);
FastHessianData.thresh = Options.tresh;
FastHessianData.octaves = Options.octaves;
FastHessianData.init_sample = Options.init_sample;
FastHessianData.img = iimg;
ipts = FastHessian_getIpoints(FastHessianData,Options.verbose);
if(~isempty(ipts))
ipts = SurfDescriptor_DecribeInterestPoints(ipts,Options.upright, Options.extended, iimg, Options.verbose);
end

Related

Filterring ActiveRecord Relation...if there are no matches. (Null the active record relation)

I have a dashboard that allows for filtering of the results by different parameters. I build methods to filter the results by the given criteria. One area where I'm having trouble is if the previous line should null out the active record relation. Should I just put a bunch of if present? stat
def find_website_stats(options = {})
if options[:date_between].present?
start_date = options[:date_between].split(/-/).first.to_date
end_date = options[:date_between].split(/-/).last.to_date + 1
elsif options[:start_date].present?
start_date = options[:start_date].to_date
end_date = options[:end_date].to_date + 1 if options[:end_date].present?
end
contractor_name = options[:search_contractor] if options[:search_contractor].present?
distributor_name = options[:search_distributor] if options[:search_distributor].present?
distributor_ids = options[:with_distributor] if options[:with_distributor].present?
contractor_ids = options[:with_contractor] if options[:with_contractor].present?
with_country = options[:with_country] if options[:with_country].present?
with_state = options[:with_state] if options[:with_state].present?
search_city = options[:search_city] if options[:search_city].present?
web_stats = self.website_stats
if web_stats.present?
web_stats = web_stats.where(contractor_id: [*contractor_ids]) if contractor_ids.present?
if distributor_ids.present?
web_stat_ids = DistributorWebsiteStat.where(distributor_id: [*distributor_ids]).pluck(:website_stat_id)
web_stats = web_stats.where(id: [*web_stat_ids])
end
web_stats = web_stats.where(date_recorded: start_date..end_date) if start_date.present? && end_date.present?
web_stats = web_stats.with_country(with_country) if with_country.present?
web_stats = web_stats.with_state(with_state) if with_state.present?
web_stats = web_stats.search_city(search_city) if search_city.present?
#untested
if contractor_name.present?
searched_contractor_ids = Brand.search_contractor(contractor_name).pluck(:id)
web_stats = web_stats.where(contractor_id: [*searched_contractor_ids])
end
if distributor_name.present?
searched_distributor_ids = Brand.search_distributor(distributor_name).pluck(:id)
web_stat_ids = DistributorWebsiteStat.where(distributor_id: [*searched_distributor_ids])
web_stats = web_stats.where(id: [*web_stat_ids])
end
#end untested
end
web_stats
end
Where I'm specifically having a problem right now is the line that says if web_stat_ids.present?
So at first I grab all the website stats this object is associated with and then look to see if there are any for the given distributor.
If there is none for the given distributor web_stat_ids obviously returns nil
Then when I go to the line web_stats.where(id: [*web_stat_ids]) that's obviously going to return the same thing that I had before, rather than an empty active record relation, which is what I need it to be?
If I make this an empty array the next few statements with "where" won't work because it's an array and not an active record relation.
I know I can wrap this stuff in a bunch of if present? && statements...but I was wondering if there is a better solution to my problem?
In case anyone else is looking for this, found the answer from this SO post: How to return an empty ActiveRecord relation?
Model.none rails 4+

Rails API: Selecting records that match parameters

I have the following Pixel model with the following attributes media, page, disabled, selective
And I am looking for the most reusable way to make an API call that selects all objects that match the parameters passed in the API call. The API call can take 0 parameters or any number of parameters
Here is what I have so far. I believe it will work, but I definitely think there has to be a better solution.
def pixels_by_params
if(params[:media] && params[:page] && params[:selective] && params[:disabled])
pixels = TrackingPixel.media(params[:media]).page(params[:media]).selective(params[:selective]).disabled(params[:disabled])
elsif(params[:media] && params[:page] && params[:selective])
pixels = TrackingPixel.media(params[:media]).page(params[:media]).selective(params[:selective])
elsif(params[:media] && params[:page])
pixels = TrackingPixel.media(params[:media]).page(params[:media])
elsif(params[:media])
pixels = TrackingPixel.media(params[:media])
...
...
...
end
You're chaining all of them anyway, so why don't you just...
pixels = params[:media] ? TrackingPixel.media(params[:media]) : TrackingPixel.all
pixels = pixels.page(params[:page]) if params[:page]
pixels = pixels.selective(params[:selective]) if params[:selective]
pixels = pixels.disabled(params[:disabled]) if params[:disabled]
Or if you prefer a fancy-fied loop, you could:
pixels = params[:media] ? TrackingPixel.media(params[:media]) : TrackingPixel.all
[:page, :selective, :disabled].each do |attr|
pixels = pixels.send(attr, params[attr]) if params[attr]
end

Creating a record from a third-party object with same field names?

I have a Charge model in my database with field names that match up to the fields returned from a third-party API.
charge = ThirdPartyChargeAPI.find(1)
Charge.create do |e|
e.object = charge.object
e.paid = charge.paid
e.amount = charge.amount
e.currency = charge.currency
e.refunded = charge.refunded
e.amount_refunded = charge.amount_refunded
e.failure_message = charge.failure_message
e.failure_code = charge.failure_code
e.description = charge.description
e.metadata = charge.metadata
e.captured = charge.captured
e.balance_transaction = charge.balance_transaction
e.customer = charge.customer
e.invoice = charge.invoice
e.created = charge.created
end
Seems painfully redundant, though. Is there some way to merge this without having to basically set every single field manually?
Assuming there's no way to get a direct hash from the API (I would imagine there would be, since it's probably coming in as XML or JSON), you could try a direct map of instance variables:
Charge.create do |c|
charge.instance_variables.each do |var|
value = charge.instance_variable_get(var)
c.instance_variable_set(var, value)
end
end
This is making some pretty bold assumptions about the structure of the charge you're getting back from the API though - any instance variable in it that you don't want will be included.

Scrape image from YouTube video page

When I copy a YouTube link, like this one, for example (http://www.youtube.com/watch?v=_Ka2kpzTAL8), and paste it into a Facebook status update, Facebook somehow grabs a still of the video. I'd like to build a feature that does the same thing, but I don't really know how to find the image.
Can anyone point me in the right direction? I'm open to PHP, Python, Ruby or Perl.
Alright, found it !
Take this link: http://i4.ytimg.com/vi/something/hqdefault.jpg and replace something with the video's ID on Youtube. For a smaller image, just remove the hq part of the URL.
I can also confirm this implementation is the way two go. I have used it a Ruby screen scraper ( search application ) # http://stripsearch.me
# search10b Youtube crawl9
#search10a1 = search_array[1]
if search_array[1].present?
#results_9b = []
params_9b = search_array[1].split.map(&:downcase).join('+')
search_url_yt_9b = url_9b << params_9b
doc_9b = Nokogiri::HTML(open(search_url_yt_9b).read, nil, 'utf-8')
entries_9b = doc_9b.css('.yt-ui-ellipsis-2.spf-link')
if entries_9b.size != 0
entries_9b.each do |entry_9b|
title = entry_9b['title']
li = "https://www.youtube.com"
nk = entry_9b['href']
link = li << nk
img_src = entry_9b['href']
uid = img_src.split('=').last
precede_url = "http://i4.ytimg.com/vi/"
append_url = "/hqdefault.jpg"
img_url = precede_url << uid << append_url
precede_tag = "<img src="
append_tag = ">"
img = precede_tag << img_url << append_tag
#results_9b.push(title, link, img)
end
else
#error_9b = "no results found here for " + params_9b
end
end

Execute big scripts with rails

I made a very big script to feel my initial datas into my rails app. I have about 3000 lines in my CSV and 10000 images.
After maybe 300 upload i got this message :
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.9/lib/active_support/core_ext/kernel/agnostics.rb:7:in ``': Cannot allocate memory - identify -format %wx%h '/tmp/stream20111104-14788-1hsumv7.jpg[0]' (Errno::ENOMEM)
My upload script :
if (row[28] != nil)
hotelalbum = HotelAlbumPhoto.find_or_create_by_title(h.title)
hotelalbum.description = "Album photo de l'hotel " + h.title.capitalize
hotelalbum.hotel_id = h.id
hotelalbum.save
files = Dir.glob('IMAGES/' + row[28].gsub(/\\/,'/') + '/*.jpg')
i =0
for file in files
i += 1
photopath = File.expand_path('../../import', __FILE__) + '/' + file
photoname = file.split('/').last
if (i==1)
hotelalbum.thumbnail = open(photopath)
hotelalbum.save
end
if (i==1)
h.thumbnail = open(photopath)
end
photo = HotelImage.find_or_create_by_image_file_name_and_hotel_album_photo_id(photoname,hotelalbum.id)
if (photo.image_file_size == nil || photo.image_file_name != photoname)
photo.image = open(photopath)
photo.activated = true
photo.alt = "Photo de l'hotel " + h.title
photo.save
else
puts photopath + ' already updated'
end
end
end
When i check my memory with top command, i see ruby process use more memory on each upload. How can i manage this?
Thank you for help
ps : My server is a virtual machine with 512Mb memory, one solution is to inscrease this memory but i hope to find an other.
I don't know where the open function is defined, but I'm suspicious that I don't see a corresponding close...
update Better idea, change photo.image = open(photopath) to photo.image = File.read(photopath)
According to the docs, read:
Opens the file, optionally seeks to the given offset, then
returns length bytes (defaulting to the rest of the file).
read ensures the file is closed before returning.
Looks like a memory leak problem of ImageMagick? Maybe it will help to process the list in big blocks or chunks with in_groups_of and force garbage collection with GC.start after each chunk:
files.in_groups_of(100) {|chunk|
# process chunk
GC.start
}

Resources