簡體   English   中英

運行python多進程進行圖像處理

[英]Running python multiprocess for Image processing

我有一個python函數,它接收圖像路徑並根據圖像是否為黑色輸出true或false。 我想在同一台計算機上處​​理多個圖像,如果其中一個不是黑色,也要停止該過程。 我在這里閱讀了很多關於python,celery等的多重處理的信息,但是我不確定從哪里開始。

我建議您查看Pools,以輕松地動態創建流程。 如果需要某種共享狀態(在這種情況下為布爾值,表明已找到非黑色圖像),請查看Managers

更新:這是我的意思的示例。

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.'

最后,這對我很好。 此處的示例復制。 為了說明起見,我將_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