Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 1225

Python • Please help, python hates me.

$
0
0
I'm trying to get this camera
Photo on 1-31-24 at 9.18 AM.jpg
to identify if a thing is a person, then output it's location in the camera image as coordinates ([x,y] that I'll convert to rotation degrees, a problem for a later time).

I'm using Thonny because it was the default & I'm lazy, the code I am using is as follows.

Code:

import cv2import numpy as np# Function to detect and track people using Haar cascadesdef detect_and_track_people(frame, cascade):    # Convert frame to grayscale    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)    # Perform object detection    people = cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))    # Draw rectangles around the detected people and track them    for (x, y, w, h) in people:        cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)    return frame# Specify the path to the Haar cascade XML file for pedestrian detection (You can use other trained cascades for different objects)cascade_path = 'path_to_haar_cascade_xml_file'# Load Haar cascade for pedestrian detectioncascade = cv2.CascadeClassifier(cascade_path)# Create a VideoCapture object to access Raspberry Pi cameracap = cv2.VideoCapture(0)  # Use 0 for internal camera or 1 for external camera# Check if camera was successfully openedif not cap.isOpened():    print("Unable to open camera")    exit()# Read and display frames from the camerawhile True:    ret, frame = cap.read()  # Read frame from camera    if not ret:        print("Failed to read from camera")        break    # Detect and track people in the frame    tracked_frame = detect_and_track_people(frame, cascade)    # Display the resulting frame    cv2.imshow('Live Feed', tracked_frame)    # Exit on 'q' key press    if cv2.waitKey(1) & 0xFF == ord('q'):        break# Release VideoCapture and close windowscap.release()cv2.destroyAllWindows()

I am getting these errors which I'm unsure how to fix (pasting the terminal output).

>>> %Run 'CodeAI2.6 2 2.py'
[ERROR:0@0.233] global ./modules/core/src/persistence.cpp (505) open Can't open file: 'path_to_haar_cascade_xml_file' in read mode
[ WARN:0@0.388] global ./modules/videoio/src/cap_gstreamer.cpp (2401) handleMessage OpenCV | GStreamer warning: Embedded video playback halted; module v4l2src0 reported: Failed to allocate required memory.
[ WARN:0@0.389] global ./modules/videoio/src/cap_gstreamer.cpp (1356) open OpenCV | GStreamer warning: unable to start pipeline
[ WARN:0@0.389] global ./modules/videoio/src/cap_gstreamer.cpp (862) isPipelinePlaying OpenCV | GStreamer warning: GStreamer: pipeline have not been created
Failed to read from camera
>>>

Any help would be very nice, thanks!

Statistics: Posted by peterkaminskas — Wed Jan 31, 2024 2:26 pm



Viewing all articles
Browse latest Browse all 1225

Trending Articles