Question: i have this code import cv 2 import numpy as np import time # Global variables to store the points of the rectangle and frame

i have this code
import cv2
import numpy as np
import time
# Global variables to store the points of the rectangle and frame copy
rectangle_points =[]
frame_copy = None
detected_contour = None
def mouse_callback(event, x, y, flags, param):
global rectangle_points, frame_copy, detected_contour
if event == cv2.EVENT_LBUTTONDOWN:
rectangle_points =[(x, y)]
elif event == cv2.EVENT_LBUTTONUP:
rectangle_points.append((x, y))
if len(rectangle_points)==2:
click_point = rectangle_points[0]
detected_contour = detect_clicked_object(frame_copy, click_point)
rectangle_points =[]
def draw_contour(frame, contour):
if contour is not None:
cv2.drawContours(frame,[contour],0,(0,255,0),3)
def detect_clicked_object(frame, click_point):
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray,50,150, apertureSize=3)
contours, _= cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for contour in contours:
if cv2.pointPolygonTest(contour, click_point, False)>=0:
approx = cv2.approxPolyDP(contour,0.02* cv2.arcLength(contour, True), True)
if len(approx)==4: # If the contour is a rectangle
return approx
return None
def check_collision(frame, main_contour):
if main_contour is None:
return False
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray,50,150, apertureSize=3)
contours, _= cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
main_contour_np = np.array(main_contour).reshape((-1,1,2))
for contour in contours:
if not np.array_equal(contour, main_contour_np):
for point in contour:
if cv2.pointPolygonTest(main_contour_np,(int(point[0][0]), int(point[0][1])), False)>=0:
return True
return False
def main():
global frame_copy, detected_contour
# Open the camera
cap = cv2.VideoCapture(0)
if not cap.isOpened():
print("Error: Could not open camera.")
return
cv2.namedWindow('Frame')
cv2.setMouseCallback('Frame', mouse_callback)
last_check_time = time.time()
while True:
ret, frame = cap.read()
if not ret:
print("Error: Failed to capture image.")
break
frame_copy = frame.copy()
# Draw the detected contour if it exists
draw_contour(frame_copy, detected_contour)
# Check for collision every second
if time.time()- last_check_time >=1:
if check_collision(frame_copy, detected_contour):
print("Collision detected!")
last_check_time = time.time()
cv2.imshow('Frame', frame_copy)
key = cv2.waitKey(1) & 0xFF
if key ==27: # Press 'Esc' to exit
break
cap.release()
cv2.destroyAllWindows()
if __name__=="__main__":
main()
it works in my computer with my webcam really well what i need is an adjusted version of this same exact code for raspberry pi 5 and i am using pi camera 3

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!