def mouseReleaseEvent(self, event: Qt.QMouseEvent): super().mouseReleaseEvent(event) file_name = self.dog_sound_1 if event.button( ) == Qt.Qt.LeftButton else self.dog_sound_2 file_name = Qt.QUrl.fromLocalFile(file_name) self.player.setMedia(Qt.QMediaContent(file_name)) self.player.play()
def mousePressEvent(self, event: QMouseEvent): # key == 1 left; key == 2 right pos_rel_app = event.pos() key = event.button() pos_rel_map = pos_rel_app.x() - self.mapLabel.x(), pos_rel_app.y() - self.mapLabel.y() if 0 < pos_rel_map[0] < self.mapLabel.width() and 0 < pos_rel_map[1] < self.mapLabel.height(): if key == 1: self.obj_by_click(pos_rel_map) elif key == 2: self.obj_by_click(pos_rel_map, True)
def mouseReleaseEvent(self, event: Qt.QMouseEvent) -> None: super().mouseReleaseEvent(event) if event.button() == Qt.Qt.LeftButton: self.setDragMode(self.drag_mode) self.mouseReleased.emit(event)
def mousePressEvent(self, event: Qt.QMouseEvent) -> None: if event.button() == Qt.Qt.LeftButton: self.drag_mode = self.dragMode() self.setDragMode(Qt.QGraphicsView.ScrollHandDrag) super().mousePressEvent(event) self.mousePressed.emit(event)
def mouse_release(self, ev: QMouseEvent): if self.state == STATE.NORMAL: return if ev.button() == Qt.LeftButton: if self.state == STATE.DRAW_RECTANGLE: pos = ev.pos() valid, relative_pos = self.pos_in_img_area(pos) self.logger.debug(valid) if valid: self.logger.debug(relative_pos) self.end_point = (int(relative_pos[0] / self.scale), int(relative_pos[1] / self.scale)) elif ev.button() == Qt.RightButton: if self.state == STATE.NORMAL: return if self.state == STATE.DRAW_RECTANGLE: if self.start_point is None or self.end_point is None: return pos = ev.pos() valid, relative_pos = self.pos_in_img_area(pos) if valid: click_pos = (int(relative_pos[0] / self.scale), int(relative_pos[1] / self.scale)) min_x, max_x = min(self.start_point[0], self.end_point[0]), max( self.start_point[0], self.end_point[0]) min_y, max_y = min(self.start_point[1], self.end_point[1]), max( self.start_point[1], self.end_point[1]) self.logger.debug(click_pos) if min_x <= click_pos[0] <= max_x and min_y <= click_pos[ 1] <= max_y: pixel_value = self.origin_img[click_pos[1], click_pos[0], 0] low, high = pixel_value + \ self.pixel_range[0], pixel_value + \ self.pixel_range[1] selected_area = self.origin_img[min_y:max_y + 1, min_x:max_x + 1, 0] x, y = np.where( (selected_area >= low) * (selected_area <= high)) x = x + min_y y = y + min_x self.labelimg[x, y] = 1 pic = self.draw_points() self.show_pic(file_name=None, content=pic) self.histroty.append((x, y)) self.modified = True elif self.state == STATE.DRAW_POLYGON: if self.polygon_points is None or self.polygon_points[ 'finish'] is False: return pos = ev.pos() valid, relative_pos = self.pos_in_img_area(pos) if valid: click_pos = (int(relative_pos[0] / self.scale), int(relative_pos[1] / self.scale)) self.logger.debug(click_pos) x_pos = np.array( [item[0] for item in self.polygon_points['points']]) y_pos = np.array( [item[1] for item in self.polygon_points['points']]) min_x, max_x = x_pos.min(), x_pos.max() min_y, max_y = y_pos.min(), y_pos.max() p = np.array(self.polygon_points['points']).reshape( [1, len(self.polygon_points['points']), 2]) p[0, :, 0] -= min_x p[0, :, 1] -= min_y mask = np.zeros([max_y - min_y + 1, max_x - min_x + 1], np.uint8) mask = cv.fillPoly(mask, p, 1) pixel_value = self.origin_img[click_pos[1], click_pos[0], 0] low, high = pixel_value + \ self.pixel_range[0], pixel_value + self.pixel_range[1] selected_img = self.origin_img[min_y:max_y + 1, min_x:max_x + 1, 0] x, y = np.where( (selected_img >= low) * (selected_img <= high) * mask) x = x + min_y y = y + min_x self.labelimg[x, y] = 1 pic = self.draw_points() self.show_pic(file_name=None, content=pic) self.histroty.append((x, y)) self.modified = True elif self.state == STATE.DRAW_ELLIPSE: if self.ellipse_points is None or len( self.ellipse_points['points'] ) < 3 or self.ellipse_points['info'] is None: return pos = ev.pos() valid, relative_pos = self.pos_in_img_area(pos) if valid: click_pos = (int(relative_pos[0] / self.scale), int(relative_pos[1] / self.scale)) self.logger.debug(click_pos) center, r, theta = self.ellipse_points['info'] theta = int(np.around(theta)) p = cv.ellipse2Poly(center, r, theta, 0, 360, 1) num_p = p.shape[0] p = p.reshape((1, num_p, 2)) min_x, max_x = np.min(p[0, :, 0]), np.max(p[0, :, 0]) min_y, max_y = np.min(p[0, :, 1]), np.max(p[0, :, 1]) p[0, :, 0] -= min_x p[0, :, 1] -= min_y mask = np.zeros((max_y - min_y + 1, max_x - min_x + 1), np.uint8) mask = cv.fillPoly(mask, p, 1) pixel_value = self.origin_img[click_pos[1], click_pos[0], 0] low, high = pixel_value + \ self.pixel_range[0], pixel_value + self.pixel_range[1] selected_img = self.origin_img[min_y:max_y + 1, min_x:max_x + 1, 0] x, y = np.where( (selected_img >= low) * (selected_img <= high) * mask) x = x + min_y y = y + min_x self.labelimg[x, y] = 1 pic = self.draw_points() self.show_pic(file_name=None, content=pic) self.histroty.append((x, y)) self.modified = True self.saved = False
def mouse_pressed(self, ev: QMouseEvent): if self.state == STATE.NORMAL: return if ev.button() == Qt.LeftButton: pos = ev.pos() valid, relative_pos = self.pos_in_img_area(pos) self.logger.debug(valid) if valid: self.logger.debug(relative_pos) point = (int(relative_pos[0] / self.scale), int(relative_pos[1] / self.scale)) if self.state == STATE.DRAW_RECTANGLE: self.start_point = point self.end_point = None self.logger.debug(self.start_point) elif self.state == STATE.DRAW_POLYGON: if self.polygon_points is None or self.polygon_points[ 'finish'] is True: self.polygon_points = { 'finish': False, 'points': [point] } else: p1 = np.array([ self.polygon_points['points'][0][0] * self.scale, self.polygon_points['points'][0][1] * self.scale ]) p2 = np.array( [point[0] * self.scale, point[1] * self.scale]) dis = np.linalg.norm(p1 - p2, 2) if dis < 10: self.polygon_points['finish'] = True self.draw_lines(self.polygon_points['points'], True) else: self.polygon_points['points'].append(point) self.draw_lines(self.polygon_points['points'], False) if self.labelimg is not None: pic = self.draw_points() self.show_pic(file_name=None, content=pic) else: self.show_pic(file_name=None, content=self.panel_pic) elif self.state == STATE.DRAW_ELLIPSE: if self.ellipse_points is None: self.ellipse_points = {'points': [point], 'info': None} elif len(self.ellipse_points['points']) == 1: self.ellipse_points['points'].append(point) self.draw_lines(self.ellipse_points['points'], False) if self.labelimg is not None: pic = self.draw_points() self.show_pic(file_name=None, content=pic) else: self.show_pic(file_name=None, content=self.panel_pic) elif len(self.ellipse_points['points']) == 2: self.ellipse_points['points'].append(point) self.ellipse_points['info'] = self.draw_ellipse( self.ellipse_points['points']) if self.labelimg is not None: pic = self.draw_points() self.show_pic(file_name=None, content=pic) else: self.show_pic(file_name=None, content=self.panel_pic) else: self.ellipse_points = {'points': [point], 'info': None} else: raise NotImplementedError() elif ev.button() == Qt.RightButton: pass