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

树莓派OpenCV报错libhdf5_serial.so.103缺失的解决求助

解决方案

1. 修复Raspbian Buster镜像源

Buster属于旧版本系统,官方源可能失效,先替换为可用镜像源:

  • 打开源配置文件:sudo nano /etc/apt/sources.list
  • 替换原有内容为:
deb http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/ buster main non-free contrib rpi
deb-src http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/ buster main non-free contrib rpi
  • 保存退出(按Ctrl+O回车确认,Ctrl+X退出),更新源:sudo apt update

2. 彻底清理混乱的OpenCV安装

移除所有通过pip和apt安装的OpenCV包,避免版本冲突:

# 卸载pip安装的OpenCV
pip uninstall -y opencv-python opencv-contrib-python
# 卸载apt安装的OpenCV
sudo apt remove -y python-opencv python3-opencv
# 清理残留依赖
sudo apt autoremove -y

3. 安装缺失的libhdf5库

通过apt安装对应版本的依赖库:

sudo apt install -y libhdf5-serial-dev libhdf5-103

4. 重新安装带跟踪模块的OpenCV

针对需要MultiTracker_create的需求,重新安装指定版本的OpenCV包:

pip install opencv-python==4.4.0.46 opencv-contrib-python==4.4.0.46

5. 验证安装

运行以下代码检查OpenCV是否正常加载:

import cv2
print(cv2.__version__)
print(cv2.MultiTracker_create)

若未报错,说明安装成功,可重新运行你的毕业设计脚本。


原问题详情

在树莓派(Raspbian Buster系统)开展本科毕业设计,最初Python脚本可正常运行,但因所用OpenCV 4.4.0缺少目标跟踪模块,未卸载旧版本便执行:

pip install opencv-python==4.4.0.46
pip install opencv-contrib-python==4.4.0.46

此后脚本持续报错:

libhdf5_serial.so.103: cannot open shared object file: no such file or directory

尝试过卸载并重装OpenCV(使用sudo apt-get install python-opencv),但问题仍未解决。经查询推测是缺少libhdf5库,多次尝试安装却因镜像源失效失败。

此前运行正常、仅缺少MultiTracker_create模块的代码如下:

import cv2
import pickle
import numpy as np

# Load the saved RandomForestClassifier model using pickle
with open('saved_model.pkl', 'rb') as model_file:
    rf_classifier = pickle.load(model_file)

# Function to calculate frame difference
def frame_difference(prev_frame, current_frame):
    diff = cv2.absdiff(prev_frame, current_frame)
    _, threshold = cv2.threshold(diff, 30, 255, cv2.THRESH_BINARY)
    return threshold

def getObjects(img, thres, nms, tracker, draw=True, objects=[]):
    # Preprocess the image
    img_resized = cv2.resize(img, (64, 64))
    img_flattened = img_resized.flatten()

    # Reshape the flattened image to match the input shape the RandomForestClassifier expects
    img_flattened_reshaped = img_flattened.reshape(1, -1)

    # Make predictions using the RandomForestClassifier
    binary_prediction = rf_classifier.predict(img_flattened_reshaped)[0]  # Assuming binary classification

    # Initialize bounding box and class name
    box = (0, 0, 64, 64)
    className = ''

    # Continue with the rest of the code for drawing bounding boxes
    if binary_prediction == 1:  # Replace this condition with your actual logic
        # You may need to adjust this based on your RandomForestClassifier output
        box = (0, 0, 64, 64)  # Replace with the actual bounding box coordinates
        className = 'chicken'  # Replace with the actual class name
        print("Binary Prediction:", binary_prediction)
        
        if draw:
            cv2.rectangle(img, (box[0], box[1]), (box[0] + box[2], box[1] + box[3]), color=(0, 255, 0), thickness=2)
            cv2.putText(img, className.upper(), (box[0] + 10, box[1] + 30),
                        cv2.FONT_HERSHEY_COMPLEX, 1, (0, 255, 0), 2)

        # Start tracking the object using the tracker
        tracker.init(img, (box[0], box[1], box[2], box[3]))

    # Update the tracker and get the new bounding box
    success, tracked_box = tracker.update(img)
    tracked_box = tuple(map(int, tracked_box))

    # Apply Non-Maximum Suppression
    if nms:
        objects.append((tracked_box, className))
        objects = apply_nms(objects)

    return img, objects, tracked_box

def apply_nms(objects):
    # Convert box coordinates to format (x1, y1, x2, y2)
    boxes = np.array([(box[0], box[1], box[0] + box[2], box[1] + box[3]) for box, _ in objects], dtype=np.float32)

    # Get scores (not used in this example, you may have scores from your model)
    scores = np.zeros(len(objects), dtype=np.float32)

    # Apply Non-Maximum Suppression
    indices = cv2.dnn.NMSBoxes(boxes.tolist(), scores, 0.5, 0.4)

    # Filter out non-maximum boxes
    filtered_objects = [objects[i[0]] for i in indices]

    return filtered_objects

# Set up the camera
cap = cv2.VideoCapture(0)
cap.set(3, 640)  # Adjusted camera width
cap.set(4, 480)  # Adjusted camera height

# Create a tracker (e.g., KCF tracker)
tracker = cv2.MultiTracker_create()

# Read the first frame
success, prev_frame = cap.read()

# Initialize objects list outside the loop
detected_objects = []

while True:
    success, current_frame = cap.read()

    # Calculate frame difference
    threshold = frame_difference(prev_frame, current_frame)

    # Use the RandomForestClassifier predictions instead of OpenCV DNN
    result, detected_objects, tracked_box = getObjects(current_frame, 0.2, nms=True, tracker=tracker, objects=detected_objects)

    # Draw bounding boxes only when objects are detected
    for (box, className) in detected_objects:
        cv2.rectangle(current_frame, (box[0], box[1]), (box[2], box[3]), color=(0, 255, 0), thickness=2)
        cv2.putText(current_frame, className.upper(), (box[0] + 10, box[1] + 30),
                    cv2.FONT_HERSHEY_COMPLEX, 1, (0, 255, 0), 2)

    cv2.imshow("Output", current_frame)
    cv2.waitKey(1)

    # Update the previous frame for the next iteration
    prev_frame = current_frame

内容的提问来源于stack exchange,提问作者Darren Tiew

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 06:24:53