11

Full warning message:

[ WARN:0] global C:\projects\opencv-python\opencv\modules\videoio\src\cap_msmf.cpp (674) SourceReaderCB::~SourceReaderCB terminating async callback  

Code:

import numpy as np
import cv2

captureDevice = cv2.VideoCapture(0) #captureDevice = camera

while True:
    ret, frame = captureDevice.read() 

    cv2.imshow('my frame', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

captureDevice.release()
cv2.destroyAllWindows()

Description:
When it runs, the my frame windows appear and when I terminate the code, it gives me that warning message.

Python: 3.7.4
OpenCV (cv2): 4.1.2
OS: Windows 10

I don't know to fix this warning and why it gets me. Hope you help me in fixing and understanding that.
In addition, answers of this link didn't help me anyway.

Thanks in advance.

M. Rostami
  • 999
  • 3
  • 16
  • 27

2 Answers2

47

This seems to be a bug in MSMF backend of opencv. You can see more details in this issue.

I don't think this problem exists on Linux platforms. So I am providing the solution for windows.

Windows only solution

For windows platform, you can change the backend to something else (most preferably DirectShow backend. For this, add to your VideoCapture like below:

captureDevice = cv2.VideoCapture(0, cv2.CAP_DSHOW) #captureDevice = camera

This works for OpenCV>=3.4.

Rahat Zaman
  • 1,322
  • 14
  • 32
  • 1
    Equivalent code in C# for reference: `using var capture = new VideoCapture(0, VideoCapture.API.DShow)` – Fiach Reid Dec 29 '20 at 17:49
  • This indeed avoids the warning, although I verify in my code that DSHOW was being automatically selected by OpenCV in my setup even without having specifically specified cv2.CAP_DHOW so explicitly. – matanster Jun 17 '22 at 10:31
1

A warning message is not an error!

But if that bothers you so much, you might try to disable MSMF by setting the following Environment Variable to 0 on Windows:

OPENCV_VIDEOIO_PRIORITY_MSMF

How to set the path and environment variables in Windows

karlphillip
  • 92,053
  • 36
  • 243
  • 426
  • Thanks for the answer. I did it but the warning message still here. – M. Rostami Feb 17 '20 at 07:52
  • 1
    Just remember that after changing an environment variable, you must close and reopen the application that is used to launch your OpenCV code. – karlphillip Feb 17 '20 at 09:45
  • In my case, adding this envi. variable in windows, I can access the video stream faster and without warning. Unfortunately, after this, I can't use function from cv2.VideoCapture(0) such cam.get(cv2.CAP_PROP_POS_FRAMES)... – Alex C. Jan 14 '21 at 02:05