video streaming through flask in raspberry pi

佐手、 提交于 2020-07-10 07:27:09

问题


Hi i have made a program in Flask that helps me to get live video steaming :

Check the image here

Main goal : the main idea of creating this program is when i click the capture button it captures the image from the live steaming video , so i coded both the below python files in flask which is running on 0.0.0.0:5000 and put the index.php file to my apache server which is running on localhost:80 so in the image above as you can see , there is an iframe from index.php file (running on localhost:80) which is showing the video running at 0.0.0.0:5000 , and has a capture button that can be used to capture image from that live steaming so i designed it in a way that when i click on capture button it redirects it to 0.0.0.0:5000/login and behind the scenes it captures the image and redirects back to localhost:80 so that the user may again see the live video steaming

the files are below :

index.php of apache port 80:

<?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()

but there is a problem or you can say a limitation , picamera doesn't allowed to be opened more than 1 time , which means whenever i click on the capture button it goes to 0.0.0.0:5000/login , and gives me the error

click here to view image

i am just so weak in python don't know the solution of it :( i just want that whenever i click the capture button it captures the image and gets redirects back to localhost:80 runs perfectly fine there also , since localhost:80 is using iframe there is a possibility that it would give the same error that an app is already using the camera .! :(

i am so much tired of it , any solution ?

来源:https://stackoverflow.com/questions/60355569/video-streaming-through-flask-in-raspberry-pi

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!