test

 

import sys
import cv2
import numpy as np
import requests
from io import BytesIO
from PyQt5.QtWidgets import QApplication, QLabel, QVBoxLayout, QWidget
from PyQt5.QtCore import QThread, pyqtSignal
from PyQt5.QtGui import QImage, QPixmap


class CameraThread(QThread):
    change_pixmap_signal = pyqtSignal(QImage)

    def __init__(self, camera_ip, username, password):
        super().__init__()
        self.camera_ip = camera_ip
        self.username = username
        self.password = password
        self._is_running = True

    def run(self):
        while self._is_running:
            url = f"http://{self.camera_ip}/video"  # 根据你的摄像头型号,URL可能会有所不同
            auth = (self.username, self.password)

            response = requests.get(url, auth=auth, stream=True)
            if response.status_code == 200:
                bytes_data = BytesIO(response.content)
                img_array = np.load(bytes_data)
                rgb_image = cv2.cvtColor(img_array, cv2.COLOR_BGR2RGB)
                h, w, ch = rgb_image.shape
                bytes_per_line = ch * w
                convert_to_Qt_format = QImage(rgb_image.data, w, h, bytes_per_line, QImage.Format_RGB888)
                self.change_pixmap_signal.emit(convert_to_Qt_format)
            else:
                print("Failed to retrieve image from camera.")

    def stop(self):
        self._is_running = False
        self.quit()


class App(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Camera Viewer")
        self.setGeometry(100, 100, 800, 600)

        self.label = QLabel(self)
        self.label.resize(800, 600)

        self.camera_thread = CameraThread(
            camera_ip="192.168.1.100",  # 替换为你的摄像头IP地址
            username="your_username",   # 替换为你的摄像头用户名
            password="your_password"    # 替换为你的摄像头密码
        )
        self.camera_thread.change_pixmap_signal.connect(self.update_image)
        self.camera_thread.start()

    def closeEvent(self, event):
        self.camera_thread.stop()
        event.accept()

    def update_image(self, image):
        self.label.setPixmap(QPixmap.fromImage(image))


if __name__ == "__main__":
    app = QApplication(sys.argv)
    a = App()
    a.show()
    sys.exit(app.exec_())

 

 

############

posted @ 2025-06-16 17:55  西北逍遥  阅读(12)  评论(0)    收藏  举报