Skip to content

Instantly share code, notes, and snippets.

@malefs
Created April 25, 2018 14:13
Show Gist options
  • Select an option

  • Save malefs/f318695319f26ea4ea2df1e991d2a583 to your computer and use it in GitHub Desktop.

Select an option

Save malefs/f318695319f26ea4ea2df1e991d2a583 to your computer and use it in GitHub Desktop.
import cv2
import time
import glob
import multiprocessing.pool as mp
def process_img(image_file):
if '_canny.jpg' not in image_file:
print 'processing', image_file
img = cv2.imread(image_file)
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img = cv2.blur(img, (3,3))
img = cv2.Canny(img, 10, 25)
cv2.imwrite(image_file[:-4] + '_canny.jpg', img)
def single_thread(image_files):
start = time.time()
for image_file in image_files:
process_img(image_file)
print 'single thread elapsed time:', int(time.time() - start)
def multi_threads(image_files, nthr=2):
start = time.time()
pool = mp.ThreadPool(nthr)
for image_file in image_files:
pool.apply_async(process_img, (image_file,))
pool.close()
pool.join()
print nthr, 'threads elapsed time:', int(time.time() - start)
if __name__ == '__main__':
files = glob.glob('images/*.jpg')
single_thread(files)
multi_threads(files, nthr=2)
multi_threads(files, nthr=4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment