def detect(anno_file, img_file, id): global model image = np.asarray(cv2.imread(img_file)) plt.imshow(image) plt.show() for img in util.pyramid(image): raw = [i for i in util.slidingWindow(img, 50, (100, 100))] hog_window = [cv2.resize(window, (128, 128)) for _, _, window in raw] hog_window = [cv2.HOGDescriptor().compute(i).reshape((-1)) for i in hog_window] detection = util.splitter(hog_window, model.predict) print(detection) with_id = zip(range(0, len(raw)), detection) with_id = [i for i in with_id if i[1] != 3] for res in with_id: print(res)
"max Y", "max Z" ] class_names = ["Sedentary", "Active"] print("Extracting features and labels for window size {} and step size {}...". format(window_size, step_size)) sys.stdout.flush() n_features = len(feature_names) X = np.zeros((0, n_features)) y = np.zeros(0, ) for i, window_with_timestamp_and_label in slidingWindow( data, window_size, step_size): # omit timestamp and label from accelerometer window for feature extraction: window = window_with_timestamp_and_label[:, 1:-1] # extract features over window: x = extract_features(window) # append features: X = np.append(X, np.reshape(x, (1, -1)), axis=0) # append label: y = np.append(y, window_with_timestamp_and_label[10, -1]) print("Finished feature extraction over {} windows".format(len(X))) print("Unique labels found: {}".format(set(y))) sys.stdout.flush() # Plot data points # print("Plotting data points...")
def predict(): """ Given a window of accelerometer data, predict the activity label. Then use the onActivityDetected(activity) function to notify the Android must use the same feature extraction that you used to train the model. """ # have to fix the window size prediction_array=np.array([]) p_time=np.array([]) clf = load("classifier.pickle") # maybe we are not even filling buffer but just running a for loop data_file_ss_11 = os.path.join('data', 'accel_data-12-08-BP-ss.csv') data_ss_11 = np.loadtxt(data_file_ss_11, delimiter=',', dtype = object, converters = {0: np.float, 1: np.float, 2: np.float, 3: lambda t: datetime.strptime(t.decode("utf-8"), "%d/%m/%Y %H:%M")}) data_ss_11 = np.insert(data_ss_11, 3, 0, axis = 1) hdata_file_ss_11 = os.path.join('data', 'BPM_2017-12-08-BP-ss.csv') hdata_ss_11 = np.loadtxt(hdata_file_ss_11, delimiter=',', dtype = object, converters = {0: lambda t: datetime.strptime(t.decode("utf-8"), "%d/%m/%Y %H:%M"), 1: np.float}) data = data_ss_11 hdata = hdata_ss_11 window_size=20 step_size=20 #because hr data in backwards count = len(hdata)-1 for i,window_with_timestamp_and_label in slidingWindow(data, window_size, step_size): temp = np.zeros((1,3)) #while time at row count is under time at accel, increase count (move to next row) #only have one window. Each row in window has own observation that needs hr for row in range(len(window_with_timestamp_and_label)): # print (hdata[count]) # print(" ") # print (window_with_timestamp_and_label[row]) while hdata[count][0] < window_with_timestamp_and_label[row][4] and count > 0: count=count-1 print("changed count ", count) if row==0: p_time=np.append(p_time,window_with_timestamp_and_label[row][4]) #remove timestamps from accel data temp = np.vstack((temp,window_with_timestamp_and_label[row][:-2])) #add hr data to accel hr_label = np.append(hdata[count][1],9) window_with_timestamp_and_label[row] = np.append(temp[row+1], hr_label) #add in label (hr_data is on form hr, t, label) #remove time and label for feature extraction window = window_with_timestamp_and_label[:,:-1] # extract features over window: # print("Buffer filled. Run your classifier.") prediction=clf.predict(np.reshape(extract_features(window),(1,-1)))[0] prediction_array= np.append(prediction_array,prediction) # print prediction # for i in range(0,len(prediction_array)): # p_time=np.append(p_time,i) plt.plot(p_time,prediction_array) plt.xlabel('Time') plt.ylabel('Predicted Label') plt.show() return
# %%--------------------------------------------------------------------------- # # Import Data from sample-data.csv # # ----------------------------------------------------------------------------- x = dict() y = dict() x[0.0] = [] x[1.0] = [] y[0.0] = [] y[1.0] = [] data_file = os.path.join('data', 'sample-data.csv') data = np.genfromtxt(data_file, delimiter=',') for i, window_with_timestamp_and_label in slidingWindow(data, 100): window = window_with_timestamp_and_label[:, 1:-1] label = scipy.stats.mstats.mode(window_with_timestamp_and_label[:, 4])[0] # Add feature extraction here x[label[0]] += [] y[label[0]] += [] stationary = plt.scatter(x[0], y[0], label='Stationary', color='red') walking = plt.scatter(x[1], y[1], label='Walking', color='green') # %%--------------------------------------------------------------------------- # # Format and Show Plot # # ----------------------------------------------------------------------------- plt.xlabel('Z entropy')