-
Notifications
You must be signed in to change notification settings - Fork 3
/
kpt.py
144 lines (122 loc) · 4.65 KB
/
kpt.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# -*- coding: utf-8 -*-
from PyQt5 import QtCore
from PyQt5 import QtGui
from PyQt5 import QtWidgets
from PyQt5.QtCore import pyqtSignal
import numpy as np
def get_foot(pt, line_begin, line_end):
dx = line_begin.x() - line_end.x()
dy = line_begin.y() - line_end.y()
if dx == 0 and dy == 0:
return QtCore.QPointF(0, 0)
u = (pt.x() - line_begin.x()) * (line_begin.x() - line_end.x()) + (pt.y() - line_begin.y()) * (line_begin.y() - line_end.y())
u = u / (dx * dx + dy * dy)
return QtCore.QPointF(line_begin.x() + u * dx, line_begin.y() + u * dy)
class Keypoint(QtWidgets.QGraphicsEllipseItem):
def __init__(self, name, pt, *args, **kwargs):
super(Keypoint, self).__init__(*args, **kwargs)
self.name = name
self.pt = pt
self.history_pos = []
self.last_pos = None
self.show_name = False
self.begin = None
self.end = None
self.is_eye = False
def setPos(self, *__args):
super(Keypoint, self).setPos(*__args)
if isinstance(__args[0], QtCore.QPointF):
x_ = __args[0].x()
y_ = __args[0].y()
else:
x_ = __args[0]
y_ = __args[1]
is_change = False
if self.last_pos is None or self.last_pos != [x_, y_]:
if self.last_pos is not None and self.last_pos != [x_, y_]:
self.pt[0] = x_
self.pt[1] = y_
self.updatePos(x_, y_)
is_change = True
self.last_pos = [x_, y_]
return is_change
def setPosNoHistory(self, *__args):
super(Keypoint, self).setPos(*__args)
if isinstance(__args[0], QtCore.QPointF):
x_ = __args[0].x()
y_ = __args[0].y()
else:
x_ = __args[0]
y_ = __args[1]
self.pt[0] = x_
self.pt[1] = y_
is_change = True
self.last_pos = [x_, y_]
return is_change
def redoPos(self):
if len(self.history_pos) > 1:
self.history_pos.pop()
lastPos = self.history_pos.pop()
self.setPos(lastPos[0], lastPos[1])
print(self.name, self.history_pos)
def updatePos(self, x, y):
self.history_pos.append([x, y])
def debug(self):
print(self.name, self.history_pos)
def paint(self, painter, option, widget=None):
if not self.is_eye:
keypoint_scale = 4 / self.parentItem().scale_flag
if keypoint_scale < 0.5:
keypoint_scale = 0.5
elif keypoint_scale > 2:
keypoint_scale = 2
self.setScale(keypoint_scale)
super(Keypoint, self).paint(painter, option, widget=widget)
if self.name is not None and self.show_name:
pen = QtGui.QPen(QtGui.QColor(253, 67, 23))
pen.setWidthF(0.2)
painter.setPen(pen)
painter.setFont(QtGui.QFont('times', 5))
painter.drawText(0, 0, str(self.name))
if self.begin is not None and self.end is not None:
tmp_foot = get_foot(self.pos(), self.begin.pos(), self.end.pos())
self.setPosNoHistory(tmp_foot)
def wheelEvent(self, event):
delta = event.delta()
cur_scale = self.scale()
if delta < 0:
self.setScale(cur_scale - 0.2)
elif delta > 0:
self.setScale(cur_scale + 0.2)
# super(Keypoint, self).wheelEvent(event)
# myshape = self.shape()
# for i in range(20):
# len = float(i) / 20
# print(self.mapToParent(myshape.pointAtPercent(len)))
def showName(self, flag):
self.show_name = flag
def setAlongPath(self, begin, end):
self.begin = begin
self.end = end
def setIsEye(self, flag):
self.is_eye = flag
def getKeypoints(self, sampleN=36, rotate_degree=0.):
if self.is_eye:
self.setRotation(rotate_degree)
path = QtGui.QPainterPath(self.shape())
path.closeSubpath()
res = np.zeros((sampleN, 2), dtype=np.float32)
for i in range(0, sampleN):
len = float(i) / sampleN * path.length() / 3
tmp_pt = self.mapToParent(path.pointAtPercent(path.percentAtLength(len)))
res[i] = [tmp_pt.x(), tmp_pt.y()]
return res
# myshape = self.shape()
# res = np.zeros((sampleN + 1, 2), dtype=np.float32)
# tmp_pt = self.mapToParent(myshape.pointAtPercent(0))
# res[0] = [tmp_pt.x(), tmp_pt.y()]
# for i in range(1, sampleN):
# len = float(i) / sampleN * 0.5
# tmp_pt = self.mapToParent(myshape.pointAtPercent(len))
# res[i] = [tmp_pt.x(), tmp_pt.y()]
# return res