forked from PacktPublishing/AdvancedPythonProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample5.py
More file actions
37 lines (24 loc) · 969 Bytes
/
example5.py
File metadata and controls
37 lines (24 loc) · 969 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
36
37
from multiprocessing import Pool
import cv2
import sys
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(im, output_name, thresh_method):
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 + output_name, thresh_im)
if __name__ == '__main__':
for n_processes in range(1, 7):
start = timer()
with Pool(n_processes) as p:
p.starmap(process_threshold, [(
cv2.imread(INPUT_PATH + name),
name,
THRESH_METHOD
) for name in names])
print('Took %.4f seconds with %i process(es).' % (timer() - start, n_processes))
print('Done.')