首页 > 解决方案 > 试图并行运行一个函数,但对多处理感到困惑

问题描述

我正在尝试并行化我的功能,但我无法让它工作,

这就是它的样子——

import face_recognition
import cv2 
import os

def get_image_from_video():
    video_path = 
'/media/ryan/shakira/video/video_data/'
    output_path = 
'/media/ryan/shakira/video/results_from_fr/'
    for video in os.listdir(video_path):
        print('This video is being processed ------->', video)
        input_movie = cv2.VideoCapture(os.path.join(video_path, video))
        length = int(input_movie.get(cv2.CAP_PROP_FRAME_COUNT))

        face_locations = []
        face_encodings = []
        face_names = []
        frame_number = 0
        break_value = False
        while True:
            # Grab a single frame of video
            ret, frame = input_movie.read()
            frame_number += 1

            # Quit when the input video file ends
            if not ret:
                break

            # Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
            rgb_frame = frame[:, :, ::-1]

            # Find all the faces and face encodings in the current frame of video
            face_locations = face_recognition.face_locations(rgb_frame)
            print(face_locations)

            if not face_locations:
                continue
            for top, right, bottom, left in face_locations:

                cv2.imwrite(os.path.join(output_path, video.split('.')[0]+'.jpg'), frame) #--- Here is where the increment variable is placed. It will be incremented for every face and thus saving every face that gets detected.
                break_value = True
                break
            if break_value:
                break

        input_movie.release()


from multiprocessing import Process
import multiprocessing as mp
from threading import Thread

if __name__ == '__main__':
  p1 = Process(target=get_image_from_video)
  p1.start()
  p2 = Process(target=get_image_from_video)
  p2.start()
  p3 = Process(target=get_image_from_video)
  p3.start()
  p4 = Process(target=get_image_from_video)
  p4.start()
  p5 = Process(target=get_image_from_video)
  p5.start()
  p6 = Process(target=get_image_from_video)
  p6.start()
  p7 = Process(target=get_image_from_video)
  p7.start()
  p8 = Process(target=get_image_from_video)
  p8.start()
  p1.join()
  p2.join()
  p3.join()
  p4.join()
  p5.join()
  p6.join()
  p7.join()
  p8.join() 

现在这不起作用,它没有正确并行化,并且当它发生时,它在每个进程上运行相同的文件。

任何关于我如何并行化的建议都会非常有帮助。在此先感谢。

标签: python

解决方案


这个想法是有许多“工人”,他们每个人都有一个视频来工作。

像这样的东西

from multiprocessing import Process

def get_image_from_video(video_file):
    # handle a single video file
    pass

if __name__ == '__main__':
    # prepare list of video files under folder ''/media/ryan/shakira/video/video_data/''
    video_files = ['video1','video2'] 
    pool = Pool(5) # 5 as an example
    pool.map(get_image_from_video, video_files ))

推荐阅读