ddiLab / SageEdu

3 stars 2 forks source link

Learn how to interact with the camera using python #8

Open andrewg23 opened 3 years ago

andrewg23 commented 3 years ago

We want to set up the camera with the nano and be able to stream video from it using Python in a Jupyter notebook.

This is a sub-issue to #5

andrewg23 commented 3 years ago

I am able to stream a pre-recorded video from within a Jupyter notebook. SeeSageEdu/camera/videoStream.ipynb. I referenced the NCCV repository in order to do this.

The next step is to figure out how to get a video stream directly from the camera while it is plugged into the Nano's ethernet port. I think this just requires us to find the correct path or url for streaming the video. This path/url would then have to be passed into the cv2.VideoCapture() function.

andrewg23 commented 3 years ago

After reading the documentation from Amcrest, I found that I needed to do an initial set up on the camera and make sure the camera's IP address was static. In order to do this initial set up of the camera I had to connect it to my router and access it's web interface. I followed this guide to do so. Using the web UI I was able to confirm that the camera is working properly.

I then followed the first part of the port forwarding guide to set up the static IP and make note of all the TCP, UDP, HTTP, and RSTP port numbers.

With these numbers noted, I connected the camera to the Nano, and tried using this template to create a link to pass to the cv2.VideoCapture() function but this did not work.

My goal now is to make sure the Nano is detecting the camera. Then if it is I will try to interact with it using Linux commands then move on to using python.

andrewg23 commented 3 years ago

I was able to detect the camera on the Nano's ethernet port two different ways. First I used nmap and got this output:

Nmap scan report for AMC060325F755E (192.168.1.106) Host is up (0.056s latency). All 1000 scanned ports on AMC060325F755E (192.168.1.106) are filtered Nmap done: 1 IP address (1 host up) scanned in 5.96 seconds

I believe the AMC followed by all the numbers and letters is the name of the camera.

I wasn't sure if this was true so I decided to try another method of detecting the camera. For the second method I used tshark to see if there were any packets being set from the camera. When I ran tshark I found many packets were being sent from an Amcrest device. So this tells me that the camera is working and can be detected by the Nano.

The next thing to do is figure out how to actually access the data stream from the camera.

andrewg23 commented 3 years ago

After reading some articles online, I manually set the IP address of the Nano to see if maybe that would help as some sources said this needed to be done in order for the camera to communicate with the computer. This didn't see, to do anything.

After looking at the packets being sent by the camera using tshark again, I noticed that the camera was looking for my router, which is not what I want it to be doing.

I then decided to perform a reset on the camera by unscrewing two screws holding on a plate on the bottom of the camera. After removing this plate there's a microSD card slot and a small button labeled rest. While running tshark I pressed and held the button until the contents of the camera's packets changed. After dong this the camera is now looking for another device, I'm assuming using a default IP address. I also noticed that after dong this, the IP address of the camera changed. Resetting it made it so the camera now has a dynamic IP address again. I'm not sure if this will cause problems in the future.

I will try going about using the stream URL for the camera again and see how that goes.

andrewg23 commented 3 years ago

Finally got it working! After resetting the camera, I used tshark to get the IP address of the camera. I then used then the template I found earlier to create the stream URL: rtsp://[username]:[password]@[IPaddress]:554/cam/realmonitor?channel=1&subtype=0 With the default username as admin and the default password as admin as well.

I ran the code, and now it all works! It streams at a very low frame rate, and the stream is hard to watch inside a Jupyter notebook. These are things we would need to figure out how to smooth out in the future. This code along with some basic documentation can be found on the new cameraDoc branch.

I think this issue can be closed if at least two other people can get their cameras working and stream video from them using the code I have provided.

andrewg23 commented 3 years ago

Since the video stream is stored as an array before it is converted into an image, I was able to manipulate the array in order to change the image that is displayed. For example, multiplying the array by -1 displays the negative of the original image, or flipping the array, flips the image. Being able to manipulate the image using numpy functions opens many possibilities in terms of what can be done with the video stream.

andrewg23 commented 3 years ago

The jupyter notebook I had created to display the camera's live feed is no longer working. The code has not changed, so it must have something to do with either the camera or the Nano. I no longer get the same output if I run Nmap on the camera's IP Address which makes me think is is something to do with the camera. I have tried resetting it like I did before I got it working last time. I have also tried using different streaming URLs, but nothing is working. The next thing I will try will be to set it up with my router again an try doing exactly what I did last time. If this doesn't work I will try working with it on a fresh install of the Nano's OS.

andrewg23 commented 3 years ago

I got the camera to work again by setting up IPv4 sharing and using tshark to find the camera's IP address, then plugging that into the jupyter notebook I had created earlier.

andrewg23 commented 3 years ago

I used the Nano headful when I set up the sharing previously, but I now know how to set it up from the command line. Using nmcli, you can simply set the ipv4 method for the ethernet connection to be shared. I will update the documentation to include this method of setting it up.

andrewg23 commented 3 years ago

I have figures out how to fix the low frame rate issue. The low frame rate seems to be caused by the high resolution of the camera. In order to be able to display the stream at a higher frame rate, the resolution of the stream must be lowered. This can be done by logging into the camera and changing its settings via a web browser. An alternative method is to access the camera's sub stream which is by default at a lower resolution. Accessing the sub stream is simply a matter of changing subtype=0 to subtype=1 at the end of the rstp stream URL.