blog:2022:0904_testing_tapo_cameras_in_python

NervHome: Access to tapo cameras from python

Lately I have installed a few cameras at home for video monitoring purpose: the idea is to get ready to leave our home for some vacations from time to time and still ensure that everything at home is safe.

The cameras I'm using so far are from “Tapo” and are working okay so far (except for a regular disconnection issue I still have to investigate). But I don't want to pay for the advanced “AI processing” offered by the company, and instead I would like to check here if there is a way to use those cameras directly for processing in custom programs I could write myself 👍!

  • So it seems I should be able to display a video stream directly from the cameras in VLC for instance, let's try that.
  • ⇒ First I fixed the IP of my C310 camera to be 192.168.0.42: OK
  • Next I have to create an account on the camera itself: OK

/

  • Then I tried to connect to the stream at rtsp://192.168.0.42/stream1 (also entering the account credentials from above) and this worked just fine:

  • ⇒ So it seems reading RTSP should be supported in OpenCV with something like this:
    import cv2
    cap = cv2.VideoCapture("rtsp://root:pass@192.168.0.91:554/axis-media/media.amp")
    
    while(cap.isOpened()):
        ret, frame = cap.read()
        cv2.imshow('frame', frame)
        if cv2.waitKey(20) & 0xFF == ord('q'):
            break
    cap.release()
    cv2.destroyAllWindows()
  • ⇒ Now Building a minimal test app to check this.
  • Preparing a python env for the homeguard app as follow:
        "homeguard_env": {
          "inherit": "default_env",
          "packages": [
            "PyQt5",
            "numpy",
            "opencv-python"
          ]
        },
  • Added support to inherit the packages when setting up a python env:
        def get_all_packages(self, desc):
            """Retrieve all the python packages requested for a given environment desc"""
            pkgs = []
    
            if "inherit" in desc:
                parent_name = desc["inherit"]
                pdesc = self.get_py_env_desc(parent_name)
                pkgs = self.get_all_packages(pdesc)
    
            # Add the packages from this desc:
            added = desc["packages"]
            for pkg in added:
                if pkg not in pkgs:
                    pkgs.append(pkg)
    
            # Return all the packages:
            return pkgs
  • Here is the initial minimal test I wrote the capture a real image from my cameras:
    def run_test():
        cap = cv2.VideoCapture("rtsp://nervctrl:xxxxxx@192.168.0.42/stream1")
    
        if not cap.isOpened():
            logger.error("Cannot open stream.")
            return
    
        count = 0
    
        while(cap.isOpened()):
            count += 1
            if count < 3:
                _, frame = cap.read()
                cv2.imshow(f'frame{count}', frame)
                time.sleep(2.0)
            if cv2.waitKey(20) & 0xFF == ord('q'):
                break
    
        cap.release()
        cv2.destroyAllWindows()
  • ⇒ And this seems to work as expected! So from that I could build a full app with access to the 4 video streams at the same time and movement detection (in theory ?) 🙃. So this should be our next target on this project. (But for now, I don't really have the time for this: too many things going on in parallel, so I'll come back to this later)
  • blog/2022/0904_testing_tapo_cameras_in_python.txt
  • Last modified: 2022/09/04 12:52
  • by 127.0.0.1