-2

I am trying to save frames and their path into the Database and file storage, but somehow frames are not getting saved in the path I have provided, printing path seems to be fine , but I am only getting the directory which I am creating not the pictures inside of it , need help in this , thanks in advance

 def get_frames(cap=0):
    frames = cv2.VideoCapture(cap)
    now_utc = dt.now(timezone('UTC'))
    now_local = now_utc.astimezone(get_localzone())
    
    data = {}
        

    success,image = frames.read()
    scaling_factor = 0.5
    count = 0
    while True:
    
        if not success:
            break
                # save frame as JPEG file      
        success,image = frames.read()
        image = cv2.flip(image,1)
        #adding guassian blur
        image = cv2.resize(image, None, fx=scaling_factor, fy=scaling_factor, interpolation=cv2.INTER_AREA)
        
        # lower = [18, 50, 50]
        # upper = [35, 255, 255]
        # lower = np.array(lower, dtype="uint8")
        # upper = np.array(upper, dtype="uint8")

        # mask = cv2.inRange(hsv, lower, upper)
    
        # output = cv2.bitwise_and(image, hsv, mask=mask)
        
        # no_red = cv2.countNonZero(mask)
        # if int(no_red) > 15000:
        #     Fire_Reported = Fire_Reported + 1
        count += 1
        
        cv2.imshow("frame", image)

        

        ret,buffer = cv2.imencode('.jpeg',image)
        
       

        dtString =  dt.now().strftime('%d-%m-%Y')
        BASE_DIR = Path(__file__).resolve().parent.parent
        path = os.path.join(BASE_DIR,f'camera_frames_{dtString}') 

        if not os.path.exists(path):
            os.makedirs(path)
      
        imgName = os.path.join(path ,dt.now().strftime('%m/%d/%Y %I:%M:%S')+'.jpeg')

        saved_frame = cv2.imwrite(imgName, image) 

        if count <= 20:
            camera_ins = get_or_create(db.session,Cameras,alias_name='test_cam',camera_no=1)
            frame_ins = CameraFrames(date_time=now_local.strftime(format),camera_image=imgName)
            camera_ins.frames.append(frame_ins)
            db.session.add(camera_ins)
            db.session.add(frame_ins)
            db.session.commit()

        frame = buffer.tobytes()
        


        yield(b'--frame\r\n'
                b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
        

        if cv2.waitKey(10) == 27:
            break
       
5
  • 1
    Can you print imgName and check whether it is a legal filename (allowed characters and path) and whether the folder already exists? Can you try to change the name/path to something else that definitely works? Make sure your application has writing permissions to that folder. Commented Oct 17, 2021 at 10:18
  • 1
    According to google hit: "On Windows systems, files and directory names cannot be created with a colon (:)" Commented Oct 17, 2021 at 10:20
  • there are some white spaces involved C:\Users\atifs\Documents\video_detect\camera_frames_17-10-2021\10/17/2021 03:43:21.jpeg paths are like this @Micka Commented Oct 17, 2021 at 10:21
  • the strftime() call also contains forward slashes, which will be interpreted to separate additional directories. you simply didn't think about what a legal path is, or file name. Commented Oct 17, 2021 at 10:50
  • 1
    I guess the colon might be the problem. Can you try .strftime('%m/%d/%Y %I_%M_%S')+'.jpeg') and also try .jpg instead of .jpeg Commented Oct 17, 2021 at 11:16

1 Answer 1

1

as @Micka pointed out initially I was using wrong naming conventions in frames, which was failing imwrite and errors where falling silent, characters like : are not allowed , I simply removed those

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.