I am trying to record Set top box signal using videorecording feature of opencv in python.
I am using capture device to capture the set top box signal whose FPS is 30.
Video Recording from External capture device is successful using opencv.
Here is the code for video recording
#VideoRecord.py
CaptureDeviceID = 1
cap = VideoCapture(CaptureDeviceID)
if cap.isOpened():
cap.set(CV_CAP_PROP_FRAME_WIDTH, 640)
cap.set(CV_CAP_PROP_FRAME_HEIGHT, 480)
else:
exit()
fourcc = cv2.cv.CV_FOURCC()
out = cv2.VideoWriter('output.avi',fourcc, 30.0, (640,480))
while(cap.isOpened()):
frameNo = 0
ret, frame = cap.read()
if ret==True:
frameNo++
# write the frame
out.write(frame)
if (frameNo > 150):
break
else:
break
# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()
Now through Script I am changing channel using IRBlaster command.
This channel change should come in recorded video.
code for sending channel+ command
# sendCommand.py
redrat_path = r'C:\Program Files\RedRat\RedRat Control\RR_CLI_V3.01\RR_CLI.exe
redrat_process=subprocess.Popen(\"%s %s\" % (redrat_path,\"RedRat-0 -output RedRatDB.xml -device STB1 -signal Channel+ "))
In words I want to record video and send command in parallel process.So that the created video file has this
channel change event recorded.
How should I achieve this effectively so that I will not miss any frame while I am sending channel change command?.
Do i have to use threading?
Please suggest some ideas.I want to use Python
Related
I am trying to capture rtsp stream with opencv but it complains about the cv2.COLOR_BGR2RGB.
I have done several tries with streaming a different web-camera which working fine but not with the rtsp. The rtsp stream it self is working and tested fine with vlc. The rtsp stream it self is h264 format.
But, if I remark in the lines with the if statement it false, because the frame is empty!
import os
import cv2
import time
import argparse
import multiprocessing
import numpy as np
import tensorflow as tf
from utils.app_utils import FPS, WebcamVideoStream, HLSVideoStream
from multiprocessing import Queue, Pool
CWD_PATH = os.getcwd()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-str', '--stream', dest="stream", action='store', type=str, default=None)
parser.add_argument('-src', '--source', dest='video_source', type=int,
default=0, help='Device index of the camera.')
parser.add_argument('-wd', '--width', dest='width', type=int,
default=640, help='Width of the frames in the video stream.')
parser.add_argument('-ht', '--height', dest='height', type=int,
default=480, help='Height of the frames in the video stream.')
parser.add_argument('-num-w', '--num-workers', dest='num_workers', type=int,
default=2, help='Number of workers.')
parser.add_argument('-q-size', '--queue-size', dest='queue_size', type=int,
default=5, help='Size of the queue.')
args = parser.parse_args()
logger = multiprocessing.log_to_stderr()
logger.setLevel(multiprocessing.SUBDEBUG)
input_q = Queue(maxsize=args.queue_size)
output_q = Queue(maxsize=args.queue_size)
print('Reading from webcam.')
#video_capture = WebcamVideoStream(src="rtsp://ip:port/stream",
# width=args.width,
# height=args.height).start()
frame_width = args.width
frame_height = args.height
print('Width:', frame_width, 'Height:', frame_height)
out = cv2.VideoWriter('output_test.avi', cv2.VideoWriter_fourcc('m','p','4','v'), 10, (frame_width,frame_height))
vcap = cv2.VideoCapture("rtsp://ip:port/stream", cv2.CAP_FFMPEG)
while True:
ret, frame = vcap.read()
#if ret == False:
# break
# Our operations on the frame come here
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
output_q.put(frame_rgb)
# Convert and store to out file
#output_rgb = cv2.cvtColor(output_q.get(), cv2.COLOR_RGB2BGR)
#out.write(output_rgb)
#cv2.imshow('VIDEO', output_rgb)
# Display the resulting frame
cv2.imshow('frame', frame_rgb)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
#video_capture.stop()
out.release()
cv2.destroyAllWindows()
Which gives me an issue (without if statement):
[DEBUG/MainProcess] Queue._after_fork()
Reading from webcam.
Width: 640 Height: 480
Traceback (most recent call last):
File "testRTSP_try.py", line 52, in <module>
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
cv2.error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
[INFO/MainProcess] process shutting down
Some more information:
Camera footage is from raspberry pi
RTSP stream created via:
raspivid -o - -t 0 -rot 180 -w 640 -h 480 -fps 25 -b 2000000 | cvlc -vvv stream:///dev/stdin --sout '#rtp{sdp=rtsp://:<port>/stream}' :demux=h264
RTSP stream monitored via windows vlc
I took a different approach by using motion instead see How to Make Raspberry Pi Webcam Server and Stream Live Video || Motion + Webcam + Raspberry Pi
This will setup a http stream instead of a rtsp!
I am running the below code to convert video into frames. Problem is it is creating Image files with 0 KB size and when I open it is not showing anything.. I don't understand what is creating the problem. Do I need to install any Image codecs?
'''
Using OpenCV takes a mp4 video and produces a number of images. I am using OpenCV 3.3.0 version and Python 2.7
Which will produce a folder called data with the images, There will be 2000+ images for example.mp4.
'''
import cv2
import numpy as np
import os
# Playing video from file:
try:
cap = cv2.VideoCapture('aa.mkv')
except:
print "Could not open video file"
raise
print cap.grab()
try:
if not os.path.exists('data'):
os.makedirs('data')
except OSError:
print ('Error: Creating directory of data')
currentFrame = 0
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
if not frame is None:
# Saves image of the current frame in jpg file
name = './data/frame' + str(currentFrame) + '.jpg'
print ('Creating...' + name)
cv2.imwrite(name, frame)
#cv2.imshow(name, frame)
else:
break
# To stop duplicate images
currentFrame += 1
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
You are not using imwrite function to write frames. Also your imshow function name is misspelled. I have made changes in your code. Try this:
import cv2
import numpy as np
import os
# Playing video from file:
cap = cv2.VideoCapture('aa.mkv')
try:
if not os.path.exists('data'):
os.makedirs('data')
except OSError:
print ('Error: Creating directory of data')
currentFrame = 0
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
if not frame is None:
# Saves image of the current frame in jpg file
name = './data/frame' + str(currentFrame) + '.jpg'
print ('Creating...' + name)
cv2.imwrite(name, frame)
cv2.imshow(name, frame)
else:
break
# To stop duplicate images
currentFrame += 1
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
I know, there is several threads already on this topic, my situation seems not to be solved yet though. I just can't make my programs take images with my webcam (B910 by Logitech) on a laptop.
I am running a program on a Ubuntu 16.04, that is working on other machines with the same webcam I am using. For the sake of easiness, here a minimum version:
import cv2
device = -1
def show_webcam(mirror=False):
cam = cv2.VideoCapture(device)
print cam.isOpened(), cam.read()
while True:
ret_val, img = cam.read()
if mirror:
img = cv2.flip(img, 1)
cv2.imshow('my webcam', img)
if cv2.waitKey(1) == 27:
break # esc to quit
cv2.destroyAllWindows()
def main():
show_webcam(mirror=True)
if __name__ == '__main__':
main()
The print command yields: False, (False, None)
Running cheese from command line, shows webcam stream as it should, also the build in camera is working.
So I tried -1, 0, 1, 2 and other values for device and nothing works. I put a time.sleep(2) after cam =..., also without result. I don't find much more on this, can anyone help? Thanks!
Note:
cv2.__file__ is 'usr/local/lib/python2.7/dist-packages/cv2/cv2.so'
cv2.__version__ is '3.2.0'
cv2.getBuildInformation() is a little long for here, might there be some important information?
Delete devices and change the code to cam = cv2.VideoCapture(0), and see if it works?
Normally if your camera is working with cheese, the driver programme should be okay.
The cv2.videocapture(0) is for the device default.
Do you have another camera device with your computer in the moment?
I am trying to save video using videocapture object in openCv using python. But after pressing 'q' the video is saved as 'output.avi' but its size is showing as 0 KB. Need your help, not able to find out the error.
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
#1.2. Gui Features in OpenCV 25
#OpenCV-Python Tutorials Documentation, Release 1
# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
while(cap.isOpened()):
ret, frame = cap.read()
if ret==True:
frame = cv2.flip(frame,0)
# write the flipped frame
out.write(frame)
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()
Here you need to play with fourCC code
FourCC code is passed as cv2.VideoWriter_fourcc('M','J','P','G') or cv2.VideoWriter_fourcc(*'MJPG) for MJPG.
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
# Define the codec and create VideoWriter object
#fourcc = cv2.cv.CV_FOURCC(*'DIVX')
#out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
out = cv2.VideoWriter('output.avi', -1, 20.0, (640,480))
while(cap.isOpened()):
ret, frame = cap.read()
if ret==True:
frame = cv2.flip(frame,0)
# write the flipped frame
out.write(frame)
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()
For more read, OpenCV saving doc and FourCC doc
It maybe that the codecs for XVID is not properly installed.
(If you are using Jupyter Notebook, there would be jupyter logs begin with OpenCV: FFMPEG: ...)
If you are on linux, you could try to use the cv2.VideoWriter_fourcc(*'mp4v') as below:
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter('output.mp4', -1, 20.0, (640,480))
Notice, the extension mp4 matters! Should choose the right extension matching the codec.
I'm trying to save video files on a raspberry pi with python2.7 and opencv. The code shown below consistently saves several video files (size 16-18 Mb) to a usb but after the first few files the file sizes drop to 6 kb and appear to be empty since they won't open.
I opened task manager to monitor the memory usage by python during the saving and noticed that the RSS memory continually increases until roughly 200 MB which is when the video files start showing up blank.
Is that a sure indicator of a memory leak, or should I run other tests?
Is there something wrong in the below code that isn't releasing variables properly?
import cv2
import numpy as np
import datetime
dispWidth = 640
dispHeight = 480
FPS = 6
SetupNewVideoFile = True # state variable
VidCaptureDurationMinutes = 3
filepath = '/media/pi/9CEE-5383/Videos/'
i = 1 # counter for the video file names
fourcc = cv2.cv.CV_FOURCC('X','V','I','D')
while True:
# timer section that ends running file saves and triggers a new file save
Now = datetime.datetime.now() # refresh current time
delta = Now - VidCaptureStartTime
print('delta: ',delta.seconds,delta.days)
if ((delta.seconds/60) >= VidCaptureDurationMinutes) and delta.days >= 0:
print('delta: ',delta.seconds,delta.days)
SetupNewVideoFile = True
Vidoutput.release()
cap.release()
# setting up new file saves
if SetupNewVideoFile:
SetupNewVideoFile = False
title = "Video_"+str(i)+".avi"
i += 1
fullpath = filepath + title
print(fullpath)
Vidoutput = cv2.VideoWriter(fullpath, fourcc, FPS,(dispWidth,dispHeight))
VidCaptureStartTime = datetime.datetime.now() # updating video start time
cap = cv2.VideoCapture(-1) # start video capture
ret, frame = cap.read()
if ret: # display and save if a frame was successfully read
cv2.imshow('webcam',frame)
Vidoutput.write(frame) # save the frames
key = cv2.waitKey(1) & 0xFF
if key == ord('q'): # quits program
break
# clean up
cap.release()
Vidoutput.release()
cv2.destroyAllWindows()
cv2.waitKey(1) # these seem to be needed to flush the cv actions
cv2.waitKey(1)
cv2.waitKey(1)
cv2.waitKey(1)
After some more trouble shooting and help from the pympler module functions and tutorials (https://pythonhosted.org/Pympler/muppy.html) my problems still looked like a memory leak but I was unable to solve the specific error.
This other S.O. post (Releasing memory in Python) mentioned improvements in memory management made in version 3.3 of Python:
"In Python 3.3 the small object allocator was switched to using anonymous memory maps instead of the heap, so it should perform better at releasing memory."
So I switched over to Python 3.3 and now the code saves valid video files well past the error out time I saw previously.
This isn't an answer as to why the blank files were occurring, but at least it's a solution.