def detect_audios(model, baseModel, audiosPath): vgg_extract.parse_audio_files(audiosPath, 'tmpImages') imagesPath = 'tmpImages' featuresLength = np.prod(baseModel.layers[-1].output.shape[1:]) imagePaths = list(paths.list_images('tmpImages')) classNames = target_names() for imagePath in imagePaths: features = [] batchImages = [] image = load_img(imagePath, target_size=(WIDTH, HEIGHT)) image = img_to_array(image) image = np.expand_dims(image, axis=0) image = imagenet_utils.preprocess_input(image) batchImages.append(image) batchImages = np.vstack(batchImages) batchFeatures = baseModel.predict(batchImages, batch_size=len(batchImages)) features.append(batchFeatures.reshape(len(batchImages), featuresLength)) features = np.vstack(features) preds = model.predict(features) pred = preds[0] print(imagePath + ": " + str(classNames[int(pred)])) shutil.rmtree('tmpImages')
def detect_real_time(model, baseModel): featuresLength = np.prod(baseModel.layers[-1].output.shape[1:]) imagePaths = ['test_sample.png'] classNames = target_names() bs = 5 while True: features = [] record_input() for i in np.arange(0, len(imagePaths), bs): batchPaths = imagePaths[i:i + bs] batchImages = [] for (_, imagePath) in enumerate(batchPaths): image = load_img(imagePath, target_size=(WIDTH, HEIGHT)) image = img_to_array(image) image = np.expand_dims(image, axis=0) image = imagenet_utils.preprocess_input(image) batchImages.append(image) batchImages = np.vstack(batchImages) batchFeatures = baseModel.predict(batchImages, batch_size=len(batchImages)) features.append( batchFeatures.reshape(len(batchImages), featuresLength)) features = np.vstack(features) pred = model.predict_proba(features)[0] index = np.argmax(pred) prob = pred[index] print(str(classNames[index]) + " - " + str(prob))
def detect_real_time(model, baseModel, storeFile=False): imagePaths = ['test_sample.png'] classNames = target_names() try: with sd.InputStream(samplerate=sample_rate, callback=callback, channels=2): while run: q.get() melspectrogram_preprocess(data, sample_rate) imagePath = imagePaths[0] image = cv2.imread(imagePath) image = aap.preprocess(image) image = iap.preprocess(image) image = np.expand_dims(image, axis=0) / 255.0 result = model.predict(image) pred = result.argmax(axis=1)[0] print(result) prob = result[0][pred] print(str(classNames[pred]) + " - " + str(prob)) except KeyboardInterrupt: print('\nRecording finished')
def detect_images(model, baseModel, imagesPath): imagePaths = list(paths.list_images(imagesPath)) classNames = target_names() labels = [p.split(os.path.sep)[-2] for p in imagePaths] preds = [] for i in np.arange(0, len(imagePaths)): imagePath = imagePaths[i] image = cv2.imread(imagePath) image = aap.preprocess(image) image = iap.preprocess(image) image = np.expand_dims(image, axis=0) / 255.0 result = model.predict(image) pred = result.argmax(axis=1)[0] preds.append(pred) labels = np.vstack(labels) lb = LabelEncoder().fit(np.vstack(classNames)) labels = lb.transform(labels) print(classification_report(labels, preds, target_names=classNames))
def detect_audios(model, baseModel, audiosPath): vgg_extract.parse_audio_files(audiosPath, 'tmpImages') imagesPath = 'tmpImages' imagePaths = list(paths.list_images('tmpImages')) classNames = target_names() for imagePath in imagePaths: image = cv2.imread(imagePath) image = aap.preprocess(image) image = iap.preprocess(image) image = np.expand_dims(image, axis=0) / 255.0 result = model.predict(image) pred = result.argmax(axis=1)[0] print(imagePath + ": " + str(classNames[int(pred)])) shutil.rmtree('tmpImages')
def detect_real_time(model, baseModel): imagePaths = ['test_sample.png'] classNames = target_names() while True: record_input() imagePath = imagePaths[0] image = cv2.imread(imagePath) image = aap.preprocess(image) image = iap.preprocess(image) image = np.expand_dims(image, axis=0) / 255.0 result = model.predict(image) pred = result.argmax(axis=1)[0] print(result) prob = result[0][pred] print(str(classNames[pred]) + " - " + str(prob))
def detect_images(model, baseModel, imagesPath): featuresLength = np.prod(baseModel.layers[-1].output.shape[1:]) imagePaths = list(paths.list_images(imagesPath)) classNames = target_names() bs = 5 labels = [p.split(os.path.sep)[-2] for p in imagePaths] features = [] for i in np.arange(0, len(imagePaths), bs): batchPaths = imagePaths[i:i + bs] batchImages = [] for (_, imagePath) in enumerate(batchPaths): image = load_img(imagePath, target_size=(WIDTH, HEIGHT)) image = img_to_array(image) image = np.expand_dims(image, axis=0) image = imagenet_utils.preprocess_input(image) batchImages.append(image) batchImages = np.vstack(batchImages) batchFeatures = baseModel.predict(batchImages, batch_size=len(batchImages)) features.append(batchFeatures.reshape(len(batchImages), featuresLength)) labels = np.vstack(labels) lb = LabelEncoder().fit(np.vstack(classNames)) labels = lb.transform(labels) features = np.vstack(features) preds = model.predict(features) print(classification_report(labels, preds, target_names=classNames))