如何在Python for for循环中使用多重处理?

xmakbtuz  于 2022-12-05  发布在  Python
关注(0)|答案(1)|浏览(141)

我是Python和多处理的新手,我想加快我目前的代码处理速度,因为80张图片大约需要8分钟。我只显示了这段代码的1张图片作为参考。我知道多处理在这方面有帮助,并尝试了一下,但不知何故没有像我预期的那样工作。

import numpy as np
import cv2
import time
import os
import multiprocessing

img = cv2.imread("C://Users/jason/Desktop/test.bmp")

gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

_,blackMask = cv2.threshold(gry, 0, 255, cv2.THRESH_BINARY_INV)

x1 = []
y1 = []

def verticle(mask, y, x):
    vertiPixel = 0
    while(y < mask.shape[0]):
        if (y + 1) == mask.shape[0]:
            break
        else:
            if(mask[y + 1][x] == 255):
                vertiPixel += 1
                y += 1
            else:
                break
            
    y1.append(vertiPixel)
    
def horizontal(mask, y, x):
    horiPixel = 0
    while(x < mask.shape[1]):
        if (x + 1) == mask.shape[1]:
            break
        else:
            if(mask[y][x + 1] == 255):
                horiPixel += 1
                x += 1
            else:
                break
            
    x1.append(horiPixel)

def mask(mask):

    for y in range (mask.shape[0]):
        for x in range (mask.shape[1]):

            if(mask[y][x] == 255):
                verticle(mask, y, x)
                horizontal(mask, y, x)

mask(blackMask)

print(np.average(x1), np.average(y1))

这是我尝试在我这边工作的。虽然它不工作,为多处理添加了池类,但得到的结果是“无”。

import numpy as np
import cv2
import time
import os
from multiprocessing import Pool

folderDir = "C://Users/ruler/Desktop/testseg/"

total = []

with open('readme.txt', 'w') as f:
    count = 0
    for allImages in os.listdir(folderDir):

        if (allImages.startswith('TRAIN_SET') and allImages.endswith(".bmp")):
            
            img = cv2.imread(os.path.join(folderDir, allImages))

            gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

            _,blackMask = cv2.threshold(gry, 0, 255, cv2.THRESH_BINARY_INV)

            x1 = []
            y1 = []

            def verticle(mask, y, x):
                vertiPixel = 0
                while(y < mask.shape[0]):
                    if (y + 1) == mask.shape[0]:
                        break
                    else:
                        if(mask[y + 1][x] == 255):
                            vertiPixel += 1
                            y += 1
                        else:
                            break
                        
                y1.append(vertiPixel)
                
            def horizontal(mask, y, x):
                horiPixel = 0
                while(x < mask.shape[1]):
                    if (x + 1) == mask.shape[1]:
                        break
                    else:
                        if(mask[y][x + 1] == 255):
                            horiPixel += 1
                            x += 1
                        else:
                            break
                        
                x1.append(horiPixel)

            def mask(mask):

                for y in range (mask.shape[0]):
                    for x in range (mask.shape[1]):

                        if(mask[y][x] == 255):
                            verticle(mask, y, x)
                            horizontal(mask, y, x)
                            equation(y,x)

            def equation(y,x):
                a = np.average(y) * (9.9 / 305)
                c = np.average(x) * (9.9 / 305)
        
                final = (a + c) / 2
        
                total.append(final)

            
        if __name__ == "__main__":
            pool = Pool(8)
            print(pool.map(mask, [blackMask] * 3))
            pool.close()
elcex8rz

elcex8rz1#

To use multiprocessing to speed up your code, you can use the Pool class from the multiprocessing module. The Pool class allows you to run multiple processes in parallel, which can help speed up your code.
To use the Pool class, you need to first create a Pool object and then use the map method to apply a function to each element in a list in parallel. For example, to use the Pool class to speed up your code, you could do the following:

# Import the Pool class from the multiprocessing module
from multiprocessing import Pool

# Create a Pool object with the desired number of processes
pool = Pool(8)

# Use the map method to apply the mask function to each element in a list in parallel
pool.map(mask, [blackMask] * 80)

# Close the pool when finished
pool.close()

This will create a Pool object with 8 processes, and then apply the mask function to 80 copies of the blackMask image in parallel. This should speed up your code by running multiple processes in parallel.
However, note that using multiprocessing can be complex and may not always result in significant speedups, especially for relatively small and simple tasks like the one in your code. It may be worth trying to optimize your code in other ways before resorting to multiprocessing.

相关问题