简体   繁体   English

运行python多进程进行图像处理

[英]Running python multiprocess for Image processing

I have a python function that takes in an image path and outputs true or false depending whether the image is black or not. 我有一个python函数,它接收图像路径并根据图像是否为黑色输出true或false。 I want to process several images on the same machine and stop the process if even one of them is not black. 我想在同一台计算机上处​​理多个图像,如果其中一个不是黑色,也要停止该过程。 I read a lot of multiprocessing in python, celery etc here, but I am not sure where to start. 我在这里阅读了很多关于python,celery等的多重处理的信息,但是我不确定从哪里开始。

I would suggest looking at Pools to easily create processes on the fly. 我建议您查看Pools,以轻松地动态创建流程。 If you need to have some shared state, in this case a boolean indicating a non-black image has been found, look at Managers . 如果需要某种共享状态(在这种情况下为布尔值,表明已找到非黑色图像),请查看Managers

Update: Here is an example of what I mean. 更新:这是我的意思的示例。

import multiprocessing.Manager as Manager
import multiprocessing.Pool as Pool

m = Manager()
p = Pool(processes=5)

state_info = m.dict()
state_info['image_found'] = False

def processImage(img):

    # ... Process Image ...

    if imageIsBlack(img):
        state_info['image_found'] = True
        p.terminate()

 p.apply(processImage, imageList)

 if state_info['image_found']:
     print 'There was a black image!!'
 else:
     print 'No black images were found.'

Finally this works nicely for me. 最后,这对我很好。 Copied it from an example here . 此处的示例复制。 For illustration purpose I have replaced my _isImgNonBlack function and the image sequence as a list of 0's and 1's, where 0 being a black image and 1 non-black image. 为了说明起见,我将_isImgNonBlack函数和图像序列替换为0和1的列表,其中0是黑色图像,而1是非黑色图像。

import multiprocessing

def isImgNonBlack(result_queue, imgSeq):
    for img in imgSeq:
        # If a non-black is found put a result
        if img==1:
            result_queue.put(1)

    # else put a zero as the result
    result_queue.put(0)

if __name__ == '__main__':
    processs = []
    result_queue = multiprocessing.Queue()
    nbProc = 20

    # making a fake list of images with 
    # 10,000 0's follwed by a single 1
    images = [0 for n in range(10000)]
    images.append(1)

    for n in range(nbProc): # start processes crawling for the result
        process = multiprocessing.Process(target=isImgNonBlack, args=[result_queue, images])
        process.start()
        processs.append(process)
        print 'Starting Process : %s' % process

    result = result_queue.get() # waits until any of the proccess have `.put()` a result

    for process in processs: # then kill them all off
        process.terminate()

    # finally print the result
    print "Seq have a non black img: %s" % result

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM