Ask Your Question

Revision history [back]

How to delay programm without stopping video feed in Python3

I have a cheap Wifi Action camera that i want to use to take a time lapse. I am able to connect to the camera and see a live feed, albeit with a little bit of delay but that doesn't matter to me. I had a hard time figuring out how to include the code properly here, as i couldn't get it to format correctly. I originally had asked a question on Stackoverflow but after 2 weeks, no one has yet to respond. https://stackoverflow.com/questions/60776535/how-to-take-time-lapse-pictures-with-opencv-in-python3

I know what my problem is, whenever I have the delay, it also stops the video feed at the time it was paused and then it resumes after. Which it should, since I'm stopping the program right there on its tracks. I have tried looking on the internet for how to delay a program without stopping it, but all i find are inconclusive answers. I have also found an option on Python3 Called Threading, but that's way out of my skill level right now, even though I have attempted to use it.

Ideally, i want to find a way on having the video feed always open, and only delay the part of the code that tells it to save a picture. And if not, the manual way to take a picture with the space bar still works so it is perfectly doable. But instead of me pressing it, find a way how to emulate a keyboard press, but then i will also need a delay so its pretty inconclusive and redundant to the first option.

I would just like to figure out if I'm heading the right direction, or if I'm just over complicating this. Any information that you can direct me to, or any tips would be really appreciated.

How to delay programm without stopping video feed in Python3

I have a cheap Wifi Action camera that i want to use to take a time lapse. I am able to connect to the camera and see a live feed, albeit with a little bit of delay but that doesn't matter to me. I had a hard time figuring out how to include the code properly here, as i couldn't get it to format correctly. I originally had asked a question on Stackoverflow but after 2 weeks, no one has yet to respond. https://stackoverflow.com/questions/60776535/how-to-take-time-lapse-pictures-with-opencv-in-python3

I know what my problem is, whenever I have the delay, it also stops the video feed at the time it was paused and then it resumes after. Which it should, since I'm stopping the program right there on its tracks. I have tried looking on the internet for how to delay a program without stopping it, but all i find are inconclusive answers. I have also found an option on Python3 Called Threading, but that's way out of my skill level right now, even though I have attempted to use it.

Ideally, i want to find a way on having the video feed always open, and only delay the part of the code that tells it to save a picture. And if not, the manual way to take a picture with the space bar still works so it is perfectly doable. But instead of me pressing it, find a way how to emulate a keyboard press, but then i will also need a delay so its pretty inconclusive and redundant to the first option.

I would just like to figure out if I'm heading the right direction, or if I'm just over complicating this. Any information that you can direct me to, or any tips would be really appreciated.

Edit: This is the code that i am currently using(that kinda works) import cv2 import time

set_limit = int(input("How many pictures would you like to take? \n: ")) set_time = int(input("What should the interval be in seconds? \n: "))

print("Starting Routine")

cam = cv2.VideoCapture() cam.open("rtsp://192.168.1.1")

limit_count = 0 img_counter = 0

while(limit_count < set_limit): ret, frame = cam.read()

if not ret:
    break

img_name = "opencv_frame_{}.png".format(img_counter)
cv2.imwrite(img_name, frame)
print("{} written!".format(img_name))
img_counter += 1
limit_count +=1

time.sleep(set_time)

cam.release()

cv2.destroyAllWindows()

time.sleep(set_time)

And this one is the one I'm experimenting with. I was attempting to further delay the program in hopes of renewing the feed a little bit longer in between

import cv2 import time import datetime

set_limit = int(input("How many pictures would you like to take? \n: ")) set_time = int(input("What should the interval be in seconds? \n: "))

print("Starting Routine")

cam = cv2.VideoCapture() cam.open("rtsp://192.168.1.1")

limit_count = 0 img_counter = 0 sec_counter = 0 sec_counter_true = 0

while(limit_count < set_limit): ret, frame = cam.read()

if not ret:
    break
if sec_counter >= set_limit:
    sec_counter_true = 1
