I am going to write a code to get the image from the IP camera. Of course, if the connection between the code and the camera is interrupted (for any reason), it will wait to connect again and start imaging again.
as we know
Receiving the image from the camera does not work with the usual loops.
I checked almost all the cases and did not get any results.
Edit my code to get the image:
python 3.6.17
opencv 3.4.2.17
import cv2
#print("Before URL")
cap = cv2.VideoCapture('rtsp://admin:123456#192.168.1.216/H264?ch=1&subtype=0')
#print("After URL")
while True:
#print('About to start the Read command')
ret, frame = cap.read()
#print('About to show frame of Video.')
cv2.imshow("Capturing",frame)
#print('Running..')
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
im working with opencv3.4 in python 3.6
and i am trying to capture rtsp stream from my ip camera
code:
if __name__ == "__main__":
cap = cv2.VideoCapture("http://admin::Admin123#192.168.0.10/cgi-bin/mjpeg?stream=1")
while (cap.isOpened()):
ret, frame = cap.read()
cv2.imshow('frame', frame)
k = cv2.waitKey(1)
if k == 27:
cap.release()
cv2.destroyAllWindows()
i had successfully manage to get the stream and display it but if i exit the script and try to run it again i get BSOD (windows blue screen of death)
my suspicion is that opencv release method doesn't manage to release the rtsp connection
someone had this problem and manage to fix it?
thank you !!
Right now I am using a web cam and it works perfectly fine with the code below -:
capture = cv2.VideoCapture(0)
Now instead of web cam, I want to use the ip camera(https://192.168.0.60)
What would be the easiest way to do it with OpenCV(Python)?
I saw a bunch of posts, but couldn't find a straight answer to this.
Can someone help, who already got it running?
Thank you!
Firstly, you must find the exact url for your video stream, and that's best done with a web browser. For example I use IP Webcam app on android (com.pass.webcam) and it's stream will be on:
http://phone-ip-address:port/video
If I visit that url with a web browser, I can see the live stream. Make sure, that what you see is only the video stream, not a html page with the stream. If there is a html page, you can right-click and select Open image in new tab (in Chrome) to get to the stream.
However it looks like OpenCV can only read the video stream if the filename/url has the right suffix. Adding ?type=some.mjpeg worked for me. So the url would be:
http://phone-ip-address:port/video?type=some.mjpeg
Try visiting such an url in the web browser before you go for opencv.
Take a look at this example with python, OpenCV, IPCAM and hikvision
import numpy as np
import cv2
cap = cv2.VideoCapture()
cap.open("rtsp://USER:PASS#IP:PORT/Streaming/Channels/2")
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
#gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Display the resulting frame
cv2.imshow('Salida',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
Image:
Get video from IPCAM with python and OpenCV
Here you go,
import numpy as np
import cv2
cap = cv2.VideoCapture('rtsp://<username_of_camera>:<password_of_camera#<ip_address_of_camera')
while(True):
ret, frame = cap.read()
cv2.imshow('Stream IP Camera OpenCV',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
For example:
import numpy as np
import cv2
cap = cv2.VideoCapture('rtsp://admin:admin#192.168.0.60')
while(True):
ret, frame = cap.read()
cv2.imshow('Stream IP camera opencv',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
Then save the file as camera.py (.py), go to command prompt or terminal, locate file and type python camera.py or python <file_name>.py enter to run script.
If you want to exit from script windows just press "q" or close cmd.
Hope this helpful.
Here is an example for a Vivotek IP8136W IP Camera. It does not support streaming.
This code continuously grabs still frames, at about 2fps. No observed CPU loading.
import numpy as np
import cv2
# for webcams, request stream only once.
#cap = cv2.VideoCapture(0)
while(True):
# For single image IP cams, request frame every time.
cap = cv2.VideoCapture("http://root:0002A78D65F2#192.168.1.38/cgi-bin/viewer/video.jpg")
ret, frame = cap.read()
# Display the resulting frame
if ret:
cv2.imshow('camera',frame)
else:
print("error getting frame.")
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Done. release the capture
cap.release()
cv2.destroyAllWindows()
I'm playing a mp4 file in python, but the sound of the video doesn't comes out, I searched for a while if it's anyway to play the sound, but I could not find anything. Does anyone knows how to play the sound?
I post the code that displays de video :
import cv2
import numpy as np
# Create a VideoCapture object and read from input file
# If the input is the camera, pass 0 instead of the video file name
cap = cv2.VideoCapture('video.mp4')
# Check if camera opened successfully
#if (cap.isOpened()== False):
# print("Error opening video stream or file")
# Read until video is completed
while(cap.isOpened()):
# Capture frame-by-frame
ret, frame = cap.read()
if ret == True:
# Display the resulting frame
cv2.imshow('Frame',frame)
# Press Q on keyboard to exit
if cv2.waitKey(25) & 0xFF == ord('q'):
break
# Break the loop
else:
break
# When everything done, release the video capture object
cap.release()
# Closes all the frames
cv2.destroyAllWindows()
It seems useless that OpenCV doesn't allow to play the sound. I'm using Python 3 by the way. Thank you.
ANSWER: OpenCV is a computer-vision library. It does not support audio. If you want to play sound, you can try ffpyplayer. – Thanks to >>> yushulx
You can use Pyglet to play video along with its audio....
Hope this helps...
import pyglet
vid_path='vid1.mp4' # Name of the video
window=pyglet.window.Window()
player = pyglet.media.Player()
source = pyglet.media.StreamingSource()
MediaLoad = pyglet.media.load(vid_path)
player.queue(MediaLoad)
player.play()
#window.event
def on_draw():
if player.source and player.source.video_format:
player.get_texture().blit(50,50)
pyglet.app.run()
The following code is trying to stream a video from an A.R Drone using python. When I run the code, I receive error error reading video feed. I am using OPENcv 3.3.0. Maybe I am suppose to download something. I am not entirely sure. I am a newbie to this.
import cv2
cam = cv2.VideoCapture('tcp://192.168.1.1:5555')
running = True
while running:
# get current frame of video
running, frame = cam.read()
if running:
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == 27:
# escape key pressed
running = False
else:
# error reading frame
print 'error reading video feed'
cam.release()
cv2.destroyAllWindows()