-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
enable flowing from directory #60
Open
cneud
wants to merge
6
commits into
master
Choose a base branch
from
binarization_flow_from_directory
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bertsky
reviewed
Apr 29, 2024
sbb_binarize/sbb_binarize.py
Outdated
Comment on lines
245
to
305
def run(self, image=None, image_path=None, save=None, use_patches=False): | ||
if (image is not None and image_path is not None) or \ | ||
(image is None and image_path is None): | ||
raise ValueError("Must pass either a opencv2 image or an image_path") | ||
if image_path is not None: | ||
image = cv2.imread(image_path) | ||
img_last = 0 | ||
for n, (model, model_file) in enumerate(zip(self.models, self.model_files)): | ||
self.log.info('Predicting with model %s [%s/%s]' % (model_file, n + 1, len(self.model_files))) | ||
|
||
res = self.predict(model, image, use_patches) | ||
|
||
img_fin = np.zeros((res.shape[0], res.shape[1], 3)) | ||
res[:, :][res[:, :] == 0] = 2 | ||
res = res - 1 | ||
res = res * 255 | ||
img_fin[:, :, 0] = res | ||
img_fin[:, :, 1] = res | ||
img_fin[:, :, 2] = res | ||
|
||
img_fin = img_fin.astype(np.uint8) | ||
img_fin = (res[:, :] == 0) * 255 | ||
img_last = img_last + img_fin | ||
|
||
kernel = np.ones((5, 5), np.uint8) | ||
img_last[:, :][img_last[:, :] > 0] = 255 | ||
img_last = (img_last[:, :] == 0) * 255 | ||
if save: | ||
cv2.imwrite(save, img_last) | ||
return img_last | ||
def run(self, image=None, image_path=None, save=None, use_patches=False, dir_in=None, dir_out=None): | ||
print(dir_in,'dir_in') | ||
if not dir_in: | ||
if (image is not None and image_path is not None) or \ | ||
(image is None and image_path is None): | ||
raise ValueError("Must pass either a opencv2 image or an image_path") | ||
if image_path is not None: | ||
image = cv2.imread(image_path) | ||
img_last = 0 | ||
for n, (model, model_file) in enumerate(zip(self.models, self.model_files)): | ||
self.log.info('Predicting with model %s [%s/%s]' % (model_file, n + 1, len(self.model_files))) | ||
|
||
res = self.predict(model, image, use_patches) | ||
|
||
img_fin = np.zeros((res.shape[0], res.shape[1], 3)) | ||
res[:, :][res[:, :] == 0] = 2 | ||
res = res - 1 | ||
res = res * 255 | ||
img_fin[:, :, 0] = res | ||
img_fin[:, :, 1] = res | ||
img_fin[:, :, 2] = res | ||
|
||
img_fin = img_fin.astype(np.uint8) | ||
img_fin = (res[:, :] == 0) * 255 | ||
img_last = img_last + img_fin | ||
|
||
kernel = np.ones((5, 5), np.uint8) | ||
img_last[:, :][img_last[:, :] > 0] = 255 | ||
img_last = (img_last[:, :] == 0) * 255 | ||
if save: | ||
cv2.imwrite(save, img_last) | ||
return img_last | ||
else: | ||
ls_imgs = os.listdir(dir_in) | ||
for image_name in ls_imgs: | ||
print(image_name,'image_name') | ||
image = cv2.imread(os.path.join(dir_in,image_name) ) | ||
img_last = 0 | ||
for n, (model, model_file) in enumerate(zip(self.models, self.model_files)): | ||
self.log.info('Predicting with model %s [%s/%s]' % (model_file, n + 1, len(self.model_files))) | ||
|
||
res = self.predict(model, image, use_patches) | ||
|
||
img_fin = np.zeros((res.shape[0], res.shape[1], 3)) | ||
res[:, :][res[:, :] == 0] = 2 | ||
res = res - 1 | ||
res = res * 255 | ||
img_fin[:, :, 0] = res | ||
img_fin[:, :, 1] = res | ||
img_fin[:, :, 2] = res | ||
|
||
img_fin = img_fin.astype(np.uint8) | ||
img_fin = (res[:, :] == 0) * 255 | ||
img_last = img_last + img_fin | ||
|
||
kernel = np.ones((5, 5), np.uint8) | ||
img_last[:, :][img_last[:, :] > 0] = 255 | ||
img_last = (img_last[:, :] == 0) * 255 | ||
|
||
cv2.imwrite(os.path.join(dir_out,image_name), img_last) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of duplicating the whole function as a loop, I recommend a little refactoring:
- rewrite the function to a loop (which can be a single image)
- allow the kwarg
image_path
to be ambiguous between a single file and a directory, check and convert to loop – no need for new kwargdir_in
- allow the kwarg
save
to be a directory in the same way – no need for the new kwargdir_out
- raise exception if
save
is not a directory butimage_path
is
Also, I wonder if this is even needed – #48 already covers prediction of a directory... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
adds the option to use a directory as input for batch processing