-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModel.py
95 lines (72 loc) · 2.71 KB
/
Model.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
90
91
92
93
94
95
from __future__ import unicode_literals
import os
import sys
import cv2
import numpy as np
import cPickle
from mozi.utils.utils import gpu_to_cpu_model
from bson.objectid import ObjectId
class Tester(object):
def __init__(self, db, task_id):
self.db = db
self.task_id = task_id
self.model = None
def load_model(self, model_file_path, convert_to_cpu):
with open(model_file_path, 'rb') as fin:
mod = cPickle.load(fin)
if convert_to_cpu:
mod = gpu_to_cpu_model(mod)
self.model = mod
def generate_mask(self, image_dims, input_images_path, output_processed_path, output_masks_path):
mod = self.model
db = self.db
task_id = self.task_id
c, h, w = image_dims
processedCount = 0
result = db.tasks.update_one({
'_id': ObjectId(task_id),
}, {
'$currentDate': {
'startedAt': True,
},
})
for filename in os.listdir(input_images_path):
basename, file_extension = os.path.splitext(filename)
file_extension = file_extension.lower()
if file_extension not in ('.jpg', '.jpeg', '.tif', '.tiff', '.png'):
continue
print("{}".format(filename))
file_path = os.path.join(input_images_path, filename)
img = cv2.imread(file_path)
img = cv2.resize(img, (w,h))
out_processed_file_path = os.path.join(output_processed_path, basename + '.jpg')
cv2.imwrite(out_processed_file_path, img)
img = np.rollaxis(img, 2, 0)
mask = mod.fprop([img])
mask = (mask > 0.5).astype(int)
out_file_path = os.path.join(output_masks_path, basename + '.png')
mask_img = np.rollaxis(mask[0], 0, 3) * 255.0
#cv2.imwrite(out_file_path, mask_img)
z = np.zeros((w, h, 4))
for i in xrange(0, w):
for j in xrange(0, h):
for k in xrange(0, 3):
z[i][j][k] = mask_img[i][j][0]
z[i][j][3] = 127 if mask_img[i][j][0] > 127 else 0
#out_file_path = os.path.join(output_masks_path, basename + '.alpha.png')
cv2.imwrite(out_file_path, z)
processedCount = processedCount + 1
result = db.tasks.update_one({
'_id': ObjectId(task_id),
}, {
'$set': {
'processedImageCount': processedCount,
},
})
result = db.tasks.update_one({
'_id': ObjectId(task_id),
}, {
'$currentDate': {
'endedAt': True,
},
})