0

I've a desktop application to detect faces written in python script, using opencv and numpy. i want to put these python files into flask and run it, would it run without problems? like

import cv2
import numpy as np
from flask import Flask
app = Flask(__name__)

## define my functions here

@app.route('/')
def hello_world():
    return 'Hello World!'

if __name__ == '__main__':
    #call the functions here
    app.run()

would that work? if not how do i get it included? thanks!

1
  • I don't think you understand what Blueprints do. They simply organize the routes of the Flask app. They are not really useful for external functions. You can use a standard Python module for that. Commented Apr 28, 2016 at 16:28

1 Answer 1

1

Yes it would work, one thing you should know is if you do like below, the HTTP request won't return until after the processing is done e.g.

@app.route('/webcam')
def webcam_capture():
    """
    Returns a picture snapshot from the webcam
    """
    image = cv2... # call a function to get an image

    response = make_response(image) # make an HTTP response with the image
    response.headers['Content-Type'] = 'image/jpeg'
    response.headers['Content-Disposition'] = 'attachment; filename=img.jpg'

    return response

Otherwise, if you put in the main function like below

if __name__ == '__main__':
    # <-- No need to put things here, unless you want them to run before 
    # the app is ran (e.g. you need to initialize something)

    app.run()

Then your flask app won't start until the init/processing is done.

Sign up to request clarification or add additional context in comments.

5 Comments

i access webcam and take a picture then process into it and return the picture? would be like take picture then return something? or do app.run() then return the result?
i know blueprints but it's used with html and css files, would it work with python ones?
app.run() starts your flask app, so I wouldn't put any code before it unless it is to initialize the app. To show pictures from the webcam you may e.g. use my first example using route('/webcam') and have that function return a picture captured from the webcam
i'm going try that , or would u suggest using blueprints? i have seen that work with html and javascript.. would it work with python files?
I'd start with a function that returns an image from the webcam, and then I'd work on a flask snippet to return a image to the browser, then tie those two together. I've modified my example to suggest a skeleton code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.