-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcv-mp-python.py
More file actions
87 lines (59 loc) · 2.61 KB
/
cv-mp-python.py
File metadata and controls
87 lines (59 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
from multiprocessing import Process, Manager
import time
from tqdm import tqdm
import cv2
import os
class FastDataProcessing(object):
def __init__(self, max_processes = 1000):
self.processes = []
self.max_processes = max_processes
def create_and_start_process(self, target_function, args):
pcs = Process(target = target_function, args = args)
self.processes.append(pcs)
pcs.start()
def join_processes(self):
for jj in range(len(self.processes)):
self.processes[jj].join()
self.processes = []
def test_function_parallel(identity, image_path, output_path, receive_data):
# Start delimiter
got_data = receive_data[identity]
del receive_data[identity]
# Your code goes here
img = cv2.imread(image_path) # Sample
img = cv2.resize(img, (got_data, got_data))
cv2.imwrite(os.path.join(output_path, str(identity) + ".jpg"), img) # Sample
return
def test_function_serial(identity, image_path, output_path, receive_data):
# Your code goes here
img = cv2.imread(image_path) # Sample
img = cv2.resize(img, (receive_data, receive_data))
cv2.imwrite(os.path.join(output_path, str(identity) + ".jpg"), img) # Sample
return
def main():
process_manager = FastDataProcessing()
resource_manager = Manager()
send_data = resource_manager.dict()
# Give path to your image
image_path = './input.jpg'
images_path = [image_path]*10000
output_path = "./outputs"
if not os.path.exists(output_path):
os.makedirs(output_path)
start_time = time.time()
# Parallel Execution
for ii, image_path in enumerate(tqdm(images_path)):
send_data[ii] = 120
process_manager.create_and_start_process(test_function_parallel, \
args = (ii, image_path, output_path, send_data))
if ii % process_manager.max_processes == 0 or ii == len(images_path) - 1:
process_manager.join_processes()
print("Time taken by Parallel Execution in seconds : ", time.time() - start_time)
# Serial Execution
start_time = time.time()
for ii, image_path in enumerate(tqdm(images_path)):
send_data1 = 120
test_function_serial(ii, image_path, output_path, send_data1)
print("Time taken by Serial Execution in seconds : ", time.time() - start_time)
if __name__== "__main__":
main()