YOLOv4摄像头识别捕获检测框时出现ImportError问题求助
ImportError: No module named detect Issue Hey, let's break down why you're hitting this import error and how to fix it, plus a few extra tweaks to your code to avoid other headaches later:
First, Why the Import Fails
Python can't find the detect module because it's not in the interpreter's default search path. Since you're using the Darknet YOLOv4 setup, that detect module is supposed to be the detect.py file from the official Darknet repo.
Step 1: Verify the detect.py File Exists
Double-check that you have the detect.py file in your Darknet root directory (it should sit alongside darknet.py). If it's missing, make sure you cloned the full Darknet repo or downloaded all necessary files.
Step 2: Add Darknet's Path to Python's Search List
If your script isn't inside the Darknet folder, Python won't automatically look there for modules. Add this at the very top of your code to point it to the right place:
import sys # Replace this with the absolute path to your Darknet root folder sys.path.append("/home/your-username/darknet")
Then proceed with your original imports. This tells Python where to find detect.py.
Step 3: Check for Case Sensitivity (Linux/macOS Users)
Make sure the file is actually named detect.py (all lowercase). Python is case-sensitive on Unix-like systems, so Detect.py or DETECT.py won't work with import detect.
Extra Code Fixes to Avoid Future Errors
While we're at it, let's fix a couple of issues in your current code that will cause problems once the import is sorted:
- Wrong File Mode & Format: You're opening a
.jpgfile with'w'mode and trying to write detection box data (which is text). Use a.txtfile instead, and manage the file properly with awithstatement (it auto-closes the file for you):
# Replace your result.open block with this with open(f'Desktop/captureyolobox/capture{count:04d}.txt', 'w') as result: for box in boxes: # Convert each box to a string and write it on a new line result.write(f"{box}\n")
- Directly Writing Box Objects: You can't write raw box objects to a file—you need to convert them to strings first, which the code above handles.
Once you make these changes, your import error should be gone, and your detection box data will save correctly.
内容的提问来源于stack exchange,提问作者help me

