Python Forum

Full Version: cv2 problem when saving image
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello. I'm wiriting a program that saves the first and the last frame from a video file. Everything is woking perfectly until I try to save the first frame of the video to my PC.
The error happens on line 17 and it says:
TypeError: Expected Ptr<cv::UMat> for argument '%s'

Thank you for any advice you can provide.

def FindScore(path):
    
    cam = cv2.VideoCapture(path)

    last_frame = int(cam.get(cv2.CAP_PROP_FRAME_COUNT))
    first_frame = "1"
    framerate = int(cam.get(cv2.CAP_PROP_FPS)) # ni pomembno
    relative = int(cam.get(cv2.CAP_PROP_POS_AVI_RATIO)) # ni pomembno

    print("Video frames: {} ".format(last_frame) )
    print("Framerate: {} fps".format(framerate))

    #  Find first frame
    cam.set(1,2-1)
    frame = cam.read()
    name = './data/frame' + str(last_frame) + '.jpg'
    cv2.imwrite(name, frame)
    myimage1 = Image.open(name)
can you post the full traceback you get?
(Jan-21-2020, 01:22 PM)buran Wrote: [ -> ]can you post the full traceback you get?

Error:
Traceback (most recent call last): File ".\LeagueClipSort.py", line 87, in <module> FindScore(filepath) File ".\LeagueClipSort.py", line 35, in FindScore cv2.imwrite(name, frame) TypeError: Expected Ptr<cv::UMat> for argument '%s'
Here is the full code
Looking at this
https://opencv-python-tutroals.readthedo...splay.html
cam.read() would return tuple and frame is the second element of that tuple
I would try to change
Error:
frame = cam.read()
to
ret, frame = cam.read()
That worked, thank you so much.

If you don't mind, I have another (probably very noobish) problem.
Since tesseract doesn't always find the required data from the video screenshots (they are league of legends screenshots), I added try/except in the main loop (line 86-92), but except doesn't work because the file is used by another process. I thought cam.release() would help, but the video file is still open even after that. Thank you again for your help.

Full traceback
Analyzing: D:/testing\a (10).mp4
Video frames: 384
Framerate: 30 fps
Traceback (most recent call last):
File ".\LeagueClipSort.py", line 87, in <module>
FindScore(filepath)
File ".\LeagueClipSort.py", line 52, in FindScore
kills_start = int(text1[text1.find(char1)+1 : text1.find(char2)])
ValueError: invalid literal for int() with base 10: 'Canna @ re TCraIL IE)\n\nvs38 £18'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File ".\LeagueClipSort.py", line 92, in <module>
os.rename(filepath, namerino)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'D:/testing\\a (10).mp4' -> '000---League of Legends.mp4'

The Code
Can you post your current code in python tags (minimal reproducible example)? also post traceback in error tags.

I would guess the problem is in myimage1 = Image.open(name)
but it's not clear what Image is (Pillow.Image?) or do you close it

Sorry - I saw the code
The problem is that the exception happens on line 52 - it try to convert str Canna @ re TCraIL IE)\n\nvs38 £18 to int which raise Value Error. At that time cam is not released thus mp4 file is still used by another process which in turn raise the second error.

So some advise - release cam immaterially when it is no longer needed - this should solve the current issue.
in addition
  • use with context manager to open the Image object - https://pillow.readthedocs.io/en/stable/...e-handling
  • don't use general except (catch-all) like on 89.
  • you may want to wrap lines 52-54 in try except of their own that catch ValueError during conversion to int
  • you may want to break FindScore() function in to smaller functions - e.g. separate function to grab frame, or first and last frame, another one to calculate scores, etc.
(Jan-21-2020, 03:30 PM)buran Wrote: [ -> ]The problem is that the exception happens on line 52 - it try to convert str Canna @ re TCraIL IE)\n\nvs38 £18 to int which raise Value Error. At that time cam is not released thus mp4 file is still used by another process which in turn raise the second error.

So some advise - release cam immaterially when it is no longer needed - this should solve the current issue.
in addition
  • use with context manager to open the Image object - https://pillow.readthedocs.io/en/stable/...e-handling
  • don't use general except (catch-all) like on 89.
  • you may want to wrap lines 52-54 in try except of their own that catch ValueError during conversion to int
  • you may want to break FindScore() function in to smaller functions - e.g. separate function to grab frame, or first and last frame, another one to calculate scores, etc.

Thanks, thats a lot of very usefull information.