基于fastSAM的自动标注小程序

import sys
from PyQt5.QtWidgets import QApplication, QLabel, QWidget, QVBoxLayout
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap, QImage
from PIL import Image
from PIL import Image, ImageDraw
import numpy as np
from fastsam import FastSAM


class ImageLabel(QLabel):
	def __init__(self, parent=None):
		super().__init__(parent)
		self.setMouseTracking(True)
		self.original_pil_image = None
		self.pil_image = None

	def set_image(self, pil_image):
		self.original_pil_image = pil_image.copy()
		self.pil_image = pil_image
		self.update_qimage()

	def update_qimage(self):
		if self.pil_image is not None:
			image_np = np.array(self.pil_image.convert("RGB"))
			height, width, channel = image_np.shape
			bytes_per_line = 3 * width
			q_image = QImage(image_np.data, width, height, bytes_per_line, QImage.Format_RGB888)
			self.setPixmap(QPixmap.fromImage(q_image))

	def mouseMoveEvent(self, event):
		x = event.x()
		y = event.y()
		self.parent().update_mouse_position(x, y)
		self.add_red_dot_to_image(x, y)

	def add_red_dot_to_image(self, x, y):
		if self.original_pil_image is not None:
			self.pil_image = self.original_pil_image.copy()
			self.pil_image = self.pil_image.convert('RGBA')
			if self.parent().display_mask is not None:
				mask = self.parent().display_mask
				# 创建红色图层
				red_layer = np.zeros((mask.shape[0], mask.shape[1], 4), dtype=np.uint8)
				red_layer[:, :, 0] = 255  # 红色通道全满
				red_layer[:, :, 3] = 100 * mask  # Alpha通道,使用mask控制透明度
				red_image = Image.fromarray(red_layer, 'RGBA')
				self.pil_image = Image.alpha_composite(self.pil_image, red_image)
				self.pil_image = self.pil_image.convert('RGB')  # 转换为RGB格式

			draw = ImageDraw.Draw(self.pil_image)
			radius = 5
			left_up_point = (x - radius, y - radius)
			right_down_point = (x + radius, y + radius)
			draw.ellipse([left_up_point, right_down_point], fill=(255, 0, 0))
			self.update_qimage()


class MainWindow(QWidget):
	def __init__(self):
		super().__init__()

		self.model = FastSAM("./weights/FastSAM.pt")
		self.input = Image.open('/home/robotics/FastSAM/daikons.jpg')
		self.input = self.input.convert("RGB")
		self.input = self.input.resize((1280, 720))
		everything_results = self.model(
			self.input,
			device='cuda',
			retina_masks=True,
			imgsz=1024,
			conf=0.4,
			iou=0.9
			)
		self.masks = everything_results[0].masks.data.cpu().numpy()
		self.display_mask = None

		self.initUI()

	def initUI(self):
		self.setWindowTitle('Display Mouse Position in Image')
		self.setGeometry(100, 100, 800, 600)

		# 创建一个标签来显示图像
		self.image_label = ImageLabel(self)
		self.image_label.setAlignment(Qt.AlignCenter)

		# 创建一个标签来显示鼠标位置
		self.position_label = QLabel(self)
		self.position_label.setAlignment(Qt.AlignCenter)

		layout = QVBoxLayout()
		layout.addWidget(self.image_label)
		layout.addWidget(self.position_label)
		self.setLayout(layout)

		self.display_pil_image(self.input)

	def display_pil_image(self, pil_image):
		self.image_label.set_image(pil_image)
		width, height = pil_image.size
		self.image_label.setFixedSize(width, height)

	def update_mouse_position(self, x, y):
		self.position_label.setText(f'Mouse Position: ({x}, {y})')
		self.display_mask = None
		for i in range(self.masks.shape[0]):
			mask = self.masks[i, :, :].astype(np.uint8)
			if mask[y, x] == 1:
				self.display_mask = mask
				break


if __name__ == '__main__':
	app = QApplication(sys.argv)
	main_window = MainWindow()
	main_window.show()
	sys.exit(app.exec_())

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值