Skip to content

Having an issue with this code for caffemodel_path and prototxt_path #1

Open
@Ph03nix4213

Description

@Ph03nix4213

as listed in the code below I'm having this issue any help would be appreciated:

Error:
Exception has occurred: error
OpenCV(4.9.0) D:\a\opencv-python\opencv-python\opencv\modules\dnn\src\caffe\caffe_io.cpp:1126: error: (-2:Unspecified error) FAILED: fs.is_open(). Can't open "c:\Users\harri\Desktop\HUD\deploy_age.prototxt" in function 'cv::dnn::ReadProtoFromTextFile'
File "C:\Users\harri\Desktop\HUD\hud_with_age_estimation.py", line 31, in main
age_net = cv2.dnn.readNetFromCaffe(prototxt_path, caffemodel_path)
File "C:\Users\harri\Desktop\HUD\hud_with_age_estimation.py", line 79, in
main()

Code:
import cv2
import os

def apply_overlay(frame, track_window, age):
# Add a rectangle overlay on the frame with age information
x, y, w, h = track_window
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.putText(frame, f"Age: {age}", (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)

def main():
# Get the absolute path to the current script's directory
script_dir = os.path.dirname(os.path.abspath(file))

# Specify the absolute paths to the model files
prototxt_path = os.path.join(script_dir, r"deploy_age.prototxt")
caffemodel_path = os.path.join(script_dir, r"age_net.caffemodel")


cap = cv2.VideoCapture(0)

if not cap.isOpened():
    print("Error: Could not open webcam.")
    return

# Load the pre-trained Haar Cascade classifier for face detection
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')

# Load the pre-trained age estimation model
age_net = cv2.dnn.readNetFromCaffe(prototxt_path, caffemodel_path)

while True:
    ret, frame = cap.read()

    if not ret:
        print("Error: Could not read frame.")
        break

    # Convert the frame to grayscale for face detection
    gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Detect faces in the frame
    faces = face_cascade.detectMultiScale(gray_frame, scaleFactor=1.3, minNeighbors=5)

    # If faces are detected, track the first face found
    if len(faces) > 0:
        x, y, w, h = faces[0]
        track_window = (x, y, w, h)

        # Extract the face region for age estimation
        face_roi = frame[y:y + h, x:x + w]

        # Preprocess the face region for age estimation
        blob = cv2.dnn.blobFromImage(face_roi, scalefactor=1.0, size=(227, 227), mean=(78.4263377603, 87.7689143744, 114.895847746), swapRB=False)
        age_net.setInput(blob)

        # Perform age estimation
        age_predictions = age_net.forward()
        age = int(age_predictions[0][0])  # Get the age prediction

    else:
        track_window = (0, 0, 0, 0)
        age = 0

    # Apply overlays
    apply_overlay(frame, track_window, age)

    # Display the frame in a window
    cv2.imshow("Webcam HUD", frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

if name == "main":
main()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions