0

I'm in the process of developing a Flask Python application that's able to find a logo within different background images. The goal is to have a solution that states "yes, the logo is in the background", or "no, it doesn't look like the logo is in the background". I'm utilizing the SIFT package to match keypoints between the logo and the image with the logo in the background.

I want to figure out how I can write an IF statement that's able to deliver the message above depending on the keypoints. Is there anyone that can give me guidance on the first steps of doing this? I'll attach the code to this message (note that the function should have an indent there):

@app.route('/identify/<filename>')
def identify(filename):
""" After uploading the image, 
show the identification of the uploaded image
"""
# TODO: Logic to load the uploaded image filename and identify other images
image_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
image_url = url_for('images', filename=filename)
background_image = cv2.imread(image_path)
background_image = cv2.cvtColor(background_image, cv2.COLOR_BGR2RGB)

#still need to figure out how to load a second photo of the user's choice
#loading in the photo to find within the uploaded photo
logo = cv2.imread("chevy-open-road-logo-300.png")
logo = cv2.cvtColor(logo, cv2.COLOR_BGR2RGB)

#creating a copy and implementing a SIFT Image Matching method
background_image_copy = background_image.copy()

#sift
sift = cv2.xfeatures2d.SIFT_create()

background_keypoints, background_descriptors = sift.detectAndCompute(background_image_copy, None)
logo_keypoints, logo_descriptors = sift.detectAndCompute(logo, None)

#feature matching
bf = cv2.BFMatcher(cv2.NORM_L1, crossCheck=True)

matches = bf.match(background_descriptors,logo_descriptors)
matches = sorted(matches, key = lambda x:x.distance)

#img = BytesIO()
image_match = cv2.drawMatches(background_image_copy, background_keypoints, logo, logo_keypoints, matches[:45], logo, flags=2)

plt.imshow(image_match), plt.show()

'''
#Converting the plot to PNG image
plt.savefig(img, format='png', bbox_inches='tight')
img.seek(0)
plot_url = base64.b64encode(img.getvalue()).decode()
'''

'''
#Printing the number of keypoints detected in the training image
x = str(len(background_keypoints))

#Printing the number of keypoints detected in the logo image
y = str((len(logo_keypoints)))

# Printing total number of matching points between the training and logo images
z = str((len(matches)))
'''

return render_template('identify.html',
image = image_url#, plot_url=plot_url
#x=x, y=y, z=z
)
CJ All
  • 1
  • I don't know much about sift, but it sounds like you want a function that returns a boolean. Example: if inBackground(): I assume Sift can do the image recognition stuff, but it might take more than one line, hence use a function. – Ben Alan Oct 25 '21 at 23:27
  • That's exactly what I'm thinking about doing Ben. SIFT gets keypoints that are properties found in both pictures and matches them together. I want to write a function/if statement that's able to give an answer back based on the keypoints. If you have any more ideas about how I can get started please let me know. – CJ All Oct 26 '21 at 02:28
  • Does this help? https://opencv24-python-tutorials.readthedocs.io/en/latest/py_tutorials/py_feature2d/py_matcher/py_matcher.html – Ben Alan Oct 26 '21 at 13:46
  • The variable 'matches' is a list. The longer the list the more of a match the images are. You will have to tweak how close you need the match to be ...something like "if len(matches) > x:" – Ben Alan Oct 26 '21 at 13:48
  • The two comments you left are both quite helpful in guiding me where I can take the application. I'll look into both of them. Thanks again for the help! – CJ All Oct 26 '21 at 22:03

0 Answers0