Python joblib.Parallel

import cv2
import os
import numpy as np
from numba import jit
from joblib import Parallel
from joblib import delayed

frame_path = 'data/ucf101_frames111111'
video_path = 'data/ucf101'
def extract_flow_img(video_full_name, frame_path1 ,i, j):
    cap = cv2.VideoCapture(video_full_name)
    video_frames = int(cap.get(7))
    success, prev =
    rgb_img_ = os.path.join(frame_path1, 'img_{:05d}.jpg'.format(1))
    cv2.imwrite(rgb_img_, prev)
    prev = cv2.cvtColor(prev, cv2.COLOR_BGR2GRAY)
    # durance = video_frames//num_frames
    for k in range(video_frames - 1):
        success, last =
        if success:
            rgb_img_ = os.path.join(frame_path1, 'img_{:05d}.jpg'.format(k + 2))
            cv2.imwrite(rgb_img_, last)
            last = cv2.cvtColor(last, cv2.COLOR_BGR2GRAY)
            flow = cv2.calcOpticalFlowFarneback(prev, last, None, 0.5, 3, 15, 3, 5, 1.2, 0)
            flow = np.byte((flow - flow.min()) / (flow.max() - flow.min()) * 255)
            flow_x = os.path.join(frame_path1, 'flow_x_{:05d}.jpg'.format(k + 1))
            flow_y = os.path.join(frame_path1, 'flow_y_{:05d}.jpg'.format(k + 1))
            cv2.imwrite(flow_x, flow[:, :, 0])
            cv2.imwrite(flow_y, flow[:, :, 1])
            prev = last

    print('The {} class, {}th video, done!!'.format(i, j))

subname1 = os.listdir(video_path)
count = 0
for i, f in enumerate(subname1):
    rootname = os.path.join(video_path, f)        #目前只到每个类的文件夹,还没到每个视频
    video_name = os.listdir(rootname)
    Parallel(n_jobs=50)(delayed(extract_flow_img)(os.path.join(rootname, v), os.path.join(frame_path, v), i, j)for j, v in enumerate(video_name))

This is the program I designed. ucf101 is a video data set. data / ucf101 is the root directory, and there are subdirectories of video classes below the data/ucf101, the videos in each subdirectory are of the same class. I want to extract all video frames and optical streams from each videos, and store them in the subdirectory which is named using its video filename, and the subdirectory is under the root directory of data / ucf101_frames. But now I have a problem. Because it takes a lot of time to compute the optical flow images, I have to choose Joblib. Parallel for parallel processing. If n_jobs sets to 1, that is to say, no parallel computing is needed, so there will be no error. But once I set this value to more than 1, it will report the error of global name’cv2’is not defined. Because the open CV is used to extract video frames. Why the error becomes when I use parallel computing? Please give me your advice. Thank you!