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

Python • Re: Please help, python hates me.

$
0
0
cap = cv2.VideoCapture(0) won't work in bookworm.

try picamera2.. example below


where have to put the haar_cascade_xml_file ?

Code:

import cv2import numpy as npfrom picamera2 import Picamera2# start Pi camerapicam2 = Picamera2()picam2.configure(picam2.create_preview_configuration(main={"format": 'XRGB8888', "size": (640, 480)}))picam2.start()   # 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)# Read and display frames from the camerawhile True:    frame = picam2.capture_array("main")    frame = cv2.cvtColor(frame, cv2.COLOR_BGRA2BGR) # Read frame from camera    # 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(100) & 0xFF == ord('q'):        break# Release VideoCapture and close windowscap.release()cv2.destroyAllWindows()
This code spat out a bunch of errors, I don't have them saved right now but I will (hopefully) be able to get them by tomorrow

Statistics: Posted by peterkaminskas — Wed Feb 07, 2024 6:45 pm



Viewing all articles
Browse latest Browse all 5346

Trending Articles