if sec_counter_true == 1:
    img_name = "opencv_frame_{}.png".format(img_counter)
    cv2.imwrite(img_name, frame)
    print("{} written!".format(img_name) + " on " + str(datetime.datetime.now()))
    img_counter += 1
    limit_count +=1
    sec_counter_true = 0
    sec_counter = 0
sec_counter += 1
print(sec_counter)
time.sleep(set_time)

cam.release()

cv2.destroyAllWindows()

Again, I sincerely apologize for the horrible formating.

click to hide/show revision 3
None

updated 2020-04-01 05:28:38 -0600

berak gravatar image

How to delay programm without stopping video feed in Python3

I have a cheap Wifi Action camera that i want to use to take a time lapse. I am able to connect to the camera and see a live feed, albeit with a little bit of delay but that doesn't matter to me. I had a hard time figuring out how to include the code properly here, as i couldn't get it to format correctly. I originally had asked a question on Stackoverflow but after 2 weeks, no one has yet to respond. https://stackoverflow.com/questions/60776535/how-to-take-time-lapse-pictures-with-opencv-in-python3

I know what my problem is, whenever I have the delay, it also stops the video feed at the time it was paused and then it resumes after. Which it should, since I'm stopping the program right there on its tracks. I have tried looking on the internet for how to delay a program without stopping it, but all i find are inconclusive answers. I have also found an option on Python3 Called Threading, but that's way out of my skill level right now, even though I have attempted to use it.

Ideally, i want to find a way on having the video feed always open, and only delay the part of the code that tells it to save a picture. And if not, the manual way to take a picture with the space bar still works so it is perfectly doable. But instead of me pressing it, find a way how to emulate a keyboard press, but then i will also need a delay so its pretty inconclusive and redundant to the first option.

I would just like to figure out if I'm heading the right direction, or if I'm just over complicating this. Any information that you can direct me to, or any tips would be really appreciated.

Edit: This is the code that i am currently using(that kinda works) works)

import cv2
import time

time set_limit = int(input("How many pictures would you like to take? \n: ")) set_time = int(input("What should the interval be in seconds? \n: "))

")) print("Starting Routine")

Routine") cam = cv2.VideoCapture() cam.open("rtsp://192.168.1.1")

cam.open("rtsp://192.168.1.1") limit_count = 0 img_counter = 0

0 while(limit_count < set_limit): ret, frame = cam.read()

cam.read()


    if not ret:
     break

 img_name = "opencv_frame_{}.png".format(img_counter)
 cv2.imwrite(img_name, frame)
 print("{} written!".format(img_name))
 img_counter += 1
 limit_count +=1

 time.sleep(set_time)

cam.release()

cv2.destroyAllWindows()

time.sleep(set_time)

cam.release() cv2.destroyAllWindows() #time.sleep(set_time) And this one is the one I'm experimenting with. I was attempting to further delay the program in hopes of renewing the feed a little bit longer in between

import cv2 import time import datetime

datetime set_limit = int(input("How many pictures would you like to take? \n: ")) set_time = int(input("What should the interval be in seconds? \n: "))

")) print("Starting Routine")

Routine") cam = cv2.VideoCapture() cam.open("rtsp://192.168.1.1")

cam.open("rtsp://192.168.1.1") limit_count = 0 img_counter = 0 sec_counter = 0 sec_counter_true = 0

0 while(limit_count < set_limit): ret, frame = cam.read()

cam.read()


    if not ret:
     break
 if sec_counter >= set_limit:
     sec_counter_true = 1
 if sec_counter_true == 1:
     img_name = "opencv_frame_{}.png".format(img_counter)
     cv2.imwrite(img_name, frame)
     print("{} written!".format(img_name) + " on " + str(datetime.datetime.now()))
     img_counter += 1
     limit_count +=1
     sec_counter_true = 0
     sec_counter = 0
 sec_counter += 1
 print(sec_counter)
 time.sleep(set_time)
cam.release()

cv2.destroyAllWindows()

cam.release()

cv2.destroyAllWindows()

Again, I sincerely apologize for the horrible formating.

