<jupyter_start><jupyter_text>take a video on webcam ```cd ../src/utilspython save_img.py```  Extract Frames from Video File<jupyter_code>from pathlib import Path


save_dir = Path('../data/target/')
try: 
    save_dir.mkdir(exist_ok=True)
except FileExistsError : 
    print("Directory exists. Abort creation")
    


img_dir = save_dir.joinpath('images')
try:
    img_dir.mkdir(exist_ok=True)
except FileExostsError:
    print("Directory exists. Abort creation")
import cv2 
import numpy as np

def extract_images_from_video(filename, subsample =  1):
    """
    filename: string
        filename of video. Location must be in ../data/target/
    subsample: int 
        use only one of subsample images.
        subsample = 1, use all images.
    
    """
    
    print("Video path: ",str(save_dir.joinpath(filename)))
    cap = cv2.VideoCapture(str(save_dir.joinpath(filename)))
    crop = True
    i = 0
    j = 0
    flag = None
    while(cap.isOpened()):
        #Extract only 1 of subsample images
        flag, frame = cap.read()
        if flag == False:
            break
        if j%subsample == 0:

            # Crop images to desired size 
            if crop:
                frame = frame[355:-10,645:-645,:]
            #frame = cv2.resize(frame, dsize=(512, 512), interpolation=cv2.INTER_CUBIC)

            if i >= 20:
                break

            cv2.imwrite(str(img_dir.joinpath('{:05d}.png'.format(i))), frame)
            i += 1


        j+= 1
    print("Number of frames:", i)
extract_images_from_video(filename = 'filename.mp4', subsample =  1)<jupyter_output>Video path:  ..\data\target\filename.mp4
Number of frames: 20<jupyter_text>Pose estimation (OpenPose)<jupyter_code>import numpy as np
import cv2
import matplotlib.pyplot as plt
import torch
from tqdm import tqdm
from pathlib import Path

%matplotlib inline
openpose_dir = Path('../src/pytorch_Realtime_Multi-Person_Pose_Estimation/')

import sys
sys.path.append(str(openpose_dir))
sys.path.append('../src/utils')<jupyter_output><empty_output><jupyter_text>Autorelaod modules<jupyter_code>%load_ext autoreload
%autoreload 2
# openpose
from network.rtpose_vgg import get_model
from evaluate.coco_eval import get_multiplier, get_outputs

# utils
from openpose_utils import remove_noise, get_pose
weight_name = openpose_dir.joinpath('network/weight/pose_model.pth')

model = get_model('vgg19')     
model.load_state_dict(torch.load(weight_name))
model = torch.nn.DataParallel(model).cuda()
model.float()
model.eval()<jupyter_output>Bulding VGG19<jupyter_text>check<jupyter_code>img_path = sorted(img_dir.iterdir())[0]
img = cv2.imread(str(img_path))
shape_dst = np.min(img.shape[:2])
# offset
oh = (img.shape[0] - shape_dst) // 2
ow = (img.shape[1] - shape_dst) // 2

img = img[oh:oh+shape_dst, ow:ow+shape_dst]
img = cv2.resize(img, (512, 512))
          
plt.imshow(img[:,:,[2, 1, 0]]) # BGR -> RGB
multiplier = get_multiplier(img)
with torch.no_grad():
    paf, heatmap = get_outputs(multiplier, img, model, 'rtpose')
    
r_heatmap = np.array([remove_noise(ht)
                      for ht in heatmap.transpose(2, 0, 1)[:-1]])\
                     .transpose(1, 2, 0)
heatmap[:, :, :-1] = r_heatmap
param = {'thre1': 0.1, 'thre2': 0.05, 'thre3': 0.5}
label,cord  = get_pose(param, heatmap, paf)
print("Label shape", label.shape)
plt.imshow(label)<jupyter_output>Label shape (512, 512)<jupyter_text>make label images for pix2pix<jupyter_code>train_dir = save_dir.joinpath('train')
try:
    train_dir.mkdir(exist_ok=True)
except FileExistsError:
    print("Directory exists. Abort creation")
    

train_img_dir = train_dir.joinpath('train_img')
try:
    train_img_dir.mkdir(exist_ok=True)
except FileExistsError:
    print("Directory exists. Abort creation")

    

train_label_dir = train_dir.joinpath('train_label')
try:
    train_label_dir.mkdir(exist_ok=True)   
except FileExistsError:
    print("Directory exists. Abort creation")
    

train_head_dir = train_dir.joinpath('head_img')
try:
    train_head_dir.mkdir(exist_ok=True)
except FileExistsError:
    print("Directory exists. Abort creation")
    
    
import os

NUM_FRAMES = len(os.listdir(str(img_dir)))

pose_cords = []
for idx in tqdm(range(20)):
    img_path = img_dir.joinpath('{:05d}.png'.format(idx))
    img = cv2.imread(str(img_path))
    shape_dst = np.min(img.shape[:2])
    oh = (img.shape[0] - shape_dst) // 2
    ow = (img.shape[1] - shape_dst) // 2

    img = img[oh:oh+shape_dst, ow:ow+shape_dst]
    img = cv2.resize(img, (512, 512))
    multiplier = get_multiplier(img)
    with torch.no_grad():
        paf, heatmap = get_outputs(multiplier, img, model, 'rtpose')
    r_heatmap = np.array([remove_noise(ht)
                      for ht in heatmap.transpose(2, 0, 1)[:-1]])\
                     .transpose(1, 2, 0)
    heatmap[:, :, :-1] = r_heatmap
    param = {'thre1': 0.1, 'thre2': 0.05, 'thre3': 0.5}
    label, cord = get_pose(param, heatmap, paf)
    index = 13
    crop_size = 25
    try:
        head_cord = cord[index]
    except:
        head_cord = pose_cords[-1] # if there is not head point in picture, use last frame

    pose_cords.append(head_cord)
    head = img[int(head_cord[1] - crop_size): int(head_cord[1] + crop_size),
           int(head_cord[0] - crop_size): int(head_cord[0] + crop_size), :]
    plt.imshow(head)
    plt.savefig(str(train_head_dir.joinpath('pose_{}.jpg'.format(idx))))
    plt.clf()
    
    cv2.imwrite(str(train_img_dir.joinpath('{:05d}.png'.format(idx))), img)    
    cv2.imwrite(str(train_label_dir.joinpath('{:05d}.png'.format(idx))), label)

    
pose_cords = np.array(pose_cords, dtype=np.int)
np.save(str((save_dir.joinpath('pose.npy'))), pose_cords)
torch.cuda.empty_cache()<jupyter_output>100%|██████████████████████████████████████████████████████████████████████████████████| 20/20 [02:29<00:00,  7.57s/it]
{'fraction_non_alphanumeric': 0.15183156453269428, 'fraction_numerical': 0.0265320095857583, 'mean_word_length': 3.56484375, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '253279', 'license': 'MIT', 'n_tokens_mistral': 2316, 'n_tokens_neox': 2157, 'n_words': 420, 'path': 'notebook/2.make_target_images.ipynb', 'repo_name': 'Psychofun/fortnite_dances_real_life', 'size': '5842'}