Ask Your Question
-1

When I trying to run the code below i am getting error

asked 2018-10-06 04:24:02 -0500

mrjohn gravatar image

updated 2018-10-06 04:46:13 -0500

berak gravatar image

i am using python 2.7 and opencv 2.4.13. The code is

# -*- coding: cp1254 -*-

import cv2

import numpy as np


def sift_detector(new_image, template):

    image1=cv2.cvtColor(new_image,cv2.COLOR_BGR2GRAY)
    image2=template
    sift=cv2.SIFT()
    keypoints1, descriptors1=sift.detectAndCompute(image1,None)
    keypoints2, descriptors2=sift.detectAndCompute(image2,None)
    FLANN_INDEX_KDTREE=0
    index_param=dict(algorithm=FLANN_INDEX_KDTREE,trees=3)
    search_params=dict(checks=100)

    flann=cv2.FlannBasedMatcher(index_params,search_params)
    matches=flann.knnMatch(descriptors1,descriptors2,k=2)

    good_matches=[]
    for m,n in matches:
        if m.distance < 0.7 * n.distance:
            good_matches.append(m)
        return len(good_matches)

cap =cv2.VideoCapture(0)

template=cv2.imread("ben.jpg",0)

while True:

    ret,frame=cap.read()
    height, width= frame.shape[:2]
    top_left_x=width/3
    top_left_y=(height/2) + (height/4)
    bottom_right_x= (width/3)*2
    bottom_right_y= (height/2) - (height/4)

    cv2.rectangle(frame, (top_left_x,top_left_y), (bottom_right_x,bottom_right_y),(255,0,0),3)
    cropped=frame[bottom_right_y:top_left_y,bottom_right_x: top_left_x]
    frame=cv2.flip(frame,1)
    matches=sift_detector(cropped,template)
    cv2.putText(frame,str(matches),(450,450),cv2.FONT_HERSHEY_COMPLEX,2,(0,255,0),3)
    threshold=15
    if matches > threshold:
        cv2.rectangle(frame, (top_left_x,top_left_y), (bottom_right_x,bottom_right_y),(255,125,0),3)
        cv2.putText(frame,"object found", (50,50), cv2.FONT_HERSHEY_COMPLEX,2,(255,255,0),2)


    cv2.imshow("after process",frame)
    if cv2.waitKey(1)==13:
        break

cap.release()

cv2.destroyAllWindows()

And the error is

Traceback (most recent call last):
  File "C:\Users\Ahmet\Desktop\artificial intelligence\opencv çalışmaları\mini project 5.py", line 41, in <module>

    matches=sift_detector(cropped,template)

  File "C:\Users\Ahmet\Desktop\artificial intelligence\opencv çalışmaları\mini project 5.py", line 10, in sift_detector

keypoints1, descriptors1=sift.detectAndCompute(image1,None)

error: C:\build\2_4_winpack-bindings-win64-vc14-static\opencv\modules\nonfree\src\sift.cpp:724: error: (-5) image is empty or has incorrect depth (!=CV_8U) in function cv::SIFT::operator ()
edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted
0

answered 2018-10-06 04:27:03 -0500

berak gravatar image

updated 2018-10-06 05:10:24 -0500

python noobs NEVER validate their input, thus the funny errors.

did your capture open at all ? did it read an image ? was the imread() output valid ?

you HAVE TO CHECK ALL OF THOSE !

then, NO, feature matching can't be abused for object detection

you can only use it to find a part of a known scene, not with arbitrary images. (you'll have to re-think the whole (broken !) approach)

also:

good_matches=[]
for m,n in matches:
    if m.distance < 0.7 * n.distance:
        good_matches.append(m)
    return len(good_matches) ## what ? indentation error (it will return after the 1st) ?

please also learn, that the number of matches is entirely irrelevant here. if you happen to have a potted plant in the background in both images, it will happily take most matches from there. it does not at all know, "what you're looking for", so results are arbitrary.

so, "back to the drawing board" for you. discard this attempt, don't copypaste code without understanding it, and do some proper research on "object recognition" first.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2018-10-06 04:24:02 -0500

Seen: 119 times

Last updated: Oct 06 '18