将视频流从网站捕获到Flutter App

gpfsuwkq  于 2022-11-30  发布在  Flutter
关注(0)|答案(1)|浏览(227)

我试图建立一个应用程序,运行视频流后,执行一些图像处理在python.which后,处理生活在一个网站上。

from flask import Flask,render_template,Response
        import string
        from datetime import datetime
        from datetime import date
        import cv2 
        import os
        import ctypes  # An included library with Python install.   

        cascPath=os.path.dirname(cv2.__file__)+"/data/haarcascade_frontalface_default.xml"
        faceCascade = cv2.CascadeClassifier(cascPath)
        app=Flask(__name__)

        def generate_frames():
        posx=0
        posy=0
        video_capture = cv2.VideoCapture(0)
        while True:
        # Capture frame-by-frame
        ret, frames = video_capture.read()

        gray = cv2.cvtColor(frames, cv2.COLOR_BGR2GRAY)

        faces = faceCascade.detectMultiScale(
        gray,
        scaleFactor=1.1,
        minNeighbors=5,
        minSize=(200, 200),
        flags=cv2.CASCADE_SCALE_IMAGE
        )

        # Draw a rectangle around the faces
        for (x, y, w, h) in faces:
        rec=cv2.rectangle(frames, (x, y), (x+w, y+h), (0, 255, 0), 1)
        posy=y
        posx=x
    
    
        cv2.line(img=frames, pt1=(100, 0), pt2=(100, 1000), color=(0, 255, 0), thickness=5, 
        lineType=8, shift=0)
    
        if (posx<(100) and posx!=0 and posy <1000 and posy!=0 ):
        s="Collision Detected at  x {} and y {}"
        ctypes.windll.user32.MessageBoxW(0,s.format(posx,posy), "Collision Detected", 1) 
        now = datetime.now()
        #9:17:45.44343
        today = date.today()
        
        current_time = now.strftime("%H-%M-%S")
        str="{} {} Capture.jpg"
        sk=str.format(today,current_time)
        cv2.imwrite(sk, frames)
        print("capture saved at ",sk)
       
    ret,buffer=cv2.imencode('.jpg',frames)
    frame=buffer.tobytes()
    
    

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

    yield(b'--frame\r\n'b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
    cv2.imshow('Video', frames)

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

    @app.route('/video')
    def video():
    return Response(generate_frames(),mimetype='multipart/x-mixed-replace; boundary=frame')

    if __name__=="__main__":
    app.run(host='0.0.0.0', port=8080)

使用命令:

<img class=".img-fluid" src="{{ url_for('video') }}" 
         width="1200"height="700">

我试图找出一种方法来访问它的flutter. video_player扩展似乎没有帮助,因为我的流是基于连续的图像流。

pcww981p

pcww981p1#

来自RTSP协议的视频流可以很容易地通过Flutter VLC Player传输到Flutter上,所以你不需要将其与Python服务器集成。
只需在控制器中添加链接:

_videoPlayerController = VlcPlayerController.network(
  'rtsp://your Link',
  hwAcc: HwAcc.FULL,
  autoPlay: false,
  options: VlcPlayerOptions(),
  );

相关问题