def index(): #json_obj = request.get_json() sensorData = request.form print("[*] REQ: *****************\n", sensorData["spiral"]) print("FILE: ", request.files) spiral = np.array(json.loads(sensorData['spiral'])) meander = np.array(json.loads(sensorData['meander'])) diado = np.array(json.loads(sensorData['diado'])) #save spiral, meander, diado in txt print("SPIRAL: {}\n".format(spiral * 1000)) print("MEANDER: {}\n".format(meander)) print("DIADO: {}\n".format(diado)) #extract features feature_vector_spiral = extractFeatures(spiral) feature_vector_meander = extractFeatures(meander) feature_vector_diado = extractFeatures(diado) print("SPIRAL: {}\n".format(feature_vector_spiral)) print("MEANDER: {}\n".format(feature_vector_meander)) print("DIADO: {}\n".format(feature_vector_diado)) #classify #predicted_spiral = return 'DIAGNOSTICO: P/H'
def onefile(i, f): if logging: print("'%s'" % (f)) features = eF.extractFeatures(f, kNumFeatures=kNumFeatures, shape='1D') assert (len(features) == kNumFeatures) return features
def classify(file_name): params = np.load('ckpts/np_params.npy') _, extension = os.path.splitext(file_name) if extension != '.npy': img = cv2.imread('train_data/notLamina/IMG_100.jpg', 0).astype('float32') else: img = np.load(file_name) features = np.array(extractFeatures(normalize(img))).reshape((1, -1)) _y = np.dot(features, params[0]) + params[1] y = np.dot(sigmoid(_y), params[2]) + params[3] return softmax(y)
def classify(file_name): print "Predicting for " + file_name.split('/')[-1] cwd = os.path.dirname(__file__) params = np.load(os.path.join(cwd, 'ckpts', 'np_params.npy')) _, extension = os.path.splitext(file_name) if extension != '.npy': img = cv2.imread(file_name, 0).astype('float32') else: img = np.load(file_name) if img.ndim > 2: out = [] for i in range(len(img)): features = np.array(extractFeatures(normalize(img[i]))).reshape( (1, -1)) _y = np.dot(features, params[0]) + params[1] y = np.dot(sigmoid(_y), params[2]) + params[3] out.append(softmax(y)) return np.array(out) features = np.array(extractFeatures(normalize(img))).reshape((1, -1)) _y = np.dot(features, params[0]) + params[1] y = np.dot(sigmoid(_y), params[2]) + params[3] return softmax(y)
def testClassifier(): clf = pickle.load(open( "classifier.p", "rb" )) classes = numpy.loadtxt(dataset_root + 'classes.txt', dtype=str) classes = column(classes, 0) image_files = sorted(os.listdir(os.path.join(project_root, test_images_dir))) for image in image_files: features = extractFeatures.extractFeatures( str( os.path.abspath(os.path.join(project_root, test_images_dir, image)) ) ) prediction = clf.predict(features) img = mpimg.imread(str( os.path.abspath(os.path.join(project_root, test_images_dir, image)) )) fig = plt.figure() fig.suptitle(classes[int(prediction[0])], fontsize=14, fontweight='bold') plt.imshow(img)
def extractFeaturesBulk (path): types = ('*.png', '*.jpg') listofFiles = [] for files in types: listofFiles.extend(glob.glob(path+'/'+ files)) elements = [[] for y in listofFiles for i in range(1)] for idx, imagepath in enumerate(listofFiles): img = cv2.imread(imagepath) elements[idx].append(img) elements[idx].append(imagepath) kp, desc = extractFeatures.extractFeatures(img) elements[idx].append(kp) elements[idx].append(desc) return elements
def cityPageScrapper(pageSoup): pageResults = [] homes = pageSoup.findAll('div', {'itemtype': 'http://schema.org/Product'}) for home in homes: resultString = " " #the link gives suburb info as well as the unique p24 identifer link = home.find('a')['href'] resultString = resultString + link + ';' #obtain the street address if it exists street = home.find('span', {"class": "p24_address"}) if type(street) == type( home): #checks to see if the address object has been found resultString = resultString + street.get_text() + ';' else: resultString = resultString + 'NULL' + ';' #obtain the property value try: price = home.find('span', {"class": "p24_price"})['content'] except KeyError: price = 'NULL' resultString = resultString + price + ';' #scrape the size of the property plotSize = home.find('span', {'class': 'p24_size'}) if type(plotSize) == type(home): plotSize = plotSize.find('span', {'class': 'p24_bold'}).get_text() resultString = resultString + plotSize + ';' else: resultString = resultString + 'NULL' + ';' #obtain information related to number of bathrooms, bedrooms and garages featureTree = home.find('span', {'class': 'p24_features'}) featureString = extractFeatures(featureTree) resultString = resultString + featureString #add the information home info to page results pageResults = pageResults + [resultString] return pageResults
import os import cv2 import numpy as np from extractFeatures import extractFeatures from theano_ops.utils import normalize if __name__ == "__main__": _ind = 1 + np.random.permutation(575) train_ind = _ind[:500] test_ind = _ind[500:] lam = [] no_lam = [] for i in train_ind: img = cv2.imread('train_data/newLam/img_{}.jpg'.format(i), 0) lam += [np.array(extractFeatures(normalize(img))).reshape((1, -1))] lam += [ np.array(extractFeatures(normalize(np.fliplr(img)))).reshape( (1, -1)) ] img = cv2.imread('train_data/notLamina/IMG_{}.jpg'.format(i), 0) no_lam += [np.array(extractFeatures(normalize(img))).reshape((1, -1))] no_lam += [ np.array(extractFeatures(normalize(np.fliplr(img)))).reshape( (1, -1)) ] train_x = np.vstack((np.array(lam).squeeze(), np.array(no_lam).squeeze())) train_y = np.hstack((np.ones((1, len(lam))), np.zeros((1, len(no_lam))))) print train_x.shape
def test_extractFeatures(self): img = cv2.imread('lena512color.tiff') enhanced_img = enhanceImage(img) features = extractFeatures(enhanced_img) self.assertIsNotNone(features)
import requests import bs4 from extractPropertyInformation import extractPropertyInformation from cityPageScrapper import cityPageScrapper from wholeCityScrapper import wholeCityScrapper from printToFile import printToFile from extractFeatures import extractFeatures #link = 'https://www.property24.com/for-sale/northam/limpopo/737' #data = wholeCityScrapper(link) #printToFile(data, 'westernCape.csv') res = requests.get( 'https://www.property24.com/for-sale/venterspos/carletonville/gauteng/4943?PropertyCategory=House%2cApartmentOrFlat%2cTownhouse%2cVacantLandOrPlot' ) res.raise_for_status() soup = bs4.BeautifulSoup(res.text, "html.parser") extractFeatures(soup.find('span', {'class': 'p24_features'}))
l2_norm_squared = sum([(p**2).sum() for p in self.to_regularize]) self.cost = T.sum((T.sub(self.outputs, self.y))** 2).mean() + self.lmbd * l2_norm_squared diff = abs(T.argmax(self.outputs, axis=1) - T.argmax(self.y, axis=1)) self.acc = T.sub(1, 1. * T.nonzero(diff)[0].shape[0] / self.y.shape[0]) if __name__ == "__main__": # x_train = np.load('train_x.npy').astype('float32') # y_train = to_categorical(np.load('train_y.npy')).astype('int8') # x_test = np.load('test_x.npy').astype('float32') # y_test = to_categorical(np.load('test_y.npy')).astype('int8') M = SpineClassifier( batch_size=16, input_shape=(1, 252), optimizer=Adam(lr=1e-4), metrics=['loss', 'acc'], init_params=SpineClassifier.restore_params('ckpts/model_snapshot.npy')) #M.train(x_train=x_train, y_train=y_train, x_validation=x_test, y_validation=y_test, nb_epochs=1000) import cv2 from extractFeatures import extractFeatures from theano_ops.utils import normalize img = cv2.imread('train_data/notLamina/IMG_64.jpg', 0) import time t = time.time() inpt = np.array(extractFeatures(normalize(img))).reshape((1, -1)) print M.predict(inpt.astype('float32')) print time.time() - t
def makeData(dim): imageCount = 580 size = (dim, dim) trainingPercent = 0.7 testPercent = 0.1 validationPercent = 0.2 winDim = ceil(2*dim/3) laminae = np.ndarray((dim*dim, winDim*winDim + 84*3)) # labels = np.zeros((dim*dim, 2)) labels = np.zeros((dim*dim)) fileCount = 0 print "Loading Laminae...\n" for i in range(1, imageCount+1): print i # img = Image.open("newLam/img_%d.jpg" %i) img = Image.open("/home/mehran/Desktop/ConvNet/newLam/img_%d.jpg" %i) img = ImageOps.fit(img, size) # imgEn = Image.open("enhanced/img_%d.jpg" %i) imgEn = Image.open("/home/mehran/Desktop/ConvNet/enhanced/img_%d.jpg" %i) imgEn = ImageOps.fit(img, size) segmented = Image.open("/home/mehran/Desktop/ConvNet/base of lamina/lamBase_%d.jpg" %i) segmented = ImageOps.fit(segmented, size) paddedImage = addPaddingZero(img, dim) paddedImageEn = addPaddingZero(imgEn, dim) #paddedSegmented = addPaddingZero(segmented, dim) count = 0 for m in range(0, dim): for n in range(0, dim): window = paddedImage[m:m+winDim, n:n+winDim] windowEn = paddedImageEn[m:m+winDim, n:n+winDim] Res = 0.7*windowEn + 0.3*window laminae[count, :] = np.append(np.reshape(Res, winDim*winDim)/np.amax(Res), np.asarray(extractFeatures.extractFeatures(Res/np.amax(Res)))) S = segmented.load() labels[count] = 1 if S[m, n] > 0 else 0 # labels[count, 0] = 1 if S[m, n] > 0 else 0 # labels[count, 1] = 1 - S[m, n] count += 1 # f = file("leftImgs/Pickles/sample_%d.p" %i, 'wb') # pickle.dump(laminae, f) # posIndices = np.nonzero(labels)[0] posSampleSize = np.count_nonzero(labels) if(posSampleSize > 0): print i negIndices = np.random.randint(0, dim*dim, posSampleSize) a = [labels[v] for v in posIndices] b = [labels[u] for u in negIndices] L = np.hstack((a, b)) Patches = np.vstack(([laminae[v,:] for v in posIndices], [laminae[u,:] for u in negIndices])) f = file("/home/mehran/Desktop/ConvNet/Pickles/balanced/enhanced_overlay+lamBase_dilated_new_labels_with_LDH/sample_balanced_%d.p" %fileCount, 'wb') pickle.dump([Patches, L], f) fileCount += 1
#wavefile = base + '.wav' #wavefile = datadir + 'pre1.wav' #'post1.wav' # wavefile = 'data/test_data/training_3s/100post1_1.wav' # wavefile = 'data/test_data/training_3s/114post2_2.wav' wavefile = 'data/test_data/m_3m/80post1.wav' wavefile = 'data/test_data/m_3m/80post2.wav' if PLAYWAVE: wu.playWavFile(wavefile, kLog=True) if kLogging: wavarr = wu.loadWav(wavefile) #(framerate, np) print(wavarr) features = extractFeatures(wavefile) if kLogging: pp.pprint(features) else: if features != None: print("features.size %d" % (len(features))) pp.pprint(features) else: print("Failed to load '%s'" % wavefile) kNumThreads = 2 datasetroot = datadir + 'test_data/' datasetname = 'm_3m_wf_3s/' heartSoundDb = heartSound.heartSound(
def classify(signal, hx, cap, camera, model): try: # take picture from camera print("taking picture...") timeStamp = strftime("%Y-%m-%d_%H:%M:%S", gmtime()) timeStamp = 'temp' imgName = IMAGE_DATA_PATH + timeStamp + '.jpg' camera.start_preview() camera.capture(imgName) time.sleep(3) camera.stop_preview() # get values from load sensor and capacitive sensor print("sensing load and capacitance") load_value = 0 capacitive = 0 for i in xrange(0, SENSOR_TRIES): # load sensing load_value += hx.get_weight(5) hx.power_down() hx.power_up() # capacitive sensing current_touched = cap.touched() for i in range(12): pin_bit = 1 << i if ((current_touched & pin_bit) and (last_touched & pin_bit)): capacitive = 1 last_touched = current_touched # wait time.sleep(0.05) load_value /= SENSOR_TRIES print("getting audio features...") audioFeatures = extractFeatures.extractFeatures( 44100, np.array(signal)) # format data to pass to classifiers args = [str(load_value), str(capacitive)] + audioFeatures for i in xrange(0, len(args)): args[i] = str(args[i]) # run classifiers on data print("running classifiers") knn_output, log_output, svm_output, rf_output = run_classifiers.run_classifiers( TRINITY_DATA_PATH, args) svm_indicies, svm_preds = svm_output knn_indicies, knn_preds = knn_output rf_indicies, rf_preds = rf_output log_indicies, log_preds = log_output ''' svm_indicies, svm_preds = run_svm.run_svm(TRINITY_DATA_PATH, args) knn_indicies, knn_preds = run_knn.run_knn(TRINITY_DATA_PATH, args) rf_indicies, rf_preds = run_randomforest.run_randomforest(TRINITY_DATA_PATH, args) log_indicies, log_preds = run_log.run_log(TRINITY_DATA_PATH, args) #img_indicies, img_preds = run_NN.predict(model, imgName) ''' max_prob_svm = max(svm_preds) max_arg_svm = svm_indicies[np.argmax(svm_preds)] max_prob_knn = max(knn_preds) max_arg_knn = knn_indicies[np.argmax(knn_preds)] max_prob_rf = max(rf_preds) max_arg_rf = rf_indicies[np.argmax(rf_preds)] max_prob_log = max(log_preds) max_arg_log = log_indicies[np.argmax(log_preds)] ''' max_prob_NN = max(img_preds) max_arg_NN = img_indicies[np.argmax(img_preds)] if (max_prob_knn >= max_prob_NN): final_pred = max_arg_knn else: final_pred = max_arg_NN ''' final_pred = 'potato' print("SVM prediction is..." + max_arg_svm) print("KNN prediction is..." + max_arg_knn) print("Random Forest prediction is..." + max_arg_rf) print("Logistic Regression prediction is..." + max_arg_log) print("Final prediction is..." + final_pred) addData(recieved, args, TRINITY_DATA_PATH_SAVE) except (KeyboardInterrupt, SystemExit): sys.exit()
def main(): print("initializing sensors...\n") # initialize load sensor hx = HX711(5, 6) hx.set_reading_format("LSB", "MSB") hx.set_reference_unit(-428.72) hx.reset() hx.tare() # initialize capacitive sensor cap = MPR121.MPR121() if not cap.begin(): print('Error initializing MPR121. Check your wiring!') sys.exit(1) last_touched = cap.touched() capacitive = 0 # initialize microphone card = 'sysdefault:CARD=Device' fs = 44100 num_ms = 132300 inp = alsaaudio.PCM(alsaaudio.PCM_CAPTURE, alsaaudio.PCM_NORMAL, card) inp.setchannels(1) inp.setrate(fs) inp.setformat(alsaaudio.PCM_FORMAT_S16_LE) inp.setperiodsize(32) # initialize camera camera = PiCamera() # initialize neural net model # model = run_NN.initializeNN() while (True): try: val = raw_input("\nPRESS ANY KEY TO START ") print("start recording...") totalLen = 0 signal = [] while (totalLen < fs * 3): l, data = inp.read() if (l > 0): signal += list(np.fromstring(data, 'int16')) totalLen += l audioFeatures = extractFeatures.extractFeatures( fs, np.array(signal)) print("done recording...") print("capturing image...") timeStamp = strftime("%Y-%m-%d_%H:%M:%S", gmtime()) timeStamp = 'temp' imgName = IMAGE_DATA_PATH + timeStamp + '.jpg' camera.start_preview() camera.capture(imgName) camera.stop_preview() # get values from load sensor and capacitive sensor count = LOAD_SENSOR_TRIES loadValue = 0 print("sensing weight and capacitance...") while (count > 0): count -= 1 # load sensing loadValue += hx.get_weight(5) hx.power_down() hx.power_up() # capacitive sensing current_touched = cap.touched() for i in range(12): pin_bit = 1 << i if ((current_touched & pin_bit) and (last_touched & pin_bit)): capacitive = 1 last_touched = current_touched # wait time.sleep(0.1) # print results to STDOUT loadValue /= LOAD_SENSOR_TRIES print("Data Features:") print("\tweight = " + str(loadValue)) print("\tcapacitance = " + str(capacitive)) print("\tamplitude = " + str(audioFeatures[0])) print("\tnumber of peaks = " + str(audioFeatures[1])) print("\tcentroid = " + str(audioFeatures[2])) print("\tspectrum = " + str(audioFeatures[3])) ''' print("\tMel-Freq Cep Coeff = " + str(audioFeatures[4])) print("\tRolloff Point = " + str(audioFeatures[5])) print("\tMax Spectral Flux = " + str(audioFeatures[6])) print("\tAvg Spectral Flux = " + str(audioFeatures[7])) ''' plotAudio(fs, signal) print("predicting recycling category...") # format data to pass to classifiers args = [str(loadValue), str(capacitive)] + audioFeatures for i in xrange(0, len(args)): args[i] = str(args[i]) # run classifiers on data # 0 = compost # 1 = metal # 2 = plastic # if highest probability is less than 50% then predict trash svm_indicies, svm_preds = run_svm.run_svm(TRINITY_DATA_PATH, args) knn_indicies, knn_preds = run_knn.run_knn(TRINITY_DATA_PATH, args) rf_indicies, rf_preds = run_randomforest.run_randomforest( TRINITY_DATA_PATH, args) log_indicies, log_preds = run_log.run_log(TRINITY_DATA_PATH, args) # img_indicies, img_preds = run_NN.predict(model, imgName) # max_prob_NN = max(img_preds) # max_arg_NN = img_indicies[np.argmax(img_preds)] max_prob_NN = 0.0 max_arg_NN = 'trash' max_prob_svm = max(svm_preds) max_arg_svm = svm_indicies[np.argmax(svm_preds)] max_prob_knn = max(knn_preds) max_arg_knn = knn_indicies[np.argmax(knn_preds)] max_prob_rf = max(rf_preds) max_arg_rf = rf_indicies[np.argmax(rf_preds)] max_prob_log = max(log_preds) max_arg_log = log_indicies[np.argmax(log_preds)] if (max_prob_svm <= 0.45): max_arg_svm = 'trash' if (max_prob_knn <= 0.45): max_arg_knn = 'trash' if (max_prob_rf <= 0.45): max_arg_rf = 'trash' if (max_prob_log <= 0.45): max_arg_log = 'trash' # if (max_prob_NN <= 0.45): # max_arg_NN = 'trash' print("SVM prediction is..." + max_arg_svm) print("KNN prediction is..." + max_arg_knn) print("Random Forest prediction is..." + max_arg_rf) print("Logistic Regression prediction is..." + max_arg_log) # print("NN prediction is..."+max_arg_NN) ''' if (max_arg_knn == 'trash'): final_pred = max_arg_NN if (max_arg_NN == 'trash'): final_pred = max_arg_knn if (max_arg_knn != 'trash' and max_arg_NN != 'trash'): if (max_prob_knn > max_prob_NN): final_pred = max_arg_knn else: final_pred = max_arg_NN print("FINAL PREDICTION IS... " + final_pred) # save data val = raw_input("should I save this data point? ") if (val == 'yes'): val = raw_input("what is the category of this point? ") addData(val,args, TRINITY_DATA_PATH) ''' except (KeyboardInterrupt, SystemExit): camera.close() GPIO.cleanup() sys.exit() '''
import cPickle as cp except: import pickle as cp import matplotlib.pyplot as plt from extractFeatures import extractFeatures if len(sys.argv) != 3: print('python ' + sys.argv[0] + '<folder_SVR> <file_audio>') sys.exit() else: print(sys.argv) folderSVR = sys.argv[1] file_audio = sys.argv[2] features = extractFeatures(file_audio, folderSVR) posnan = np.isnan(features) features[posnan] = 0 filest = folderSVR + 'scaler.obj' try: fid2 = open(filest, 'r') st = cp.load(fid2) fid2.close() except: with open(filest, 'rb') as f: st = cp.load(f, encoding='latin1') #features=st.transform(features)
sigma = 1.5 threshold = 0.0005 max_corners = 200 isSimple = False c1 = detectCorners(im1, sigma, threshold) c2 = detectCorners(im2, sigma, threshold) n1 = min(max_corners, c1.shape[1]) c1 = c1[:, 0:n1] n2 = min(max_corners, c2.shape[1]) c2 = c2[:, 0:n2] #Compute feature descriptors patch_radius = 5 f1 = extractFeatures(im1, c1, patch_radius) f2 = extractFeatures(im2, c2, patch_radius) #Compute matches matches = computeMatches(f1, f2) showMatches(im1, im2, c1, c2, matches, title='All correspondences') plt.show() #Estimate transformation inliers, transf = ransac(matches, c1, c2) good_matches = np.zeros_like(matches) - 1 good_matches[inliers] = matches[inliers] showMatches(im1, im2, c1, c2, good_matches, title='Inliers') plt.show()