多重处理不调用函数

时间:2018-10-09 12:31:04

标签: python multithreading image parallel-processing multiprocessing

我试图将我的图像从文件夹中移到一个数组中,并一一处理它们。但我想同时进行。我尝试在不同的过程中实现每个图像并执行,但这似乎不起作用

我的代码:

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

file_path=[]
path="C:/Users/User/Desktop/Dataset_Screens/picture_dataset_Small/"
for root,dir,files in os.walk(path):
    for f in files:
        file_path.append(os.path.join(root,f))


path1="C:/Users/User/Desktop/Dataset_Screens/picture_dataset_Big/"
for root1,dir1,files1 in os.walk(path1):
     for f1 in files:
        file_path.append(os.path.join(root1,f1))

print(len(file_path))


def get_image(inputfilepath):
    img_original= cv2.imread(inputfilepath)
    img_array=np.asarray(img_original)
    blur= cv2.pyrMeanShiftFiltering(img_original,21,49)
    gray_image= cv2.cvtColor(blur, cv2.COLOR_BGR2GRAY)
    ret,thresh= cv2.threshold(gray_image,70,255,cv2.THRESH_BINARY)    
    _, contours,hierarchy =cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    countourimage=cv2.drawContours(img_original,contours,-1,0,3)

    largest_area=0
    print('I am inside the loop')
    for i,c in enumerate(contours):
        contour_areas=cv2.contourArea(c)
        if(contour_areas>largest_area):
            largest_area= contour_areas
            x_rect,y_rect,w_rect,h_rect=cv2.boundingRect(c)
            cropped=img_original[y_rect:y_rect+h_rect,x_rect:x_rect+w_rect]

    cv2.imwrite('C:/Users/User/Anaconda3/contourimage.jpg',cropped)

t= time.time()      
if __name__ == '__main__':

    for i in range(4):
        p = Process(target=get_image, args=(file_path[i],))
        p.start()

print('Done in ', time.time()-t)
print('We are DONEEEEEEE!!!!!!')

我使用的Spyder似乎也不起作用,所以我在conda提示符下运行,输出如下所示:

spyder中的输出:

132
Done in  0.022994279861450195
DONEEE!!

conda提示符中的输出:

C:\Users\User\Anaconda3>python examples.py
132
Done in  0.05248904228210449
DONEEE!!
132
Done in  0.0
DONEEE!!
132
Done in  0.0
DONEEE!!
132
Done in  0.0
DONEEE!!
132
Done in  0.0
DONEEE!!
Corrupt JPEG data: premature end of data segment
Corrupt JPEG data: premature end of data segment
Corrupt JPEG data: premature end of data segment
Corrupt JPEG data: premature end of data segment
I am inside the loop
I am inside the loop
I am inside the loop
I am inside the loop

我正在尝试将我的python程序转换为并行程序以减少花费的时间

我已经在函数中添加了p.join()

if __name__ == '__main__':
    for i in range(4):
        p = multiprocessing.Process(target=get_image, args=(file_path[i],))
        p.start()
        p.join()

以及spyder中的输出:

132
Done in  30.90500521659851
DONEEE!!

和conda提示:

C:\Users\User\Anaconda3>python examples.py
132
132
Done in  0.0
DONEEE!!
Corrupt JPEG data: premature end of data segment
I am inside the loop
132
Done in  0.0
DONEEE!!
Corrupt JPEG data: premature end of data segment
I am inside the loop
132
Done in  0.0
DONEEE!!
Corrupt JPEG data: premature end of data segment
I am inside the loop
132
Done in  0.0
DONEEE!!
Corrupt JPEG data: premature end of data segment
I am inside the loop
Done in  30.726163625717163
DONEEE!!

我绝对是并行处理的初学者。请提出建议。谢谢!!

0 个答案:

没有答案