Django web displaying stream from webcam using jetson tx2 as a server - python

I am writing web app, where I would like to:
Display LOCAL stream from webcam - it means, that I want to stream video from server (I do not want to open webcam of client)
Read QR Codes and list them in text box
These two were already achieved but! I came across some unexpected behaviour. Functionality that I have described is working perfectly, but only on localhost. I want to deploy it, so it could be accessible via different computer (it is meant to be used on a robot).
So to describe my architecture: I am using Jetson TX2 as a server (webcam is connected here). I am using Django web framework, django-channels, daphne as a web server and ngingx as a proxy. I am running daphne and background process in supervisor.
I am using worker (background process) to capture frames from webcam and send it via redis to the web backend.
So when I run it on localhost everything work as expected. When I set Debug to FALSE and I add Jetson's IP to ALLOWED_HOSTS and try to access the web from different computer this happens:
I can see, that webcam is accessed because the webcam light turns on. I put some QR code in front of the webcam and the code appears in the textbox on web! BUT the video is not there (when ALLOWED_HOSTS contains localhost video IS there). Output of background process which collects the camera frames gives following error:
libv4l2: error setting pixformat: Device or resource busy
OpenCV Error: Unspecified error (GStreamer: unable to start pipeline)
in cvCaptureFromCAM_GStreamer, file /home/nvidia/prototype/opencv/opencv-3.4.0/modules/videoio/$
VIDEOIO(cvCreateCapture_GStreamer (CV_CAP_GSTREAMER_FILE, filename)):raised OpenCV exception:
/home/nvidia/toyota_prototype/opencv/opencv-3.4.0/modules/videoio /src/cap_gstreamer.cpp:890: error: (-2)$
in function cvCaptureFromCAM_GStreamer
I will not post whole code here, since I do not know where exactly is the problem. Does anyone have an idea where the problem could be?
Thank you for your help!

So, I figured it out. In my html template I had one line, where I was linking to the stream address:
<img src="http://127.0.0.1:8000/webcam-stream">
I think, now you all know, where the problem was. I needed to change the IP to HOST address.

Related

raspberry pi using a webcam to output to a website to view

I am currently working on a project in which I am using a webcam attached to a raspberry pi to then show what the camera is seeing through a website using a client and web server based method through python, However, I need to know how to link the raspberry pi to a website to then output what it sees through the camera while then also outputting it through the python script, but then i don't know where to start
If anyone could help me I would really appreciate it.
Many thanks.
So one way to do this with python would be to capture the camera image using opencv in a loop and display it to a website hosted on the Pi using a python frontend like flask (or some other frontend). However as others have pointed out, the latency on this would be so bad any processing you wish to do would be nearly impossible.
If you wish to do this without python, take a look at mjpg-streamer, that can pull a video feed from an attached camera and display it on a localhost website. The quality is fairly good on localhost. You can then forward this to the web (if needed) using port forwarding or an application like nginx.
If you want to split the recorded stream into 2 (to forward one to python and to broadcast another to a website), ffmpeg is your best bet, but the FPS and quality would likely be terrible.

Why can't I access my ESP3288 USB after detaching REPL from UART0?

What I put into my otherwise mostly empty main function was basically:
import uos
uos.dupterm(None, 1)
I uploaded this code to my microcontroller and it stopped being able to connect to my computer. My aim was trying to connect to a Bluetooth module (ZS-040). Now I can't even connect to the microcontroller.
I was intending to make a serial connection to the Bluetooth module while keeping the UART0 bus separate for USB and REPL connection. Now, I am stranded, having done quite the opposite. How do I fix this?
If you have enabled the webrepl, try using it to connect & upload a new, blank 'main.py'.
https://docs.micropython.org/en/latest/esp8266/tutorial/repl.html?highlight=webrepl
(this works on my ESP8266 after running dupterm(None, 1)). Failing that, re-flash the firmware to erase 'main.py' and re write the script to either issue dupterm manually at the start or to issue 'dupterm(UART(0, 115200), 1)' after some later event to re-attach the repl.

