0
votes

I have a CPU problem while reading and writing frames from IP camera:

What should be the problematic in this code snippet which results in high CPU overload?

Here are I am suspecting to be the reason of CPU overload:

  • 1920x1080 streaming without decreasing FPS of stream (which is 25)
  • Image resizing function of OpenCV
  • fourcc Codec choice (.mp4 is chosen)
  • Stream FPS and Desired FPS unmatching (Stream:25 , Desired FPS: 1/60 )
  • Using headless server (I cannot show images due to this but i am not sure this might be the reason)

Things I tried to slow down CPU usage but did not work well:

  • time.sleep(0.1)
  • cv.waitKey(1000*int(0.1*(1/fps))
    import os
    import cv2 as cv
    import numpy as np
    import time
    import math
    from datetime import datetime
    import re
    
    cap = cv.VideoCapture('rtsp://**.***.**.*:*****/************',cv.CAP_FFMPEG)
         
    def frameNaming():
      today = datetime.now()
      date1 = today.strftime("%d/%m/%Y %H:%M:%S")
      abc = re.findall(r'\d+',date1)
      frameName = ''.join(abc)
      return frameName
    
    down_height = 720
    down_width = 1280
    down_points = (down_width, down_height)
    
    fpsSystem = cap.get(cv.CAP_PROP_FPS)
    print("Fps of System",fpsSystem)
    frameRead       = 0             # successfully readed frame number
    fps = 1.0/60
    print("Desired FPS of video",fps)
    DesFrameCount =  60 # Desired frame count
    print("Desired Frame Number in Video: %.1f"%(DesFrameCount))
    takeFrameTime = 1/fps # every TakeFrameTime seconds frames will be stored
    
    frameCycle      = 0             # every frameCycle. frame will be stored
    frameWritten = 0
    firstStart = time.time()
    start=firstStart
    print("First Start Time",firstStart)
    print("time", frameNaming())
    success = True
    # Define the codec and create VideoWriter object
    fourcc = cv.VideoWriter_fourcc(*'mp4v')
    randomName = np.random.randint(0,1000)
    out = cv.VideoWriter('output'+str(randomName) + "_60min_.mp4", fourcc, fps, (down_width,  down_height))

    while success:   
        success, frame = cap.read()
        #time.sleep(0.1)
        if not success:
            print("Can't receive frame (stream end?). Exiting ...")
            break
        else:
            frameRead += 1
            frameCycle += 1
        frame = cv.resize(frame,down_points,interpolation= cv.INTER_AREA)

        if frameCycle == math.floor(fpsSystem*takeFrameTime): # Stop the loop when desired number of Frame obtained
            frameCycle  = 0
            out.write(frame)
            frameWritten += 1
            timeNow = frameNaming()
            datenow = timeNow[:-6]
            hournow = timeNow[-6:]
            print("Frame written Time", hournow)
        if  (cv.waitKey(1000*int(0.1*(1/fps))) & 0xFF == ord('q')) or (frameWritten == DesFrameCount):
            print("Curr Time")
            print("stop Time",time.time()-firstStart)
            print("firstStart",firstStart)
            print("Read Frame Count",frameRead)
            print("Written Frame Count",frameWritten)
            print("Frame written Date", datenow)
            break
    # Release everything if job is finished
    cap.release()
    out.release()
    cv.destroyAllWindows()

Here is the CPU load:

CPU load