问题
I am reading an image and then displaying the image using cv2.imshow however this crashes my script with the below stack trace.
Code
from flask_restful import Resource
from flask import jsonify, request
from .repository import DocumentRepository
import cv2
class DocumentsResource(Resource):
def __init__(self):
self.document_repository = DocumentRepository()
def post(self):
img = cv2.imread('front.jpg')
cv2.imshow('front.jpg', img)
cv2.waitKey(0)
Stack Trace
2018-10-23 11:35:09.828 python3[47256:2814041] * Assertion failure in +[NSUndoManager _endTopLevelGroupings], /BuildRoot/Library/Caches/com.apple.xbs/Sources/Foundation/Foundation-1451/Foundation/Misc.subproj/NSUndoManager.m:361 2018-10-23 11:35:09.830 python3[47256:2814041] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+[NSUndoManager(NSInternal) _endTopLevelGroupings] is only safe to invoke on the main thread.' *** First throw call stack: ( 0 CoreFoundation 0x00007fff32116fcb __exceptionPreprocess + 171 1 libobjc.A.dylib
0x00007fff58db0c76 objc_exception_throw + 48 2 CoreFoundation
0x00007fff3211cd62 +[NSException raise:format:arguments:] + 98 3
Foundation 0x00007fff3421b300 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 193 4
Foundation 0x00007fff341a9e54 +[NSUndoManager(NSPrivate) _endTopLevelGroupings] + 469 5 AppKit 0x00007fff2f668e56 -[NSApplication run] + 997 6 QtGui
0x000000010dc27c12 _ZN26QEventDispatcherMacPrivate22ensureNSAppInitializedEv + 128 7 QtGui 0x000000010dc274ad _ZN19QEventDispatcherMac13processEventsE6QFlagsIN10QEventLoop17ProcessEventsFlagEE + 693 8 QtCore 0x000000010d335d9f _ZN16QCoreApplication13processEventsE6QFlagsIN10QEventLoop17ProcessEventsFlagEE + 81 9 cv2.cpython-37m-darwin.so 0x00000001082677ef cvWaitKey + 207 10 cv2.cpython-37m-darwin.so
0x0000000108267e37 _ZN11GuiReceiver12createWindowE7QStringi + 263 11 cv2.cpython-37m-darwin.so 0x0000000108267c6c cvNamedWindow + 540 12 cv2.cpython-37m-darwin.so 0x000000010826a3a1 _ZN11GuiReceiver9showImageE7QStringPv + 161 13 cv2.cpython-37m-darwin.so 0x000000010826a24c cvShowImage + 572 14 cv2.cpython-37m-darwin.so 0x000000010826298b _ZN2cv6imshowERKNS_6StringERKNS_11_InputArrayE + 475 15 cv2.cpython-37m-darwin.so 0x0000000107853114 _ZL18pyopencv_cv_imshowP7_objectS0_S0_ + 404 16 Python 0x0000000105e2fa4e _PyMethodDef_RawFastCallKeywords + 541 17 Python 0x0000000105e2ef8b _PyCFunction_FastCallKeywords + 44 18 Python
0x0000000105ec7ad7 call_function + 554 19 Python
0x0000000105ebfb57 _PyEval_EvalFrameDefault + 5281 20 Python
0x0000000105e2f379 function_code_fastcall + 117 21 Python
0x0000000105e2fd21 _PyObject_Call_Prepend + 150 22 Python
0x0000000105e2f0a2 PyObject_Call + 137 23 Python
0x0000000105ebfebd _PyEval_EvalFrameDefault + 6151 24 Python
0x0000000105ec83ff _PyEval_EvalCodeWithName + 1747 25 Python
0x0000000105e2eb8a _PyFunction_FastCallDict + 441 26 Python
0x0000000105e2fd21 _PyObject_Call_Prepend + 150 27 Python
0x0000000105e2f0a2 PyObject_Call + 137 28 Python
0x0000000105ebfebd _PyEval_EvalFrameDefault + 6151 29 Python
0x0000000105ec83ff _PyEval_EvalCodeWithName + 1747 30 Python
0x0000000105e2eb8a _PyFunction_FastCallDict + 441 31 Python
0x0000000105ebfebd _PyEval_EvalFrameDefault + 6151 32 Python
0x0000000105ec83ff _PyEval_EvalCodeWithName + 1747 33 Python
0x0000000105e2eb8a _PyFunction_FastCallDict + 441 34 Python
0x0000000105ebfebd _PyEval_EvalFrameDefault + 6151 35 Python
0x0000000105e2f379 function_code_fastcall + 117 36 Python
0x0000000105ec7b42 call_function + 661 37 Python
0x0000000105ebfb3e _PyEval_EvalFrameDefault + 5256 38 Python
0x0000000105e2f379 function_code_fastcall + 117 39 Python
0x0000000105ec7b42 call_function + 661 40 Python
0x0000000105ebfb3e _PyEval_EvalFrameDefault + 5256 41 Python
0x0000000105e2f379 function_code_fastcall + 117 42 Python
0x0000000105ec7b42 call_function + 661 43 Python
0x0000000105ebfb3e _PyEval_EvalFrameDefault + 5256 44 Python
0x0000000105e2f379 function_code_fastcall + 117 45 Python
0x0000000105e2fd21 _PyObject_Call_Prepend + 150 46 Python
0x0000000105e6df5b slot_tp_call + 71 47 Python
0x0000000105e2ede0 _PyObject_FastCallKeywords + 381 48 Python
0x0000000105ec7b3b call_function + 654 49 Python
0x0000000105ebfb57 _PyEval_EvalFrameDefault + 5281 50 Python
0x0000000105e3afe4 gen_send_ex + 242 51 Python
0x0000000105ebf28e _PyEval_EvalFrameDefault + 3032 52 Python
0x0000000105ec83ff _PyEval_EvalCodeWithName + 1747 53 Python
0x0000000105e2ef50 _PyFunction_FastCallKeywords + 225 54 Python
0x0000000105ec7b42 call_function + 661 55 Python
0x0000000105ebfbfd _PyEval_EvalFrameDefault + 5447 56 Python
0x0000000105ec83ff _PyEval_EvalCodeWithName + 1747 57 Python
0x0000000105e2ef50 _PyFunction_FastCallKeywords + 225 58 Python
0x0000000105ec7b42 call_function + 661 59 Python
0x0000000105ebfb3e _PyEval_EvalFrameDefault + 5256 60 Python
0x0000000105e2f379 function_code_fastcall + 117 61 Python
0x0000000105ec7b42 call_function + 661 62 Python
0x0000000105ebfb3e _PyEval_EvalFrameDefault + 5256 63 Python
0x0000000105e2f379 function_code_fastcall + 117 64 Python
0x0000000105ec7b42 call_function + 661 65 Python
0x0000000105ebfb57 _PyEval_EvalFrameDefault + 5281 66 Python
0x0000000105e2f379 function_code_fastcall + 117 67 Python
0x0000000105ec7b42 call_function + 661 68 Python
0x0000000105ebfb3e _PyEval_EvalFrameDefault + 5256 69 Python
0x0000000105e2f379 function_code_fastcall + 117 70 Python
0x0000000105e2fd21 _PyObject_Call_Prepend + 150 71 Python
0x0000000105e6e96f slot_tp_init + 80 72 Python
0x0000000105e6b464 type_call + 178 73 Python
0x0000000105e2ede0 _PyObject_FastCallKeywords + 381 74 Python
0x0000000105ec7b3b call_function + 654 75 Python
0x0000000105ebfb57 _PyEval_EvalFrameDefault + 5281 76 Python
0x0000000105e2f379 function_code_fastcall + 117 77 Python
0x0000000105ec7b42 call_function + 661 78 Python
0x0000000105ebfb3e _PyEval_EvalFrameDefault + 5256 79 Python
0x0000000105e2f379 function_code_fastcall + 117 80 Python
0x0000000105e2fd21 _PyObject_Call_Prepend + 150 81 Python
0x0000000105e2f0a2 PyObject_Call + 137 82 Python
0x0000000105ebfebd _PyEval_EvalFrameDefault + 6151 83 Python
0x0000000105e2f379 function_code_fastcall + 117 84 Python
0x0000000105ec7b42 call_function + 661 85 Python
0x0000000105ebfb3e _PyEval_EvalFrameDefault + 5256 86 Python
0x0000000105e2f379 function_code_fastcall + 117 87 Python
0x0000000105ec7b42 call_function + 661 88 Python
0x0000000105ebfb3e _PyEval_EvalFrameDefault + 5256 89 Python
0x0000000105e2f379 function_code_fastcall + 117 90 Python
0x0000000105e2fd21 _PyObject_Call_Prepend + 150 91 Python
0x0000000105e2f0a2 PyObject_Call + 137 92 Python
0x0000000105f2ecea t_bootstrap + 71 93 libsystem_pthread.dylib
0x00007fff59c2b6c1 _pthread_body + 340 94 libsystem_pthread.dylib
0x00007fff59c2b56d _pthread_body + 0 95 libsystem_pthread.dylib
0x00007fff59c2ac5d thread_start + 13 ) libc++abi.dylib: terminating with uncaught exception of type NSException
operating system
macOS High Sierra version 10.13.3
architecture (e.g. x86)
x64
opencv-python version
*
回答1:
You're trying to mix two worlds. Flask (via Werkzeug underneath it) does some I/O-rewiring based on the assumption of running headless. It's possible that what you're seeing is what what looks like when you violate that assumption on a Mac.
来源:https://stackoverflow.com/questions/52946974/cv2-imshow-crashing-libcabi-dylib-terminating-with-uncaught-exception-of-ty