Sort detecting object

asked 2016-12-15 07:35:54 -0600

updated 2019-01-12 17:41:10 -0600

Hello everybody,

I'm really new with OpenCV and I'm trying to sort invoices with logo detection. I have 2 directories : the first one owns logo database and the second one has invoices

This is my script :

#-*- coding: utf-! -*-

import os, shutil
import numpy as np
import cv2

#########################
# SIFT descriptors part #
#########################

# 1 invoice in invoice directory
img2 = cv2.imread('/Users/valentinjungbluth/Desktop/SIFT_SURF_Algo/Factures/facture_CSA.jpg',0)

# Loop over all logo in logo directory
for element in os.listdir('/Users/valentinjungbluth/Desktop/SIFT_SURF_Algo/Logos') :

    img = cv2.imread('/Users/valentinjungbluth/Desktop/SIFT_SURF_Algo/Logos/'+ element,0)
    print img.dtype

    # Initiate SIFT detector
    sift = cv2.xfeatures2d.SIFT_create()

    kp1, des1 = sift.detectAndCompute(img,None)
    kp2, des2 = sift.detectAndCompute(img2,None)

    bf = cv2.BFMatcher()
    matches = bf.knnMatch(des1,des2,k=2)

    good = []
    for m,n in matches :
        if m.distance < 0.5*n.distance :
            good.append([m])

    img3 = cv2.drawMatchesKnn(img,kp1,img2,kp2,good,None,flags=2)

# Condition which create directory and copy file only if matches between logo and invoice

    cv2.imwrite(element,img3)

    try :
        os.mkdir("/Users/valentinjungbluth/Desktop/Datasystems_facture")
        shutil.copy("/Users/valentinjungbluth/Desktop/SIFT_SURF_Algo/Factures/facture_CSA.jpg" ,"/Users/valentinjungbluth/Desktop/Datasystems_facture")
    except OSError :
        pass

    print ("Déplacement de la facture dans le dossier de son entreprise")

But I want to write a condition which is : if they are descriptors and matching points between logo and invoice, create directory and copy the invoice into this directory, else do nothing.

I am not be able to write this condition for now because I don't know How I can get numbers of descriptors.

Thank you so much by advance !

edit retag flag offensive close merge delete