K
K
Konstantin Malyarov2019-04-08 11:59:52
Python
Konstantin Malyarov, 2019-04-08 11:59:52

How to restart an image in Qt5 from a separate thread?

class ImageStream(QObject):
        image = pyqtSignal(bytes)
        
        def __init__(self):
            super().__init__()

        def run(self):
            response = requests.get('http://192.168.122.1:8080/liveview/liveviewstream', stream=True)
            for line in response.iter_content(chunk_size=1024*1024):
                if line.find(b'\xff\xd8') != -1 and line.find(b'\xff\xd9') != -1:
                    b_jpeg_s = line.find(b'\xff\xd8')
                    b_jpeg_e = line.find(b'\xff\xd9')
                    self.image.emit(line[b_jpeg_s:b_jpeg_e + 2])

        def flush(self):
            pass


class ExampleApp(QtWidgets.QMainWindow, Qt5_action_cam_main.Ui_MainWindow):

    def __init__(self):
        # Это здесь нужно для доступа к переменным, методам
        # и т.д. в файле design.py
        super().__init__()
        self.setupUi(self)  # Это нужно для инициализации нашего дизайна
        self.start_stream.clicked.connect(self.start)
        self.stop_stream.clicked.connect(self.stop)

    def start(self):
        self.start_stream.setEnabled(False)
        print('Stream Mode Strat')
        response = requests.post('http://192.168.122.1:8080/sony/camera', json={"method": "startLiveview","params": [],"id": 1,"version": "1.0" })
        print(response.content)
        self.pixmap = QPixmap()
        self.thread = QThread()
        self.image_stream = ImageStream()
        self.image_stream.moveToThread(self.thread)
        self.image_stream.image.connect(self.streamThread)
        self.thread.started.connect(self.image_stream.run)
        self.thread.start()

    def streamThread(self, image):
        self.pixmap.loadFromData(image)
        self.label_image.setPixmap(self.pixmap)

    def stop(self):
        response = requests.post('http://192.168.122.1:8080/sony/camera', json={"method": "getEvent","params": [False],"id": 1,"version": "1.0" })
        all_info_cam = json.loads((response.content).decode('utf8').replace("'", '"'))
        if all_info_cam["result"][3]["liveviewStatus"] is True:
            response = requests.post('http://192.168.122.1:8080/sony/camera', json={"method": "stopLiveview","params": [],"id": 1,"version": "1.0" })
            print(response.content)
            print("Stream Paused")
        else:
            response = requests.post('http://192.168.122.1:8080/sony/camera', json={"method": "startLiveview","params": [],"id": 1,"version": "1.0" })
            print(response.content)
            print("Stream Play")

When the stream is stopped and restarted, the image does not update after restarting. How can I make the stream start accepting images again?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question