np.
np.

Reputation: 2379

Search images using c# in local images folder

We have a images folder which has about a million images in it. We need to write a program which would fetch the image based upon a keyword that is entered by the user. We need to match the file names while searching to find the right image. Looking for any suggestions. Thanks N

Upvotes: 3

Views: 5030

Answers (9)

AMissico
AMissico

Reputation: 21684

Depending on the operating system, I suggest you use Indexing Service, Windows Desktop Search, or the latest version of Windows Search. This solves your problem of file lookup based on keyword, it addresses the performance issues in regards to the number of files within a folder, it is scalable, and easily extended.

The DSearch sample at http://msdn.microsoft.com/en-us/library/dd940335(VS.85).aspx does almost exactly what you want and is easy to implement.

For example, if querying a million files and need to move file into subfolders to increase performance then you can simply create the folders and move the files. You will not need to change any code.

If you need to change how keywords are applied, such as using the keywords of the file's summary properties, then you only need to change the query.

For the later operating systems, you do not even need to install any software because the search feature is part of the operting system and available through OleDB. If you want to use Advance Query Syntax (AQS), Microsoft provides a typed-library to access the COM Interfaces that make it easy to generate the SQL command to query the index database.

Honestly, all these other suggestions about databases, and so on, are a waste of time.

Upvotes: 2

CannibalSmith
CannibalSmith

Reputation: 4820

Just rename all the images to their respective keywords delimited by spaces. Then use the OS's own search feature.

If that doesn't work, only then look for fancier solutions.

Upvotes: 0

Juan Nunez
Juan Nunez

Reputation: 529

One simple solution is a database in which you store the an ID, the path, and a varchar (string) field in which you'll keep all the keywords. (That could be stored in a different table for efficiency purposes)

That way you could search by filename or by keywords associated to an image.

Upvotes: 0

Paul Hiles
Paul Hiles

Reputation: 9778

This is the obvious but would imagine it would be pretty slow for a million images:

public IList<string> GetMatchingImages(string path, string keyword)
    {
        var matches = new List<string>();

        var images = System.IO.Directory.GetFiles(path);

        foreach (var image in images)
        {
            if (image.Contains(keyword))
            {
                matches.Add(image);
            }
        }

        return matches;
    }

Upvotes: 2

Ben Cawley
Ben Cawley

Reputation: 1636

My first thoughts for such a large number of images would be to create an inverted-list to use as an index.

If you are able to maintain this list it would make searching relatively quick and you wouldn't have to trawl through a million images which I'm guessing would be too time consuming for you.

I'd start with looking for some inverted-list implementations.

Upvotes: 0

Guffa
Guffa

Reputation: 700312

Getting a million file names from a folder will take a lot of time. I would suggest that you get the file names and put them in a database. That way you can search the names within seconds instead of minutes.

Upvotes: 1

justadreamer
justadreamer

Reputation: 2440

There is Win32 API FindFirstFile, FindNextFile, FindClose: http://msdn.microsoft.com/en-us/library/aa364418(VS.85).aspx - probably they map somehow into .NET as well. Use them to search for the image without any databases.

Upvotes: 0

GvS
GvS

Reputation: 52518

Store all (images & keywords) in a database.

You can use a full-text index to search for the words, or store each word as a seperate entry.

And you will have much faster access to the meta data (filename, creation date, etc) without retrieving (or opening) the image itself.

This is probably much faster as relying on a file system that is not made to store one million entries in a single folder.

Upvotes: 2

egrunin
egrunin

Reputation: 25053

  1. Keep the images on a separate site or subdomain. You probably don't want all 1M files in a single directory, of course.

  2. You need a database with (at least) three tables:

    ImageFile  
        ID  
        Filepath

    Keyword
        ID
        theWord

    ImageKeyword
        ImageID
        KeywordID

Upvotes: 4

Related Questions