实现pyqt5在表单中显示labelimg标记的信息的方法并给出实现代码
时间: 2023-12-27 21:05:23 浏览: 106
pyqt5 从本地选择图片 并显示在label上的实例
5星 · 资源好评率100%
要在PyQt5中显示LabelImg标记的信息,可以使用QPixmap和QPainter将图像加载到QWidget中,并在上面绘制标记框和标记文本。以下是一个简单的实现:
```python
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel
from PyQt5.QtGui import QPixmap, QPainter, QPen, QFont
from PyQt5.QtCore import Qt
class ImageLabel(QWidget):
def __init__(self, image_path, annotations):
super().__init__()
self.image_path = image_path
self.annotations = annotations
self.initUI()
def initUI(self):
self.setGeometry(100, 100, 800, 600)
self.setWindowTitle('LabelImg Annotations')
# Load image
pixmap = QPixmap(self.image_path)
# Create label widget and set pixmap
self.label = QLabel(self)
self.label.setPixmap(pixmap)
self.label.setGeometry(0, 0, pixmap.width(), pixmap.height())
# Draw annotations on label widget
painter = QPainter(self.label.pixmap())
pen = QPen(Qt.red)
font = QFont('Helvetica', 10)
painter.setFont(font)
painter.setPen(pen)
for annotation in self.annotations:
x, y, w, h = annotation['bbox']
painter.drawRect(x, y, w, h)
painter.drawText(x, y - 5, annotation['label'])
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
image_path = 'example.jpg'
annotations = [
{'label': 'person', 'bbox': (100, 100, 50, 100)},
{'label': 'car', 'bbox': (200, 200, 100, 50)}
]
label_widget = ImageLabel(image_path, annotations)
sys.exit(app.exec_())
```
在此示例中,我们定义了一个名为ImageLabel的QWidget子类,它接受图像路径和注释列表作为输入,并使用QPixmap将图像加载到QLabel中。然后,我们使用QPainter在QLabel上绘制标记框和标记文本。最后,我们使用QApplication运行应用程序并显示QWidget。
阅读全文