Initially my python script can run normally, but due to the missing of the object tracking module in the version of my OpenCV (4.4.0), I installed another version of OpenCV, but then the python script shows this error message ever since:
libhdf5_serial.so.103: cannot open shared object file: no such file or directory.
I have tried uninstalling and installing the OpenCV library, but the issue still persists. All work is done on a raspberry pi powered by Raspbian Buster by the way. In need of some help, I am working on my College Final Year Project… ToT
Below is my coding, it used to work fine except that the Multitracker_create module was missing as mentioned above:
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
' ' '