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 cv
import numpy as np
import time
# Global variables to store the points of the rectangle and frame copy
rectanglepoints
framecopy None
detectedcontour None
def mousecallbackevent x y flags, param:
global rectanglepoints, framecopy, detectedcontour
if event cvEVENTLBUTTONDOWN:
rectanglepoints x y
elif event cvEVENTLBUTTONUP:
rectanglepoints.appendx y
if lenrectanglepoints:
clickpoint rectanglepoints
detectedcontour detectclickedobjectframecopy, clickpoint
rectanglepoints
def drawcontourframe contour:
if contour is not None:
cvdrawContoursframecontour
def detectclickedobjectframe clickpoint:
gray cvcvtColorframe cvCOLORBGRGRAY
edges cvCannygray apertureSize
contours, cvfindContoursedges cvRETREXTERNAL, cvCHAINAPPROXSIMPLE
for contour in contours:
if cvpointPolygonTestcontour clickpoint, False:
approx cvapproxPolyDPcontour cvarcLengthcontour True True
if lenapprox: # If the contour is a rectangle
return approx
return None
def checkcollisionframe maincontour:
if maincontour is None:
return False
gray cvcvtColorframe cvCOLORBGRGRAY
edges cvCannygray apertureSize
contours, cvfindContoursedges cvRETREXTERNAL, cvCHAINAPPROXSIMPLE
maincontournp nparraymaincontourreshape
for contour in contours:
if not nparrayequalcontour maincontournp:
for point in contour:
if cvpointPolygonTestmaincontournpintpoint intpoint False:
return True
return False
def main:
global framecopy, detectedcontour
# Open the camera
cap cvVideoCapture
if not cap.isOpened:
printError: Could not open camera."
return
cvnamedWindowFrame
cvsetMouseCallbackFrame mousecallback
lastchecktime time.time
while True:
ret, frame cap.read
if not ret:
printError: Failed to capture image."
break
framecopy frame.copy
# Draw the detected contour if it exists
drawcontourframecopy, detectedcontour
# Check for collision every second
if time.time lastchecktime :
if checkcollisionframecopy, detectedcontour:
printCollision detected!"
lastchecktime time.time
cvimshowFrame framecopy
key cvwaitKey & xFF
if key : # Press 'Esc' to exit
break
cap.release
cvdestroyAllWindows
if namemain:
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 and i am using pi camera
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
