I'm new to Python and multiprocessing, I would like to speed up my current code processing speed as it takes around 8 mins for 80 images. I only show 1 image for this code for reference purpose. I got into know that multiprocessing helps on this and gave it a try but somehow not working as what I expected.
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))
This is what I tried to work on my side. Although it's not working, added pool class for multiprocessing but getting None result.
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()
CodePudding user response:
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.