捕获从网站到Flutter应用程序的视频流
我正在尝试构建一个在Python中执行一些图像处理后运行视频流的应用程序。处理后,它将其生存在网站上。
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)
using command:
<img class=".img-fluid" src="{{ url_for('video') }}"
width="1200"height="700">
i am trying to figure out a way to access it on flutter. Video_player扩展名似乎没有帮助,因为我的流是基于连续图像流的。
i am trying to build an app that runs video stream after performing some image processing in python. which after processing lives it on a site.
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)
using command:
<img class=".img-fluid" src="{{ url_for('video') }}"
width="1200"height="700">
i am trying to figure out a way to access it on flutter. video_player extension doesn't seem to help as my stream is based on continuous stream of images.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自RTSP协议的视频流可以轻松地进行流式传输,以使用Flutter VLC播放器播放。因此,您无需将其集成到Python服务器。
只需在控制器中添加链接:
Video Stream Coming from RTSP Protocol can easily be Streamed on to Flutter using Flutter VLC Player. So you don't need to integrate it with Python Server.
Just Add Link in the Controller: