Answered step by step
Verified Expert Solution
Question
1 Approved Answer
so i have this code for my project import cv 2 import numpy as np # Initialize the camera cap = cv 2 . VideoCapture
so i have this code for my project import cv
import numpy as np
# Initialize the camera
cap cvVideoCapture # using the default camera
# Initialization of variables
selectedobject None
alarmtriggered False
# Function to handle mouse clicks and select object via edge detection
def selectobjectevent x y flags, param:
global selectedobject
if event cvEVENTLBUTTONDOWN:
ret, refframe cap.read
if ret:
refframegray cvcvtColorrefframe, cvCOLORBGRGRAY
edges cvCannyrefframegray,
contours, cvfindContoursedges cvRETRTREE, cvCHAINAPPROXSIMPLE
for cnt in contours:
# Check if contour is circular
perimeter cvarcLengthcnt True
area cvcontourAreacnt
circularity nppi area perimeter perimeter
if circularity : # Adjust this threshold as needed
selectedobject cnt
break
# Function to check if a point is inside the selected object
def isinsideobjectpoint:
global selectedobject
return cvpointPolygonTestselectedobject, tuplepoint False if selectedobject is not None else False
# Function to detect motion in the video frames
def detectmotionframe:
gray cvcvtColorframe cvCOLORBGRGRAY
thresh cvthresholdgray cvTHRESHBINARY
dilated cvdilatethresh None, iterations
contours, cvfindContoursdilated cvRETREXTERNAL, cvCHAINAPPROXSIMPLE
return contours
# Set up the window and mouse callback
cvnamedWindowFrame
cvsetMouseCallbackFrame selectobject
# Main program loop
while True:
ret, frame cap.read
if not ret:
break
motioncontours detectmotionframe
if selectedobject is not None:
cvdrawContoursframeselectedobject # Draw the selected object
for contour in motioncontours:
x y w h cvboundingRectcontour
centerofcontour x w y h
if selectedobject is not None and isinsideobjectcenterofcontour:
alarmtriggered True
if alarmtriggered:
cvputTextframe "ALARM! Object touched!", cvFONTHERSHEYSIMPLEX,
printALARM Object touched!"
alarmtriggered False # Reset the alarm trigger
cvimshowFrame frame
# Exit the loop on q key press
if cvwaitKey & xFF ordq:
break
# Cleanup resources
cap.release
cvdestroyAllWindows
what i need is an adjusted version of these code which detects rectangle objects edges instead of circles i still want to detect it by clicking with mouse everything should stay same but i want rectangle object as selected object not a circle
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started