Running python multiprocess for Image processing

后端 未结 2 1337
温柔的废话
温柔的废话 2021-01-27 18:37

I have a python function that takes in an image path and outputs true or false depending whether the image is black or not. I want to process several images on the same machine

2条回答
  •  旧巷少年郎
    2021-01-27 19:13

    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.

    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
    

提交回复
热议问题