def run(self): # print "Starting " + self.name while not exitFlag: queueLock.acquire() if not workQueue.empty() and not filesQueue.full(): path = self.q.get() print "%d" % filesQueue.qsize() queueLock.release() # t1 = cv2.getTickCount() des2 = np.asarray( image.loadImageFromPath(path, cv2.IMREAD_GRAYSCALE, False), np.float32) name = image.fileName(path) # t2 = cv2.getTickCount() if len(des2) >= 2: queueLock.acquire() filesQueue.put({'n': name, 'd': des2}) if filesQueue.qsize() > WORK_THREAD_COUNT * 10: e.set() queueLock.release() # t3 = cv2.getTickCount() # time = (e2 - e1) / cv2.getTickFrequency() # print "R: %s s P: %s s" % ((t2 - t1) / cv2.getTickFrequency(), (t3 - t2) / cv2.getTickFrequency()) else: queueLock.release() time.sleep(5) print "Exiting " + self.name
def run(self): # print "Starting " + self.name while not self.event.is_set(): self.lock.acquire() if not self.queue.empty(): path = self.queue.get() self.lock.release() # t1 = cv2.getTickCount() des2 = np.asarray( image.loadImageFromPath(path, cv2.IMREAD_GRAYSCALE, False), np.float32) name = image.fileName(path) # t2 = cv2.getTickCount() if len(des2) >= 2: self.lock.acquire() self.files.append({'n': name, 'd': des2}) self.lock.release() # t3 = cv2.getTickCount() # time = (e2 - e1) / cv2.getTickFrequency() # print "R: %s s P: %s s" % ((t2 - t1) / cv2.getTickFrequency(), (t3 - t2) / cv2.getTickFrequency()) else: self.lock.release()
def run(self): i = 0 print("Starting " + self.name) while not self.event.is_set(): self.lock.acquire() if not self.workQueue.empty(): path = self.workQueue.get() self.lock.release() i += 1 des2 = image.loadImageFromPath(path, cv2.IMREAD_GRAYSCALE, False) des2 = np.asarray(des2, np.float32) if len(des2) >= 2: name = image.fileName(path) m = image.match(self.des, des2) if len(m) >= 50: self.lock.acquire() self.finds.append({'m': len(m), 'n': name}) self.lock.release() print("Matched %s file %s" % (len(m), name)) # time.sleep(2) else: self.lock.release() print("Exiting %s - %d" % (self.name, i))
def testFlan(): flann = cv2.FlannBasedMatcher(index_params, search_params) load_descriptors(flann) # img = im.loadImageFromUrl('http://comicstore.cf/uploads/diamonds/STK360887.jpg', resize=True, maxSize=200) img = im.loadImageFromPath('../images/z13.jpg', resize=True, maxSize=500) _kp, _des = sift.detectAndCompute(img, None) kp, des = im.sortKp(_kp, _des, 1000) t_start = cv2.getTickCount() matches = flann.knnMatch(des, k=2) t_end = cv2.getTickCount() print "Time match: %s" % ((t_end - t_start) / cv2.getTickFrequency()) m = [(m.queryIdx, m.trainIdx, m.imgIdx) for m, n in matches if m.distance < n.distance * 0.75] duples = [ c for n, (a, b, c) in enumerate(m) if c in [cc for aa, bb, cc in m[:n if n > 0 else n + 1]] ] no_duples = [ c for n, (a, b, c) in enumerate(m) if c not in duples and c not in [cc for aa, bb, cc in m[:n]] ] print "Points: %d,\n no duples(%d): %s,\n duples(%d): %s" % ( len(m), len(no_duples), ' '.join([str(i) for i in no_duples]), len(duples), ' '.join([str(i) for i in duples]))
def checkFromRAM(imgPath, threadsCount=200): finds = [] img = image.loadImageFromPath(imgPath, cv2.IMREAD_GRAYSCALE, True, 200) kp, des = image.getKpDes(img) threadList = [] count = 0 while count < threadsCount: threadList.append("Thread-%d" % count) count += 1 if len(files) == 0: print("Hash files count is empty") else: print("Files count: %d " % len(files)) event = threading.Event() queueLock = threading.Lock() workQueue = Queue.Queue(len(files) + 10) threads = [] threadID = 1 # Create new threads for tName in threadList: thread = RamThread(threadID, tName, event, queueLock, workQueue, kp, des, finds) thread.daemon = True thread.start() threads.append(thread) threadID += 1 # Fill the queue queueLock.acquire() for i in range(0, len(files) - 1): workQueue.put(i) queueLock.release() # Wait for queue to empty while not workQueue.empty(): pass # Notify threads it's time to exit event.set() # Wait for all threads to complete for t in threads: t.join() return finds
def run(self): print "Starting " + self.name queueLock.acquire() des1 = des.copy() queueLock.release() while not exitFlag: queueLock.acquire() if not workQueue.empty(): path = self.q.get() queueLock.release() des2 = image.loadImageFromPath(path, cv2.IMREAD_GRAYSCALE, False) des2 = np.asarray(des2, np.float32) name = image.fileName(path) if isinstance(des2, list) and len(des2) >= 2: m = image.match(des1, des2) if len(m) >= 50: print "Matched %s file %s" % (len(m), name) else: queueLock.release() print "Exiting " + self.name
queueLock.release() print("Exiting " + self.name) ap = argparse.ArgumentParser() ap.add_argument("-t", "--threads", required=True, type=int, help="Threads count") ap.add_argument("-i", "--image", required=True, help="Path to the image") ap.add_argument("-hs", "--hash", required=True, help="Path to the hash files") args = vars(ap.parse_args()) img = image.loadImageFromPath(args['image'], cv2.IMREAD_GRAYSCALE, True, 200) kp, des = image.getKpDes(img) threadList = [] count = 0 while count < args['threads']: threadList.append("Thread-%d" % count) count += 1 nameList = [] for imagePath in glob.glob("%s*%s" % (args['hash'], KPC_EXT)): nameList.append(imagePath) if len(nameList) == 0: exit(0) else:
queueLock.acquire() if not workQueue.empty(): url = q.get() queueLock.release() name2 = image.fileName(url) img2 = image.loadImageFromUrl(url) image.compare(name1, name2, img1.copy(), img2) # print "%s processing %s" % (threadName, data) else: queueLock.release() url1 = '../images/M7.jpg' img1 = image.loadImageFromPath(url1) # url1 = 'http://comicstore.cf/uploads/diamonds/STK309612.jpg' # img1 = image.loadImageFromUrl(url1) name1 = image.fileName(url1) threadList = [] count = 0 while count < THREAD_COUNT: threadList.append("Thread-%d" % count) count += 1 nameList = sql.executeQuery() if len(nameList) == 0:
data = self.q.get() queueLock.release() des2 = data['d'] name = data['n'] m = image.match(des1, des2) if len(m) >= 50: print "Matched %s file %s" % (len(m), name) else: queueLock.release() time.sleep(1) print "Exiting " + self.name img = image.loadImageFromPath('../images/M6.jpg', cv2.IMREAD_GRAYSCALE, True, 200) kp, des = image.getKpDes(img) e = threading.Event() workThreadList = [] count = 0 while count < WORK_THREAD_COUNT: workThreadList.append("WorkThread-%d" % count) count += 1 filesThreadList = [] count = 0 while count < FILES_THREAD_COUNT: filesThreadList.append("FileThread-%d" % count) count += 1
def check(imgPath, hashPath=HASH_PATH, withSubFolders=True, threadsCount=200, ext=DES_EXT): finds = [] img = image.loadImageFromPath(imgPath, cv2.IMREAD_GRAYSCALE, True, 200) kp, des = image.getKpDes(img) threadList = [] count = 0 while count < threadsCount: threadList.append("Thread-%d" % count) count += 1 nameList = [] if withSubFolders: folder = 1 path = "%s%s/" % (hashPath, folder) print(path) while os.path.exists(path): for imagePath in glob.glob("%s*%s" % (path, ext)): nameList.append(imagePath) if len(nameList) == 50000: break folder += 1 path = "%s%s/" % (hashPath, folder) else: for imagePath in glob.glob("%s*%s" % (hashPath, ext)): nameList.append(imagePath) # if len(nameList) == 50000: # break if len(nameList) == 0: print("Hash files count is empty") else: print("Files count: %d " % len(nameList)) event = threading.Event() queueLock = threading.Lock() workQueue = Queue.Queue(1000000) threads = [] threadID = 1 # Create new threads for tName in threadList: thread = HashThread(threadID, tName, event, queueLock, workQueue, kp, des, finds) thread.daemon = True thread.start() threads.append(thread) threadID += 1 # Fill the queue queueLock.acquire() for word in nameList: workQueue.put(word) queueLock.release() # Wait for queue to empty while not workQueue.empty(): pass # Notify threads it's time to exit event.set() # Wait for all threads to complete for t in threads: t.join() return finds
inpt = -1 while inpt != 0: try: inpt = int(raw_input(' 0 - exit \n 1 - check \n')) except ValueError: inpt = -1 continue if inpt == 1: try: file_name = raw_input('Input filename: ') except ValueError: file_name = None continue img = im.loadImageFromPath('../images/%s' % file_name, resize=True, maxSize=800) _kp, _des = sift.detectAndCompute(img, None) kp, des = im.sortKp(_kp, _des, 200) lock.acquire() for t in tasks: t.put(des) lock.release() pass else: pass stopThreads(threads, event)
def loadFiles(filesInQueue=10000, hashPath=HASH_PATH, ext=DES_EXT): def load_descriptors(bow, hashPath=HASH_PATH, withSubFolders=True, ext=DES_EXT): des_list = [] kp_list = [] folder = 0 path = "%s%s/" % (hashPath, folder) while os.path.exists(path): for imagePath in glob.glob("%s*%s" % (path, '.des')): des_list.append(imagePath) kp_list.append('%s%s.kp' % (path, im.fileName(imagePath))) if len(des_list) == FILES_CHECK: folder = -2 break folder += 1 path = "%s%s/" % (hashPath, folder) if len(des_list) == 0: print("Hash files count is empty") return [] print("Files count: %d " % len(des_list)) for n in des_list: bow.add(im.loadDesFromPath(n)) # bow.write(HASH_PATH + 'hash.bat') print("Loaded") images = [] for n in des_list: images.append('http://comicstore.cf/uploads/diamonds/%s.jpg' % im.fileName(n)) return images, kp_list flann = cv2.FlannBasedMatcher(index_params, search_params) bow_train = cv2.BOWKMeansTrainer(50) # toy world, you want more. bow_extract = cv2.BOWImgDescriptorExtractor(sift, flann) images, kps = load_descriptors(bow_train) print 'Getting cluster ...' voc = bow_train.cluster() print 'Set vocabulary ...' bow_extract.setVocabulary(voc) traindata, trainlabels = [], [] j = 0 for i in images: img = im.loadImageFromUrl(i, resize=True, maxSize=800) kp = im.loadKeypointFromPath(kps[j]) traindata.extend(bow_extract.compute(img, kp)) trainlabels.append(j) j += 1 print 'Training SVM' svm = cv2.ml.SVM_create() svm.train(np.array(traindata), cv2.ml.ROW_SAMPLE, np.array(trainlabels)) img = im.loadImageFromPath('%s%s' % (IMG_PATH, 'M2.jpg'), resize=True, maxSize=800) kp, des = im.keypointDesCalc(img, count=100) fbow = bow_extract.compute(img, kp) t_start = cv2.getTickCount() p = svm.predict(fbow) t_end = cv2.getTickCount() print "Time match: %s" % ((t_end - t_start) / cv2.getTickFrequency())