-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CLI improvements - autodiscover previews images, is more robust (#27)
* Preview continues past first error. * Autodiscover previews images, and is more robust. * cleaning up exception handling a bit. * Bumping version to 0.4.5 * Automatically reformatting code with black and isort * Woops - failed to fully refactor. --------- Co-authored-by: Auto-format Bot <[email protected]>
- Loading branch information
1 parent
0549bb8
commit fa5e071
Showing
4 changed files
with
96 additions
and
44 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "framegrab" | ||
version = "0.4.4" | ||
version = "0.4.5" | ||
description = "Easily grab frames from cameras or streams" | ||
authors = ["Groundlight <[email protected]>"] | ||
license = "MIT" | ||
|
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import ascii_magic | ||
import cv2 | ||
from imgcat import imgcat | ||
from PIL import Image | ||
|
||
|
||
def imgcat_preview(name: str, frame): | ||
"""Displays the given frame in the terminal using imgcat.""" | ||
print(f"Previewing image from camera {name} in terminal. This requires an advanced terminal like iTerm2.") | ||
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) | ||
imgcat(frame_rgb) | ||
|
||
|
||
def cv2_preview(name: str, frame): | ||
"""Displays the given frame in a cv2 window, and wait for a key.""" | ||
cv2.imshow(name, frame) | ||
print(f"Previewing image in cv2 window. Select the window and press any key to continue.") | ||
_ = cv2.waitKey(0) | ||
cv2.destroyAllWindows() | ||
|
||
|
||
def ascii_preview(name: str, frame): | ||
"""Displays the given frame in the terminal using ascii art.""" | ||
columns, _ = shutil.get_terminal_size() | ||
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) | ||
pil_image = Image.fromarray(frame_rgb) | ||
out = ascii_magic.from_pillow_image(pil_image) | ||
out.to_terminal(columns=columns) | ||
|
||
|
||
def null_preview(name: str, frame): | ||
"""Does nothing.""" | ||
pass | ||
|
||
|
||
_PREVIEW_COMMANDS = { | ||
"imgcat": imgcat_preview, | ||
"cv2": cv2_preview, | ||
"ascii": ascii_preview, | ||
"none": null_preview, | ||
} | ||
|
||
PREVIEW_COMMAND_CHOICES = list(_PREVIEW_COMMANDS.keys()) | ||
|
||
|
||
def preview_image(frame, title: str, output_type: str): | ||
"""Displays the given frame using the given output method.""" | ||
if output_type not in PREVIEW_COMMAND_CHOICES: | ||
raise ValueError(f"Invalid output method: {output_type}. Valid options are {PREVIEW_COMMAND_CHOICES}.") | ||
command = _PREVIEW_COMMANDS[output_type] | ||
if frame is None: | ||
print(f"Trying to preview None frame from {title}.") | ||
return | ||
command(title, frame) |
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