click to hide/show revision 4
None

updated 2020-04-01 05:30:08 -0600

berak gravatar image

How to delay programm without stopping video feed in Python3

I have a cheap Wifi Action camera that i want to use to take a time lapse. I am able to connect to the camera and see a live feed, albeit with a little bit of delay but that doesn't matter to me. I had a hard time figuring out how to include the code properly here, as i couldn't get it to format correctly. I originally had asked a question on Stackoverflow but after 2 weeks, no one has yet to respond. https://stackoverflow.com/questions/60776535/how-to-take-time-lapse-pictures-with-opencv-in-python3

I know what my problem is, whenever I have the delay, it also stops the video feed at the time it was paused and then it resumes after. Which it should, since I'm stopping the program right there on its tracks. I have tried looking on the internet for how to delay a program without stopping it, but all i find are inconclusive answers. I have also found an option on Python3 Called Threading, but that's way out of my skill level right now, even though I have attempted to use it.

Ideally, i want to find a way on having the video feed always open, and only delay the part of the code that tells it to save a picture. And if not, the manual way to take a picture with the space bar still works so it is perfectly doable. But instead of me pressing it, find a way how to emulate a keyboard press, but then i will also need a delay so its pretty inconclusive and redundant to the first option.

I would just like to figure out if I'm heading the right direction, or if I'm just over complicating this. Any information that you can direct me to, or any tips would be really appreciated.

Edit: This is the code that i am currently using(that kinda works)

import cv2
import time

set_limit = int(input("How many pictures would you like to take? \n: "))
set_time = int(input("What should the interval be in seconds? \n: "))

print("Starting Routine")

cam = cv2.VideoCapture()
cam.open("rtsp://192.168.1.1")

limit_count = 0
img_counter = 0

while(limit_count < set_limit):
    ret, frame = cam.read()


    if not ret:
        break

    img_name = "opencv_frame_{}.png".format(img_counter)
    cv2.imwrite(img_name, frame)
    print("{} written!".format(img_name))
    img_counter += 1
    limit_count +=1

    time.sleep(set_time)

cam.release()

cv2.destroyAllWindows()
#time.sleep(set_time)
 

And this one is the one I'm experimenting with. I was attempting to further delay the program in hopes of renewing the feed a little bit longer in between

import cv2
import time
import datetime

set_limit = int(input("How many pictures would you like to take? \n: "))
set_time = int(input("What should the interval be in seconds? \n: "))

print("Starting Routine")

cam = cv2.VideoCapture()
cam.open("rtsp://192.168.1.1")

limit_count = 0
img_counter = 0
sec_counter = 0
sec_counter_true = 0

while(limit_count < set_limit):
    ret, frame = cam.read()


    if not ret:
        break
    if sec_counter >= set_limit:
        sec_counter_true = 1
    if sec_counter_true == 1:
        img_name = "opencv_frame_{}.png".format(img_counter)
        cv2.imwrite(img_name, frame)
        print("{} written!".format(img_name) + " on " + str(datetime.datetime.now()))
        img_counter += 1
        limit_count +=1
        sec_counter_true = 0
        sec_counter = 0
    sec_counter += 1
    print(sec_counter)
    time.sleep(set_time)
cam.release()

cv2.destroyAllWindows()

Again, I sincerely apologize for the horrible formating.

How to delay programm without stopping video feed in Python3

I have a cheap Wifi Action camera that i want to use to take a time lapse. I am able to connect to the camera and see a live feed, albeit with a little bit of delay but that doesn't matter to me. I had a hard time figuring out how to include the code properly here, as i couldn't get it to format correctly. I originally had asked a question on Stackoverflow but after 2 weeks, no one has yet to respond. https://stackoverflow.com/questions/60776535/how-to-take-time-lapse-pictures-with-opencv-in-python3

I know what my problem is, whenever I have the delay, it also stops the video feed at the time it was paused and then it resumes after. Which it should, since I'm stopping the program right there on its tracks. I have tried looking on the internet for how to delay a program without stopping it, but all i find are inconclusive answers. I have also found an option on Python3 Called Threading, but that's way out of my skill level right now, even though I have attempted to use it.

