0

I have

  • takePhoto.py : a Python script that takes a picture from the webcam
  • app.py a Flask app that handles POST request

How can I send the image taken from takePhoto.py to the Flask app with a POST request?

takePhoto.py

import cv2 

cap = cv2.VideoCapture(0)
r, f = cap.read()

if r == True: 
    cv2.imwrite("cheese.jpg", f)
    # ---> Here I have the image and I want to send it to the Flask app

cap.release()

app.py

import os
from flask import Flask, render_template, request

app = Flask(__name__)
IMAGE_FOLDER = os.path.join('static', 'photos')
app.config['UPLOAD_FOLDER'] = IMAGE_FOLDER


@app.route('/')
def show_index():
    return render_template("index.html")

@app.route('/uploadPicture', methods=['POST'])
def uploadPicture():
    print("uploadPicture function triggered")
    file = request.files['image']
    complete_file_path = os.path.join(app.config['UPLOAD_FOLDER'], file.filename)
    file.save(complete_file_path)
    return render_template("gallery.html", current_image = complete_file_path )

if __name__ == ("__main__"):
    app.run()
Employee
  • 3,109
  • 5
  • 31
  • 50

1 Answers1

0

As you describe, you are running two processing. they do different job but you want it work together. so you have to use IPC or something like queue or redis to communicate each other. In here, you can use PUB/SUB pattern, but flask doesn't support it by default. Here are something dos you can check.

Migurel's Blog

SO

Frank AK
  • 1,705
  • 15
  • 28