通过孪生神经网络实现在一幅或两幅图像中利用鼠标单击方式采集样本。 (1)若两区域特征相同,则在两区域的中心分别单击鼠标,然后将以鼠标为中心的两区域保存并设置为相同类别(可保存多个尺寸)。 (2)以同样的方式保存不同的区域并设置为不同类别。并输出代码
时间: 2024-02-01 18:15:05 浏览: 93
在图像上实现鼠标交互,选择感兴趣区域进行处理
5星 · 资源好评率100%
这里提供一个基于PyQt5和TensorFlow的样采集工具的代码示例。代码实现了两个窗口,一个用于显示图像,另一个用于设置类别和保存样本。
首先需要安装PyQt5和TensorFlow:
```
pip install PyQt5 tensorflow
```
然后是代码实现:
```python
import sys
import os
import numpy as np
import tensorflow as tf
from PyQt5.QtWidgets import QMainWindow, QLabel, QPushButton, QApplication, QWidget, QVBoxLayout, QHBoxLayout, QFileDialog, QInputDialog
from PyQt5.QtGui import QPixmap, QImage, QPainter, QPen, QBrush
from PyQt5.QtCore import Qt, QPoint
class ImageLabel(QLabel):
def __init__(self, parent=None):
super().__init__(parent)
self.setMouseTracking(True)
self.pixmap = None
self.painted = False
self.painter = QPainter()
self.pen = QPen(Qt.red)
self.brush = QBrush(Qt.red, Qt.SolidPattern)
self.label = QLabel(self)
self.label.setAlignment(Qt.AlignCenter)
self.label.setStyleSheet("border: 1px solid black;")
self.label.hide()
self.rect = None
def set_pixmap(self, pixmap):
self.pixmap = pixmap
self.setFixedSize(pixmap.width(), pixmap.height())
self.setPixmap(pixmap)
def mousePressEvent(self, event):
if not self.painted:
self.painter.begin(self.pixmap)
self.painter.setPen(self.pen)
self.painter.setBrush(self.brush)
self.painter.drawEllipse(event.pos(), 10, 10)
self.painter.end()
self.update()
self.rect = (event.pos().x() - 50, event.pos().y() - 50, 100, 100)
self.label.setGeometry(*self.rect)
self.label.show()
self.painted = True
else:
self.painter.begin(self.pixmap)
self.painter.setPen(self.pen)
self.painter.setBrush(self.brush)
self.painter.drawEllipse(event.pos(), 10, 10)
self.painter.end()
self.update()
self.rect = (self.rect[0], self.rect[1], event.pos().x() - self.rect[0] + 50, event.pos().y() - self.rect[1] + 50)
self.label.setGeometry(*self.rect)
def mouseMoveEvent(self, event):
if not self.painted:
return
pixmap = self.pixmap.copy()
painter = QPainter(pixmap)
painter.setPen(self.pen)
painter.setBrush(self.brush)
painter.drawEllipse(event.pos(), 10, 10)
painter.end()
self.setPixmap(pixmap)
def save_image(self, path):
if self.painted:
image = self.pixmap.copy(*self.rect)
image.save(path)
class SampleCollector(QMainWindow):
def __init__(self):
super().__init__()
# 初始化界面
self.image_label = ImageLabel()
self.image_label.setAlignment(Qt.AlignCenter)
self.open_button = QPushButton("打开")
self.open_button.clicked.connect(self.open_image)
self.save_button = QPushButton("保存")
self.save_button.clicked.connect(self.save_sample)
self.class_button = QPushButton("设置类别")
self.class_button.clicked.connect(self.set_class)
self.class_label = QLabel("未设置类别")
self.class_label.setAlignment(Qt.AlignCenter)
self.class_label.setStyleSheet("border: 1px solid black;")
self.class_label.hide()
button_layout = QHBoxLayout()
button_layout.addWidget(self.open_button)
button_layout.addWidget(self.save_button)
button_layout.addWidget(self.class_button)
main_layout = QVBoxLayout()
main_layout.addWidget(self.image_label)
main_layout.addLayout(button_layout)
main_layout.addWidget(self.class_label)
central_widget = QWidget()
central_widget.setLayout(main_layout)
self.setCentralWidget(central_widget)
# 初始化数据
self.image_path = None
self.class_name = None
def open_image(self):
file_path, _ = QFileDialog.getOpenFileName(self, "打开图片", "", "Images (*.png *.jpg *.jpeg)")
if file_path:
self.image_path = file_path
pixmap = QPixmap(file_path)
self.image_label.set_pixmap(pixmap)
def save_sample(self):
if not self.image_path:
return
if not self.class_name:
return
file_path, _ = QFileDialog.getSaveFileName(self, "保存样本", "", "Images (*.png *.jpg *.jpeg)")
if file_path:
self.image_label.save_image(file_path)
label_path = os.path.splitext(file_path)[0] + ".txt"
with open(label_path, "w") as f:
f.write(self.class_name)
def set_class(self):
text, ok = QInputDialog.getText(self, "设置类别", "请输入类别名称:")
if ok and text:
self.class_name = text
self.class_label.setText(text)
self.class_label.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = SampleCollector()
window.show()
sys.exit(app.exec_())
```
运行代码后,会弹出一个界面,点击打开按钮选择图片,然后设置类别并在图片上用鼠标画出样本区域,最后点击保存按钮保存样本。
注意:这里仅提供了代码示例,实际应用中需要根据具体需求进行修改和完善。
阅读全文