def get_test_data(): ''' Takes in the data for GAIT and resizes the array in the form most suitable for the train ''' Final_Array = np.zeros(shape=(20, 9, 2)) trainer_vid("test/pic") for j in range(20): image = imread("test/pic" + str(j) + ".PNG", mode='RGB') image_batch = data_to_input(image) # Compute prediction with the CNN outputs_np = sess.run(outputs, feed_dict={inputs: image_batch}) scmap, locref, _ = predict.extract_cnn_output(outputs_np, cfg) # Extract maximum scoring location from the heatmap, assume 1 person pose = predict.argmax_pose_predict(scmap, locref, cfg.stride) temp = np.zeros(shape=(9, 2)) list_indexes = [0, 1, 4, 5, 6, 7, 10, 11, 13] for k in range(9): temp[k] = pose[list_indexes[k]][0:2] Final_Array[j] = temp print(Final_Array.shape) temp2 = np.zeros(shape=(2, 20, 9)) temp2[0] = Final_Array[:, :, 0] temp2[1] = Final_Array[:, :, 1] Answer = np.zeros(shape=(9, 2, 20)) for i in range(9): Answer[i] = temp2[:, :, i] print(Answer.shape) return Answer
def im_process(sess, cfg, inputs, outputs, image, out_port, fig="preview"): image_batch = data_to_input(image) # image = image[:, :, (2, 1, 0)] // Remove comment to get "good" image in opencv timer = Timer() timer.tic() # Compute prediction with the CNN outputs_np = sess.run(outputs, feed_dict={inputs: image_batch}) scmap, locref, _ = predict.extract_cnn_output(outputs_np, cfg) # Extract maximum scoring location from the heatmap, assume 1 person pose = predict.argmax_pose_predict(scmap, locref, cfg.stride) timer.toc() print('Detection took {:.3f}s'.format(timer.total_time)) # Visualise # visualize.show_heatmaps(cfg, image, scmap, pose) # plt.figure() # plt.imshow(visualize.visualize_joints(image, pose)) CONF_THRES = 0.8 stream_parts(out_port, pose) image = draw_links(image, pose) image = visualize.visualize_joints(image, pose, threshold=CONF_THRES) if args.cv_show: cv2.imshow(fig, image) return image
def main(): # paths to setup annotation_path = '/home/babybrain/Escritorio/300145_via.json' frames_path = '/home/babybrain/Escritorio/300145' # get the x-y anotations for each frame annotations = load_annotations(annotation_path) # get x, y positions for a certain part part_id_index = 4 # we'll get elbows, need left and right (algorithm doesn't discriminate) file_anno, x_anno_r, y_anno_r = get_xy_for('r-elbow', annotations) _, x_anno_l, y_anno_l = get_xy_for('l-elbow', annotations) # get the x,y model prediction for each frame annotated cfg = load_config( "/home/babybrain/PycharmProjects/pose-tensorflow/demo/pose_cfg_babybrain.yaml" ) # Load and setup CNN part detector sess, inputs, outputs = predict.setup_pose_prediction(cfg) # run session for each frame image annotated x_model = np.empty(len(file_anno)) y_model = np.empty(len(file_anno)) for index, an_image in enumerate(file_anno): infile = "{path}/{name}".format(path=frames_path, name=an_image) image = imread(infile, mode='RGB') image_batch = data_to_input(image) # Compute prediction with CNN outputs_np = sess.run(outputs, feed_dict={inputs: image_batch}) scmap, locref, _ = predict.extract_cnn_output(outputs_np, cfg) # Extract maximum scoring location from the heatmap, assume 1 person pose = predict.argmax_pose_predict(scmap, locref, cfg.stride) x_model[index] = pose[part_id_index, 0] y_model[index] = pose[part_id_index, 1] # now calculate distances distances_r = calculate_distances(x_model, y_model, x_anno_r, y_anno_r) distances_l = calculate_distances(x_model, y_model, x_anno_l, y_anno_l) # merge the best distance results distances = [min(xr, xl) for xr, xl in zip(distances_r, distances_l)] distances = np.array(distances) distance_steps, rates = detection_rate(distances, nsteps=50) rates = rates * 100 # finally plot the graph fig, ax = plt.subplots() ax.plot(distance_steps, rates) ax.set_xlabel('Normalized Distance') ax.set_ylabel('Detection %') ax.set_title('Distance threshold vs Detection Ratio') ax.set_xlim([0, 0.5]) plt.show()
def preprocess(video_name, duration): source_path = f'./data/video/{video_name}.mp4' csv_base_path = './data/poses/' if not os.path.exists(csv_base_path): os.makedirs(csv_base_path) csv_path = f'{csv_base_path}{video_name}_poses.csv' audio_base_path = './data/audio/' if not os.path.exists(audio_base_path): os.makedirs(audio_base_path) audio_path = f'{audio_base_path}{video_name}.mp3' start_time = datetime.now() video = mpe.VideoFileClip(source_path) if duration < 0: duration = video.duration frame_count = int(video.fps * duration) frame_length = 1 / video.fps print( f'video length: {video.duration}s fps: {video.fps} frame count: {frame_count}' ) # Load and setup CNN part detector cfg = load_config('./pose_cfg.yaml') sess, inputs, outputs = predict.setup_pose_prediction(cfg) print('pose model loaded') poses = [] times = [] for i in range(frame_count): t = i * frame_length frame = video.get_frame(t) image_batch = data_to_input(frame) # Compute prediction with the CNN outputs_np = sess.run(outputs, feed_dict={inputs: image_batch}) scmap, locref, _ = predict.extract_cnn_output(outputs_np, cfg) # Extract maximum scoring location from the heatmap, assume 1 person pose = predict.argmax_pose_predict(scmap, locref, cfg.stride) poses.append(pose) times.append(t) if i % 100 == 0: print( f'processed frame: {i}/{frame_count} elapsed time: {datetime.now() - start_time}', end='\r') sess.close() print(f'saving poses at {csv_path}') save_poses(np.array(poses), times, cfg, csv_path) print(f'saving audio at {audio_path}') video.audio.write_audiofile(audio_path) print(f'total time: {datetime.now() - start_time}')
def getpose(image, cfg, outputs, outall=False): ''' Adapted from DeeperCut, see pose-tensorflow folder''' image_batch = data_to_input(skimage.color.gray2rgb(image)) outputs_np = sess.run(outputs, feed_dict={inputs: image_batch}) scmap, locref = predict.extract_cnn_output(outputs_np, cfg) pose = predict.argmax_pose_predict(scmap, locref, cfg.stride) if outall: return scmap, locref, pose else: return pose
def get_pose(image, d=cfg): image = resize_image(image) image_batch = data_to_input(image) outputs_np = d['sess'].run(d['outputs'], feed_dict={d['inputs']: image_batch}) scmap, locref, _ = predict.extract_cnn_output(outputs_np, d['cfg']) pose = predict.argmax_pose_predict(scmap, locref, d['cfg'].stride) return pose
def test_net(visualise, cache_scoremaps): logging.basicConfig(level=logging.INFO) cfg = load_config() dataset = create_dataset(cfg) dataset.set_shuffle(False) dataset.set_test_mode(True) sess, inputs, outputs = setup_pose_prediction(cfg) if cache_scoremaps: out_dir = cfg.scoremap_dir if not os.path.exists(out_dir): os.makedirs(out_dir) num_images = dataset.num_images predictions = np.zeros((num_images, ), dtype=np.object) for k in range(num_images): print('processing image {}/{}'.format(k, num_images - 1)) batch = dataset.next_batch() outputs_np = sess.run(outputs, feed_dict={inputs: batch[Batch.inputs]}) scmap, locref, pairwise_diff = extract_cnn_output(outputs_np, cfg) pose = argmax_pose_predict(scmap, locref, cfg.stride) pose_refscale = np.copy(pose) pose_refscale[:, 0:2] /= cfg.global_scale predictions[k] = pose_refscale if visualise: img = np.squeeze(batch[Batch.inputs]).astype('uint8') visualize.show_heatmaps(cfg, img, scmap, pose) visualize.waitforbuttonpress() if cache_scoremaps: base = os.path.basename(batch[Batch.data_item].im_path) raw_name = os.path.splitext(base)[0] out_fn = os.path.join(out_dir, raw_name + '.mat') scipy.io.savemat(out_fn, mdict={'scoremaps': scmap.astype('float32')}) out_fn = os.path.join(out_dir, raw_name + '_locreg' + '.mat') if cfg.location_refinement: scipy.io.savemat( out_fn, mdict={'locreg_pred': locref.astype('float32')}) scipy.io.savemat('predictions.mat', mdict={'joints': predictions}) sess.close()
def test_net(visualise, cache_scoremaps): logging.basicConfig(level=logging.INFO) cfg = load_config() dataset = create_dataset(cfg) dataset.set_shuffle(False) dataset.set_test_mode(True) sess, inputs, outputs = setup_pose_prediction(cfg) if cache_scoremaps: out_dir = cfg.scoremap_dir if not os.path.exists(out_dir): os.makedirs(out_dir) num_images = dataset.num_images predictions = np.zeros((num_images,), dtype=np.object) for k in range(num_images): print('processing image {}/{}'.format(k, num_images-1)) batch = dataset.next_batch() outputs_np = sess.run(outputs, feed_dict={inputs: batch[Batch.inputs]}) scmap, locref, pairwise_diff = extract_cnn_output(outputs_np, cfg) pose = argmax_pose_predict(scmap, locref, cfg.stride) pose_refscale = np.copy(pose) pose_refscale[:, 0:2] /= cfg.global_scale predictions[k] = pose_refscale if visualise: img = np.squeeze(batch[Batch.inputs]).astype('uint8') visualize.show_heatmaps(cfg, img, scmap, pose) visualize.waitforbuttonpress() if cache_scoremaps: base = os.path.basename(batch[Batch.data_item].im_path) raw_name = os.path.splitext(base)[0] out_fn = os.path.join(out_dir, raw_name + '.mat') scipy.io.savemat(out_fn, mdict={'scoremaps': scmap.astype('float32')}) out_fn = os.path.join(out_dir, raw_name + '_locreg' + '.mat') if cfg.location_refinement: scipy.io.savemat(out_fn, mdict={'locreg_pred': locref.astype('float32')}) scipy.io.savemat('predictions.mat', mdict={'joints': predictions}) sess.close()
def get_position(image, prev_pos): x = 0 y = 0 cropped_image = image if prev_pos[0] > 0 and prev_pos[1] > 0: BOX_SIZE = 120 x = max(0, prev_pos[0] - BOX_SIZE / 2) y = max(0, prev_pos[1] - BOX_SIZE / 2) cropped_image = image[y:(y + BOX_SIZE), x:(x + BOX_SIZE)] ''' Adapted from DeeperCut, see pose-tensorflow folder''' image_batch = data_to_input(skimage.color.gray2rgb(cropped_image)) outputs_np = sess.run(outputs, feed_dict={inputs: image_batch}) scmap, locref = predict.extract_cnn_output(outputs_np, cfg) pose = predict.argmax_pose_predict(scmap, locref, cfg.stride) pose[0][0] += x pose[0][1] += y return pose[0][0], pose[0][1], pose[0][2]
def getpose(image, cfg, outputs, outall=False): ''' Adapted from DeeperCut, see pose-tensorflow folder''' # image_batch = data_to_input(skimage.color.gray2rgb(image)) image_batch = image outputs_np = sess.run(outputs, feed_dict={inputs: image_batch}) scmap, locref = predict.extract_cnn_output(outputs_np, cfg) img_num = scmap.shape[0] pose = np.empty((image_batch.shape[0], cfg.num_joints*3), dtype='float64') # times 3 because each joint has x, y, and confidence values for i in range(img_num): pose[i] = predict.argmax_pose_predict(scmap[i], locref[i], cfg.stride).flatten() if outall: return scmap, locref, pose else: return pose
def predict_frame(video, t): frame_count = int(video.fps * video.duration) frame_length = 1 / video.fps # Load and setup CNN part detector cfg = load_config('./pose_cfg.yaml') sess, inputs, outputs = predict.setup_pose_prediction(cfg) frame = video.get_frame(t) image_batch = data_to_input(frame) # Compute prediction with the CNN outputs_np = sess.run(outputs, feed_dict={inputs: image_batch}) scmap, locref, _ = predict.extract_cnn_output(outputs_np, cfg) # Extract maximum scoring location from the heatmap, assume 1 person pose = predict.argmax_pose_predict(scmap, locref, cfg.stride) return pose
def disp_pic(new=False, where="your_file4"): ''' Displays image ''' if (new): new_pic("QWERTY12345") where = "QWERTY12345" image = imread(where + ".PNG", mode='RGB') image_batch = data_to_input(image) # Compute prediction with the CNN outputs_np = sess.run(outputs, feed_dict={inputs: image_batch}) scmap, locref, _ = predict.extract_cnn_output(outputs_np, cfg) # Extract maximum scoring location from the heatmap, assume 1 person pose = predict.argmax_pose_predict(scmap, locref, cfg.stride) visualize.show_heatmaps(cfg, image, scmap, pose) visualize.waitforbuttonpress()
def arms(new=False, where="your_file"): ''' Does arm_span and arm_span_est ''' scale = 0 #Number of inches per pixel if (new): new_pic(where + ".PNG") image = imread(where + ".PNG", mode='RGB') image_batch = data_to_input(image) # Compute prediction with the CNN outputs_np = sess.run(outputs, feed_dict={inputs: image_batch}) scmap, locref, _ = predict.extract_cnn_output(outputs_np, cfg) # Extract maximum scoring location from the heatmap, assume 1 person pose = predict.argmax_pose_predict(scmap, locref, cfg.stride) scale = set_scale(pose) print(arm_span(pose, scale)) print(arm_span_est(pose, scale)) disp_pic(where=where)
def get_person_data(person): ''' Takes in the data for GAIT and resizes the array in the form most suitable for the train ''' Final_Array = np.zeros(shape=(15, 20, 9, 2)) for i in range(15): trainer_vid("walk_data/" + person + "/vid" + str(i) + "/pic") if (input("Video index: " + str(i) + " has been taken. Type yeet to leave:\n") == "yeet"): return for i in range(15): for j in range(20): image = imread("walk_data/" + person + "/vid" + str(i) + "/pic" + str(j) + ".PNG", mode='RGB') image_batch = data_to_input(image) # Compute prediction with the CNN outputs_np = sess.run(outputs, feed_dict={inputs: image_batch}) scmap, locref, _ = predict.extract_cnn_output(outputs_np, cfg) # Extract maximum scoring location from the heatmap, assume 1 person pose = predict.argmax_pose_predict(scmap, locref, cfg.stride) temp = np.zeros(shape=(9, 2)) list_indexes = [0, 1, 4, 5, 6, 7, 10, 11, 13] for k in range(9): temp[k] = pose[list_indexes[k]][0:2] Final_Array[i][j] = temp print(str(i) + " is finished") print(Final_Array.shape) Answer = np.zeros(shape=(9, 15, 20, 2)) for i in range(9): Answer[i] = Final_Array[:, :, i, :] print(Answer.shape) return Answer
def play(): global VIDEO VIDEO = True cap = cv2.VideoCapture(0) #lancement de la caméra cap.set( 3, 160) #redimensionnement de la video (reduire la taille des données) cap.set(4, 120) while VIDEO: # Capture frame-by-frame ret, image = cap.read() image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) image_batch = data_to_input(image) # Compute prediction with the CNN outputs_np = sess.run(outputs, feed_dict={inputs: image_batch}) scmap, locref, _ = predict.extract_cnn_output(outputs_np, cfg) # print (scmap) # Extract maximum scoring location from the heatmap, assume 1 person pose = predict.argmax_pose_predict(scmap, locref, cfg.stride) # Visualise #visualize.show_heatmaps(cfg, image, scmap, pose) visim = visualize.visualize_joints(image, pose) #visim = image fenetre_fft.display_image(visim) print("OK") cv2.VideoCapture(0).release() #extinction de la camera
def kick_vid(new=False): ''' Takes a video, takes 40 frames from the video, maps the body in each, and finds the maximum height ''' shoe_size = int(input("What is your shoe size (US):\n")) scale = 0 #Number of inches per pixel if (new): vid_view(sec=10) print("GO") vid_pics(sec=4) ans = [] for i in range(40): image = imread("temp/vid_pic" + str(i) + ".PNG", mode='RGB') image_batch = data_to_input(image) # Compute prediction with the CNN outputs_np = sess.run(outputs, feed_dict={inputs: image_batch}) scmap, locref, _ = predict.extract_cnn_output(outputs_np, cfg) # Extract maximum scoring location from the heatmap, assume 1 person pose = predict.argmax_pose_predict(scmap, locref, cfg.stride) if (i == 0): scale = set_scale(pose) ans.append(kick_height(pose, scale)) cv2.destroyAllWindows() ans = np.array(ans) print(ans) print("We measured " + str(max(ans)) + " using the ankles as our points") print("The true value is approximately " + str(max(ans) + (4 / 3 * shoe_size))) print(np.argmax(ans)) # Visualise disp_pic(where="temp/vid_pic" + str(np.argmax(ans)))
# Compute predictions over images ################################################## for imageindex, imagename in tqdm(enumerate(Data.index)): image = io.imread(os.path.join(models_folder_path, unaugmented_folder_name, 'data-' + Task, imagename), mode='RGB') image = skimage.color.gray2rgb(image) image_batch = data_to_input(image) # Compute prediction with the CNN outputs_np = sess.run(outputs, feed_dict={inputs: image_batch}) scmap, locref = predict.extract_cnn_output(outputs_np, cfg) # Extract maximum scoring location from the heatmap, assume 1 person pose = predict.argmax_pose_predict(scmap, locref, cfg.stride) PredicteData[imageindex, :] = pose.flatten( ) # NOTE: thereby cfg_test['all_joints_names'] should be same order as bodyparts! index = pd.MultiIndex.from_product( [[DLCscorer], cfg['all_joints_names'], ['x', 'y', 'likelihood']], names=['scorer', 'bodyparts', 'coords']) # Saving results: auxiliaryfunctions.attempttomakefolder("Results") DataMachine = pd.DataFrame(PredicteData, columns=index, index=Data.index.values) DataMachine.to_hdf(os.path.join("Results", DLCscorer + '.h5'), 'df_with_missing',
def poser(): global state global points global reps import os import sys import cv2 import time import numpy as np sys.path.append(os.path.dirname(__file__) + "/../") from scipy.misc import imread from config import load_config from nnet import predict from util import visualize from dataset.pose_dataset import data_to_input cfg = load_config("demo/pose_cfg.yaml") # Load and setup CNN part detector sess2, inputs, outputs = predict.setup_pose_prediction(cfg) camera = cv2.VideoCapture(0) # Read image from file prevPoints = -1 while self.running: r, image = camera.read() image_batch = data_to_input(image) # Compute prediction with the CNN outputs_np = sess2.run(outputs, feed_dict={inputs: image_batch}) scmap, locref, _ = predict.extract_cnn_output(outputs_np, cfg) # Extract maximum scoring location from the heatmap, assume 1 person pose = predict.argmax_pose_predict(scmap, locref, cfg.stride) # Visualise data = visualize.visualize_joints(image, pose) frame = cv2.cvtColor(data, 4) img = QtGui.QImage(frame, frame.shape[1], frame.shape[0], QtGui.QImage.Format_RGB888) pix = QtGui.QPixmap.fromImage(img) try: self.lblVideo.setPixmap(pix) except: return arr = [] for i in range(14): arr += pose[i].tolist()[0:2] predictedPose = sess.run(prediction, feed_dict={X: [arr]}) processPose(predictedPose) if prevPoints != points: print("Current points: " + str(points)) prevPoints = points
from scipy.misc import imread from config import load_config from nnet import predict from util import visualize from dataset.pose_dataset import data_to_input cfg = load_config("demo/pose_cfg.yaml") # Load and setup CNN part detector sess, inputs, outputs = predict.setup_pose_prediction(cfg) # Read image from file file_name = "demo/image.png" image = imread(file_name, mode='RGB') image_batch = data_to_input(image) # Compute prediction with the CNN outputs_np = sess.run(outputs, feed_dict={inputs: image_batch}) scmap, locref, _ = predict.extract_cnn_output(outputs_np, cfg) # Extract maximum scoring location from the heatmap, assume 1 person pose = predict.argmax_pose_predict(scmap, locref, cfg.stride) # Visualise visualize.show_heatmaps(cfg, image, scmap, pose) visualize.waitforbuttonpress()
# Read all images, call cnn model and make predictions about human main body parts for images in glob.glob(pose_image_resources_warrior): try: image_name = images.title() image = plt.imread(images) picture_name.append(image_name) image_batch: ndarray = data_to_input(image) # Compute prediction with the CNN outputs_np = sess.run(outputs, feed_dict={inputs: image_batch}) scmap, locref, _ = predict.extract_cnn_output(outputs_np, cfg) # Extract maximum scoring location from the heatmap, assume 1 person pose: ndarray = predict.argmax_pose_predict(scmap, locref, cfg.stride) # print(pose.toarr) # Visualise # visualize.show_heatmaps(cfg, image, scmap, pose) # visualize.waitforbuttonpress() features_df = list(chain.from_iterable(pose)) y0 = '_' features_df.append(y0) y1 = '_' features_df.append(y1) features.append(features_df) # print(features_df) # this needs reshape --> features_pandas = pd.DataFrame(features_df, columns=labels)
def test_net(visualise, cache_scoremaps): # 打开python的日志功能 logging.basicConfig(level=logging.INFO) # 加载配置文件 cfg = load_config() # 根据配置文件中的信息产生数据读取类的实例 dataset = create_dataset(cfg) # 不用对数据进行洗牌 dataset.set_shuffle(False) # 告诉数据读取类没有类标,即处于测试模式 dataset.set_test_mode(True) # 该函数返回session,输入算子,输出算子 sess, inputs, outputs = setup_pose_prediction(cfg) # 是否需要保存测试过程中的heatmap if cache_scoremaps: # 保存heatmap的目录 out_dir = cfg.scoremap_dir # 目录不存在则创建 if not os.path.exists(out_dir): os.makedirs(out_dir) # 图片个数 num_images = dataset.num_images # 预测的关节坐标都保存在这里 predictions = np.zeros((num_images, ), dtype=np.object) for k in range(num_images): print('processing image {}/{}'.format(k, num_images - 1)) # 获得一批数据 batch = dataset.next_batch() # 进行预测 outputs_np = sess.run(outputs, feed_dict={inputs: batch[Batch.inputs]}) # 得到heatmap和精细化的heatmap scmap, locref = extract_cnn_output(outputs_np, cfg) # 获得最终的关节坐标 ''' pose = [ [ pos_f8[::-1], [scmap[maxloc][joint_idx]] ] .... ..... .... ] 用我的话说就是下面的结构 pose = [ [关节的坐标, 关节坐标的置信度] .... ..... .... ] ''' pose = argmax_pose_predict(scmap, locref, cfg.stride) pose_refscale = np.copy(pose) # 除以尺度,就能恢复到未经过缩放的图像的坐标系上去 # 注意0:2是左开右闭的区间只取到了0和1 pose_refscale[:, 0:2] /= cfg.global_scale predictions[k] = pose_refscale if visualise: # 获取图片 img = np.squeeze(batch[Batch.inputs]).astype('uint8') # 显示heatmap visualize.show_heatmaps(cfg, img, scmap, pose) # 等待按键按下 visualize.waitforbuttonpress() if cache_scoremaps: # 保存heatmap base = os.path.basename(batch[Batch.data_item].im_path) raw_name = os.path.splitext(base)[0] out_fn = os.path.join(out_dir, raw_name + '.mat') scipy.io.savemat(out_fn, mdict={'scoremaps': scmap.astype('float32')}) # 保存精细化关节定位的heatmap out_fn = os.path.join(out_dir, raw_name + '_locreg' + '.mat') if cfg.location_refinement: scipy.io.savemat( out_fn, mdict={'locreg_pred': locref.astype('float32')}) # 将最终预测的关节坐标保存起来 scipy.io.savemat('predictions.mat', mdict={'joints': predictions}) sess.close()
def get_frame_pose(frame): image_batch = data_to_input(frame) outputs_np = sess.run(outputs, feed_dict={inputs: image_batch}) scmap, locref, _ = predict.extract_cnn_output(outputs_np, cfg) # Extract maximum scoring location from the heatmap, assume 1 person return predict.argmax_pose_predict(scmap, locref, cfg.stride)