You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python视频车牌识别代码运行报错,求助解决Assertion failed问题

Fixing the OpenCV !_src.empty() Error in Your License Plate Recognition Code

Hey there, let's tackle this error you're facing—it's a common one when working with OpenCV, and it's pretty straightforward to resolve once you know what's going on.

What the Error Means

The error (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor' is OpenCV's way of telling you: the image you're trying to convert to grayscale (plateFrame) is empty. In other words, when you call preprocessOne(plateFrame, ...) in Main.py, the plateFrame variable doesn't hold a valid image—it might be None, or it might be a zero-sized array.

Why This Happens

There are a few likely culprits:

  • Your video stream (file or camera) isn't being read correctly—maybe the video path is wrong, the camera is occupied, or the stream ended.
  • The code that detects the plate region (which outputs plateFrame) didn't find any valid plate, so it returned an empty image.

Step-by-Step Fixes

1. Validate plateFrame Before Processing It

First, add a check in Main.py right before calling preprocessOne to make sure you're only passing valid images:

# Add this check before your existing call to preprocessOne
if plateFrame is not None and plateFrame.size > 0:
    plate_Original, plate_morphEx, edge = preprocessOne(plateFrame, (42,10), True)
else:
    print("Warning: No valid plate frame to process—skipping this step.")

2. Add Defensive Checks in preprocessOne

Modify your Basic.py function to handle empty inputs gracefully, instead of crashing:

def preprocessOne(plateFrame, se_shape, Show = False):
    # First, check if the input frame is valid
    if plateFrame is None or plateFrame.size == 0:
        print("Error: Input plate frame is empty. Cannot proceed with preprocessing.")
        return None, None, None  # Return empty values to avoid downstream errors
    
    plateOrigin = copy.copy(plateFrame)
    plateGray = enhance(cv2.cvtColor(plateFrame, cv2.COLOR_BGR2GRAY))
    plateGaussian = cv2.GaussianBlur(plateGray, (5,5), 0)
    plateSobel = cv2.Sobel(plateGaussian, -1, 1, 0)
    h, plateThresh = cv2.threshold(plateSobel, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
    se = cv2.getStructuringElement(cv2.MORPH_RECT, se_shape)
    plateMorphEx = cv2.morphologyEx(plateThresh, cv2.MORPH_CLOSE, se)
    edge = np.copy(plateThresh)
    
    # Rest of your function (like showing images if Show is True)
    return plateOrigin, plateMorphEx, edge

3. Check Your Video/Input Source

If plateFrame comes from a video stream:

  • For local video files: Double-check the file path is correct, and the file format is supported by OpenCV (MP4, AVI, etc.).
  • For camera inputs: Make sure the camera isn't being used by another program, and you're using the correct device index (e.g., cv2.VideoCapture(0) for the default camera, sometimes 1 for external cameras).
  • Always validate frame reads:
    cap = cv2.VideoCapture("your_video_path.mp4")  # Or camera index
    while cap.isOpened():
        ret, frame = cap.read()
        if not ret:
            print("Failed to read frame—video may have ended.")
            break
        # Your plate detection code here to get plateFrame
        # Then the check for plateFrame before preprocessing
    

Quick Debug Tip

To confirm plateFrame is the issue, add a print statement right before calling preprocessOne:

print(f"Plate frame shape: {plateFrame.shape if plateFrame is not None else 'None'}")

If it prints None or (0,0,3) (or similar zero dimensions), that confirms your input is empty.

内容的提问来源于stack exchange,提问作者Prajwal Dhage

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 09:08:26