Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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. As for you, its okay for rstp.

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

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

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. IP camera. As for you, its okay for rstp.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

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

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

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