Answered step by step
Verified Expert Solution
Question
1 Approved Answer
i need python code for a project. My project is an alarm system which is programable according to area and enviroment. Firstly i want my
i need python code for a project. My project is an alarm system which is programable according to area and enviroment. Firstly i want my code to detect the object that i want to protect and then if anything touches it alarm will go on alarm is just a printed message i also want my program to protect an area same way. In this case we need restriction for the shape of the protected area or object you can do circle triangle it does not matter but select the easiest shape to do this project. Im using opencv also i will implement this on raspberry pi Lastly i want my program to select the object and then remember it all times so that it can check if anything touches it
THIS IS WHAT I SENT PREVIOUSLY
i got this code
import cv
import numpy as np
# Initialize the camera
cap cvVideoCapture
# Initialize variables
selectedobjectcenter None
selectedobjectradius None
alarmtriggered False
def selectobjectevent x y flags, param:
global selectedobjectcenter, selectedobjectradius, alarmtriggered
if event cvEVENTLBUTTONDOWN:
selectedobjectcenter x y
selectedobjectradius # Example fixed radius for simplicity
alarmtriggered False
def isinsidecirclecenter radius, point:
return nplinalg.normnparraycenter nparraypoint radius
def detectmotionframe background:
# Compute the absolute difference between the background and current frame
diff cvabsdiffbackground frame
gray cvcvtColordiff cvCOLORBGRGRAY
thresh cvthresholdgray cvTHRESHBINARY
dilated cvdilatethresh None, iterations
contours, cvfindContoursdilated cvRETREXTERNAL, cvCHAINAPPROXSIMPLE
return contours
cvnamedWindowFrame
cvsetMouseCallbackFrame selectobject
# Allow the camera to warm up and capture the initial background frame
printPlease wait for the camera to warm up
background cap.read
background cvGaussianBlurbackground
cvwaitKey
while True:
ret, frame cap.read
if not ret:
break
blurredframe cvGaussianBlurframe
motioncontours detectmotionblurredframe, background
# Draw the selected object area
if selectedobjectcenter:
cvcircleframe selectedobjectcenter, selectedobjectradius,
# Check if any contour intersects with the protected area
for contour in motioncontours:
if cvcontourAreacontour:
continue
x y w h cvboundingRectcontour
centerofcontour x w y h
if selectedobjectcenter and isinsidecircleselectedobjectcenter, selectedobjectradius, centerofcontour:
alarmtriggered True
break
if alarmtriggered:
cvputTextframe "ALARM! Object touched!", cvFONTHERSHEYSIMPLEX,
printALARM Object touched!"
cvimshowFrame frame
# Break the loop on q key press
if cvwaitKey & xFF ordq:
break
cap.release
cvdestroyAllWindows
there are few problems with it firstly i wanna do edge detection on the object or area by clicking it thats how i want to choose the object secondly i want my program to watch area full of objects not a plain area like a wall i need the program to be able to seperate my object from the sheet that it is on i hope you can help me with the adjustments
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