Ideally, i want to find a way on having the video feed always open, and only delay the part of the code that tells it to save a picture. And if not, the manual way to take a picture with the space bar still works so it is perfectly doable. But instead of me pressing it, find a way how to emulate a keyboard press, but then i will also need a delay so its pretty inconclusive and redundant to the first option.

I would just like to figure out if I'm heading the right direction, or if I'm just over complicating this. Any information that you can direct me to, or any tips would be really appreciated.

Edit: This is the code that i am currently using(that kinda works)

import cv2
import time

set_limit = int(input("How many pictures would you like to take? \n: "))
set_time = int(input("What should the interval be in seconds? \n: "))

print("Starting Routine")

cam = cv2.VideoCapture()
cam.open("rtsp://192.168.1.1")

limit_count = 0
img_counter = 0

while(limit_count < set_limit):
    ret, frame = cam.read()


    if not ret:
        break

    img_name = "opencv_frame_{}.png".format(img_counter)
    cv2.imwrite(img_name, frame)
    print("{} written!".format(img_name))
    img_counter += 1
    limit_count +=1

    time.sleep(set_time)

cam.release()

cv2.destroyAllWindows()
#time.sleep(set_time)

And this one is the one I'm experimenting with. I was attempting to further delay the program in hopes of renewing the feed a little bit longer in between

import cv2
import time
import datetime

set_limit = int(input("How many pictures would you like to take? \n: "))
set_time = int(input("What should the interval be in seconds? \n: "))

print("Starting Routine")

cam = cv2.VideoCapture()
cam.open("rtsp://192.168.1.1")

limit_count = 0
img_counter = 0
sec_counter = 0
sec_counter_true = 0

while(limit_count < set_limit):
    ret, frame = cam.read()


    if not ret:
        break
    if sec_counter >= set_limit:
        sec_counter_true = 1
    if sec_counter_true == 1:
        img_name = "opencv_frame_{}.png".format(img_counter)
        cv2.imwrite(img_name, frame)
        print("{} written!".format(img_name) + " on " + str(datetime.datetime.now()))
        img_counter += 1
        limit_count +=1
        sec_counter_true = 0
        sec_counter = 0
    sec_counter += 1
    print(sec_counter)
    time.sleep(set_time)
cam.release()

cv2.destroyAllWindows()

Again, I sincerely apologize for the horrible formating.

Edit:: Well, i didn't know that i couldn't answer my own question yet. So my long answer with my solution was deleted, so that's cool, i wish i had a warning though.

This worked for me

import cv2
import time
import datetime

set_limit = int(input("How many pictures would you like to take? \n: "))
set_loop = int(input("What should the interval be in loops? \n: "))

print("Starting Routine")

cam = cv2.VideoCapture()
cam.open("rtsp://192.168.1.1")
#cv2.namedWindow("Camera Feed") #I dont need to have the camara window open

limit_count = 0
img_counter = 0
loop_counter = 0
loop_counter_true = 0

while(limit_count < set_limit):
    ret, frame = cam.read()


    if not ret:
        break
    if loop_counter >= set_loop:
        loop_counter_true = 1
    if loop_counter_true == 1:
        img_name = "opencv_frame_{}.png".format(img_counter)
        cv2.imwrite(img_name, frame)
        print("{} written!".format(img_name) + " on " + str(datetime.datetime.now()))
        img_counter += 1
        limit_count +=1
        loop_counter_true = 0
        loop_counter = 0
    loop_counter += 1
    #print(sec_counter)
cam.release()

cv2.destroyAllWindows()

I really appreciate the other responses, but they still had the same problems that i was having. I am 100% sure that its because of the WIFI connection instead of the USB/ hardwired one. I just took out the delay, and made it loop itself 1000 times. That is roughly 30 seconds real time. So doubling it is roughly 1 minute real time. Taking a picture after the count was reached and resetting the count to zero. Incredibly inefficient, but it works out.