Flask Opencv Recode mini Video clips - python

I am struggle with generate a mini video clips(Each are 10s video clips) when recording my webcam using open cv and python.
Basically I want to do cut the webcam video into 10S duration clips and store in a folder.
When I doing this video clips cut but when I checked first video clip has 100% of full video.
Second has about 75% of full video and third has about 50% and etc.
So how I can solve this.
I will put my hole code in below. Hope your help to fix that one
camera = cv2.VideoCapture(0)
global rec, img, out
rec = 0
def gen_frames():
global img
while True:
success, img = camera.read()
def record(out):
global rec, img
while(rec != False):
time.sleep(0.05)
out.write(img)
#app.route('/requests',methods=['POST','GET'])
def tasks():
if request.form.get('rec') == 'Start/Stop Recording':
global rec, img, out
rec= not rec
############### This Part work when manualy recode on and off ###############
# if rec:
# print("start")
# global out
# now=datetime.datetime.now()
# fourcc = cv2.VideoWriter_fourcc(*'XVID')
# p = os.path.sep.join(['clips', "vid_{}.avi".format(str(now).replace(":",''))])
# out = cv2.VideoWriter(p, fourcc, 25.0, size)
# thread = Thread(target = record, args=[out,])
# thread.start()
# if(rec==False):
# print("stop")
# out.release()
class TimerClass(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.event = threading.Event()
def run(self):
while (rec != False) and not self.event.is_set():
now=datetime.datetime.now()
fourcc = cv2.VideoWriter_fourcc(*'XVID')
p = os.path.sep.join(['clips', "vid_{}.avi".format(str(now).replace(":",''))])
out = cv2.VideoWriter(p, fourcc, 25.0, size)
thread = Thread(target = record, args=[out,])
thread.start()
self.event.wait(10)
def stop(self):
self.event.set()
tmr = TimerClass()
if(rec):
print("start")
tmr.start()
if(rec==False):
print("stop")
tmr.stop()
elif request.method=='GET':
return render_template('index.html')
return render_template('index.html')
if __name__ == '__main__':
app.run(host='0.0.0.0', threaded=True)

As for me problem is that in loop in TimerClass you create new thread every 10 seconds but you never stop previous thread and it still records frames. I would write frames directly in TimerClass instead of using another thread.
OR thread record() should check time and stop after 10 seconds.
I use timedelta to calculate when to stop recording one file and to create next one. And I do it in TimerClass but you could do something similar in record()
VideoWriter(..., 25.0) doesn't write with 25fps but it is only information for video players how fast to display this video. To get 10 seconds video you need sleep 0.04 because 1s / 25fps = 0.04 or you would have to write 250 frames (10s * 25fps = 250fps).
Because code needs also some time to work I would have to use 0.03 instead of 0.04 to get 10 seconds video.
Full working code.
I use render_template_string instead of render_template to have all in one file - and everyone can simply copy and test it.
from flask import Flask, Response, request, render_template, render_template_string
import cv2
import os
import datetime, time
import threading
# global varaibles
capture = False
rec = False
out = None
img = None
app = Flask(__name__)
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 300
camera = cv2.VideoCapture(0)
frame_width = int(camera.get(3))
frame_height = int(camera.get(4))
size = (frame_width, frame_height)
os.makedirs('./shots', exist_ok=True)
os.makedirs('./clips', exist_ok=True)
def gen_frames():
global capture
global img
print('[DEBUG] gen_frames: start')
while True:
success, img = camera.read()
if not success:
break
if capture:
capture = False
now = datetime.datetime.now()
filename = "shot_{}.png".format(str(now).replace(":",''))
path = os.path.sep.join(['shots', filename])
print('[DEBUG] capture:', path)
cv2.imwrite(path, img)
frame = cv2.imencode('.jpg', img)[1].tobytes()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
#app.route('/video_feed')
def video_feed():
return Response(gen_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')
#app.route('/')
def index():
#return render_template('index.html')
return render_template_string('''
Go to FORM
''')
# define class only once
class TimerClass(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.event = threading.Event()
def run(self):
seconds_10 = datetime.timedelta(seconds=10)
while rec and not self.event.is_set():
now = datetime.datetime.now()
filename = "vid_{}.avi".format(str(now).replace(":", ''))
path = os.path.sep.join(['clips', filename])
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter(path, fourcc, 25.0, size)
end = now + seconds_10
print('[DEBUG] end:', end)
while now < end and rec and not self.event.is_set():
if img is not None: # `img` can be `numpy.array` so it can't check `if img:`
out.write(img)
time.sleep(0.03) # 1s / 25fps = 0.04 # it needs some time for code.
now = datetime.datetime.now()
out.release()
def stop(self):
self.event.set()
#app.route('/requests', methods=['POST', 'GET'])
def tasks():
global capture
global rec
print('[DEBUG] click:', request.form.get('click'))
print('[DEBUG] rec :', request.form.get('rec'))
if request.method == 'POST':
if request.form.get('click') == 'Capture':
capture = True
if request.form.get('rec') == 'Start/Stop Recording':
rec = not rec
tmr = TimerClass()
if rec:
print("start")
tmr.start()
else:
print("stop")
tmr.stop()
#return render_template_string('index.html')
return render_template_string('''
<img src="/video_feed"><br/>
<form method="POST">
<button type="submit" name="click" value="Capture">Capture</button>
<button type="submit" name="rec" value="Start/Stop Recording">Start/Stop Recording</button>
</form>
''')
if __name__ == '__main__':
thread_cam = threading.Thread(target=gen_frames)
thread_cam.start()
app.run(host='0.0.0.0', threaded=True)
EDIT:
The same with thread record()
def record(seconds):
now = datetime.datetime.now()
filename = "vid_{}.avi".format(str(now).replace(":", ''))
path = os.path.sep.join(['clips', filename])
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter(path, fourcc, 25.0, size)
end = now + datetime.timedelta(seconds=seconds)
print('[DEBUG] end:', end)
while now < end and rec:
if img is not None: # `img` can be `numpy.array` so it can't check `if img:`
out.write(img)
time.sleep(0.03) # 1s / 25fps = 0.04 # it needs some time for code.
now = datetime.datetime.now()
out.release()
# define class only once
class TimerClass(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.event = threading.Event()
def run(self):
length = 10
while rec and not self.event.is_set():
t = threading.Thread(target=record, args=(length,))
t.start()
self.event.wait(length)
def stop(self):
self.event.set()

Related

PyQt5 QNetworkReply stops updating of continuous response when in signal handler

I'm trying to continuously get frames from this server:
def get_feed():
while True:
time.sleep(0.06)
buffer = get_shot()
buffer = buffer.tobytes()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n'
b'Content-Length: ' + str(len(buffer)).encode() + '\r\n\r\n' +
buffer + b'\r\n')
#app.route('/', methods=['GET'])
def index():
return Response(get_feed(), mimetype='multipart/x-mixed-replace; boundary=frame')
For that I use this code:
class ExampleApp(QtWidgets.QMainWindow, test.Ui_MainWindow):
def __init__(self, parent=None):
super(ExampleApp, self).__init__(parent)
self.setupUi(self)
self._source_url = QUrl("*link_here*")
self._image_request = QNetworkRequest(self._source_url)
self._network_manager = QNetworkAccessManager()
self._image_reply = self._network_manager.get(self._image_request)
...
self.frameTimer = QTimer(self)
self.timeBetweenFrames = 1
self.timeBetweenReads = 0.06
self.frameTimer.timeout.connect(self.updateFrame)
self.frameTimer.start(self.timeBetweenFrames)
...
def updateFrame(self):
# Get how many bytes do I need to read for the frame
content_length = 0
buff = self._image_reply.readAll()
frameSizeAddress = buff.lastIndexOf(b'Content-Length:') + len(b'Content-Length: ')
frameSizeEndAddress = buff[frameSizeAddress:].indexOf(b'\r\n')
if frameSizeAddress == -1 or frameSizeEndAddress == -1:
self.frameTimer.start(self.timeBetweenFrames)
return
frameSize = int(str(buff[frameSizeAddress:frameSizeAddress+frameSizeEndAddress], 'utf-8'))
frameBegin = frameSizeAddress + frameSizeEndAddress + 4
imageBuffer = buff[frameBegin:frameBegin+frameSize]
readenBytes = len(imageBuffer)
# If the image came partially
while(readenBytes < frameSize):
time.sleep(self.timeBetweenReads)
buff = self._image_reply.read(frameSize-readenBytes)
imageBuffer += buff[:frameSize-readenBytes]
readenBytes = len(imageBuffer)
print(len(buff))
And here comes the problem. If the image came partially it will never be read. Last print always give 0.
If use read(1024) instead of readAll() during first read here
content_length = 0
buff = self._image_reply.readAll()
Then everything works but if frames come faster then the app reads them: they begin stacking and delay between feed and display-image becomes bigger and bigger.
For me it looks like QNetworkReply doesn't update when it is in signal handler, so server do not write in it new bytes.
How can I solve this?

Python problem with flask and opencv capture

I want to get the signal from a webcam, or csi cam on my jetson nano, display it on a web browser and push some buttons to make a snapshot and depending of the button place the picture in different folders.
I made following code, which it seems to work, but after few button actions, the browser starts to load indefinitely. After a while, the web browser does not load indefinitely. It loads until I press one of the button. And in this case, I'm not more able to see the live signal from the camera, I just see the snapshot taken when I pressed the button.
from flask import Flask, render_template, Response, request
import cv2
import datetime, time
import os, sys
import numpy as np
from threading import Thread
global rec_frame, Polluted, Clear, Treated, OutOfService
Polluted=0
Clear=0
Treated=0
OutOfService=0
#instatiate flask app
app = Flask(__name__, template_folder='./templates')
#select webcam or CSI
#camera = cv2.VideoCapture("nvarguscamerasrc ! video/x-raw(memory:NVMM), width=(int)1280, height=(int)960,format=(string)NV12, framerate=(fraction)20/1 ! nvvidconv flip-method=0 ! video/x-raw,format=(string)BGRx ! videoconvert ! video/x-raw,width=(int)1280, height=(int)960, format=(string)BGR ! appsink"
#, cv2.CAP_GSTREAMER)
camera = cv2.VideoCapture(1)
def snapshot(frame,folder):
cropped = frame[100:400,200:500]
resized = cv2.resize(cropped,(100,100))
cv2.imwrite("/home/ava/Documents/AVA/Get pictures/" + folder + "/frame-" + time.strftime("%d-%m-%Y-%H-%M-%S") + ".jpg",cv2.cvtColor(resized,cv2.COLOR_RGB2BGR))
def gen_frames(): # generate frame by frame from camera
global rec_frame,Polluted,Clear,Treated,OutOfService
while True:
success,frame = camera.read()
if success:
#get snapshot if button pressed
if(Polluted):
snapshot(frame,"Polluted Water")
Polluted = 0
if(Clear):
snapshot(frame,"Cleared Water")
Clear = 0
if(Treated):
snapshot(frame,"Treated Water")
Treated = 0
if(OutOfService):
snapshot(frame,"Out of Service")
OutOfService = 0
try:
ret, buffer = cv2.imencode('.jpg', cv2.flip(frame,1))
frame = buffer.tobytes()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
except Exception as e:
print('pass')
pass
else:
pass
#app.route('/')
def index():
return render_template('index.html')
#app.route('/video_feed')
def video_feed():
return Response(gen_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')
#app.route('/requests',methods=['POST','GET'])
def tasks():
global switch,camera
if request.method == 'POST':
if request.form.get('Polluted') == 'Polluted':
global Polluted,rec_frame
Polluted=1
print('in polluted')
elif request.form.get('Clear') == 'Clear':
global Clear
Clear = 1
elif request.form.get('Treated') == 'Treated':
global Treated
Treated = 1
elif request.form.get('OutOfService') == 'OutOfService':
global OutOfService
OutOfService = 1
elif request.method=='GET':
return render_template('index.html')
return render_template('index.html')
if __name__ == '__main__':
app.run()
camera.release()
cv2.destroyAllWindows()

OpenCV module not repeating loop in Raspberry Pi 4. Working fine on windows

Let me start by saying that I am very new to Python and Raspberry Pi.
I've "made"(more like copied from a lot of diff. sources and compiled) a module on windows to capture images from a web cam on key press and save it in a folder(code attached). It is working fine on windows and repeats the loop but throws an error on Raspberry Pi after the first loop.
Code for windows:-
# Import Modules #######################################################################################################
from datetime import datetime
import cv2
import time
import queue
import threading
# Module Level Variables ###############################################################################################
inpath = "D:\\Python Projects\\OCR Trial2\\Input\\Training Data\\"
outpath = "D:\\Python Projects\\OCR Trial2\\Output\\"
intpath = "D:\\Python Projects\\OCR Trial2\\Intermediate\\"
file_Prefix = 'IMG100'
file_Extension = '.png'
# Class Definitions ####################################################################################################
class VideoCapture:
def __init__(self, name):
self.cap = cv2.VideoCapture(name)
self.q = queue.Queue()
t = threading.Thread(target=self._reader)
t.daemon = True
t.start()
def _reader(self):
while True:
ret, frame = self.cap.read()
if not ret:
break
if not self.q.empty():
try:
self.q.get_nowait()
except queue.Empty:
pass
self.q.put(frame)
def read(self):
return self.q.get()
# Functions ############################################################################################################
def main():
while True:
try:
windowName = "Live Video Feed"
cv2.namedWindow(windowName)
if cv2.waitKey(1) == ord("c"):
time.sleep(1)
now = datetime.now()
formatted_time = now.strftime('%Y-%m-%d %H-%M-%S.%f')[:-3]
cam = VideoCapture(0 + cv2.CAP_DSHOW)
frame1 = cam.read()
cv2.imshow(windowName,frame1)
cv2.imwrite(intpath + file_Prefix + formatted_time + file_Extension, frame1)
print(formatted_time)
else:
continue
except:
pass
# Execute Code #########################################################################################################
if __name__ == "__main__":
main()
Output for Windows:-
2021-01-06 17-20-05.255
2021-01-06 17-20-07.404
2021-01-06 17-20-08.601
2021-01-06 17-20-10.766
2021-01-06 17-20-12.408
Process finished with exit code -1
Code for Raspberry Pi:-
# Import Modules #######################################################################################################
from datetime import datetime
import cv2
import time
import queue
import threading
# Module Level Variables ###############################################################################################
intpath = "/home/pi/Python Images/"
file_Prefix = 'IMG100'
file_Extension = '.png'
# Class Definitions ####################################################################################################
class VideoCapture:
def __init__(self, name):
self.cap = cv2.VideoCapture(name)
self.q = queue.Queue()
t = threading.Thread(target=self._reader)
t.daemon = True
t.start()
def _reader(self):
while True:
ret, frame = self.cap.read()
if not ret:
break
if not self.q.empty():
try:
self.q.get_nowait()
except queue.Empty:
pass
self.q.put(frame)
def read(self):
return self.q.get()
# Functions ############################################################################################################
def main():
while True:
try:
windowName = "Live Video Feed"
cv2.namedWindow(windowName)
if cv2.waitKey(1) == ord("c"):
time.sleep(1)
now = datetime.now()
formatted_time = now.strftime('%Y-%m-%d %H-%M-%S.%f')[:-3]
cam = VideoCapture(0)
frame1 = cam.read()
cv2.imshow(windowName,frame1)
cv2.imwrite(intpath + file_Prefix + formatted_time + file_Extension, frame1)
print(formatted_time)
else:
continue
except:
pass
# Execute Code #########################################################################################################
if __name__ == "__main__":
main()
Output for Raspberry Pi :-
2021-01-06 17-07-59.501
[ WARN:4] global /tmp/pip-wheel-qd18ncao/opencv-python/opencv/modules/videoio/src/cap_v4l.cpp (893) open VIDEOIO(V4L2:/dev/video0): can't open camera by index
Process finished with exit code 137 (interrupted by signal 9: SIGKILL)
Open CV module on Raspberry Pi was installed by PIP and not manually compiled. General OpenCV functions like Video capture and imshow work fine on Raspberry Pi and it captures the first photo successfully but cannot capture the second one.
Please suggest what could be the problem, what can I try next.
Edit 1 - Added this is the whole error after printing the exception:-
/home/pi/PycharmProjects/pythonProject/venv/bin/python "/home/pi/PycharmProjects/pythonProject/Image Capture.py"
2021-01-07 15-07-36.555
[ WARN:4] global /tmp/pip-wheel-qd18ncao/opencv-python/opencv/modules/videoio/src/cap_v4l.cpp (893) open VIDEOIO(V4L2:/dev/video0): can't open camera by index
Traceback (most recent call last):
File "/home/pi/PycharmProjects/pythonProject/Image Capture.py", line 72, in <module>
main()
File "/home/pi/PycharmProjects/pythonProject/Image Capture.py", line 59, in main
frame1 = cam.read()
File "/home/pi/PycharmProjects/pythonProject/Image Capture.py", line 42, in read
return self.q.get()
File "/usr/lib/python3.7/queue.py", line 170, in get
self.not_empty.wait()
File "/usr/lib/python3.7/threading.py", line 296, in wait
waiter.acquire()
KeyboardInterrupt
Process finished with exit code 1
Your mistake can be cam = VideoCapture(0) inside loop.
You should create it only once - before loop.
If you try to use it second time (for example in loop) then system can't access it before it still use previous cam = VideoCapture(0).

stop a thread from within another thread in python

I have a function that replay some steps from a .json file and another fuction which record those steps. I need the recordingScreen function to stop once the playActions function finishes simultaneously (by using the flag) and create a video for each iteration, but it only creates the video for the last file (iteration)
I have tried with a flag that changes from false when the playActions function finishes I have also tried with queue from this example link and using using a threadsafe threading.Event() from this example link. But as I am a beginner I have not been able to implement any of them correctly within my code, which is as follow:
files= ["actions_test_10-07-2020_15-56-43.json", "actions_test_10-08-2020_14-59-00.json"]
date = datetime.today().strftime("%m-%d-%Y_%H-%M-%S")
Stop_recording = False
def main():
initializePyAutoGUI()
countdownTimer()
for i in range(len(files)):
global Stop_recording
Stop_recording = False
t1 = threading.Thread(target=playActions, args=[files[i]])
t2 = threading.Thread(target=recordScreen)
t1.start()
t2.start()
t1.join()
t2.join()
print("Done")
def recordScreen():
output = '{}.avi'.format(date)
img = pyautogui.screenshot()
img = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
# get info from img
height, width, channels = img.shape
# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(output, fourcc, 20.0, (width, height))
while not Stop_recording:
img = pyautogui.screenshot()
image = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
out.write(image)
StopIteration(0.5)
out.release()
cv2.destroyAllWindows()
def playActions(filename):
# Read the file
script_dir = os.path.dirname(__file__)
filepath = os.path.join(script_dir, 'recordings', filename)
with open(filepath, 'r') as jsonfile:
# parse the json
data = json.load(jsonfile)
# loop over each action
# Because we are not waiting any time before executing the first action, any delay before the initial
# action is recorded will not be reflected in the playback.
for index, action in enumerate(data):
action_start_time = time()
# look for escape input to exit
if action['button'] == 'Key.esc':
break
# perform the action
if action['type'] == 'keyDown':
key = convertKey(action['button'])
pyautogui.keyDown(key)
print("keyDown on {}".format(key))
elif action['type'] == 'keyUp':
key = convertKey(action['button'])
pyautogui.keyUp(key)
print("keyUp on {}".format(key))
elif action['type'] == 'click' and action['button'] == "Button.right":
pyautogui.rightClick(action['pos'][0], action['pos'][1], duration=0.25)
print("right click on {}".format(action['pos']))
elif action['type'] == 'click' and action['button'] == "Button.left":
# Check if the period between clicks is short and perform a double click then, otherwise
# it performs a single click
if index > 0:
if (data[index]['time']) - (data[index - 1]['time']) < 0.5:
pyautogui.doubleClick(action['pos'][0], action['pos'][1])
print("Double click on {}".format(action['pos']))
pyautogui.leftClick(action['pos'][0], action['pos'][1], duration=0.25)
print("left click on {}".format(action['pos']))
# then sleep until next action should occur
try:
next_action = data[index + 1]
except IndexError:
# this was the last action in the list
break
elapsed_time = next_action['time'] - action['time']
# if elapsed_time is negative, that means our actions are not ordered correctly. throw an error
if elapsed_time < 0:
raise Exception('Unexpected action ordering.')
# adjust elapsed_time to account for our code taking time to run
elapsed_time -= (time() - action_start_time)
if elapsed_time < 0:
elapsed_time = 0
print('sleeping for {}'.format(elapsed_time))
sleep(elapsed_time)
global Stop_recording
Stop_recording = True

Trying to display multiple streams with Opencv and Flask

I'm trying to capture two rtsp streams with opencv and then present them with a simple flask server. I can show the two streams together when just using opencv, but when I try to display it through flask it just picks either stream and shows it twice.
Here's the original creators blog
Here is my flask code:
#!/usr/bin/env python
from importlib import import_module
import os
from flask import Flask, render_template, Response
# import camera driver
'''
if os.environ.get('CAMERA'):
Camera = import_module('camera_' + os.environ['CAMERA']).Camera
else:
from camera import Camera
'''
#
from camera_opencv import Camera1, Camera2
app = Flask(__name__)
#app.route('/')
def index():
"""Video streaming home page."""
return render_template('index.html')
def gen(camera):
"""Video streaming generator function."""
while True:
frame = camera.get_frame()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
#app.route('/video_feed1')
def video_feed1():
"""Video streaming route. Put this in the src attribute of an img tag."""
return Response(gen(Camera1()),
mimetype='multipart/x-mixed-replace; boundary=frame')
#app.route('/video_feed2')
def video_feed2():
"""Video streaming route. Put this in the src attribute of an img tag."""
return Response(gen(Camera2()),
mimetype='multipart/x-mixed-replace; boundary=frame')
if __name__ == '__main__':
app.run(host='0.0.0.0', threaded=True, port=8888)
Here's the camera_opencv file
import cv2
from base_camera import BaseCamera
class Camera1(BaseCamera):
video_source = 0
#staticmethod
def set_video_source(source):
Camera1.video_source = source
#staticmethod
def frames():
camera = cv2.VideoCapture(Camera1.video_source)
if not camera.isOpened():
raise RuntimeError('Could not start camera.')
while True:
# read current frame
_, img = camera.read()
# encode as a jpeg image and return it
yield cv2.imencode('.jpg', img)[1].tobytes()
class Camera2(BaseCamera):
video_source = 1
#staticmethod
def set_video_source(source):
Camera2.video_source = source
#staticmethod
def frames():
camera = cv2.VideoCapture(Camera2.video_source)
if not camera.isOpened():
raise RuntimeError('Could not start camera.')
while True:
# read current frame
_, img = camera.read()
# encode as a jpeg image and return it
yield cv2.imencode('.jpg', img)[1].tobytes()
Base camera file
import time
import threading
try:
from greenlet import getcurrent as get_ident
except ImportError:
try:
from thread import get_ident
except ImportError:
from _thread import get_ident
class CameraEvent(object):
"""An Event-like class that signals all active clients when a new frame is
available.
"""
def __init__(self):
self.events = {}
def wait(self):
"""Invoked from each client's thread to wait for the next frame."""
ident = get_ident()
if ident not in self.events:
# this is a new client
# add an entry for it in the self.events dict
# each entry has two elements, a threading.Event() and a timestamp
self.events[ident] = [threading.Event(), time.time()]
return self.events[ident][0].wait()
def set(self):
"""Invoked by the camera thread when a new frame is available."""
now = time.time()
remove = None
for ident, event in self.events.items():
if not event[0].isSet():
# if this client's event is not set, then set it
# also update the last set timestamp to now
event[0].set()
event[1] = now
else:
# if the client's event is already set, it means the client
# did not process a previous frame
# if the event stays set for more than 5 seconds, then assume
# the client is gone and remove it
if now - event[1] > 5:
remove = ident
if remove:
del self.events[remove]
def clear(self):
"""Invoked from each client's thread after a frame was processed."""
self.events[get_ident()][0].clear()
class BaseCamera(object):
thread = None # background thread that reads frames from camera
frame = None # current frame is stored here by background thread
last_access = 0 # time of last client access to the camera
event = CameraEvent()
def __init__(self):
"""Start the background camera thread if it isn't running yet."""
if BaseCamera.thread is None:
BaseCamera.last_access = time.time()
# start background frame thread
BaseCamera.thread = threading.Thread(target=self._thread)
BaseCamera.thread.start()
# wait until frames are available
while self.get_frame() is None:
time.sleep(0)
def get_frame(self):
"""Return the current camera frame."""
BaseCamera.last_access = time.time()
# wait for a signal from the camera thread
BaseCamera.event.wait()
BaseCamera.event.clear()
return BaseCamera.frame
#staticmethod
def frames():
""""Generator that returns frames from the camera."""
raise RuntimeError('Must be implemented by subclasses.')
#classmethod
def _thread(cls):
"""Camera background thread."""
print('Starting camera thread.')
frames_iterator = cls.frames()
for frame in frames_iterator:
BaseCamera.frame = frame
BaseCamera.event.set() # send signal to clients
time.sleep(0)
# if there hasn't been any clients asking for frames in
# the last 10 seconds then stop the thread
if time.time() - BaseCamera.last_access > 10:
frames_iterator.close()
print('Stopping camera thread due to inactivity.')
break
BaseCamera.thread = None
Index.html
<html>
<head>
<title>Video Streaming Demonstration</title>
</head>
<body>
<h1>Video Streaming Demonstration</h1>
<img src="{{ url_for('video_feed1') }}">
<img src="{{ url_for('video_feed2') }}">
</body>
</html>
So I kind of managed to make a hackey workaround. For whatever reason I could not resolve, the app just couldn't handle multiple streams individually.
So I changed the camera class and added multiple sources to it and used numpy.hstack(()) to merge both the frames together thus returning one unique stream.
Will be very grateful if someone could help out here as my method is not at all scalable.
import cv2
from base_camera import BaseCamera
import numpy as np
class Camera(BaseCamera):
video_source1 = 0
video_source2 = 1
#staticmethod
def set_video_source(sources):
Camera.video_source1 = sources[0]
Camera.video_source2 = sources[1]
#staticmethod
def frames():
camera1 = cv2.VideoCapture(Camera.video_source1)
camera2 = cv2.VideoCapture(Camera.video_source2)
if not (camera1.isOpened() or camera2.isOpened()):
raise RuntimeError('Could not start camera.')
while True:
# read current frame
_, img1 = camera1.read()
_, img2 = camera2.read()
img1 = cv2.resize(img1, (704, 396))
img2 = cv2.resize(img2, (704, 396))
img = np.hstack((img1, img2))
# encode as a jpeg image and return it
yield cv2.imencode('.jpg', img)[1].tobytes()
According to the blog, I use two generator and send the image to the index.html. And I can see tow streaming.
def generate2():
# it is a generator
global outputFrame2, lock
while True:
with lock:
if outputFrame is None:
continue
(flag, encodedImage2) = cv2.imencode(".jpg", outputFrame2)
if not flag:
continue
yield(b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' +
bytearray(encodedImage2) + b'\r\n')
And here is videofeed2
#app.route("/video_feed2")
def video_feed2():
return Response(generate2(),
mimetype = "multipart/x-mixed-replace; boundary=frame")

Categories

Resources