-
Notifications
You must be signed in to change notification settings - Fork 14
Fix: fix loadimage function to robustly load image #34
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
Changes from 3 commits
c124f16
757b223
3f4f892
9a28a4b
b2cc5c5
0383f1f
6124e29
709b48c
e34638e
22a2bcd
e5685ca
8b00059
98c7220
46f175b
45f2952
4369cc2
556e141
ac7ece7
1567b45
03ae177
c8b83e5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| **Added:** | ||
|
|
||
| * <news item> | ||
|
|
||
| **Changed:** | ||
|
|
||
| * <news item> | ||
|
|
||
| **Deprecated:** | ||
|
|
||
| * <news item> | ||
|
|
||
| **Removed:** | ||
|
|
||
| * <news item> | ||
|
|
||
| **Fixed:** | ||
|
|
||
| * Fixed `loadimage` function in any directory. | ||
|
|
||
| **Security:** | ||
|
|
||
| * <news item> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ | |
| import fnmatch | ||
| import os | ||
| import time | ||
| from pathlib import Path | ||
|
|
||
| import numpy as np | ||
|
|
||
|
|
@@ -66,31 +67,40 @@ def flipImage(self, pic): | |
| return pic | ||
|
|
||
| def loadImage(self, filename): | ||
|
||
| """Load image file, if failed (for example loading an incomplete | ||
| file), then it will keep trying loading file for 5s. | ||
| """Load image file using pathlib. If loading fails (e.g. | ||
sbillinge marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| incomplete file), retry for 5 seconds (10×0.5s). | ||
|
|
||
| :param filename: str, image file name | ||
| :return: 2d ndarray, 2d image array (flipped) | ||
| :param filename: str or Path, image file name or path | ||
| :return: 2D ndarray, flipped image array | ||
sbillinge marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """ | ||
| if os.path.exists(filename): | ||
| filename = Path( | ||
| filename | ||
| ).expanduser() # handle "~", make it a Path object | ||
| if filename.exists(): | ||
| filenamefull = filename | ||
| else: | ||
| filenamefull = os.path.join(self.opendirectory, filename) | ||
| image = np.zeros(10000).reshape(100, 100) | ||
| if os.path.exists(filenamefull): | ||
| i = 0 | ||
| while i < 10: | ||
| try: | ||
| if os.path.splitext(filenamefull)[-1] == ".npy": | ||
| image = np.load(filenamefull) | ||
| else: | ||
| image = openImage(filenamefull) | ||
| i = 10 | ||
| except FileNotFoundError: | ||
| i = i + 1 | ||
| time.sleep(0.5) | ||
| image = self.flipImage(image) | ||
| image[image < 0] = 0 | ||
| found_files = list(Path.home().rglob(filename.name)) | ||
| filenamefull = found_files[0] if found_files else None | ||
|
|
||
| if filenamefull is None or not filenamefull.exists(): | ||
| print(f"Warning: file not found: {filename}") | ||
| return np.zeros((100, 100)) | ||
|
|
||
| image = np.zeros((100, 100)) | ||
| for _ in range(10): # retry 10 times (5 seconds total) | ||
| try: | ||
| if filenamefull.suffix == ".npy": | ||
| image = np.load(filenamefull) | ||
| else: | ||
| image = openImage( | ||
sbillinge marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| str(filenamefull) | ||
|
||
| ) # openImage expects str | ||
| break | ||
| except FileNotFoundError: | ||
| time.sleep(0.5) | ||
|
|
||
| image = self.flipImage(image) | ||
sbillinge marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| image[image < 0] = 0 | ||
| return image | ||
|
|
||
| def genFileList( | ||
|
|
||
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.
load_image()function correctly finds files when passed a relative path