Hi Everyone!
today I will show you how to stream live video feed
straight from your mobile phone camera into your Python code
and it is actually so simple! - you gonna LOVE this tutorial!
on your phone, navigate to the Play Store and search for "ip webcam"
then download the app and make sure you're connected
to the exact same WIFI network as your computer
then press on "start server"
and pay close attention to this ip address
and then we will type the ipv4 address we are presented with inside our browser
I usually select the "browser" for our video rendering
and then I usually expend it to a full screen
there are too many Mariyas on the same screen
so let's quickly fix it by opening the developer tools
so with a right click on the video
we will hit on "inspect"
then we will hover over the "src" attribute
that my head is blocking
and we will click on the "current source" link
and we will then copy this URL
and we will include it in our code
and as you guys can see I already prepared a blank Python file
which I called "livestream.py"
and we will begin by importing Open CV
now if you guys are not familiar with Open CV
it is a brilliant computer vision library
and we can import it with "import cv2"
and right away we will initialize a Video Capture object
by typing "cv2.VideoCapture()" in camo case
and inside the round brackets, inside a string - we will paste our URL
and then we will assign this expression to a variable called "capture"
now, because we are dealing with live streaming video
it is not pre-recorded, it is actually ongoing!
we will need to use a "while loop" to display it
so right underneath, we will type "while(True):"
and before you guys freak out and think that we're entering an infinite loop
that will transmit our video for ever and ever
- we will actually include a "break statement" in the very end
and in this statement - we will have a special character
that if we press it on the keyboard - our live stream collapses. so don't worry!
and we will begin this loop by fetching data from our video capture
we can do this by typing "capture.read()"
and we will unpack this expression into two different variables
so the first variable, we're not planning to use it, so I'll call it "_"
and the second variable is called "frame"
and if you guys are curious - the first variable represents a Boolean value
is there a video frame, or not?
the second variable actually represents the frame itself
which is a single image in a sequence of images that we call "video"
and now we can go ahead and display these frames by typing:
"cv2.imshow()" as in image show
and inside the round brackets, we will first specify the name of the window
in my case - I'm gonna call it "livestream"
and as the second value we will specify our frame
and as you guys remember, we will need to include a "break statement"
to avoid an infinite loop
for this, we will type: "if cv2.waitKey(1)"
which is going to listen for a key press event
and if this key equals "ord('q')"
which represents "q" on our keyboard
then - "break"
so as soon as we press "q" on our keyboard
as in "quit", then our video capture window collapses
and last but not least (because it's very important!)
we will leave the "while loop"
and we will free the resources with "capture.release()"
and then right away - we will collapse our video capture window
with: "cv2.desctoryAllWindows()"
off with their heads!!!
and then we will save this file and we will run it from our terminal
with "python livestream.py"
and hello again to giant Mariya!
and to the green screen behind me
which you almost never get to see
so we are obviously streaming this live
and then if we go ahead and press "q" on our keyboard...
the window is gone!
and I will quickly show you how to manipulate this video with Open CV
so let's say I would like to convert it to Grayscale
for this, I'll create a brand new variable called "gray"
which will equal to: "cv2.cvtColor()"
and inside the round brackets we first specify the frame
followed by the filter that we would like to apply
in may case that would be: "cv2.COLOR_RGB2GRAY"
and then, instead of displaying our frame - we will display our "gray" variable
we will save the file, we will re-run it
Hello Hello greyscale Mariya!
good to see you!
another cool transformation I will show you today is mirroring!
so in the next line of code we will type "mirror"
which will equal to: "cv2.flip()"
and the first parameter would be our grayscale image
and the second parameter would be our flip code
now flip code 0 will mirror the image vertically
while flip code 1 - will mirror the image horizontally
and flip code -1 will do both!
now in my case, I am only going to flip it horizontally
so I'll remove the minus and we will leave it as: 1
and as a last step - instead of displaying our greyscale image
we will display our mirrored image
we will save this file and we will re-run it
[ goofy song ] "and hello to flipped Mariya"
"it's good to see ya - mmm!"
that was actually a good song!
and thank you so much for watching!
I really hope you found this video healpful
and if you did - please give it a like, a comment
maybe subscribe to may channel, or even better - share this video
with your friends, with your family, with your co-workers - with anybody!
and thanks again for watching and I will see you - very soon!