I have a flask server in python that receives a string of bytes in base64 (they are video frames) constant, these are saved in a variable, what I want is to be able to access that stream with opencv videocapture() to be able to use them in a real-time analysis and show in the html the question would be how to analyze this flow of video frames in real time.
# Importing the relevant libraries
import asyncio
from flask import Flask
from flask import render_template
from flask import Response
import cv2
from flask import Flask, render_template
from flask_sock import Sock
import time
import numpy as np
import base64
app = Flask(__name__)
sock = Sock(app)
@sock.route('/')
def socket():
while True:
data = ws.receive() #---->data in base64 frames
img=base64.b64decode((data))
if img is not None:
img = cv2.imdecode(np.frombuffer(img, dtype=np.byte), flags=cv2.IMREAD_COLOR)
#img is a numpy.ndarray
#I would like to be able to use this byte stream here below.**
cap = cv2.VideoCapture(img) #------> put here the stream frame
face_detector = cv2.CascadeClassifier(cv2.data.haarcascades +
"haarcascade_frontalface_default.xml")
def generate():
while True:
ret, frame = cap.read()
if ret:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_detector.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
(flag, encodedImage) = cv2.imencode(".jpg", frame)
if not flag:
continue
yield(b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' +
bytearray(encodedImage) + b'\r\n')
@app.route("/")
def index():
return render_template("index.html")
@app.route("/video_feed")
def video_feed():
return Response(generate(),
mimetype = "multipart/x-mixed-replace; boundary=frame")
if __name__ == "__main__":
app.run(debug=False)
cap.release()
thank you!