Estoy haciendo un proyecto de aprendizaje automático. Que funciona en tiempo real. Necesito enviar el video desde la cámara web a una API específica (frasco) y mostrárselo al usuario nuevamente (después de la predicción).
Cuando se usa el siguiente método POST, es realmente muy lento en localhost.
CÓDIGO 1
var canvas = document.createElement("canvas"); canvas.width = 320; canvas.height = 240; var context = canvas.getContext('2d'); var image = document.getElementById('image'); if(navigator.mediaDevices && navigator.mediaDevices.getUserMedia) { navigator.mediaDevices.getUserMedia({ video: true }).then(function(stream) { video.srcObject = stream; window.setInterval(function() { context.drawImage(video, 0, 0, 320, 240); canvas.toBlob(upload, "image/jpeg"); }, 100); }); } function upload(file) { var formdata = new FormData(); formdata.append("snap", file); var xhr = new XMLHttpRequest(); var url = "http://localhost:5000/vid" xhr.open("POST", url , true); xhr.responseType = 'blob'; xhr.onload = function() { if(this.status = 200) { } else { console.error(xhr); } image.src = URL.createObjectURL(this.response); }; xhr.send(formdata); }Pero cuando se usa el límite directo = cv2.VideoCapture(0) sin la publicación de la API, funciona correctamente.
¿Alguna solución?
EDITAR (parte de fondo del matraz)
def send_file_data(data, mimetype='image/jpeg', filename='output.jpg'): response = make_response(data) response.headers.set('Content-Type', mimetype) response.headers.set('Content-Disposition', 'attachment', filename=filename) return response #predicting result def generate_frames(frame): img_face = cv2.resize(frame,(256,256)) ** prediction process ** ret,buffer=cv2.imencode('.jpg',img_face) frame=buffer.tobytes() return frame #calling this from frontend @app.route('/upload', methods=['GET', 'POST']) def upload(): if request.method == 'POST': fs = request.files.get('snap') if fs: img = cv2.imdecode(np.frombuffer(fs.read(), np.uint8), cv2.IMREAD_UNCHANGED) img = np.array(img) return send_file_data(generate_frames(img)) else: return 'ERROR' return 'WELCOME'