-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguiOpencv.py
61 lines (50 loc) · 1.4 KB
/
guiOpencv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import cv2
import numpy as np
class GUI(object):
def __init__(self, res, rectCallback, windowName='vid'):
w, h = res
self.windowName = windowName
cv2.imshow(windowName, np.zeros((h, w)))
self.rs = RectSelector(windowName, rectCallback)
def display(self, frame):
if self.rs.dragging:
self.rs.draw(frame)
cv2.imshow(self.windowName, frame)
self.key = chr(cv2.waitKey(1) & 0xFF)
def close(self):
cv2.destroyAllWindows()
class RectSelector:
def __init__(self, win, callback):
self.win = win
self.callback = callback
cv2.setMouseCallback(win, self.onmouse)
self.drag_start = None
self.drag_rect = None
def onmouse(self, event, x, y, flags, param):
x, y = np.int16([x, y]) # BUG
if event == cv2.EVENT_LBUTTONDOWN:
self.drag_start = (x, y)
return
if self.drag_start:
if flags & cv2.EVENT_FLAG_LBUTTON:
xo, yo = self.drag_start
x0, y0 = np.minimum([xo, yo], [x, y])
x1, y1 = np.maximum([xo, yo], [x, y])
self.drag_rect = None
if (x1 - x0) > 0 and (y1 - y0) > 0:
self.drag_rect = (x0, y0, x1, y1)
else:
rect = self.drag_rect
self.drag_start = None
self.drag_rect = None
if rect:
self.callback(rect)
def draw(self, vis):
if not self.drag_rect:
return False
x0, y0, x1, y1 = self.drag_rect
cv2.rectangle(vis, (x0, y0), (x1, y1), (0, 255, 0), 2)
return True
@property
def dragging(self):
return self.drag_rect is not None