Ask Your Question
0

How to delay programm without stopping video feed in Python3

asked 2020-04-01 04:08:58 -0600

updated 2020-04-02 07:01:18 -0600

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/6...

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 ...
(more)
edit retag flag offensive close merge delete

Comments

could you you add your (current) code to the question, please ?

only delay the part of the code that tells it to save a picture.

probaby a simple "count to 20 then take snapshot" logic will do the trick

berak gravatar imageberak ( 2020-04-01 04:13:34 -0600 )edit

Post is still awaiting moderation, but i am still using the same code in the link i have provided.

ElectroBot gravatar imageElectroBot ( 2020-04-01 04:45:07 -0600 )edit

btw, formatting: mark it, then press ctrl-k

berak gravatar imageberak ( 2020-04-01 05:29:21 -0600 )edit

Why are you using duplicated inside loop? You should remove this..

sec_counter_true = 0
sec_counter = 0
supra56 gravatar imagesupra56 ( 2020-04-01 15:25:42 -0600 )edit

You are looping again depending on number of frames.

sec_counter += 1
print(sec_counter)
supra56 gravatar imagesupra56 ( 2020-04-02 09:18:20 -0600 )edit
1

That's the point, it keeps the video feed up and refreshed. After a certain amount of iterations, it can take a picture from the live feed. Which is something that i couldn't do before. Also that print(sec_counter) is commented out, because i don't want to see it print from 1 to 2000 each time.

ElectroBot gravatar imageElectroBot ( 2020-04-02 10:45:26 -0600 )edit

I recommend used timedelta.

supra56 gravatar imagesupra56 ( 2020-04-03 21:18:47 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
0

answered 2020-04-01 15:36:03 -0600

supra56 gravatar image

updated 2020-04-01 15:39:29 -0600

Try this. Btw, I am using script for raspberry pi 4 using w/out monitor and keyboards. If I wanted to stop it. I have to unplug it. I got derived from Raspberry pi picamera. So I entered 0 and set interval to whatever. I set it to 5 sec for timelapse. Btw, I'm not using IP camera. As for you, its okay for rstp. This code in below doesn't stop. Until you powered off.

import cv2
import time
import datetime

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

print(f"Starting Routine")

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

limit_count = 1
img_counter = 1
sec_counter = 0

while(cam.isOpened()):
    ret, frame = cam.read()

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

    sec_counter += 1
    print(sec_counter)
    time.sleep(set_time)

cam.release()
cv2.destroyAllWindows()

Output:

How many pictures would you like to take? 
: 0
What should the interval be in seconds? 
: 5
Starting Routine
opencv_frame_1.png written! on 2020-04-01 16:20:42.941376
1
opencv_frame_2.png written! on 2020-04-01 16:20:45.105738
2
opencv_frame_3.png written! on 2020-04-01 16:20:47.255398
3
opencv_frame_4.png written! on 2020-04-01 16:20:49.415141
4
opencv_frame_5.png written! on 2020-04-01 16:20:51.558066
5
opencv_frame_6.png written! on 2020-04-01 16:20:53.689211
6
opencv_frame_7.png written! on 2020-04-01 16:20:55.818787
7
opencv_frame_8.png written! on 2020-04-01 16:20:57.907024
8
opencv_frame_9.png written! on 2020-04-01 16:21:00.068015
9
opencv_frame_10.png written! on 2020-04-01 16:21:02.216263
10
opencv_frame_11.png written! on 2020-04-01 16:21:04.363354
11
opencv_frame_12.png written! on 2020-04-01 16:21:06.507479
12

From 1 to 7 images is warmup for webcam. After 7 images, you set to go.

edit flag offensive delete link more

Comments

1

Thanks for the response, funny enough i began using the datetime.now yesterday to better help me out. But that just kept duplicating the pictures, whilst just counting the numbers up. I have edited my comment with a workaround i thought up that works, but is inefficient. I am still open to suggestions though.

ElectroBot gravatar imageElectroBot ( 2020-04-02 07:05:38 -0600 )edit
0

answered 2020-04-02 12:21:42 -0600

crackwitz gravatar image

try this. it uses threading to keep receiving frames, no matter what you do elsewhere. https://gist.github.com/crackwitz/15c...

edit flag offensive delete link more

Comments

Thank you for the response, i will look into it.

ElectroBot gravatar imageElectroBot ( 2020-04-02 12:42:15 -0600 )edit

This doesn't work for you. You are working on your rtsp nott deep learning or machine learning.. Your code is better. I have similar picamera.

supra56 gravatar imagesupra56 ( 2020-04-02 12:57:47 -0600 )edit

Question Tools

Stats

Asked: 2020-04-01 04:08:58 -0600

Seen: 2,815 times

Last updated: Apr 02 '20