Windows7下Python3.6(PyCharm)运行OpenCV报-1073741819错误求助
Hey there, that exit code (0xC0000005) is an access violation error—meaning your program’s trying to access memory it shouldn’t. Let’s walk through the most likely fixes tailored to your setup:
1. Double-check your camera index
Your code uses cv2.VideoCapture(1), but Windows usually assigns the default built-in camera to index 0. External cameras might use higher indices, but they’re not guaranteed. Run this quick script to find working camera indices:
import cv2 for i in range(5): cap = cv2.VideoCapture(i) if cap.isOpened(): print(f"✅ Camera {i} is accessible") cap.release() else: print(f"❌ Camera {i} failed to open")
Replace the index in your original code with one that shows as accessible.
2. Match OpenCV version to Python 3.6 & Windows 7
Python 3.6 and Windows 7 are no longer supported by the latest OpenCV releases, which can cause compatibility crashes. Uninstall your current OpenCV and install a compatible version:
pip uninstall opencv-python -y pip install opencv-python==4.2.0.32
Version 4.2.0.32 is a reliable choice that works with Python 3.6 on Windows 7.
3. Run PyCharm as Administrator
Windows 7 sometimes restricts camera access to non-admin processes. Right-click your PyCharm shortcut, select Run as administrator, then re-run your script. This fixes permission-related memory access issues.
4. Reset your PyCharm Python interpreter
Corrupted interpreter configurations can break library loading. Here’s how to fix it:
- Go to
File > Settings > Project: [Your Project Name] > Python Interpreter - Check if
opencv-pythonappears in the list. If not, click the+button, search for it, and install the compatible version from step 2. - If it’s already listed, click the gear icon >
Show All, select your interpreter, then clickReload Listor delete and re-add the interpreter entirely.
5. Verify camera hardware & drivers
If your camera itself isn’t working, no code will fix this:
- Test the camera with a native Windows app (like Windows Live Messenger’s camera tool or a third-party webcam utility) to confirm it’s functional.
- Open Device Manager, find your camera under Imaging devices, right-click it, and select Update driver to ensure you have the latest compatible drivers for Windows 7.
6. Test with a minimal script
Strip down your code to the basics to rule out extra steps causing issues:
import cv2 cap = cv2.VideoCapture(0) # Use the working index from step 1 if not cap.isOpened(): print("Couldn't open camera") exit() while True: ret, frame = cap.read() if not ret: print("Lost camera stream, exiting...") break cv2.imshow('Camera Test', frame) if cv2.waitKey(1) == ord('q'): break cap.release() cv2.destroyAllWindows()
If this works, re-add the grayscale conversion step (gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)) to see if that’s the culprit (though this is unlikely for this error code).
内容的提问来源于stack exchange,提问作者Tôn Thất Minh Quân

