pythonimageimage-processingmotion-detection

How do I save particular frames of video into folders using python?


I have got a code that does motion detection. It takes video as an input and then when an object comes to the scene, background changes and code publishes "object is seen" as text on that detected frame.

My question is that how do I save frames "when objects are seen" into folders( both for colorful and gray images)? I understand that I can write that frames using " cv2.imwrite("frame%d.jpg" % count, resizedFrame)" but unfortunately it does not work.

How do I save those detected frames to "colorful" and "gray" folders?


Solution

  • You have to read the image first by cv2.imread() and then cv2.imwrite() here is a small example

    import
    cv2
     
    # read image as grey scale
    grey_img=cv2.imread('/home/img/python.png',cv2.IMREAD_GRAYSCALE)
     # save image
    status=cv2.imwrite('/home/img/python_grey.png',grey_img)
     print("Image written to file-system : ",status)
    

    If you're don't have the folder path then use this status=cv2.imwrite('python_grey.png',grey_img) it will save the photo in default folder in which you save your .py file

    if you want to save different images here is the code

    import cv2
    import time
    
    # for number of images you need different names so set name automatically
    name=str(time.time())+str(.png))
     # read image as grey scale
    grey_img=cv2.imread('/home/img/python.png',cv2.IMREAD_GRAYSCALE)
     # save image
    status=cv2.imwrite('%s'%name,grey_img)
     print("Image written to file-system : ",status)