Skip to content

Commit caef034

Browse files
committed
[feat][graph][msl] support distance measurement
1 parent d771a54 commit caef034

1 file changed

Lines changed: 108 additions & 7 deletions

File tree

COMTool/plugins/gragh_widgets.py

Lines changed: 108 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -645,11 +645,19 @@ def __init__(self, parent=None, hintSignal=lambda type, title, msg: None, rmCall
645645
self.config["header"], escape=True, encoding="utf-8")
646646
self.layout = QGridLayout()
647647
self.setLayout(self.layout)
648+
649+
class ImageItemWithMouseHover(pg.ImageItem):
650+
def __init__(self, image=None, **kargs):
651+
super().__init__(image=image, kargs=kargs)
652+
653+
pg.setConfigOptions(imageAxisOrder='row-major')
648654
self.imv = pg.ImageView(view=pg.PlotItem())
649-
# self.imv.setMouseTracking(True)
655+
self.imv.setMouseTracking(True)
656+
self.setMouseTracking(True)
650657
self.queue = Queue()
651658
self.onceShot = False
652659
self.frames = []
660+
self.distances = []
653661
# jetcolors = [(128, 0, 0), (132, 0, 0), (136, 0, 0), (140, 0, 0), (144, 0, 0), (148, 0, 0), (152, 0, 0), (156, 0, 0), (160, 0, 0), (164, 0, 0), (168, 0, 0), (172, 0, 0), (176, 0, 0), (180, 0, 0), (184, 0, 0), (188, 0, 0), (192, 0, 0), (196, 0, 0), (200, 0, 0), (204, 0, 0), (208, 0, 0), (212, 0, 0), (216, 0, 0), (220, 0, 0), (224, 0, 0), (228, 0, 0), (232, 0, 0), (236, 0, 0), (240, 0, 0), (244, 0, 0), (248, 0, 0), (252, 0, 0), (255, 0, 0), (255, 4, 0), (255, 8, 0), (255, 12, 0), (255, 16, 0), (255, 20, 0), (255, 24, 0), (255, 28, 0), (255, 32, 0), (255, 36, 0), (255, 40, 0), (255, 44, 0), (255, 48, 0), (255, 52, 0), (255, 56, 0), (255, 60, 0), (255, 64, 0), (255, 68, 0), (255, 72, 0), (255, 76, 0), (255, 80, 0), (255, 84, 0), (255, 88, 0), (255, 92, 0), (255, 96, 0), (255, 100, 0), (255, 104, 0), (255, 108, 0), (255, 112, 0), (255, 116, 0), (255, 120, 0), (255, 124, 0), (255, 128, 0), (255, 132, 0), (255, 136, 0), (255, 140, 0), (255, 144, 0), (255, 148, 0), (255, 152, 0), (255, 156, 0), (255, 160, 0), (255, 164, 0), (255, 168, 0), (255, 172, 0), (255, 176, 0), (255, 180, 0), (255, 184, 0), (255, 188, 0), (255, 192, 0), (255, 196, 0), (255, 200, 0), (255, 204, 0), (255, 208, 0), (255, 212, 0), (255, 216, 0), (255, 220, 0), (255, 224, 0), (255, 228, 0), (255, 232, 0), (255, 236, 0), (255, 240, 0), (255, 244, 0), (255, 248, 0), (255, 252, 0), (254, 255, 1), (250, 255, 6), (246, 255, 10), (242, 255, 14), (238, 255, 18), (234, 255, 22), (230, 255, 26), (226, 255, 30), (222, 255, 34), (218, 255, 38), (214, 255, 42), (210, 255, 46), (206, 255, 50), (202, 255, 54), (198, 255, 58), (194, 255, 62), (190, 255, 66), (186, 255, 70), (182, 255, 74), (178, 255, 78), (174, 255, 82), (170, 255, 86), (166, 255, 90), (162, 255, 94), (158, 255, 98), (154, 255, 102), (150, 255, 106), (146, 255, 110), (142, 255, 114), (138, 255, 118), (134, 255, 122), (130, 255, 126),
654662
# (126, 255, 130), (122, 255, 134), (118, 255, 138), (114, 255, 142), (110, 255, 146), (106, 255, 150), (102, 255, 154), (98, 255, 158), (94, 255, 162), (90, 255, 166), (86, 255, 170), (82, 255, 174), (78, 255, 178), (74, 255, 182), (70, 255, 186), (66, 255, 190), (62, 255, 194), (58, 255, 198), (54, 255, 202), (50, 255, 206), (46, 255, 210), (42, 255, 214), (38, 255, 218), (34, 255, 222), (30, 255, 226), (26, 255, 230), (22, 255, 234), (18, 255, 238), (14, 255, 242), (10, 255, 246), (6, 255, 250), (2, 255, 254), (0, 252, 255), (0, 248, 255), (0, 244, 255), (0, 240, 255), (0, 236, 255), (0, 232, 255), (0, 228, 255), (0, 224, 255), (0, 220, 255), (0, 216, 255), (0, 212, 255), (0, 208, 255), (0, 204, 255), (0, 200, 255), (0, 196, 255), (0, 192, 255), (0, 188, 255), (0, 184, 255), (0, 180, 255), (0, 176, 255), (0, 172, 255), (0, 168, 255), (0, 164, 255), (0, 160, 255), (0, 156, 255), (0, 152, 255), (0, 148, 255), (0, 144, 255), (0, 140, 255), (0, 136, 255), (0, 132, 255), (0, 128, 255), (0, 124, 255), (0, 120, 255), (0, 116, 255), (0, 112, 255), (0, 108, 255), (0, 104, 255), (0, 100, 255), (0, 96, 255), (0, 92, 255), (0, 88, 255), (0, 84, 255), (0, 80, 255), (0, 76, 255), (0, 72, 255), (0, 68, 255), (0, 64, 255), (0, 60, 255), (0, 56, 255), (0, 52, 255), (0, 48, 255), (0, 44, 255), (0, 40, 255), (0, 36, 255), (0, 32, 255), (0, 28, 255), (0, 24, 255), (0, 20, 255), (0, 16, 255), (0, 12, 255), (0, 8, 255), (0, 4, 255), (0, 0, 255), (0, 0, 252), (0, 0, 248), (0, 0, 244), (0, 0, 240), (0, 0, 236), (0, 0, 232), (0, 0, 228), (0, 0, 224), (0, 0, 220), (0, 0, 216), (0, 0, 212), (0, 0, 208), (0, 0, 204), (0, 0, 200), (0, 0, 196), (0, 0, 192), (0, 0, 188), (0, 0, 184), (0, 0, 180), (0, 0, 176), (0, 0, 172), (0, 0, 168), (0, 0, 164), (0, 0, 160), (0, 0, 156), (0, 0, 152), (0, 0, 148), (0, 0, 144), (0, 0, 140), (0, 0, 136), (0, 0, 132), (0, 0, 128)]
655663
# self.imv.setColorMap(pg.ColorMap(
@@ -695,6 +703,12 @@ def __init__(self, parent=None, hintSignal=lambda type, title, msg: None, rmCall
695703
binnItem = QComboBox()
696704
baudItem = QComboBox()
697705

706+
focusCb = QCheckBox()
707+
focusX = QLineEdit("0")
708+
focusY = QLineEdit("0")
709+
focusD = QLineEdit("0")
710+
self.focus = (focusCb, (focusX, focusY), focusD)
711+
698712
unitSliderBox = QWidget()
699713
unitSliderBox.layout = QHBoxLayout()
700714
unitSliderBox.setLayout(unitSliderBox.layout)
@@ -755,6 +769,13 @@ def __init__(self, parent=None, hintSignal=lambda type, title, msg: None, rmCall
755769
itemCb.layout.addWidget(binnItem)
756770
itemCb.layout.addWidget(QLabel(_("Baud:")))
757771
itemCb.layout.addWidget(baudItem)
772+
itemCb.layout.addWidget(focusCb)
773+
itemCb.layout.addWidget(QLabel(_("D:")))
774+
itemCb.layout.addWidget(focusD)
775+
itemCb.layout.addWidget(QLabel(_("X:")))
776+
itemCb.layout.addWidget(focusX)
777+
itemCb.layout.addWidget(QLabel(_("Y:")))
778+
itemCb.layout.addWidget(focusY)
758779

759780
unitSliderBox.layout.addWidget(unitLabel)
760781
unitSliderBox.layout.addWidget(unitSlider)
@@ -806,9 +827,27 @@ def __init__(self, parent=None, hintSignal=lambda type, title, msg: None, rmCall
806827
imgItem = self.imv.getImageItem()
807828
# p = imgItem.ItemCursorHasChanged.connect(lambda x: print(x))
808829
scene = imgItem.scene()
809-
sigMouseMoved = scene.sigMouseMoved
810-
self.proxy = pg.SignalProxy(sigMouseMoved, rateLimit=60,
811-
slot=lambda x: print(x))
830+
831+
# imgItem.mouseMoveEvent =
832+
833+
# def ptint_wr(wr):
834+
# if len(wr[0]) > 1:
835+
# ichild = self.imv.children()[1].children()[0].children()[3]
836+
# # print(ichild.pos())
837+
# imgItm = wr[0][1]
838+
# cursor_pos = imgItm.cursor().pos()
839+
840+
# ise = imgItm.scene()
841+
# iv = ise.views()[0]
842+
# print(iv.pos(), end=', ')
843+
# abs_pos = iv.mapToGlobal(iv.pos())
844+
# print(abs_pos)
845+
846+
# self.proxy = pg.SignalProxy(scene.sigMouseHover, rateLimit=60,
847+
# slot=ptint_wr)
848+
849+
# self.proxy1 = pg.SignalProxy(scene.sigMouseMoved , rateLimit=60,
850+
# slot=lambda x:print(x))
812851

813852
# rangeConf.textChanged.connect(self.setRange)
814853
# rangeEnable.clicked.connect(lambda: self.setEnableRange(rangeEnable.isChecked()))
@@ -849,6 +888,29 @@ def dispChanged():
849888
baudItem.currentIndexChanged.connect(
850889
lambda i: self.sendCmd("AT+BAUD=%1d\r" % (i)))
851890

891+
def focusCbChanged(sta):
892+
if sta:
893+
x, y = 0, 0
894+
try:
895+
x = int(focusX.text())
896+
y = int(focusY.text())
897+
assert(x >= 0 and y >= 0)
898+
assert(x < 100 and y < 100)
899+
except Exception:
900+
x, y = 0, 0
901+
focusX.setText(str(x))
902+
focusY.setText(str(y))
903+
focusCb.setChecked(True)
904+
# print("set ({x},{y})".format(x=x, y=y))
905+
focusCb.toggled.connect(focusCbChanged)
906+
907+
def focusChanged(text):
908+
focusCb.setChecked(False)
909+
focusX.textChanged.connect(focusChanged)
910+
focusY.textChanged.connect(focusChanged)
911+
912+
focusD.setEnabled(False)
913+
852914
unitSlider.setMaximum(10)
853915
unitSlider.setSingleStep(1)
854916
unitSlider.setTickInterval(1)
@@ -1023,19 +1085,58 @@ def update(self, data: dict):
10231085

10241086
# print(rgba_img.shape)
10251087
# print(v)
1026-
self.frames.append(arr.T)
1088+
1089+
if len(self.frames) != 0 and self.frames[len(self.frames)-1].shape != arr.T.shape:
1090+
self.frames.clear()
1091+
self.frames.append(arr)
1092+
np_arrs = np.array(self.frames)
1093+
1094+
if self.focus[0].isChecked():
1095+
def compute_real_distance(val, unit):
1096+
ret = 0
1097+
if unit != 0:
1098+
ret = val * unit
1099+
else:
1100+
ret = int(val) / 5.1
1101+
ret *= ret
1102+
1103+
if len(self.distances) == 0:
1104+
for _ in range(8):
1105+
self.distances.append(ret)
1106+
else:
1107+
self.distances.append(ret)
1108+
self.distances = self.distances[-8:]
1109+
return int(np.mean(self.distances))
1110+
1111+
self.focus[2].setText(str(compute_real_distance(np_arrs[len(
1112+
self.frames)-1][int(self.focus[1][0].text()), int(self.focus[1][1].text())], self.unitSlider.value()))+"mm")
1113+
# print(self.focus[2].text())
1114+
1115+
def draw_reticle_and_info(buf, x, y):
1116+
ll = buf.shape[0] // 25
1117+
u, d, l, r = min(ll*25, max(0, y-ll)), max(0, min(ll*25, y+ll+1)
1118+
), min(ll*25, max(0, x-ll)), max(0, min(ll*25, x+ll+1))
1119+
buf[u:d, x] = 0x00
1120+
buf[y, l:r] = 0x00
1121+
draw_reticle_and_info(np_arrs[len(self.frames)-1],
1122+
int(self.focus[1][0].text()), int(self.focus[1][1].text()))
1123+
10271124
self.imv.clear()
1028-
self.imv.setImage(np.array(self.frames))
1125+
self.imv.setImage(np_arrs, autoRange=False)
10291126
self.imv.setCurrentIndex(len(self.frames)-1)
10301127
# color = self.pickColor(k)
10311128
# self.colors[k] = color
10321129
# self.curves[k] = self.p.plot(pen=pg.mkPen(color=color, width=2),
10331130
# name=k,)
10341131
# self.curves[k].setData(x=v["x"], y=v["y"])
10351132

1133+
# imgItem = self.imv.getImageItem()
1134+
# imgView = self.imv.getView()
1135+
# print(imgItem.boundingRect())
1136+
# print(imgView.viewGeometry())
1137+
10361138
def capture(self):
10371139
self.onceShot = True
1038-
pass
10391140

10401141
def onData(self, data: bytes):
10411142
while True:

0 commit comments

Comments
 (0)