Access IP Camera in Python OpenCV
I answer my own question reporting what therefore seems to be the most comprehensive overall procedure to Access IP Camera in Python OpenCV.
Given an IP camera:
- Find your camera
IP
address - Find the
port
where the IP address is accessed - Find the
protocol
(HTTP/RTSP etc.) specified by the camera provider
Then, if your camera is protected go ahead and find out:
- your
username
- your
password
Then use your data to run the following script:
"""Access IP Camera in Python OpenCV"""
import cv2
stream = cv2.VideoCapture('protocol://IP:port/1')
# Use the next line if your camera has a username and password
# stream = cv2.VideoCapture('protocol://username:password@IP:port/1')
while True:
r, f = stream.read()
cv2.imshow('IP Camera stream',f)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cv2.destroyAllWindows()
NOTE: In my original question I specify to being working with Teledyne Dalsa Genie Nano XL Camera. Unfortunately for this kind of cameras this normal way of accessing the IP Camera video stream does not work and the Sapera SDK must be employed in order to grab frames from the device.
OpenCV with Network Cameras
rtsp protocol did not work for me.
mjpeg worked first try. I assume it is built into my camera (Dlink DCS 900).
Syntax found here:
http://answers.opencv.org/question/133/how-do-i-access-an-ip-camera/
I did not need to compile OpenCV with ffmpg support.
OpenCV Python IP Camera live image
If you want to decouple your reading loop from your GUI loop you can use multithreading to separate the code. You can have a thread running your livestream function and dumping the image out to a global image variable where your GUI loop can pick it up and do whatever to it.
I can't really test out the livestream part of the code, but something like this should work. The read function is an example of how to write a generic looping function that will work with this code.
import cv2
import time
import threading
import numpy as np
# generic threading class
class Reader(threading.Thread):
def __init__(self, func, *args):
threading.Thread.__init__(self, target = func, args = args);
self.start();
# globals for managing shared data
g_stop_threads = False;
g_lock = threading.Lock();
g_frame = None;
# reads frames from vidcap and stores them in g_frame
def read():
# grab globals
global g_stop_threads;
global g_lock;
global g_frame;
# open vidcap
cap = cv2.VideoCapture(0);
# loop
while not g_stop_threads:
# get a frame from camera
ret, frame = cap.read();
# replace the global frame
if ret:
with g_lock:
# copy so that we can quickly drop the lock
g_frame = np.copy(frame);
# sleep so that someone else can use the lock
time.sleep(0.03); # in seconds
# your livestream func
def livestream():
# grab globals
global g_stop_threads;
global g_lock;
global g_frame;
# open stream
stream = urlopen('http://192.168.4.1:81/stream')
bytes = b''
# process stream into opencv image
while not g_stop_threads:
try:
bytes += stream.read(1024)
a = bytes.find(b'\xff\xd8')
b = bytes.find(b'\xff\xd9')
if a != -1 and b != -1:
jpg = bytes[a:b+2]
bytes = bytes[b+2:]
getliveimage = cv2.imdecode(np.frombuffer(jpg, dtype=np.uint8), cv2.IMREAD_COLOR)
livestreamrotated1 = cv2.rotate(getliveimage, cv2.ROTATE_90_CLOCKWISE) #here I am rotating the image
# acquire lock and replace image
with g_lock:
g_frame = livestreamrotated1;
# sleep to allow other threads to get the lock
time.sleep(0.03); # in seconds
except Exception as e:
print(e)
print("failed at this point")
def main():
# grab globals
global g_stop_threads;
global g_lock;
global g_frame;
# start a thread
# reader = Reader(read);
reader = Reader(livestream);
# show frames from g_frame
my_frame = None;
while True:
# grab lock
with g_lock:
# show
if not g_frame is None:
# copy # we copy here to dump the lock as fast as possible
my_frame = np.copy(g_frame);
# now we can do all the slow manipulation / gui stuff here without the lock
if my_frame is not None:
cv2.imshow("Frame", my_frame);
# break out if 'q' is pressed
if cv2.waitKey(1) == ord('q'):
break;
# stop the threads
g_stop_threads = True;
if __name__ == "__main__":
main();
streaming 2 cameras at the same time in python with ip camera support
From a first glance cv2.imdecode doesn't return a value for ret1
video_capture.read() will return two values:
a boolean which represents whether the frame was read successfully
and a numpy matrix containing the frame
It only returns a numpy matrix of the image, therefore you can only unpack one value, and not 2
you can replace if(ret1):
with if(img is not None):
maybe to check whether the frame from the ip camera is not rubbish
Related Topics
How to Propagate Exceptions Between Threads
Constexpr If and Static_Assert
Why Is 'This' a Pointer and Not a Reference
Resolution of Std::Chrono::High_Resolution_Clock Doesn't Correspond to Measurements
Should I Use Std::Function or a Function Pointer in C++
What Kind of Optimization Does Const Offer in C/C++
Size_T' VS 'Container::Size_Type'
Foreach Macro on Macros Arguments
Returning Arrays from a Function in C++
Efficient String Concatenation in C++
Functions With Const Arguments and Overloading
How to Remove Certain Characters from a String in C++
Why Is C++11'S Pod "Standard Layout" Definition the Way It Is
Initializing a Two Dimensional Std::Vector
Why Are String Literals L-Value While All Other Literals Are R-Value
Why Does Printf() Promote a Float to a Double
How to Detect Unnecessary #Include Files in a Large C++ Project