I'm working on a small project for personal use where I want to load the CCTV streams I have from home into Opencv. I've done alot of research and understand that I need to use multithreading to get them to work correctly. and using the following code I've got it working on one camera perfectly!
from threading import Thread
import imutils
import cv2, time
camlink1 = "rtsp://xx.xx.xx.xx.:xxx/user=xxx&password=xxx&channel=1&stream=0./"
class VideoStreamWidget(object):
def __init__(self, link, camname, src=0):
self.capture = cv2.VideoCapture(link)
# Start the thread to read frames from the video stream
self.thread = Thread(target=self.update, args=())
self.thread.daemon = True
self.thread.start()
self.camname = camname
self.link = link
print(camname)
print(link)
def update(self):
# Read the next frame from the stream in a different thread
while True:
if self.capture.isOpened():
(self.status, self.frame) = self.capture.read()
time.sleep(.01)
def show_frame(self):
# Display frames in main program
frame = imutils.resize(self.frame, width=400)
cv2.imshow('Frame ' + self.camname, frame)
key = cv2.waitKey(1)
if key == ord('q'):
self.capture.release()
cv2.destroyAllWindows()
exit(1)
if __name__ == '__main__':
video_stream_widget = VideoStreamWidget(camlink1,"Cam1")
while True:
try:
video_stream_widget.show_frame()
except AttributeError:
pass
But now I want run another camera alongside it (in parallel) but I'm not sure how what i need to change to the code to start another thread and run it side by side. I thought I did by using the following:
from threading import Thread
import imutils
import cv2, time
camlink1 = "rtsp://xx.xx.xx.xx.:xxx/user=xxx&password=xxx&channel=1&stream=0./"
camlink2 = "rtsp://xx.xx.xx.xx.:xxx/user=xxx&password=xxx&channel=2&stream=0./"
class VideoStreamWidget(object):
def __init__(self, link, camname, src=0):
self.capture = cv2.VideoCapture(link)
# Start the thread to read frames from the video stream
self.thread = Thread(target=self.update, args=())
self.thread.daemon = True
self.thread.start()
self.camname = camname
self.link = link
print(camname)
print(link)
def update(self):
# Read the next frame from the stream in a different thread
while True:
if self.capture.isOpened():
(self.status, self.frame) = self.capture.read()
time.sleep(.01)
def show_frame(self):
# Display frames in main program
frame = imutils.resize(self.frame, width=400)
cv2.imshow('Frame ' + self.camname, frame)
key = cv2.waitKey(1)
if key == ord('q'):
self.capture.release()
cv2.destroyAllWindows()
exit(1)
if __name__ == '__main__':
video_stream_widget = VideoStreamWidget(camlink1,"Cam1")
video_stream_widget = VideoStreamWidget(camlink2,"Cam2")
while True:
try:
video_stream_widget.show_frame()
except AttributeError:
pass
But this just shows the second camera only, overriding the first. I know somewhere I'm missing something simple but after looking at this for hours I can't figure it out. Any help is appreciated
Cheers
Chris
The problem is in these lines of code:
if __name__ == '__main__':
video_stream_widget = VideoStreamWidget(camlink1,"Cam1")
video_stream_widget = VideoStreamWidget(camlink2,"Cam2")
while True:
try:
video_stream_widget.show_frame()
except AttributeError:
pass
To be precise, you are overwriting the the variable video_stream_widget and then calling once video_stream_widget.show_frame() This will be only call in the last one (even if the thread that captures the images is still working), so only the last one will be showing, i.e. "Cam2".
Solution
Add different names for each and call the show_frame() function on both, like this:
if __name__ == '__main__':
video_stream_widget = VideoStreamWidget(camlink1,"Cam1")
video_stream_widget2 = VideoStreamWidget(camlink2,"Cam2")
while True:
try:
video_stream_widget.show_frame()
video_stream_widget2.show_frame()
except AttributeError:
pass
Related
For Context: I'm trying to write a program that is able to record video from a varying amount of webcams.
I got everything working except that I am getting a strange behavior in the video file itself.
After reading about other questions, fixing all my codecs and so forth, I figured it's an issue relating to threading. The strange part is when I tried it with a code I found around here, I get the opposite problem !
The Issue: My own code creates super-speedy videos (5 recorded seconds plays in 1 second). While the other code creates super-slow videos (5 recorded seconds plays in 25 seconds).
The Question: Can anyone explain this behavior to Me?
My simplified code:
import cv2
import threading
import os
import time
class Cameras():
def __init__(self, parent=None):
super().__init__()
self.cam1 = cv2.VideoCapture(0)
self.cam1check = self.cam1.isOpened()
newfolder = "./Unnamed"
os.makedirs(newfolder, exist_ok=True)
full_path = os.path.abspath(newfolder)
self.path = os.path.join(full_path,"Speedy")
self.namecam = self.path + " Example 0.avi"
self.Format = cv2.VideoWriter_fourcc(*'H264')
self.FPS = 60
self.cam1.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
self.cam1.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
self.cam1.set(cv2.CAP_PROP_FPS, self.FPS)
self.width1 = int(self.cam1.get(cv2.CAP_PROP_FRAME_WIDTH))
self.height1 = int(self.cam1.get(cv2.CAP_PROP_FRAME_HEIGHT))
self.size1 = (self.width1,self.height1)
def capturecam1(self):
while self.cam1check is True:
rect1, frame1 = self.cam1.read(cv2.CAP_FFMPEG)
if rect1:
self.video_writer_cam1.write(frame1)
def start_rec(self):
self.video_writer_cam1 = cv2.VideoWriter(self.namecam, self.Format, self.FPS, self.size1, cv2.CAP_FFMPEG)
if self.cam1check is True:
thread = threading.Thread(target=self.capturecam1, daemon=True)
thread.start()
if __name__ == '__main__':
Launch = Cameras()
Launch.start_rec()
while True:
time.sleep(1)
The other code:
from threading import Thread
import cv2
import time
import os
class VideoWriterWidget(object):
def __init__(self, video_file_name, src=0):
# Create a VideoCapture object
print(src)
self.frame_name = src # if using webcams, else just use src as it is.
self.video_file = video_file_name
# self.video_file_name = video_file_name + '.avi'
# self.capture = cv2.VideoCapture(src)
newfolder = "./Unnamed"
os.makedirs(newfolder, exist_ok=True)
full_path = os.path.abspath(newfolder)
self.path = os.path.join(full_path,"Slowmo")
self.video_file_path = self.path + " Example {}.avi".format(self.video_file)
# Default resolutions of the frame are obtained (system dependent)
self.cam1 = cv2.VideoCapture(0)
self.cam1check = self.cam1.isOpened()
print(self.cam1check)
self.cam1.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
self.cam1.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
self.cam1.set(cv2.CAP_PROP_FPS, 60)
# Set up codec and output video settings
self.codec = cv2.VideoWriter_fourcc(*'H264')
self.output_video = cv2.VideoWriter(self.video_file_path, self.codec, 60, (1280, 720))
# Start the thread to read frames from the video stream
self.thread = Thread(target=self.update, args=())
self.thread.daemon = True
self.thread.start()
# Start another thread to show/save frames
self.start_recording()
print('initialized {}'.format(self.video_file))
def update(self):
# Read the next frame from the stream in a different thread
while True:
if self.cam1check is True:
(self.status, self.frame) = self.cam1.read()
def show_frame(self):
# Display frames in main program
print(self.status)
if self.status:
print("Showing!")
cv2.imshow(self.frame_name, self.frame)
# Press Q on keyboard to stop recording
key = cv2.waitKey(1)
if key == ord('q'):
self.cam1.release()
self.output_video.release()
cv2.destroyAllWindows()
exit(1)
def save_frame(self):
# Save obtained frame into video output file
self.output_video.write(self.frame)
def start_recording(self):
# Create another thread to show/save frames
def start_recording_thread():
while True:
try:
# self.show_frame()
self.save_frame()
except AttributeError:
pass
self.recording_thread = Thread(target=start_recording_thread, args=())
self.recording_thread.daemon = True
self.recording_thread.start()
if __name__ == '__main__':
src1 = '0'
video_writer_widget1 = VideoWriterWidget('Camera 1', src1)
# src2 = '1'
# video_writer_widget2 = VideoWriterWidget('Camera 2', src2)
# src3 = '2'
# video_writer_widget3 = VideoWriterWidget('Camera 3', src3)
# Since each video player is in its own thread, we need to keep the main thread alive.
# Keep spinning using time.sleep() so the background threads keep running
# Threads are set to daemon=True so they will automatically die
# when the main thread dies
while True:
time.sleep(1)
Credit for the other code here: https://stackoverflow.com/a/71624807/9599824
I have a task to process a streaming video from Hikvision IP cam using OpenCV
I try this
RTSP
"""
cap = cv2.VideoCapture()
cap.open("rtsp://yourusername:yourpassword#172.16.30.248:555/Streaming/channels/1/")
and this
using API Hikvision
"""
cam = Client('http://192.168.1.10', 'admin', 'password', timeout=30)
cam.count_events = 2
response = cam.Streaming.channels[101].picture(method='get', type='opaque_data')
for chunk in response.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
img = cv2.imread('screen.jpg')
cv2.imshow("show", img)
cv2.waitKey(1)
In the first case, I have a delay between realtime and cap.read() about 9-20 seconds.
I solved it with such a "hack", but no results.
"""
class CameraBufferCleanerThread(threading.Thread):
def __init__(self, camera, name='camera-buffer-cleaner-thread'):
self.camera = camera
self.last_frame = None
super(CameraBufferCleanerThread, self).__init__(name=name)
self.start()
def run(self):
while True:
ret, self.last_frame = self.camera.read()
"""
The second case shows frames with a delay of 1-2 seconds, which is acceptable, but fps = 1, which is not very good.
Are there any options that can help you get a stream with low latency and normal fps?
In nathancys's post I finded working solutuion. it is done.
I used simple modification his code for my case for getting frame in main function.
from threading import Thread
import cv2, time
from threading import Thread
import cv2, time
class ThreadedCamera(object):
def __init__(self, src=0):
self.capture = cv2.VideoCapture(src)
self.capture.set(cv2.CAP_PROP_BUFFERSIZE, 1)
self.FPS = 1/100
self.FPS_MS = int(self.FPS * 1000)
# First initialisation self.status and self.frame
(self.status, self.frame) = self.capture.read()
# Start frame retrieval thread
self.thread = Thread(target=self.update, args=())
self.thread.daemon = True
self.thread.start()
def update(self):
while True:
if self.capture.isOpened():
(self.status, self.frame) = self.capture.read()
time.sleep(self.FPS)
if __name__ == '__main__':
src = 'rtsp://admin:password#192.168.7.100:554/ISAPI/Streaming/Channels/101'
threaded_camera = ThreadedCamera(src)
while True:
try:
cv2.imshow('frame', threaded_camera.frame)
cv2.waitKey(threaded_camera.FPS_MS)
except AttributeError:
pass
I am building an app that records frames from IP camera through RTSP.
My engine is in charge to save a video in mp4 with Opencv VideoWriter working well.
What I am looking for is to create a startRecord and a stopRecord class method that will respectively start and stop recording according to a trigger (it could be an argument that I pass to the thread).
Is anyone know what the best way to do that kind of stuff?
Here is my class:
from threading import Thread
import cv2
import time
import multiprocessing
import threading
class RTSPVideoWriterObject(object):
def __init__(self, src=0):
# Create a VideoCapture object
self.capture = cv2.VideoCapture(src)
# Start the thread to read frames from the video stream
self.thread = Thread(target=self.update, args=())
self.thread.daemon = True
self.thread.start()
def update(self):
# Read the next frame from the stream in a different thread
while True:
if self.capture.isOpened():
(self.status, self.frame) = self.capture.read()
def endRecord(self):
self.capture.release()
self.output_video.release()
exit(1)
def startRecord(self,endRec):
self.frame_width = int(self.capture.get(3))
self.frame_height = int(self.capture.get(4))
self.codec = cv2.VideoWriter_fourcc(*'mp4v')
self.output_video = cv2.VideoWriter('fileOutput.mp4', self.codec, 30, (self.frame_width, self.frame_height))
while True:
try:
self.output_video.write(self.frame)
if endRec:
self.endRecord()
except AttributeError:
pass
if __name__ == '__main__':
rtsp_stream_link = 'rtsp://foo:192.5545....'
video_stream_widget = RTSPVideoWriterObject(rtsp_stream_link)
stop_threads = False
t1 = threading.Thread(target = video_stream_widget.startRecord, args =[stop_threads])
t1.start()
time.sleep(15)
stop_threads = True
As you can see in the main I reading frames and store them in a separate thread. Then I am starting to record (record method is with an infinite loop so blocking) and then after 15 sec, I am trying to pass a 'stop_record' argument to stop recording properly.
A part of the code comes from Storing RTSP stream as video file with OpenCV VideoWriter
Is someone have an idea?
I read a lot that OpenCV can be very tricky for multithreading
N.
Instead of passing arguments to the thread, use a internal flag in the class to determine when to start/stop recording. The trigger could be as simple as pressing the spacebar to start/stop recording. When the spacebar is pressed it will switch an internal variable, say self.record to True to start recording and False to stop recording. Specifically, to check when the spacebar is pressed, you can check if the returned key value from cv2.waitKey() is 32. If you want the trigger based on any other key, take a look at this post to determine the key code. Here's a quick example to start/stop recording a video using the spacebar:
from threading import Thread
import cv2
class RTSPVideoWriterObject(object):
def __init__(self, src=0):
# Create a VideoCapture object
self.capture = cv2.VideoCapture(src)
self.record = True
# Default resolutions of the frame are obtained (system dependent)
self.frame_width = int(self.capture.get(3))
self.frame_height = int(self.capture.get(4))
# Set up codec and output video settings
self.codec = cv2.VideoWriter_fourcc(*'mp4v')
self.output_video = cv2.VideoWriter('output.mp4', self.codec, 30, (self.frame_width, self.frame_height))
# Start the thread to read frames from the video stream
self.thread = Thread(target=self.update, args=())
self.thread.daemon = True
self.thread.start()
def update(self):
# Read the next frame from the stream in a different thread
while True:
if self.capture.isOpened():
(self.status, self.frame) = self.capture.read()
def show_frame(self):
# Display frames in main program
if self.status:
cv2.imshow('frame', self.frame)
if self.record:
self.save_frame()
# Press Q on keyboard to stop recording
key = cv2.waitKey(1)
if key == ord('q'):
self.capture.release()
self.output_video.release()
cv2.destroyAllWindows()
exit(1)
# Press spacebar to start/stop recording
elif key == 32:
if self.record:
self.record = False
print('Stop recording')
else:
self.record = True
print('Start recording')
def save_frame(self):
# Save obtained frame into video output file
self.output_video.write(self.frame)
if __name__ == '__main__':
rtsp_stream_link = 'Your stream link!'
video_stream_widget = RTSPVideoWriterObject(rtsp_stream_link)
while True:
try:
video_stream_widget.show_frame()
except AttributeError:
pass
I solved the problem by creating a global variable inside the class, for your case endRec.
I am trying to save images from three cameras at the same moment using muti(3)-threads and press of a button.
I picked a code online and created three object instance. The threads are running fine and windows are display and can save the images. But i am not sure if i am using the "Locks"(acquire&release) in right sense. In total there are 4 locks.
Current Code:
#!/usr/bin/env python
from threading import Thread, Lock
import cv2
class WebcamVideoStream :
def __init__(self, src = 0, width = 1024, height = 768) :
# width = 320, height = 240
self.stream = cv2.VideoCapture(src)
self.stream.set(cv2.CAP_PROP_FRAME_WIDTH, width)
self.stream.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
(self.grabbed, self.frame) = self.stream.read()
self.started = False
self.read_lock = Lock()
def start(self) :
if self.started :
print "already started!!"
return None
self.started = True
self.thread = Thread(target=self.update, args=())
self.thread.start()
return self
def update(self) :
while self.started :
#if self.stream.grab():
#(grabbed, frame) = self.stream.retrieve()
(grabbed, frame) = self.stream.read()
self.read_lock.acquire()
self.grabbed, self.frame = grabbed, frame
self.read_lock.release()
def read(self) :
self.read_lock.acquire()
frame = self.frame.copy()
self.read_lock.release()
return frame
def stop(self) :
self.started = False
self.thread.join()
def __exit__(self, exc_type, exc_value, traceback) :
self.stream.release()
if __name__ == "__main__" :
mainer_lock = Lock()
vs = WebcamVideoStream(src = 0).start()
vs2 = WebcamVideoStream(src = 1).start()
vs3 = WebcamVideoStream(src = 2).start()
while True :
frame = vs.read()
frame2 = vs2.read()
frame3 = vs3.read()
cv2.imshow('webcam', frame)
cv2.imshow('webcam2', frame2)
cv2.imshow('webcam3', frame3)
if cv2.waitKey(1) == 99:
mainer_lock.acquire()
cv2.imwrite('cam_pics/frame.jpg', frame)
cv2.imwrite('cam_pics/frame2.jpg', frame2)
cv2.imwrite('cam_pics/frame3.jpg', frame3)
mainer_lock.release()
vs.stop()
vs2.stop()
vs3.stop()
cv2.destroyAllWindows()
I think you do not need lock at all.
First, mainer_lock is meaningless. I know you want use it to protect frame[2, 3] from being changed by vs[2, 3]. But it is impossible as the frame[2, 3] in main thread is a copy of original frame(I suppose you are using copy rightly). vs[2, 3] will only change the frame inside their instances, and it won't affect the frame in main thread.
Second, read_lock is also meaningless. Do you really mind that the frame is changed while being read? As you are capturing real frames, the consequent frames are almost the same. So let's say you want capture frame at 1000ms but Oh because of threads competition you actually capture frame at 1002ms. But does it really matter? As far as I think, it doesn't.
Im using the following code to run webcam in background thread. I have to do heavy processing so I have done this hoping that it will improve the fps
import cv2
import time
from threading import Thread
cap = cv2.VideoCapture(0)
threads = []
class WorkerThread(Thread):
def run(self):
print("start")
ret, frame = cap.read()
cv2.imshow('Face', frame)
if __name__ == '__main__':
try:
print("Trying to open camera")
while(cap.isOpened()):
thread = WorkerThread()
thread.start()
threads.append(thread)
time.sleep(0.35)
except KeyboardInterrupt:
for thread in threads:
thread.join()
cap.release()
the issue is that the frame is not visible. how can I make it visible?
This the answer of your problem
import cv2
import time
from threading import Thread
cap = cv2.VideoCapture(0)
threads = []
class WorkerThread(Thread):
def run(self):
print("started")
while True:
ret, frame = cap.read()
cv2.imshow('Face', frame)
k = cv2.waitKey(5) & 0xFF
if k == ord('q'):
break
if __name__ == '__main__':
try:
print("Trying to open camera")
if(cap.isOpened()):
thread = WorkerThread()
thread.start()
threads.append(thread)
time.sleep(0.35)
except KeyboardInterrupt:
for thread in threads:
thread.join()
cap.release()