I am programing a QR authentication pdf printing system using flask.
So I get a problem with that.
In hear I compare the generate QR code with the user input Qr code.
If both matches do the print function vise versa.
After generating the QR code it shows on the webpage. Then the user can get a photo and it should show it to the raspberry pi camera. Then it decodes the QR code using OpenCV and sends it back to the flask server.
this is my code up to now
python file
#app.route('/', methods=['GET', 'POST'])
def index():
global qr_cv # get from raspberry pi
global qrdata # generate from flask
if flask.request.method == 'GET':
return flask.render_template("index.html")
if flask.request.method == 'POST':
if qrdata != qr_cv:
return flask.render_template("oops.html")
if (qrdata == qr_cv):
# Print Function
return flask.render_template("printed.html")
return flask.render_template("index.html")
HTML file
<form method=post enctype=multipart/form-data">
<input type=file name=file">
<input type=submit value=Print">
</form>
So my question is when I press the Print button flask should search for the certain time for the qr_cv data because the user had to get a photo of the QR code and go to the raspberry pi camera and show it to it. After showing it will send the data to the flask server.
This code suddenly renders the oops.html file because initially qr_cv send nothing.
So, I need any help from you and I highly appreciate your suggestions.
Thank you!
Related
I am running my Flask app on Ubuntu VPS (linode) as server. I am capturing image from android studio sending it to flask server to get response. Image is being sent as base64 string from android studio.
But the response is too slow when using request.form.to_dict(). Both ubuntu vps and android app are running on different IP.
I've put timestamps on different processes and found this request.form.to_dict() is the problem taking around 22 to 35 secs.
I am also using image compressing techniques on my android studio code but still too much time.
Tried on gunicorn with nginx but same.
threaded = True
Images are of around 400kb to 500kb size.
Kindly help me to figure out this issue.
Below is my flask code:
#app.route('/predict', methods=['POST', 'GET'])
def predict():
if request.method == 'POST':
file = request.form.to_dict()['upload'] # 22.2491 sec
b64_string = file
image = imread(io.BytesIO(base64.b64decode(b64_string))) #0.0023 sec
image = cv2.cvtColor(np.array(image), cv2.COLOR_BGR2RGB) # 0.000171 sec
.....
.....
.....
sending the images in base64 format is not the recommended method, you should try to send the photo via a multipart request.
Look here to see how to send an image from android in multipart form data (if you are using java).
If you send the images via multipart form data you will then be able to access them on flask using the method request.files['my_file_name']:
#app.route('/upload', methods=['POST'])
def upload_file():
my_file = request.files['my_file_name']:
I'm attempting to redirect to an external site (ex: google.com) via flask after a python script succeeds in the background. i'm not sure how to redirect and render a template at the same time .
Here's the flask code:
#app.route ("/result",methods=['POST','GET'])
def result():
if request.method == "POST":
ip1=request.form.get('ip')
output=subprocess.getstatusoutput(["ping -c 2 " +str(ip1)])
out=output[1]
ec=output[0]
return render_template("home.html",out=output[1])
sleep(3) // Time given so that the output of ping command above gets printed in html page
return redirect ("http://www.google.com/",code=307) // Sample External site
The above code works till rendering the page "home.html" and prints the output from "ping" command , but doesn't redirect upon successful execution . Is there a way where we can get the redirect based on the exit code of a command's success or show a failure message on error exit code
I believe that when you return the template at line (-3), your method finishes.
It seems to me that you want to send a redirect header to the client, but you already have sent some stuff. You cannot do it.
My idea: keep the return render_template part and perform the redirect later by javascript (check here)
I am developing a micro services architecture locally on my laptop, where a flask app communicates with a Google cloud function (using functions-framework library).
In it,the following happens:
The user uploads a file to a form. Upon submit, the file is sent to Flask
Flask will then extract the file from the request and then send the file to the cloud function, running on port 8080
When the file is sent though the cloud function is returning a 400 status code (bad request).
What am I doing wrong?
===form.html===
<html>
<body>
<h1>Report</h1>
<form action = "http://localhost:3000**/process**" method = "post" enctype = "multipart/form-data">
<input type = "file" name = "file" />
<input type = "hidden" name = "report" />
<p><input type = "submit" value = "submit" /></p>
</form>
</body>
</html>
===Flask===
...
#app.route('/process',methods = ['POST'])
def process():
f = request.files['file']
files = {'document': f.read()}
headers = {'Content-type': 'multipart/form-data'}
url='http://127.0.0.1:8080'
r = requests.post(url,files=files,headers=headers)
if r:
return redirect(url_for('success',tool=r.text))
#app.route('/form')
def form():
return render_template('form.html')
if __name__ == '__main__':
app.run(port=3000)
===functions-framework===
def func2(request):
file=request.files['document']
return str(type(file))
In the documentation for Functions Framework, there are more links to learn more about this, particularly on how to use the Functions Framework for Python runtime. Here you can find a quickstart about error handling.
In this answer from another question, you could check how to implement an error handler for a given code error and get the description of the error in order to debug your code and know why you're getting the 400 status code error.
I have a problem. I'm making a Flask application that requires camera input, which is sent to opencv, and then I process it and return an image. I have no problem with the streaing. My issue here is the fact that I need to access the user's webcam, and give the information to Flask so it can process the image, and THEN return it. To do this, I need access to the user's webcam. I imagine something like this:
main.py
from flask import Flask, request, render_template
from myapp import process #Import function to process image
app = Flask(__name__)
#app.route("/webcam", methods=["GET", "POST"])
def camera():
if request.method == "POST":
image = request.get_json()['image']
result = process(image) #Process image
return result
else:
return('camera.html') #Return camera site
#Other code/functions...
Then in the html file, I'd have a <script> tag with the script to send a post request with the image.
(If there is a better way to do this, tell me.)
I don't know how to get the image to the user, though, in a way that OpenCV can process it.
I am writing a home workout program that gives me audio cues for when to switch exercises, which exercises to do, etc... I have this working on my laptop using a small python program to play some short pre-recorded audio files (aiff format) at specified intervals.
However, I'd like to be able to run this program from my iPhone. I attempted to do this by setting up a Flask server on my computer. I can get the program to run via a web browser on the same machine hosting the server, but when I use a different computer or my iPhone, the audio still plays on the host computer's speakers, not the client as desired. The computer hosting the Flask server is running OS X 10.11.6. Here is a basic version of the program:
Flask Python:
app = Flask(__name__)
#app.route('/')
def index():
return render_template('rockrings_stackoverflow.html')
#app.route('/click', methods=['POST'])
def start_workout():
return workout.workout()
if __name__ == '__main__':
app.run(host='0.0.0.0',debug=True)
workout.py:
def workout():
for minute in range(10):
audio_file = homedir + '/audio_files/easy_workout_minute%i.aiff' % minute
print audio_file
os.system('afplay %s' % audio_file)
index.html:
<!DOCTYPE html>
<html>
<body>
<h1>Welcome to the Hangboard Webtool! </h1>
Click the button below to start the easy workout
<br/><br/>
<form action="/click" method="post">
<button type="submit" value="Easy workout">
</form>
</body>
</html>
What is a proper implementation (if there is one) with Flask?