Controlling a robot from the web

I am trying to get an RPi3 robot to accept commands from both the Web and from a touchscreen/keyboard interface. I have a script that handles the keyboard and I am looking for that current script I have in Python to be expanded to accept real-time input from the web. As well as trying to figure out how to send the data to the script.
More detail is below but that is the basic question.
I created the robot using a raspberry pi and 3 arduinos controlling DC motors and servos to make the bot move. The program is written in Python and is run from the command line. when run it:
queries the active serial ports on the Raspberry
opens each available port
sends an integer to the receiving arduino
the arduino responds with an identifying integer so the RPI can name the port
then the script waits for user input like "Forward"
the command is translated and sent to the correct port
the robot moves
This all works perfect. Now I want to expand it. I want to keep this functionality intact and add in a web interface so I can control it from anywhere. I've tried a couple of different things with no success. I have installed apache and I am able to serve the pages with no problem, I can get the data on the page, but I can't figure out how to get the web page to send the data to the running arduino script. My issue stems from the fact that the bot control script needs to run independent of the web page. I want to still keep the same input now from the keyboard, but I also want it to accept the data from the web page. If I invoke the bot controller from the web page each time it will need to re-establish the port connections each time which takes up to 20 seconds...
I am thinking if I create a listening script I can have the website invoke the listener which will run only to receive the data from the web and pass it to the bot controller and back. But I am not sure how to do this or if this is even the best way.
I've looked at websockets, CGI/wsgi, Flask, writing a file, and JSON and I just can't seem to string it all together.
Thanks in advance and apologies if this is not in the right place or has been answered before. Also, I have not included any code as the only solid code is the bot controller. I am hoping someone with some real expertise can help me unravel this.
thanks in advance
KenV
I would say Flask is your best option. A simple example of how you could use it:
from my_robot_stuff import move_forward
#app.route('/move_forward')
def move_forward_flask():
move_forward()
return redirect('/')
Your html would then have a button that says move forward that directs to mysite.com/move_forward. flask would process it, run the code, then redirect back to the root.

Flask server not visible local network

I'm trying to get the server that comes with Flask to run inside my network but I can't get it to work properly, it simply doesn't load.
For test purposes I'm running a simple hello world app using app.run(host='0.0.0.0') and it loads fine from the host but I get a timeout when connecting from another machine.
I tried forcing other ports using app.run(host='0.0.0.0', port='80') but I had no success. Using the IP explicitly like app.run(host='10.X.X.X') didn't help either.
However I don't think it is a network problem since I can successfully do a ping 10.X.X.X from the second machine and get the response.
The question: Can it maybe have anything to do with the IP being in the format "10.X.X.X"? What else can be causing this behavior?

Trigger an cognex camera by script

I have got a Cognex Advantage 100 camera connected to my PC via ethernet.
After pressing F5 in the inSight Explorer to trigger the camera I can use the captured image in a Python script.
Can I make the Python script trigger the image capture itself?
I'm not very familiar with the Advantage series, but I am quite familiar with the other In-Sight cameras. I'm going to assume the Advantage is similar to other In-Sight cameras.
You should be able to achieve a trigger from python by opening a telnet connection to the camera (on port 23), logging in (default username: admin, password: ), and sending the command 'SE8'. The camera trigger mode must be set to External, Manual or Network. If the command is successful, it will respond with a '1'. I'd suggest trying this with a telnet client before trying it in python. Suggested telnet clients: Putty or Hercules.
More information can be found in the In-Sight Explorer help file. From the contents, go to 'Communications Reference -> Native Mode Communications'.
Possibly you could simulate a key press. This answer here and this answer here, might help you do that.
Apart from that, your camera software doesn't allow you to interact with it via python, but it does supply it own method of programming the camera here. Try that instead, it seems to be the indented way of doing this.

Categories

Resources