0

I have a RaspberryPi, a few IP camera's and I would like to get a fresh image from all these camera's every 5 minutes. I have the following script, which open the RTSP feed af grabs images ALL THE TIME, talking 10-25 every second it runs.

Is there a way to open the videofeed an take only 1 image?

import cv2
import time
cap = cv2.VideoCapture('rtsp://192.168.86.81:554/11') # it can be rtsp or http $

ret, frame = cap.read()
while ret:
    cv2.imwrite('images/{}.jpg'.format(time.time()), frame)
    ret, frame = cap.read()
AgoraLive
  • 81
  • 3
  • 10
  • 1
    There's two main ways to do this; either your script continuously runs and you sleep for 5 minutes between frame grabs (not recommended, as if the program crashes it stops indefinitely, and you might get connection issues), or, schedule your script to execute once every 5 minutes (IMO, a better approach). With the second approach, just modify your script to take a single frame (no `while` loop) and write it. Then, look up `cron job` or `crontab` to schedule your script to automatically execute every 5 minutes. It's very easy to set up and there's many tutorials. – alkasm Feb 17 '19 at 10:26
  • If I remove the "while" then I get an error with the script? – AgoraLive Feb 17 '19 at 12:38

2 Answers2

0

This solved my problem. I removed time as I do not need this. I will let the aboce code stand in case anybody would want to play around with this

import cv2
cap = cv2.VideoCapture('rtsp://192.168.86.81:554/11') # it can be rtsp or http stream

ret, frame = cap.read()

if cap.isOpened():
    _,frame = cap.read()
    cap.release() #releasing camera immediately after capturing picture
    if _ and frame is not None:
        cv2.imwrite('images/latest.jpg', frame)
AgoraLive
  • 81
  • 3
  • 10
  • 2
    This is good, but you should remove the first `cap.read()` line before the if block. Just check that the capture is open and read a frame. I mean it's not a big deal but you're actually grabbing two frames here and not one, so there's just no need for the first one. – alkasm Feb 18 '19 at 00:33
0
import cv2
import time
from datetime import datetime
import getpass

#imagesFolder = "C:/Users/<user>/documents"

# https://stackoverflow.com/questions/842059/is-there-a-portable-way-to-get-the-current-username-in-python
imagesFolder = "C:/Users/" + getpass.getuser() + "/documents"

#cap = cv2.VideoCapture("rtsp://192.168.86.81:554/11")

# Use public RTSP Streaming for testing, but I am getting black frames!
cap = cv2.VideoCapture("rtsp://192.168.86.81:554/11")
frameRate = cap.get(5) #frame rate
count = 0


while cap.isOpened():
    start_time = time.time()

    frameId = cap.get(1)  # current frame number
    ret, frame = cap.read()

    if (ret != True):
        break

    filename = imagesFolder + "/image_" + str(datetime.now().strftime("%d-%m-%Y_%I-%M-%S_%p"))  + ".jpg"
    cv2.imwrite(filename, frame)

    # Show frame for testing
    cv2.imshow('frame', frame)
    cv2.waitKey(1)

    count += 1

    #Break loop after 5*60 minus
    if count > 5*60:
        break

    elapsed_time = time.time() - start_time

    # Wait for 60 seconds (subtract elapsed_time in order to be accurate).
    time.sleep(60 - elapsed_time)


cap.release()
print ("Done!")

cv2.destroyAllWindows()
user13214870
  • 21
  • 1
  • 3