Skip to content

Instantly share code, notes, and snippets.

@duhaime
Last active January 31, 2019 16:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save duhaime/c815744f2e201780c92960f661bc9297 to your computer and use it in GitHub Desktop.
Save duhaime/c815744f2e201780c92960f661bc9297 to your computer and use it in GitHub Desktop.
detect_frame_pose.py
import os, sys, glob, math
sys.path.append('pose-tensorflow')
sys.path.append('.')
sys.path.append('..')
sys.path.append('pose-tensorflow/models')
from scipy.misc import imread
from config import load_config
from nnet import predict
from dataset.pose_dataset import data_to_input
import numpy as np
def subdivide(l, n):
units = math.ceil(len(l) / n)
return [l[i*units:(i+1)*units] for i in range(n)]
# identify images to process
images = sorted(glob.glob(sys.argv[1]))
worker_id = int(sys.argv[2])-1
total_workers = int(sys.argv[3])
print(' * args', sys.argv)
print(' * images', len(images))
# get the list of files for which this worker is responsible
worker_images = list(subdivide(images, total_workers))[worker_id]
print(' * worker_images:', worker_images)
# prepare model
cfg = load_config("pose-tensorflow/demo/pose_cfg.yaml")
# load and setup CNN part detector
sess, inputs, outputs = predict.setup_pose_prediction(cfg)
frames = []
for idx, i in enumerate(worker_images):
print(' * processing', i)
# Read image from file
file_name = i
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)
frames.append(pose)
frames = np.array(frames)
wid = str(worker_id)
while len(wid) < 7: wid = '0' + wid
if not os.path.exists('frames'): os.makedirs('frames')
np.save('frames/frames-' + wid + '.npy', frames)

downloading mp4 from youtube

# download binary that will download videos
wget https://yt-dl.org/downloads/latest/youtube-dl

# make the binary executable
chmod a+rx youtube-dl

# download a video
./youtube-dl https://www.youtube.com/watch?v=eFkuPO253mk

# rename the video
mv "Cheap Thrills by Sia ft Sean Paul _ Dance Tutorial by Aditi Saxena _ Dancercise-eFkuPO253mk.mp4" cheap-thrills.mp4

split mp4 into frames

# download ffmpeg binary (ffmpeg is a video processing utility)
wget https://s3-us-west-2.amazonaws.com/lab-apps/dancing-with-robots/ffmpeg-release-amd64-static.tar.xz

# unzip the binary
tar -xf ffmpeg-release-amd64-static.tar.xz

# grab the actual binary bit
mv ffmpeg-4.1-64bit-static/ffmpeg .

# download script that processes videos
wget https://gist.githubusercontent.com/duhaime/c815744f2e201780c92960f661bc9297/raw/bb4cb15cf7d220d1fddb47ed23202b5862eeb42f/video_to_frames.py

# run script
python video_to_frames.py cheap-thrills.mp4

process video frames

# download the processing script
wget https://gist.githubusercontent.com/duhaime/c815744f2e201780c92960f661bc9297/raw/ca9f7cee110b2175036a801198f5b6ceea4b7ac3/run.job

# install dependencies
pip install https://github.com/eldar/pose-tensorflow --user

# run a test go with the processing script
python detect_frame_pose.py "frames/*.jpg" 1 1000

# run the processing script
sbatch run.job
#!/bin/bash
#SBATCH --partition scavenge
#SBATCH --time 1-0:00:00
#SBATCH --job-name pose
#SBATCH --output logs/pose-estimate-%A-%a-%J.log
#SBATCH --array=1-1000
#module load Langs/Python/3.6.4
#module load GPU/Cuda/9.0
#module load GPU/cuDNN/9.0-v7
module load Langs/Python/3.6-anaconda
source activate pose
python detect_frame_pose.py '/home/fas/leonard/ded34/code/tensorflow-pose-estimation/prepare-youtube-data/frames/*.jpg' ${SLURM_ARRAY_TASK_ID} 1000
import os, sys
mp4 = sys.argv[1]
out_dir = ''
cmd = './ffmpeg -i ' + mp4 + ' ' + out_dir + os.path.basename(mp4) + '-%08d.jpg -hide_banner'
print(cmd)
os.system(cmd)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment