/
distance_compute_write_to_file.py
89 lines (65 loc) · 2.2 KB
/
distance_compute_write_to_file.py
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
88
89
from skimage.feature import canny
from skimage import img_as_ubyte
import multiprocessing
import glob
import cv2
import os
import re
canny_sigma = 2.25
canny_sigma_closeup = 1.5
def _find_bottom_edge(img_bw):
for i in reversed(xrange(len(img_bw))):
for j in xrange(len(img_bw[0])):
if(img_bw[i][j] > 0):
point = (j, i)
return point
def worker(input_file_path, queue):
int_values = []
path_sections = input_file_path.split("/")
for string in path_sections:
if re.search(r'\d+', string) is not None:
int_values.append(int(re.search(r'\d+', string).group()))
file_count = int_values[-1]
image = cv2.imread(input_file_path, cv2.CV_LOAD_IMAGE_GRAYSCALE)
edges = img_as_ubyte(canny(image, sigma=canny_sigma))
img_bw = cv2.threshold(edges, 250, 255, cv2.THRESH_BINARY)[1]
point = _find_bottom_edge(img_bw)
try:
distance = len(img_bw) - point[1]
except TypeError:
try:
edges = img_as_ubyte(canny(image, sigma=canny_sigma_closeup))
img_bw = cv2.threshold(edges, 250, 255, cv2.THRESH_BINARY)[1]
distance = len(img_bw) - point[1]
except TypeError:
distance = 0
output = str(file_count) + ":" + str(distance) + "\n"
queue.put(output)
return output
def listener(queue):
fh = open("distances_sigma_" + str(canny_sigma) + "_" + str(canny_sigma_closeup) + ".txt", "a")
while True:
queue_value = queue.get()
if(queue_value == "kill"):
print "Listener Killed"
break
fh.write(queue_value)
fh.flush()
fh.close()
def main():
manager = multiprocessing.Manager()
queue = manager.Queue()
pool = multiprocessing.Pool(multiprocessing.cpu_count() + 2)
files = glob.glob(os.getcwd() + "/resize150/*")
watcher = pool.apply_async(listener, (queue,))
jobs = []
for i in xrange(len(files)):
job = pool.apply_async(worker, (files[i], queue))
jobs.append(job)
for i in xrange(len(jobs)):
jobs[i].get()
print str(i / float(len(jobs) - 1) * 100.0) + "% done"
queue.put("Kill")
pool.close()
if __name__ == "__main__":
main()