def cluster_images(self): image.LOAD_TRUNCATED_IMAGES = True model = NNModel(weights='imagenet', include_top=False) filelist = glob.glob(os.path.join(self.org_images, '*.jpg')) filelist.sort() featurelist = [] for i, imagepath in enumerate(filelist): info = "Progress {curr}/{total}".format(curr=i, total=len(filelist)) print("\r" + info, end="") # noinspection PyBroadException try: img = image.load_img(imagepath, target_size=(224, 224)) img_data = image.img_to_array(img) img_data = np.expand_dims(img_data, axis=0) img_data = preprocess_input(img_data) features = np.array(model.predict(img_data, batch_size=1000)) featurelist.append(features.flatten()) except: continue # Clustering to_fit = np.array(featurelist) kmeans = KMeans(n_clusters=self.k, random_state=0, verbose=1).fit(to_fit) # This is a computation heavy task so I'm copying the images renamed according to cluster for the next time. try: os.makedirs(self.cluster_home) except OSError: pass for i, m in enumerate(kmeans.labels_): shutil.copy( filelist[i], self.cluster_home + str(m) + "_" + os.path.basename(filelist[i])) return kmeans
framedur = time.time() for batchframeNum in range(0, 1000): s = proc.stdout.read(nbytes) if len(s) == nbytes: result = np.frombuffer(s, dtype='uint8') result.shape = MODLE_INPUT_DIMS + (-1, ) frameBatch.append(result) if cv2.waitKey(1) == ord('q'): run = False framedur = time.time() - framedur if len(frameBatch) > 0: x = np.array(frameBatch) x = preprocess_input(x) preddur = time.time() pred_features_batch = model.predict(x) pred_features_batch.shape = (pred_features_batch.shape[0], -1) preddur = time.time() - preddur lastAccepted = False for result, pred_features in zip(frameBatch, pred_features_batch): frameNum += 1 timeStamp = frameNum * (1 / RATE) total += 1 if lastAccepted: lastAccepted = False skipsize += 1 continue