Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Udayraj123
GitHub Repository: Udayraj123/OMRChecker
Path: blob/master/src/utils/interaction.py
228 views
1
from dataclasses import dataclass
2
3
import cv2
4
from screeninfo import get_monitors
5
6
from src.logger import logger
7
from src.utils.image import ImageUtils
8
9
monitor_window = get_monitors()[0]
10
11
12
@dataclass
13
class ImageMetrics:
14
# TODO: Move TEXT_SIZE, etc here and find a better class name
15
window_width, window_height = monitor_window.width, monitor_window.height
16
# for positioning image windows
17
window_x, window_y = 0, 0
18
reset_pos = [0, 0]
19
20
21
class InteractionUtils:
22
"""Perform primary functions such as displaying images and reading responses"""
23
24
image_metrics = ImageMetrics()
25
26
@staticmethod
27
def show(name, origin, pause=1, resize=False, reset_pos=None, config=None):
28
image_metrics = InteractionUtils.image_metrics
29
if origin is None:
30
logger.info(f"'{name}' - NoneType image to show!")
31
if pause:
32
cv2.destroyAllWindows()
33
return
34
if resize:
35
if not config:
36
raise Exception("config not provided for resizing the image to show")
37
img = ImageUtils.resize_util(origin, config.dimensions.display_width)
38
else:
39
img = origin
40
41
if not is_window_available(name):
42
cv2.namedWindow(name)
43
44
cv2.imshow(name, img)
45
46
if reset_pos:
47
image_metrics.window_x = reset_pos[0]
48
image_metrics.window_y = reset_pos[1]
49
50
cv2.moveWindow(
51
name,
52
image_metrics.window_x,
53
image_metrics.window_y,
54
)
55
56
h, w = img.shape[:2]
57
58
# Set next window position
59
margin = 25
60
w += margin
61
h += margin
62
63
w, h = w // 2, h // 2
64
if image_metrics.window_x + w > image_metrics.window_width:
65
image_metrics.window_x = 0
66
if image_metrics.window_y + h > image_metrics.window_height:
67
image_metrics.window_y = 0
68
else:
69
image_metrics.window_y += h
70
else:
71
image_metrics.window_x += w
72
73
if pause:
74
logger.info(
75
f"Showing '{name}'\n\t Press Q on image to continue. Press Ctrl + C in terminal to exit"
76
)
77
78
wait_q()
79
InteractionUtils.image_metrics.window_x = 0
80
InteractionUtils.image_metrics.window_y = 0
81
82
83
@dataclass
84
class Stats:
85
# TODO Fill these for stats
86
# Move qbox_vals here?
87
# badThresholds = []
88
# veryBadPoints = []
89
files_moved = 0
90
files_not_moved = 0
91
92
93
def wait_q():
94
esc_key = 27
95
while cv2.waitKey(1) & 0xFF not in [ord("q"), esc_key]:
96
pass
97
cv2.destroyAllWindows()
98
99
100
def is_window_available(name: str) -> bool:
101
"""Checks if a window is available"""
102
try:
103
cv2.getWindowProperty(name, cv2.WND_PROP_VISIBLE)
104
return True
105
except Exception as e:
106
print(e)
107
return False
108
109