forked from PacktPublishing/AdvancedPythonProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample6.py
More file actions
35 lines (22 loc) · 933 Bytes
/
example6.py
File metadata and controls
35 lines (22 loc) · 933 Bytes
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
from multiprocessing import Pool
import cv2
import sys
from functools import partial
from timeit import default_timer as timer
THRESH_METHOD = cv2.ADAPTIVE_THRESH_GAUSSIAN_C
INPUT_PATH = 'input/large_input/'
OUTPUT_PATH = 'output/large_output/'
n = 20
names = ['ship_%i_%i.jpg' % (i, j) for i in range(n) for j in range(n)]
def process_threshold(name, thresh_method):
im = cv2.imread(INPUT_PATH + name)
gray_im = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
thresh_im = cv2.adaptiveThreshold(gray_im, 255, thresh_method, cv2.THRESH_BINARY, 11, 2)
cv2.imwrite(OUTPUT_PATH + name, thresh_im)
if __name__ == '__main__':
for n_processes in range(1, 7):
start = timer()
with Pool(n_processes) as p:
p.map(partial(process_threshold, thresh_method=THRESH_METHOD), names)
print('Took %.4f seconds with %i process(es).' % (timer() - start, n_processes))
print('Done.')