pycharmers.opencv.windows module¶
Define functions and classes to make OpenCV window easier to use.
This KEY table is created by the following programs.
>>> from pycharmers.utils import tabulate
>>> from pycharmers.opencv import *
>>> lst = []
>>> for DEFAULT_KEY_NAME in list(filter(lambda x:x.startswith("DEFAULT_") and x.endswith("_KEYS"), globals().keys())):
>>> for group,(fmt,keys) in eval(DEFAULT_KEY_NAME).items():
>>> for name, key in keys.items():
>>> lst.append([DEFAULT_KEY_NAME, group, fmt.format(name=name), f"'{key}'"])
>>> DEFAULT_KEY_NAME, group = ("", "")
>>> tabulate(lst, headers=["VARNAME", "group", "description ('name')", "key"], tablefmt="grid")
VARNAME |
group |
description (‘name’) |
key |
---|---|---|---|
DEFAULT_CV_KEYS |
BASE |
To send the window the command ‘info’ |
‘i’ |
To send the window the command ‘quit’ |
‘q’ |
||
To send the window the command ‘delete’ |
‘<delete>’ |
||
To send the window the command ‘enter’ |
‘<enter>’ |
||
MOVING |
To move the window to the ‘left’ |
‘h’ |
|
To move the window to the ‘right’ |
‘l’ |
||
To move the window to the ‘down’ |
‘j’ |
||
To move the window to the ‘up’ |
‘k’ |
||
RATIO |
To ‘expansion’ the window |
‘+’ |
|
To ‘reduction’ the window |
‘-‘ |
||
POSITION |
To set/know the window ‘fullscreen’ |
‘f’ |
|
To set/know the window ‘topleft’ |
‘o’ |
||
To set/know the window ‘rectangle’ |
‘r’ |
||
DEFAULT_FRAME_KEYS |
FRAME |
To ‘advance’ frame |
‘w’ |
To ‘back’ frame |
‘b’ |
||
To ‘jump’ frame |
‘@’ |
||
RANGE |
To select range ‘start’ |
‘s’ |
|
To select range ‘end’ |
‘e’ |
||
TAKE |
To take ‘picture’ |
‘p’ |
|
To take ‘video’ |
‘v’ |
||
DEFAULT_REALTIME_KEYS |
TAKE |
To take ‘picture’ |
‘p’ |
To take ‘video’ |
‘v’ |
||
DEFAULT_TRACKING_KEYS |
TRACKING |
To track ‘start’ |
‘t’ |
To track ‘stop’ |
‘c’ |
||
To track ‘init’ |
‘n’ |
-
pycharmers.opencv.windows.
cv2key2chr
(key)[source]¶ Convert a key event into a Unicode string or an easy-to-understand string.
- Parameters
key (int) – The key event. (return of
cv2.waitKey
)- Returns
a Unicode string of one character or an easy-to-understand string.
- Return type
(str)
Examples
>>> from pycharmers.opencv import cv2key2chr >>> import cv2 >>> import numpy as np >>> from pycharmers.opencv import cv2key2chr ... >>> winname = "cv2key2chr" >>> image = np.random.randint(low=0, high=255, size=(200,200,3), dtype=np.uint8) >>> while True: ... key = cv2.waitKey(1) ... cv2.imshow(winname=winname, mat=image) ... if key!=-1: print(key, cv2key2chr(key)) ... if key==27: break >>> cv2.destroyWindow(winname=winname)
-
class
pycharmers.opencv.windows.
cvKeys
(**cv_keys)[source]¶ Bases:
object
Keys for using openCV
- Parameters
cv_keys (dict) – Key information. See
set_keys
.{group: [fmt, {name1:key1, name2:key2,...}]}
-
groups
¶ Group list.
- Type
list
-
GROUP_NAME_KEY
¶ Key for a command.
- Type
str
-
GROUP_NAME_KEY_ORD
¶ Ord of the Key (
GROUP_NAME_KEY
)- Type
int
-
GROUP_KEYS
¶ Keys for a group commands.
- Type
list
-
GROUP_KEYS_ORD
¶ Ords of the all Keys in a group (
GROUP_KEYS
)- Type
list
-
ALL_KEYS
¶ All Keys for commands.
- Type
list
-
ALL_KEYS_ORD
¶ All ords of the all Keys (
ALL_KEYS
)- Type
list
Examples
>>> from pycharmers.opencv import cvKeys, DEFAULT_CV_KEYS >>> cvKey = cvKeys(**DEFAULT_CV_KEYS) >>> cvKey.MOVING_LEFT_KEY 'h' >>> cvKey.MOVING_LEFT_KEY_ORD 104 >>> cvKey.MOVING_KEYS ['h', 'l', 'j', 'k'] >>> cvKey.MOVING_KEYS_ORD [104, 108, 106, 107] >>> cvKey.ALL_KEYS ['i', 'q', '<delete>', '<enter>', 'h', 'l', 'j', 'k', '+', '-', 'f', 'o'] >>> cvKey.ALL_KEYS_ORD [105, 113, 8, 13, 104, 108, 106, 107, 43, 45, 102, 111]
-
set_keys
(group, fmt, **keys)[source]¶ Set keys.
- Parameters
group (str) – A group name. (ex.
"MOVING"
)fmt (str) – How to describe group keys. Need to contain “{name}” (ex.
"To move the window to the {name}"
)keys (dict) –
{name1:key1, name2:key2, ...}
* name (str) : A name in thegroup
. (ex."left"
) * key (str) : A key corresponding to thename
. (ex."l"
)
-
static
ord
(c=None)[source]¶ Return the Unicode code point for a one-character string.
- Parameters
c (str) – A character
- Returns
Unicode code point
- Return type
i (int)
Examples
>>> from pycharmers.opencv import cvKeys >>> cvKey = cvKeys() >>> cvKey.ord("l") == ord("l") True >>> cvKey.ord("<delete>") 8 >>> ord("<delete>") TypeError: ord() expected a character, but string of length 8 found
-
property
ALL_KEYS
¶ Return all the keys that have been set.
-
property
BASE_KEYS
¶
-
property
FRAME_KEYS
¶
-
property
MOVING_KEYS
¶
-
property
POSITION_KEYS
¶
-
property
RANGE_KEYS
¶
-
property
RATIO_KEYS
¶
-
property
TAKE_KEYS
¶
-
property
TRACKING_KEYS
¶
-
pycharmers.opencv.windows.
wait_for_input
(fmt='Your input : {val}', cvKey=<pycharmers.opencv.windows.cvKeys object>)[source]¶ Wait until the valid input is entered.
- Parameters
- Returns
Input string.
- Return type
curt_val (str)
Examples
>>> import cv2 >>> from pycharmers.opencv import wait_for_input, SAMPLE_LENA_IMG >>> cv2.imshow("Lena", cv2.imread(SAMPLE_LENA_IMG)) >>> val = wait_for_input() >>> cv2.destroyAllWindows()
-
pycharmers.opencv.windows.
wait_for_choice
(*choices)[source]¶ Wait until choicing the one of the keys.
- Parameters
choices (tuple) – Choices.
- Returns
The ith element in
choices
. (i
means Your input. )- Return type
choice (element)
Examples
>>> import cv2 >>> from pycharmers.opencv import wait_for_choice, SAMPLE_LENA_IMG >>> cv2.imshow("Lena", cv2.imread(SAMPLE_LENA_IMG)) >>> val = wait_for_choice(*list("ltrb")) >>> cv2.destroyAllWindows()
-
class
pycharmers.opencv.windows.
cvWindow
(winname=None, dirname=None, move_distance=10, expansion_rate=1.1, cvKey=<pycharmers.opencv.windows.cvKeys object>)[source]¶ Bases:
object
Window & Image Location
Ix,Iy,Iw,Ih =
getWindowImageRect
moveWindow
make the window left top to(x,y)
Variable Names:
(Wx,Wy) ----------------------- (x+w,y) | ^ | | | Fh | | v | | (Ix,Iy) ------ (Ix+w,Iy) | | Fw | | | |<------>| Image | | | | | | | (Ix,Iy+h) ---- (Ix+w,Iy+h) | | | (Wx,Wy+h) --------------------- (Wx+w,Wy+h)
- Parameters
no (int) – The number of windows.
-
video_save_dir
¶ Path/to/created_video/directory
- Type
str
-
img_save_dir
¶ Path/to/created_image/directory
- Type
str
-
reduction_rate
¶ 1./expansion_rate
- Type
float
Examples
>>> import cv2 >>> from pycharmers.opencv import cvWindow >>> window = cvWindow() >>> while True: ... key = cv2.waitKey(0) ... is_break = window.recieveKey(key) ... if is_break: ... break >>> cv2.destroyAllWindows()
-
no
= 0¶
-
property
reduction_rate
¶
-
setup
(winname=None, dirname=None)[source]¶ Setting up for the OpenCV windows.
Decide window name.
Decide and Create the location of the directory to save image.
- Parameters
winname (str) – Unique winname.
dirname (str) – dirname for saved image or directory.
-
property
frame_size
¶ Get Frame width (
Fw
) and Frame height (Fh
)
-
property
image_size
¶ Get Image width (
Iw
) and Image height (Ih
)
-
resizeWindow
(width, height)[source]¶ Resizes window to the specified size
- Parameters
width (int) – The new window width.
height (int) – The new window height.
-
moveWindow
(x, y)[source]¶ Moves window to the specified position
- Parameters
x (int) – The new x-coordinate of the window.
y (int) – The new y-coordinate of the window.
-
getWindowImageRect
()[source]¶ Provides rectangle of image in the window.
The function getWindowImageRect returns the client screen coordinates, width and height of the image rendering area.
-
get_anchors
()[source]¶ Get the
(Wx,Wy,Fw,Fh)
Notes
getWindowImageRect
gets the rectangle of image in the window.moveWindow
make the window left top to(x,y)
-
class
pycharmers.opencv.windows.
RealTimeWindow
(winname=None, dirname=None, ext='.jpg', move_distance=10, expansion_rate=1.1, cam=0, cvKey=<pycharmers.opencv.windows.cvKeys object>)[source]¶ Bases:
pycharmers.opencv.windows.cvWindow
OpenCV window for RealiTime Video.
- Parameters
ext (str) – File extension. (default=
".jpg"
)cam (int) – The id of the web camera.
-
basenames
¶ Concatenate of the final components of
path
- Type
str
-
gen
¶ cv2.VideoCapture(cam)
- Type
gen
-
crt_frame
¶ The current image.
- Type
ndarray
-
crt_fname
¶ The current image name.
- Type
str
-
is_capture
¶ Whether capturing or not.
- Type
bool
Examples
>>> import cv2 >>> from pycharmers.opencv import RealTimeWindow >>> window = RealTimeWindow() >>> while True: ... key = cv2.waitKey(0) ... is_break = window.recieveKey(key) ... if is_break: ... break >>> cv2.destroyAllWindows()
-
class
pycharmers.opencv.windows.
FrameWindow
(*path, winname=None, dirname=None, ext='.jpg', move_distance=10, expansion_rate=1.1, cvKey=<pycharmers.opencv.windows.cvKeys object>)[source]¶ Bases:
pycharmers.opencv.windows.cvWindow
OpenCV window for Frames (images or video).
- Parameters
path (str) – path to video file, or directory which stores sequential images.
ext (str) – File extension. (default=
".jpg"
)
-
basenames
¶ Concatenate of the final components of
path
- Type
str
-
input_path
¶ path
- Type
tuple
-
frame_generator
¶ function which generates sequential frames. - If
len(path)==1
->mono_frame_generator
- Otherwise ->multi_frame_generator_concat
- Type
function
-
gen
¶ Generator created by
frame_generator
- Type
gen
-
total_num
¶ The total number of frames.
- Type
int
-
crt_frame_no
¶ The current number of frames. (1-based index.)
- Type
int
-
crt_frame
¶ The current image.
- Type
ndarray
-
range_start
¶ Start number in the selected range.
- Type
int
-
range_end
¶ End number in the selected range.
- Type
int
Examples
>>> import cv2 >>> from pycharmers.opencv import FrameWindow, SAMPLE_VTEST_VIDEO >>> window = FrameWindow(SAMPLE_VTEST_VIDEO) >>> while True: ... key = cv2.waitKey(0) ... is_break = window.recieveKey(key) ... if is_break: ... break >>> cv2.destroyAllWindows()
-
fnaming
(*no, ext=None)[source]¶ Naming the file based on the number of frame.
- Parameters
no (int) – Index of the frame.
ext (str) – File extension.
-
show
(mat=None)[source]¶ Displays an image in the specified window. (
self.winname
)- Parameters
mat (np.ndarray) – Image to be shown.
-
class
pycharmers.opencv.windows.
TrackingWindow
(path, tracker='boosting', coord_type='xywh', bbox=(0, 0, 0, 0), winname=None, dirname=None, move_distance=10, expansion_rate=1.1, cvKey=<pycharmers.opencv.windows.cvKeys object>, **metadata)[source]¶ Bases:
pycharmers.opencv.windows.FrameWindow
,pycharmers.opencv.windows.RealTimeWindow
OpenCV window for Trackings (images or video).
Examples
>>> import cv2 >>> from pycharmers.opencv import TrackingWindow, SAMPLE_VTEST_VIDEO >>> window = TrackingWindow(path=SAMPLE_VTEST_VIDEO, tracker="boosting", coord_type="xywh") >>> while True: ... key = cv2.waitKey(0) ... is_break = window.recieveKey(key) ... if is_break: ... break >>> cv2.destroyAllWindows()
-
property
init_bbox_winname
¶
-
property