0

I am using python for image matching. The feature vectors for reference images is stored in a mysql database. For every new image, i generate the feature vector, find the difference between the test image and the reference vectors in mysql db and return the matches based on distance. This is working well. However, I want to sort the results based on the distance and get only the top 10 results that has the highest distance. My code so far is as follows:

for i,a in enumerate(data):
    features=[float(x) for x in a[6:]]
    d = chi2_distance(features, queryFeatures)
    results=a[1],d
    #res=sorted(results, key=itemgetter(1))
    #results.sort(key=lambda x:x[1])
    #results=sorted(results,key=lambda x: x[1])
    #results=sorted(results, key=lambda x: x[1], reverse=True)
    print(type(results))

When I print type of results i get the following:

<type 'tuple'>
<type 'tuple'>
<type 'tuple'>

When I print just the results, I get:

(127209905L, 6.459347693123215)
(127281515L, 7.790508647801378)
(127312977L, 6.374409391826666)

I tried various methods including, converting the tuples to list and then sorting by list, but none of them worked. I am relatively a new bee working on python...any guidance on how to go about this would be very helpful.

The expected output is

(127281515L, 7.790508647801378)
(127209905L, 6.459347693123215)
(127312977L, 6.374409391826666)
1
  • @Rakesh added the expected output Commented Jun 5, 2018 at 10:19

1 Answer 1

2

First of all, instead of

results=a[1],d

use

results.append((a[1], d))

to collect all the data. And then

results = sorted(results, key=lambda x: x[1], reverse=True)

to sort results based on distance.

Sign up to request clarification or add additional context in comments.

3 Comments

I think based on example output should add reverse=True argument.
@thesilkworm Thanks
@ravi thank you...i worked. My mistake was....I used results={} as a dict and not as a list [].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.