python+opencv+pyqt 获取rtsp流的两种方法

第一种:
利用opencv自带的
        def open():
            rtsp = "rtsp://" + url + "/stream_main"
            print(rtsp)
            cap = cv2.VideoCapture(rtsp)
            while True:
                ret, frame = cap.read()
                if not ret:
                    break
                cv2.imshow('video', frame)
                cv2.waitKey(1)
                if cv2.getWindowProperty('video', 4) < 1:
                    self.ui.pushButton_7.setEnabled(True)
                    self.ui.pushButton_4.setEnabled(True)
                    break
            cv2.destroyAllWindows()
            cap.release()
        thread = Thread(target=open)
        thread.start()

第二种:
		label_width = label_video_test.width()
        label_height = label_video_test.height()
        results = self.run_main('post', url="/camera/searchCameraConfig")
        #核心代码
        def open():
            cap = cv2.VideoCapture(rtsp)
            ret, frame = cap.read()
            while ret:
                ret, frame = cap.read()
                # 将图片转换为QImage
                temp_imgSrc = QImage(frame[:], frame.shape[1], frame.shape[0], frame.shape[1] * 3,
                                     QImage.Format_RGB888)
                # 将图片转换为QPixmap方便显示
                pixmap_imgSrc = QPixmap.fromImage(temp_imgSrc).scaled(label_width, label_height)
                try:
                    label_video_test.move(0, 0)
                    label_video_test.setPixmap(QPixmap(pixmap_imgSrc))
                except:
                    break
                if cv2.waitKey(1) & 0xFF == ord('q'):
                    break
            cv2.destroyAllWindows()
            cap.release()
        thread = Thread(target=open)
        thread.start()
   
 第二种存在色彩失真,优化
        class MyThread(QThread):
            # 定义信号,定义参数为str类型
            def __init__(self, parent=None):
                super().__init__(parent)
                self.stoped = False

            def run(self):
                	#获取Label标签的长宽
                    label_width = ui.enterLabel.width()
                    label_height = ui.enterLabel.height()
                    # cap = cv2.VideoCapture("rtsp:xxx/[email protected]/h264/ch1/sub/av_stream")
                    cap = cv2.VideoCapture(rtsp)
                    while cap.isOpened():
                        if self.stoped:
                            return
                        ret, frame = cap.read()
                        height, width, bytesPerComponent = frame.shape
                        bytesPerLine = bytesPerComponent * width
                        # 变换彩色空间顺序
                        cv2.cvtColor(frame, cv2.COLOR_BGR2RGB, frame)
                        image = QImage(frame.data, width, height, bytesPerLine, QImage.Format_RGB888)
                        pixmap_imgSrc = QPixmap.fromImage(image).scaled(label_width, label_height)
                        ui.enterLabel.setPixmap(QPixmap(pixmap_imgSrc))
       
	       self.res = MyThread()
           self.res.start()

你可能感兴趣的:(opencv,python,计算机视觉)