当前位置:   article > 正文

PYQT6做UI,用YOLOV8检测RTSP视频流_pyqt6 yolov8

pyqt6 yolov8
  1. import sys
  2. import cv2
  3. import imutils
  4. from PyQt6.QtCore import QThread, pyqtSignal
  5. from PyQt6.QtGui import QImage, QPixmap
  6. from PyQt6.QtWidgets import QApplication, QLabel, QMainWindow, QPushButton, QVBoxLayout, QWidget
  7. from ultralytics import YOLO
  8. model = YOLO("D:/YOLOHIDOZ/ultralytics/yolov8n.pt") # 填入自己的模型地址
  9. class VideoThread(QThread):
  10. model_detection_signal = pyqtSignal(QImage)
  11. def __init__(self, rtsp_url):
  12. super().__init__()
  13. self.rtsp_url = rtsp_url
  14. self.running = False
  15. def run(self):
  16. self.running = True
  17. cap = cv2.VideoCapture(self.rtsp_url)
  18. while self.running:
  19. # 从视频流中读取一帧
  20. ret, frame = cap.read()
  21. # 如果读取失败(例如,视频结束或连接断开),则跳出循环
  22. if not ret:
  23. break
  24. # 使用imutils的resize函数调整帧的大小到640像素宽
  25. frame = imutils.resize(frame, width=640)
  26. # 使用模型预测处理帧,并将结果存储在results中
  27. results = model.predict(source=frame)
  28. # 从results中获取第一个元素并绘制它
  29. annotated_frame = results[0].plot()
  30. # 获取帧的高度、宽度和通道数
  31. height, width, channel = annotated_frame.shape
  32. # 计算每行字节数,因为QImage使用每行像素数*每像素字节数的方式来表示图像数据
  33. bytes_per_line = 3 * width
  34. # 创建QImage对象,从annotated_frame中获取数据,并设置宽度、高度、每行字节数和图像格式
  35. # rgbSwapped()方法是因为OpenCV默认使用BGR格式,而QImage使用RGB格式
  36. qimage = QImage(annotated_frame.data, width, height, bytes_per_line,
  37. QImage.Format.Format_RGB888).rgbSwapped()
  38. # 发出信号,将处理后的QImage对象发送出去,用于在GUI中显示
  39. self.model_detection_signal.emit(qimage)
  40. cap.release()
  41. def stop(self):
  42. self.running = False
  43. self.wait()
  44. class MainWindow(QMainWindow):
  45. def __init__(self):
  46. super().__init__()
  47. self.setWindowTitle("测试")
  48. self.setGeometry(100, 100, 800, 600)
  49. layout = QVBoxLayout()
  50. self.label = QLabel("视频监控")
  51. self.button = QPushButton("打开")
  52. layout.addWidget(self.label)
  53. layout.addWidget(self.button)
  54. container = QWidget()
  55. container.setLayout(layout)
  56. self.setCentralWidget(container)
  57. self.thread = VideoThread(0) # 你的RTSP视频流
  58. # 让信号绑定函数,用来在label上更新图片帧
  59. self.thread.model_detection_signal.connect(self.update_image)
  60. self.button.clicked.connect(self.start_streams)
  61. def start_streams(self):
  62. """
  63. # 定义方法改变按钮的字样,同时打开/关闭线程
  64. @return:
  65. """
  66. if self.button.text() == "打开":
  67. self.button.setText("关闭")
  68. # 开启线程
  69. self.thread.start()
  70. else:
  71. self.button.setText("打开")
  72. # 关闭线程
  73. self.thread.stop()
  74. def update_image(self, qimage):
  75. """
  76. # 用来更新图片
  77. @param qimage: 获取信号的图片帧
  78. @return: None
  79. """
  80. pixmap = QPixmap.fromImage(qimage)
  81. self.label.setPixmap(pixmap)
  82. if __name__ == "__main__":
  83. app = QApplication(sys.argv)
  84. window = MainWindow()
  85. window.show()
  86. sys.exit(app.exec())

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/611090
推荐阅读
相关标签
  

闽ICP备14008679号