OpenCV VideoWriter silently stops after a time - python

After writing videos successfully for awhile, VideoWriter will silently start creating only empty video files.
This is on a Raspberry PI 3
OpenCV 2.4.9
The following script illustrates the issue:
Here's a standalone script that illustrates the issue:
#!/usr/bin/pyton
import cv2
import time
import numpy
import subprocess
fourcc = cv2.cv.CV_FOURCC(*'MJPG')
allret = 0
f = numpy.ones((480,640,3), numpy.uint8)
h,w,c = f.shape
print h,w,c
counter = 0
while True:
counter += 1
print "Iteration: ", counter
time.sleep(0.1)
writer = cv2.VideoWriter("test.avi", fourcc, 5, (w,h))
for i in xrange(20):
writer.write(f)
writer.release()
writer = None
ret = subprocess.call(["avprobe","test.avi"])
allret += ret
print "FAILURES:", allret
if allret > 5:
break
After about 800 or so successful videos we then only get bad videos. Restarting the scripts starts the process over.

Related

How to run two python3 file with both file using the same Raspberry Pi Camera

I have two python3 file
#1 Facemask recognition (Deep Learning)
#2 QR Code scanner (Machine Learning)
Im using Raspberry Pi 3 B+ & 8 MP single Pi Cam
Both program does not have any connection, however I need to run both at the same time to detect someone who mask or not AND to scan QR code of user
The problem is I found conflict on pi camera because I only use one pi camera for two different program. Plus, facemask using videostream while QR Scan using videocaptures.
Hope someone can help me on pi camera conflict with two python3 file that using it.
Below is my code:
#1 Facemask Recognition based on Github
https://github.com/manish-1305/facemask_detection/blob/73f37f724b519731eec7d46cb4a23482147db24b/detect.py
#2 QR Code Scanner
import cv2
import re
from time import time
import datetime
import board
cap = cv2.VideoCapture(0)
detector = cv2.QRCodeDetector()
def sw1Pressed():
global sw1Press
sw1Press = True
sw1.when_pressed = sw1Pressed
sw1Press = False
print("Press SW1 to scan.")
while True:
if sw1Press == True:
led.toggle()
_, img = cap.read()
data, bbox, _ = detector.detectAndDecode(img)
if bbox is not None:
for i in range(len(bbox)):
cv2.line(img, tuple(bbox[i][0]), tuple(bbox[(i+1) % len(bbox)][0]), color=(255,
0, 0), thickness=2)
cv2.putText(img, data, (int(bbox[0][0][0]), int(bbox[0][0][1]) - 10), cv2.FONT_HERSHEY_SIMPLEX,
0.5, (0, 255, 0), 2)
if data:
sw1Press = False
data = data.split(",")
print("ID: " + data[0])
print("NAME: " + data[1])
print()
userScanned = False
with open('XXX.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
if row["ID"] == data[0]:
buzzer.beep(0.1, 0.1, 1)
userScanned = True
if userScanned == False:
buzzer.beep(0.1, 0.1, 2)
with open('XXX.csv', 'a') as csvfile:
fieldNames = ['ID', 'NAME']
writer = csv.DictWriter(csvfile, fieldnames=fieldNames)
writer.writerow({'ID': data[0], 'NAME': data[1]})
currentTime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
print("Date & Time: {}".format(currentTime))
print()
data = {
'ID' : data[0],
'NAME' : data[1],
'TIME' : currentTime,
}
cv2.imshow("code detector", img)
else:
cap.read()
cv2.destroyAllWindows()
if cv2.waitKey(1) == ord("q"):
break
led.off()
cap.release()
cv2.destroyAllWindows()
Run one program (or thread) that reads the camera continuously and stores the picture somewhere the two clients (mask and QR) can read it.
In practical terms that could be:
one program with 3 threads, one thread reading from camera into shared Numpy array, and two threads reading from that shared array, or
three programs, one reading images into a Python v3.8 multiprocessing shared memory and two programs attaching to that shared memory to read frames
three programs, perhaps on different machines, one reading frames from the camera and throwing them into Redis and the other two reading frames out of Redis

Error trying to crop frames of a video in OpenCV python

I been trying working in a program that crop some parts of a frame in a video with opencv.
the first program works, but when i try to resize the cropped images at some point crash giving me this error:
cv2.error: OpenCV(4.5.2) /tmp/pip-req-build-eirhwqtr/opencv/modules/imgcodecs/src/loadsave.cpp:721: error: (-215:Assertion failed) !_img.empty() in function 'imwrite'
i read that i could be an error of windows, but i try it in Ubuntu and its was the same result, the program pick the data from a csv that saves [frame, x, y]
All its in the main(path, lent) function!
import os
import cv2
import csv
video_path = ''
rl = 0
label = 1
def main(path, lent):
global video_path
global rl
global label
rl = lent
frames = []
coord = []
with open(path, "r") as data:
reader = csv.reader(data, delimiter=',')
for idx, row in enumerate(reader):
if idx == 0:
video_path = row[0]
else:
frames.append(float(row[0]))
coord.append((int(row[1]), int(row[2])))
video = cv2.VideoCapture(video_path)
if not video.isOpened():
print("Error! No file found!")
i = 0
while i < len(frames):
video.set(1, frames[i])
ret, frame = video.read()
img_resized = frame
if not os.path.exists(f"src/data/{video_path[-10:-4]}/resized{rl}"):
os.makedirs(f"src/data/{video_path[-10:-4]}/resized{rl}")
else:
print(i)
img_resized = img_resized[(coord[i][1] - rl):(coord[i][1] + rl), (coord[i][0] - rl):(coord[i][0] + rl)]
cv2.imwrite(f"src/data/{video_path[-10:-4]}/resized{rl}/frame{i}.jpg", img_resized)
i += 1
print("Success!")
video.release()
thanks for the help!

How do I open pkl files with opencv?

I have a folder of pkl files in 'somefile' that I need to open as videos with opencv, but I keep getting the _pickle.UnpicklingError: unpickling stack underflow error. What am I doing wrong? I know that my code isn't pretty... Please don't roast me lol
import cv2 import os import pickle import numpy as np
subdir ='somefile' files = os.listdir(subdir)
# open pkl filesfor f in files:
with open(subdir + '/' + f, 'rb') as infile:
try:
unpickled_videos = pickle.load(infile)
for video in unpickled_videos:
print('{} has been unpickled'.format(os.path.abspath(video)))
# play video from file
for video in unpickled_videos:
if video == 'eye':
# create VideoCapture object, read from input file
cap = cv2.VideoCapture('eye' + '.mp4')
# check if camera opened successfully
if (cap.isOpened() == False):
print("Error opening {}".format(os.path.abspath(video)))
# convert resolutions from float to integer
frame_width = int(cap.get(3))
frame_height = int(cap.get(4))
fps = cap.get(cv2.cv.CV_CAP_PROP_FPS)
# define codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
out = cv2.VideoWriter(video + '.MP4', fourcc, fps, (frame_width, frame_height), True)
# read until video is completed
while True:
# capture frame-by-frame
ret, frame = cap.read()
# display resulting frame
cv2.imshow('frame', frame)
# press Q on keyboard to exit
if cv2.waitKey(0) & 0xFF == ord('q'):
break
except FileNotFoundError:
print('{} not found!'.format(f))
pass
except EOFError:
print('End of file error')
pass
#when everything done, release video capture object and close all frames
#cap.release() out.release() cv2.destroyAllWindows()
You could try to load it as a numpy array with:
import numpy as np
data = np.load(input_filename, allow_pickle=True)
The error sounds like it could be an issue with your data. In the case that some of your data is problematic, you could wrap the call in a try/except.

Python: take screenshot from video

The idea is that, user should be able to load a video from their local machine and tell the program to take a screenshot from the video every 5sec or 30sec. Is there any library to help me with this task? Any idea from how to proceed would be helpful.
install opencv-python (which is an unofficial pre-built OpenCV package for Python) by issuing the following command:
pip install opencv-python
# Importing all necessary libraries
import cv2
import os
import time
# Read the video from specified path
cam = cv2.VideoCapture("C:/Users/User/Desktop/videoplayback.mp4")
try:
# creating a folder named data
if not os.path.exists('data'):
os.makedirs('data')
# if not created then raise error
except OSError:
print('Error: Creating directory of data')
# frame
currentframe = 0
while (True):
time.sleep(5) # take schreenshot every 5 seconds
# reading from frame
ret, frame = cam.read()
if ret:
# if video is still left continue creating images
name = './data/frame' + str(currentframe) + '.jpg'
print('Creating...' + name)
# writing the extracted images
cv2.imwrite(name, frame)
# increasing counter so that it will
# show how many frames are created
currentframe += 1
else:
break
# Release all space and windows once done
cam.release()
cv2.destroyAllWindows()
the above answer is partially right but time.sleep here does not help at all but rather it makes the process slower. however, if you want to take a screenshot at a certain time of a video you need to understand that every time you do "ret, frame = cam.read()" it reads the next frame of the video. every second in a video has a number of frames depends on the video. you get that number using:
frame_per_second = cam.get(cv2.CAP_PROP_FPS)
so if you need to take a screenshot of the 3rd second you can keep the iteration as is in the above answer and just add
if currentframe == (3*frame_per_second):
cv2.imwrite(name, frame)
this will take a screenshot of the first frame in the 3rd second.
#ncica & Data_sniffer solution remake
import cv2
import os
import time
step = 10
frames_count = 3
cam = cv2.VideoCapture('video/example.MP4')
currentframe = 0
frame_per_second = cam.get(cv2.CAP_PROP_FPS)
frames_captured = 0
while (True):
ret, frame = cam.read()
if ret:
if currentframe > (step*frame_per_second):
currentframe = 0
name = 'photo/frame' + str(frames_captured) + '.jpg'
print(name)
cv2.imwrite(name, frame)
frames_captured+=1
if frames_captured>frames_count-1:
ret = False
currentframe += 1
if ret==False:
break
cam.release()
cv2.destroyAllWindows()
#a generic function incorporating all the comments mentioned above.
def get_frames(inputFile,outputFolder,step,count):
'''
Input:
inputFile - name of the input file with directoy
outputFolder - name and path of the folder to save the results
step - time lapse between each step (in seconds)
count - number of screenshots
Output:
'count' number of screenshots that are 'step' seconds apart created from video 'inputFile' and stored in folder 'outputFolder'
Function Call:
get_frames("test.mp4", 'data', 10, 10)
'''
#initializing local variables
step = step
frames_count = count
currentframe = 0
frames_captured = 0
#creating a folder
try:
# creating a folder named data
if not os.path.exists(outputFolder):
os.makedirs(outputFolder)
#if not created then raise error
except OSError:
print ('Error! Could not create a directory')
#reading the video from specified path
cam = cv2.VideoCapture(inputFile)
#reading the number of frames at that particular second
frame_per_second = cam.get(cv2.CAP_PROP_FPS)
while (True):
ret, frame = cam.read()
if ret:
if currentframe > (step*frame_per_second):
currentframe = 0
#saving the frames (screenshots)
name = './data/frame' + str(frames_captured) + '.jpg'
print ('Creating...' + name)
cv2.imwrite(name, frame)
frames_captured+=1
#breaking the loop when count achieved
if frames_captured > frames_count-1:
ret = False
currentframe += 1
if ret == False:
break
#Releasing all space and windows once done
cam.release()
cv2.destroyAllWindows()
To add on data_sniffer's answer. I would recommend using round (Math function) on frame_per_second when checking as if the frame rate is a decimal number then it will go into an infinite loop.
The solutions provided do not work for me in several cases.
The FPS from cv2.CAP_PROP_FPS is a floating point value and the FPS rate of my testvid.mp4 was 23.976023976023978 according to this property.
When looping through current_frame / fps % 3, we will almost always have leftovers because of this floating point value. Same goes for (3*frame_per_second):, causing our imwrite to never be reached.
I solved this issue by using the same calculations, but storing the remainders and comparing those:
current_frame = 0
fps_calculator_previous = 0
while (True):
ret, frame = cam.read()
if ret:
# Still got video left.
file_name = f"./data_generation/out/{_fn}-{current_frame}.jpg"
fps_calculator = (current_frame / fps) % every_x_sec
if(fps_calculator - fps_calculator_previous < 0):
print("Found a frame to write!")
cv2.imwrite(file_name, frame)
fps_calculator_previous = fps_calculator
current_frame += 1
else:
break
This seems to work well for me with any value for both cv2.CAP_PROP_FPS as well as every_x_sec
My video was 18 minutes and 7 seconds long, and I captured 362 unique frames from that with every_x_sec set to 3.
Edited #ncica's code and noted that it is working fine.
import cv2
import os
import time
cam = cv2.VideoCapture("/path/to/videoIn.mp4")
try:
if not os.path.exists('data'):
os.makedirs('data')
except OSError:
print('Error: Creating directory of data')
intvl = 2 #interval in second(s)
fps= int(cam.get(cv2.CAP_PROP_FPS))
print("fps : " ,fps)
currentframe = 0
while (True):
ret, frame = cam.read()
if ret:
if(currentframe % (fps*intvl) == 0):
name = './data/frame' + str(currentframe) + '.jpg'
print('Creating...' + name)
cv2.imwrite(name, frame)
currentframe += 1
else:
break
cam.release()
cv2.destroyAllWindows()
The trick is it is looping frame-by-frame.
So, here we are capturing frames that we want and write disk as snapshot image file.
eg : If you want snapshot two second by two second intvl must be 2

Python OpenCV, can't write a video (.avi) to file

I'm writing a programme in Windows XP using Python and OpenCV that adds image frames captured from a webcam to a video (.avi), but only when a certain condition is met. For testing purposes this condition is time based, but this will change. The only problem is, my programme does not output anything. Everything seems to run fine, but then when I check for the .avi file that it should be outputting, there is nothing.
Here's my code. I've been told my style is a little unconventional, but hopefully you can see what I'm up to. Thank you in advance.
import cv, time ##Import modules
cv.NamedWindow("Experiment", cv.CV_WINDOW_AUTOSIZE) ##Open window
camera_index = 1
capture = cv.CaptureFromCAM(camera_index)
x = time.clock()+5
try:
while 1:
if time.clock() < x: ##Clock to capture 5 seconds of footage
frame = cv.QueryFrame(capture) ##Capture webcam frame
cv.ShowImage("Experiment", frame) ##Display image
writer = cv.CreateVideoWriter("C:\/test.avi", cv.CV_FOURCC('F', 'M', 'P', '4'),
24, (640, 480), 1) ##Write video file (codec = MPEG-4 - .avi)
if writer: ##Write frame to file
cv.WriteFrame(writer, frame)
if not writer: ##Failure to register video writer
print "Error: Video writer malfunction"
sys.exit(1)
cv.DestroyWindow("Experiment")
break
else:
if writer:
print "Video capture succeeded"
cv.DestroyWindow("Experiment")
camera_index += 1
capture = cv.CaptureFromCAM(-1)
break
c = cv.WaitKey(10)
if(c=="n"):
camera_index += 1
capture = cv.CaptureFromCAM(-1)
except:
print "Video capture failed" ##Total systems failure escape routine!!!
cv.DestroyWindow("Experiment")
--Edit--
If I change cv.CV_FOURCC('F', 'M', 'P', '4') in line 12 to "-1" a video is output, but it is 0 bytes.

Categories