banner



How To Get Data From Youtube Video In Python Using Cv2

Streaming live videos from IP Cameras or Webcam with Calculator Vision

Photo by Rob Sarmiento on Unsplash

You took the problem of installing a webcam or a surveillance camera at your dwelling, or office or any identify that you ain. Apparently, you would want to be able to watch the live stream of the video anyplace and anytime y'all like.

Well-nigh of the people utilise IP cameras (Cyberspace Protocol cameras) instead of CCTV (Airtight-Excursion Tv) for surveillance purposes equally they accept a much higher resolution and reduced price for cabling. You can find the detailed differences between both these systems here. In this article nosotros volition be focusing on IP cameras.

IP camera, is a type of digital video photographic camera that receives control information and sends image information via an IP network and require no local recording device. Most IP cameras are RTSP (Real Fourth dimension Streaming Protocol) based and is therefore "not supported" natively in internet browsers.

Image by Author-Working of RTSP Protocol

So how do yous use your web browser to view the live streaming ?

In this commodity we will acquire how to do that using Estimator Vision.

Computer Vision is an interdisciplinary field that deals with how computers can be made to proceeds a high-level agreement from digital images or videos.

For implementing the figurer vision part we volition use the OpenCV module in Python and to brandish the live stream in the web browser we will utilize the Flask web framework. Before diving into the coding role let u.s.a. first know about these modules briefly. If yous are already familiar with these modules, y'all can straight jump to the next section.

Co-ordinate to the Wikipedia, Flask is a micro web framework written in Python. It is classified equally a microframework because it does not require detail tools or libraries. It has no database abstraction layer, class validation, or any other components where pre-existing 3rd-party libraries provide common functions.

According to GeeksForGeeks, OpenCV is the huge open-source library for the estimator vision, motorcar learning, and image processing and now it plays a major role in real-fourth dimension operation which is very important in today'southward systems.

Step1- Install Flask & OpenCV :

Y'all tin employ the 'pip install flask' and 'pip install opencv-python' control. I use the PyCharm IDE to develop flask applications. To easily install libraries in PyCharm follow these steps.

Step2- Import necessary libraries, initialize the flask app :

We will now import the necessary libraries and initialize our flask app.

                      #Import necessary libraries            
from flask import Flask, render_template, Response
import cv2
#Initialize the Flask app
app = Flask(__name__)

Step3- Capture Video using OpenCV :

Create a VideoCapture() object to trigger the camera and read the outset prototype/frame of the video. We can either provide the path of the video file or use numbers to specify the utilize of local webcam. To trigger the webcam we pass '0' as the argument. To capture the live feed from an IP Camera nosotros provide the RTSP link equally the argument. To know the RTSP address for your IP Camera go through this — Finding RTSP addresses.

          camera = cv2.VideoCapture(0)          '''
for ip camera use - rtsp://username:countersign@ip_address:554/user=username_password='password'_channel=channel_number_stream=0.sdp'
for local webcam apply cv2.VideoCapture(0)
'''

Step4- Calculation window and generating frames from the camera:

Image past Author-Frame generation function

The gen_frames() part enters a loop where it continuously returns frames from the camera every bit response chunks. The function asks the camera to provide a frame so it yields with this frame formatted as a response chunk with a content type of image/jpeg, as shown to a higher place. The code is shown below :

Frame generating function

Step5- Ascertain app route for default page of the web-app :

Routes refer to URL patterns of an app (such as myapp.com/home or myapp.com/about). @app.road("/") is a Python decorator that Flask provides to assign URLs in our app to functions easily.

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

The decorator is telling our @app that whenever a user visits our app domain (localhost:5000 for local servers) at the given .route(), execute the alphabetize() function. Flask uses the Jinja template library to render templates. In our application, we volition use templates to render HTML which will brandish in the browser.

Step6- Define app route for the Video feed:

          @app.road('/video_feed')
def video_feed():
return Response(gen_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')

The '/video_feed' route returns the streaming response. Because this stream returns the images that are to be displayed in the spider web folio, the URL to this route is in the "src" attribute of the paradigm tag (see 'index.html' beneath). The browser volition automatically go on the prototype element updated by displaying the stream of JPEG images in information technology, since multipart responses are supported in well-nigh/all browsers

Allow's have a expect at our alphabetize.html file :

          <body>
<div class="container">
<div form="row">
<div class="col-lg-viii showtime-lg-2">
<h3 course="mt-5">Live Streaming</h3>
<img src="{{ url_for('video_feed') }}" width="100%">
</div>
</div>
</div>
</body>

Step7- Starting the Flask Server :

          if __name__ == "__main__":
app.run(debug=True)

app.run() is called and the web-application is hosted locally on [localhost:5000].

"debug=Truthful" makes sure that nosotros don't require to run our app every time we makes changes, nosotros can simply refresh our spider web folio to see the changes while the server is withal running.

Projection Construction :

Epitome by Author-Projection Structure

The projection is saved in a folder called "Photographic camera Detection". We run the 'app.py' file. On running this file, our application is hosted in the local server at port 5000.

Y'all can simply type "localhost:5000" on your web browser to open your spider web-application after running 'app.py'

  • app.py — This is the Flask application nosotros created above
  • templates — This folder contains our 'index.html' file. This is mandatory in Flask while rendering templates. All HTML files are placed under this folder.

Let'due south see what happens when we run 'app.py' :

Paradigm past Author-Local Server for Flask Application

On clicking on the provided URL, our web browser opens up with the live feed. Since I used VideoCapture(0) above, the webcam feed is displayed on the browser:

Image past Author-Local Web Camera feed on Spider web Browser

And that'southward it !!

Yous have the alive video stream from your IP camera/webcam on your web browser which can be used for security and surveillance purposes.

Support me if you enjoyed reading this article. Click on the pic above. Cheers

Refer to my GitHub Lawmaking.

Annotation : All the resources that you will crave to go started accept been mentioned and their links provided in this article equally well. I promise y'all make adept use of it :)

I hope this commodity will get you lot interested in trying out new things in the Computer Vision domain and help you add together to your knowledge. If you lot have enjoyed reading this commodity do share it with your friends and family. Thanks for your fourth dimension.

Source: https://towardsdatascience.com/video-streaming-in-web-browsers-with-opencv-flask-93a38846fe00

Posted by: johnsonalitels.blogspot.com

0 Response to "How To Get Data From Youtube Video In Python Using Cv2"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel