How to save one image every 25 frames into a folder - python

I am trying to write images from IP camera into a new folder. However, as for now, it is writing for each frame. How to only save one image every 25 frames? This is my current code:
import cv2
import os
folder = 'test_python'
os.mkdir(folder)
url = "rtsp://axis-media/media.amp"
count = 0
cap = cv2.VideoCapture(url)
while True:
# read next frame
ret, frame = cap.read()
cv2.imshow('frame', frame)
cv2.imwrite(os.path.join(folder, "frame{:d}.jpg".format(count)), frame)
count += 1

The trivial solution would be:
if count % 25 == 0:
cv2.imwrite(...)

Related

How to restrict a camera on capturing and not capturing images on python, opencv?

I have a camera that faces an LED sensor that I want to restrict on capturing images when the object is in sight otherwise not capturing. I will designate a point or region where the color pixels will change as an object arrives it will wait for 3sec and then capture. Thereafter it will not capture the same object if it still remains in the designated region. Here are my codes.
import cv2
import numpy as np
import os
os.chdir('C:/Users/Man/Desktop')
previous_flag = 0
current_flag = 0
a = 0
video = cv2.VideoCapture(0)
while True:
ret, frame = video.read()
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
cv2.imshow('Real Time', rgb)
cv2.waitKey(1)
if(rgb[400, 200, 0]>200): # selecting a point that will change in terms of pixel values
current_flag = 0
previous_flag = current_flag
print('No object Detected')
else: # object arrived
current_flag = 1
change_in_flags = current_flag - previous_flag
if(change_in_flags==1):
time.sleep(3)
cv2.imwrite('.trial/cam+str(a).jpg', rgb)
a += 1
previous_flag = current_flag
video.release()
cv2.destroyAllWindows()
When I implement the above program, for the first case (if) it prints several lines of No object Detected.
How can I reduce those sequentially printed lines for No object Detected statement? So that the program can just print one line without repetition.
I also tried to add a while loop to keep the current status true after a+=1 like:
while True:
previous_flag = current_flag
continue
It worked but the system became so slow is there any way to avoid such a while loop such that the system becomes faster? Any advice on that
import cv2
import numpy as np
import os
os.chdir('C:/Users/Man/Desktop')
state1=0
a = 0
video = cv2.VideoCapture(0)
while True:
ret, frame = video.read()
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
cv2.imshow('Real Time', rgb)
cv2.waitKey(1)
if(rgb[400, 200, 0]>200): # selecting a point that will change in terms of pixel values
if(state1 == 0):
print('No object Detected')
state1=1
else: # object arrived
time.sleep(1)
if(state1==1)
print('Object is detected')
cv2.imwrite('./trial/cam+str(a).jpg', rgb)
a += 1
state1=0
video.release()
cv2.destroyAllWindows()

Extract the text from the Video data

I want to extract the text from the video data. I can extract the text but facing some problems. I am getting repetitive texts from the video. I want to extract the exact text from the whole video. How could I solve my problem? I am sharing my code.
import cv2
import os
import pytesseract
import numpy as np
import time
# Mention the installed location of Tesseract-OCR in your system
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
FRAME_RATE = 2
# Read the video from specified path
video_data = cv2.VideoCapture(r"jonty.mp4")
video_length= int(video_data.get(cv2.CAP_PROP_FRAME_COUNT)) - 1
fps=int(video_data.get(cv2.CAP_PROP_FPS))
pos_frame= int(video_data.get(cv2.CAP_PROP_POS_FRAMES))
width = int(video_data.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(video_data.get(cv2.CAP_PROP_FRAME_HEIGHT))
frame_time = 0
frame_count = 0
print("total_frames: ", video_length)
print("FRAME_RATE", FRAME_RATE)
print("fps", fps)
print("pos_frame", pos_frame)
text_data=[]
while(True):
video_data.set(cv2.CAP_PROP_POS_MSEC, frame_time * 10000) # move frame to a timestamp
frame_time += 1/FRAME_RATE
# reading from frame
ret,frame = video_data.read()
if ret:
# if video is still left continue creating images
image = r'\images\frame' + str(frame_count) + '.jpg
# writing the extracted images
cv2.imwrite(image, frame)
# Apply OCR on the cropped image
text = pytesseract.image_to_string(image,lang='eng+deu+fra')
#print(text)
text_data.append(text)
# frame in every 1 second
frame_count += 1
else:
break
# Release all space and windows once done
video_data.release()
cv2.destroyAllWindows()

video to images then images to video in python

I'm trying to convert the following video to images
https://www.signingsavvy.com/media/mp4-ld/24/24851.mp4
however, I have done it by using OpenCV
# Importing all necessary libraries
import cv2
import os
# Read the video from specified path
cam = cv2.VideoCapture("C:\Users\ahmad\Hi_ASL.mp4")
print(cam.get(cv2.CAP_PROP_FPS))
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):
# 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
# ret,frame = cam.read()
# Release all space and windows once done
cam.release()
cv2.destroyAllWindows()
After I have done it. I want to convert those images to video to be like the one above and I wrote this code
img = [img for img in os.listdir('data')]
frame = cv2.imread('data\' + img[0])
h , w , l = frame.shape
vid = cv2.VideoWriter('hiV.mp4' , 0 ,1 ,(w,h))
for imgg in img:
vid.write(cv2.imread('data\' + imgg))
cv2.destroyAllWindows()
vid.release()
The problem is the result of combining the images to a video using OpenCV is not the same as the original video. So, what is the problem? I want it to be the same as the original one.
The result of the code above is this video https://drive.google.com/file/d/16vwT35wzc95tBleK5VCpZJQkaLxSiKVd/view?usp=sharing
And thanks.
You should change cv2.VideoWriter('hiV.mp4' , 0 ,1 ,(w,h)) to cv2.VideoWriter('hiV.mp4' , 0 ,30 ,(w,h)) As the 1 sets the fps and that means that you write 1 frame every second and not 30 or 29.97(NTSC) as the original video.

Grab video frames from all videos in folder, process with openCV, save to own folder?

I have code like this. I know it's not correct, but I'm new at this and not sure how to fix it. How do I make it so cv2 runs on every video I have in a folder, taking a frame every 3 seconds, and saving the frame images in a new folder within /data/ --- ex. /data/rise_of_skywalker?
Thanks
import cv2
import numpy as np
import os
def frame_capture(file):
cap = cv2.VideoCapture(file)
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()
'''
(((how do I change this block here to get it for every 3 seconds?)))
'''
if currentFrame == 5:
name = './data/frame' + str(currentFrame) + '.jpg'
print ('Creating...' + name)
cv2.imwrite(name, frame)
# To stop duplicate images
currentFrame += 1
cap.release()
cv2.destroyAllWindows()
print ("Done!")
for file in os.listdir("/users/x/Desktop/y/videos"):
if file.endswith(".mp4"):
path=os.path.join("/users/x/Desktop/y/videos", file))
frame_capture(path)
If I understand correctly, what you want to do is to change the line if currentFrame == 5: to if currentFrame % (3 * fps) == 0:
And before that line, outside the loop, add fps = cap.get(cv2.CAP_PROP_FPS)

OpenCV - Save video segments based on certion condition

Aim : Detect the motion and save only the motion periods in files with names of the starting time.
Now I met the issue about how to save the video to the files with video starting time.
What I tested :
I tested my program part by part. It seems that each part works well except the saving part.
Running status: No error. But in the saving folder, there is no video. If I use a static saving path instead, the video will be saved successfully, but the video will be override by the next video. My codes are below:
import cv2
import numpy as np
import time
cap = cv2.VideoCapture( 0 )
bgst = cv2.createBackgroundSubtractorMOG2()
fourcc=cv2.VideoWriter_fourcc(*'DIVX')
size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
n = "start_time"
while True:
ret, frame = cap.read()
dst = bgst.apply(frame)
dst = np.array(dst, np.int8)
if np.count_nonzero(dst)>3000: # use this value to adjust the "Sensitivity“
print('something is moving %s' %(time.ctime()))
path = r'E:\OpenCV\Motion_Detection\%s.avi' %n
out = cv2.VideoWriter( path, fourcc, 50, size )
out.write(frame)
key = cv2.waitKey(3)
if key == 32:
break
else:
out.release()
n = time.ctime()
print("No motion Detected %s" %n)
What I meant is:
import cv2
import numpy as np
import time
cap = cv2.VideoCapture( 0 )
bgst = cv2.createBackgroundSubtractorMOG2()
fourcc=cv2.VideoWriter_fourcc(*'DIVX')
size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
path = r'E:\OpenCV\Motion_Detection\%s.avi' %(time.ctime())
out = cv2.VideoWriter( path, fourcc, 16, size )
while True:
ret, frame = cap.read()
dst = bgst.apply(frame)
dst = np.array(dst, np.int8)
for i in range(number of frames in the video):
if np.count_nonzero(dst)<3000: # use this value to adjust the "Sensitivity“
print("No Motion Detected")
out.release()
else:
print('something is moving %s' %(time.ctime()))
#label each frame you want to output here
out.write(frame(i))
key = cv2.waitKey(1)
if key == 32:
break
cap.release()
cv2.destroyAllWindows()
If you see the code there will be a for loop, within which the process of saving is done.
I do not know the exact syntax involving for loop with frames, but I hope you have the gist of it. You have to find the number of frames present in the video and set that as the range in the for loop.
Each frame gets saved uniquely (see the else condition.) As I said I do not know the syntax. Please refer and follow this procedure.
Cheers!

Categories

Resources