Nanda Kumar P
Nanda Kumar P

Reputation: 135

How to Use the Android 'IP Webcam' App with Python

working on simple GUI project. I've got some code from online,and found out how to connect the IP-webcam app, but the question is how do I use this code in my PyQt4 GUI so that the visual of the camera will be shown in the scroll-area widget.

This is the code i used:

import urllib
import cv2
import numpy as np

url='http://192.168.0.100:8080/shot.jpg'

while True:
    imgResp=urllib.urlopen(url)
    imgNp=np.array(bytearray(imgResp.read()),dtype=np.uint8)
    img=cv2.imdecode(imgNp,-1)

    # all the opencv processing is done here
    cv2.imshow('test',img)
    if ord('q')==cv2.waitKey(10):
        exit(0)

Upvotes: 1

Views: 682

Answers (1)

eyllanesc
eyllanesc

Reputation: 244202

As @furas points out, a possible option is to use numpy and cv2 to convert it to QPixmap and display it in a QLabel, and so that it looks like streaming run it in a loop.

But instead of getting complicated with all of the above, the simplest thing is to use QtNetwork to get the bytes and convert it directly to QPixmap and send it through signals:

from PyQt4 import QtCore, QtGui, QtNetwork


class IPWebcam(QtCore.QObject):
    pixmapChanged = QtCore.pyqtSignal(QtGui.QPixmap)

    def __init__(self, url, parent=None):
        super(IPWebcam, self).__init__(parent)
        self._url = url
        self.m_manager = QtNetwork.QNetworkAccessManager(self)
        self.m_manager.finished.connect(self._on_finished)

        self.m_stopped = True

    def start(self):
        self.m_stopped = False
        self._launch_request()

    def stop(self):
        self.m_stopped = True

    def _launch_request(self):
        request = QtNetwork.QNetworkRequest(QtCore.QUrl(self._url))
        self.m_manager.get(request)

    @QtCore.pyqtSlot(QtNetwork.QNetworkReply)
    def _on_finished(self, reply):
        ba = reply.readAll()
        pixmap = QtGui.QPixmap()
        if pixmap.loadFromData(ba):
            self.pixmapChanged.emit(pixmap)
        if not self.m_stopped:
            self._launch_request()


class Widget(QtGui.QWidget):
    def __init__(self, parent=None):
        super(Widget, self).__init__(parent)

        self.m_label = QtGui.QLabel()
        self.m_button = QtGui.QPushButton(
            "Start", clicked=self.onClicked, checkable=True
        )

        lay = QtGui.QVBoxLayout(self)
        lay.addWidget(self.m_label)
        lay.addWidget(self.m_button)

        self.resize(640, 480)

        url = "http://192.168.0.100:8080/shot.jpg"

        self.m_webcam = IPWebcam(url, self)
        self.m_webcam.pixmapChanged.connect(self.m_label.setPixmap)

    @QtCore.pyqtSlot(bool)
    def onClicked(self, checked):
        if checked:
            self.m_button.setText("Stop")
            self.m_webcam.start()
        else:
            self.m_button.setText("Start")
            self.m_webcam.stop()


if __name__ == "__main__":
    import sys

    app = QtGui.QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())

Upvotes: 1

Related Questions