Python Forum
cv2 problem when saving image
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
cv2 problem when saving image
#1
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)
Reply
#2
can you post the full traceback you get?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(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
Reply
#4
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()
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
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
Reply
#6
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#8
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem in saving .xlsm (excel) file using pandas dataframe in python shantanu97 2 4,161 Aug-29-2021, 12:39 PM
Last Post: snippsat
  saving only one line of a figure as an image (python matplotlib) nitrochloric 0 1,991 Nov-23-2020, 01:41 PM
Last Post: nitrochloric
  Problem posting image to clipboard noel 0 2,125 Sep-26-2020, 10:50 AM
Last Post: noel
  Problem loading an image dataset into Tensorflow MohammedSohail 1 1,719 Jun-09-2020, 02:09 PM
Last Post: nuffink
  Array problem in pylab module - Image processing bobfat 0 1,683 Dec-31-2019, 06:02 PM
Last Post: bobfat
  New to Python, help with saving image from camera tantony 2 3,782 Sep-13-2019, 05:19 PM
Last Post: tantony
  Problem with file not saving current output jameseroni 2 2,363 Oct-28-2018, 02:02 PM
Last Post: jameseroni
  csv file saving problem 7vinBB 1 2,276 Aug-01-2018, 09:07 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020