通过树莓派的烧瓶中的视频流

时间:2020-02-22 18:47:53

标签: flask picamera

嗨,我在Flask中制作了一个程序,可以帮助我进行实时视频播放:

Check the image here

主要目标:创建此程序的主要思想是,当我单击“捕获”按钮时,它将捕获来自实时汽蒸视频的图像,因此,我将下面的python文件都编码在了运行于0.0.0.0:5000的flask中,并且将index.php文件放到我在本地主机上运行的apache服务器上:80 因此,如您所见,在上图中,有一个iframe来自index.php文件(在localhost:80上运行),该iframe显示了以0.0.0.0:5000运行的视频,并具有可用于捕获的捕获按钮。现场蒸腾的图像 所以我设计了一种方式,当我单击“捕获”按钮时,它将其重定向到0.0.0.0:5000/login,并在幕后捕获了图像并将其重定向回localhost:80,以便用户可以再次看到实时视频蒸

文件在下面:

Apache端口80的index.php:

<?php
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);

socket_connect($sock, "8.8.8.8", 53);

socket_getsockname($sock, $name); // $name passed by reference



$localAddr = $name;

$me="http://".$localAddr.":5000/login";
$video="http://".$localAddr.":5000/video";
?>

<html>

   <body>
  <iframe width="40%" height="80%"

<?php echo "src='$video'>"; ?>

</iframe>

<?php
   echo  "<form action ='$me'  method = 'post'>";
     ?>
      <input type = "submit" name="capture" value = "capture" />
      </form>

   </body>

</html>

main.py:

from flask import Flask, redirect, url_for, request
from flask import Flask, render_template, Response
import picamera
from picamera.array import PiRGBArray
from cameraa import VideoCamera
from picamera import PiCamera
from cameraa import VideoCamera
import cv2
import time
import socket
import io
import socket


s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
ip=s.getsockname()[0]
s.close()
text="http://"
ip=text+ip

app = Flask(__name__)

@app.route('/')
def hello():
    #return "Hello World!"
    return render_template('index.html')

def gen(cameraa):
    while True:
        frame = cameraa.get_frame()
        yield (b'--frame\r\n'
                b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')

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



@app.route('/login',methods = ['POST', 'GET'])
def login():
   if request.method == 'POST':
      user = request.form['capture']
      camera = picamera.PiCamera()
      camera.capture('/root/Desktop/Umair/new/image.jpg')
      camera.close()
      return redirect(ip, code=302)

   else:
      user = request.args.get('capture')
      camera =picamera.PiCamera()
      camera.capture('/root/Desktop/Umair/new/image.jpg')
      camera.close()
      return redirect(ip, code=302)
if __name__ == '__main__':
   app.run(debug=True,host='0.0.0.0')

cameraa.py:

import cv2
import picamera
from picamera.array import PiRGBArray
import time


class VideoCamera(object):

    def __init__(self):
        self.camera = picamera.PiCamera()
        self.rawCapture = PiRGBArray(self.camera)
        self.car_cascade = cv2.CascadeClassifier('cars.xml')
        #self.video = cv2.VideoCapture('/root/Desktop/flask/virtualenv/project_env/car.mp4')   #cv2.VideoCapture(0)

    def __del__(self):
        self.camera.release()

    def get_frame(self):
        self.rawCapture.truncate(0)
        for frame in self.camera.capture_continuous(self.rawCapture, format="bgr", use_video_port=True):
            image = frame.array
            # convert to gray scale of each frames 
            gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
            # Detects cars of different sizes in the input image 
            cars = self.car_cascade.detectMultiScale(gray, 1.1, 1)
            # To draw a rectangle in each cars 
            for (x,y,w,h) in cars:
                cv2.rectangle(image,(x,y),(x+w,y+h),(0,0,255),2)

            ret, jpeg = cv2.imencode('.jpg', image)
            return jpeg.tobytes()

但是有问题,或者可以说是一个限制,picamera不允许打开超过1次,这意味着每当我单击捕获按钮时,它将转到0.0.0.0:5000/login,并给出我的错误

click here to view image

我在python中太弱了,不知道解决方案:( 我只是希望每当我单击捕获按钮时,它都会捕获图像并重定向回localhost:80,在那儿也可以很好地运行,因为localhost:80正在使用iframe,所以它可能会给出与应用程序相同的错误已经在使用相机了! :(

我对此感到非常厌倦,有什么解决办法吗?

0 个答案:

没有答案