I've been trying to implement a code in Python, using the OpenCV library, that is able to read some input image and look for a match, live through camera. So far, I've succeed to accomplish this goal, adapting a script found online, as posted below. It's basically read the image and if it has a match live it prints "Image found..." and if doesn't print "Not Enough match found-...".
Now, I'm trying to put a break if the image doesn't match but with a time wait. In other words, if there is no correspondent image to match, I want that the code wait for n seconds and if in this period no matching found, the loop will break.
After some research online, I've implemented the
if (time.time() - start) > MAX_TIME_ALLOWED
show in code but this isn't working as expected, since it sometimes break even if there's a match.
import cv2
import numpy as np
import time
MIN_MATCH_COUNT=30
MAX_TIME_ALLOWED = 10 # seconds
start = time.time()
# Initiate SIFT detector
detector=cv2.xfeatures2d.SIFT_create()
# FLANN parameters
FLANN_INDEX_KDITREE=0
flannParam=dict(algorithm=FLANN_INDEX_KDITREE,tree=5)
flann=cv2.FlannBasedMatcher(flannParam,{})
# read the image and find the keypoints and descriptors with SIFT
trainImg=cv2.imread('IMAGE_EXAMPLE.jpg',0)
trainKP,trainDesc=detector.detectAndCompute(trainImg,None)
cam=cv2.VideoCapture(0)
while True:
ret, QueryImgBGR=cam.read()
QueryImg=cv2.cvtColor(QueryImgBGR,cv2.COLOR_BGR2GRAY)
queryKP,queryDesc=detector.detectAndCompute(QueryImg,None)
matches=flann.knnMatch(queryDesc,trainDesc,k=2)
goodMatch=[]
for m,n in matches:
if(m.distance<0.75*n.distance):
goodMatch.append(m)
if(len(goodMatch)>MIN_MATCH_COUNT):
print ("Image found - %d"%(len(goodMatch)))
continue
else:
if (time.time() - start) > MAX_TIME_ALLOWED:
print ("Not Enough match found- %d/%d"%(len(goodMatch),MIN_MATCH_COUNT))
break
cam.release()
cv2.destroyAllWindows()
Is there any other way I can put a wait of n seconds before the code break?
Aucun commentaire:
Enregistrer un commentaire