Answered step by step
Verified Expert Solution
Link Copied!

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 cv2
import numpy as np
# Initialize the camera
cap = cv2.VideoCapture(0)
# Initialize variables
selected_object_center = None
selected_object_radius = None
alarm_triggered = False
def select_object(event, x, y, flags, param):
global selected_object_center, selected_object_radius, alarm_triggered
if event == cv2.EVENT_LBUTTONDOWN:
selected_object_center =(x, y)
selected_object_radius =50 # Example fixed radius for simplicity
alarm_triggered = False
def is_inside_circle(center, radius, point):
return np.linalg.norm(np.array(center)- np.array(point))<= radius
def detect_motion(frame, background):
# Compute the absolute difference between the background and current frame
diff = cv2.absdiff(background, frame)
gray = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY)
_, thresh = cv2.threshold(gray,25,255, cv2.THRESH_BINARY)
dilated = cv2.dilate(thresh, None, iterations=2)
contours, _= cv2.findContours(dilated, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
return contours
cv2.namedWindow('Frame')
cv2.setMouseCallback('Frame', select_object)
# Allow the camera to warm up and capture the initial background frame
print("Please wait for the camera to warm up...")
_, background = cap.read()
background = cv2.GaussianBlur(background,(21,21),0)
cv2.waitKey(2000)
while True:
ret, frame = cap.read()
if not ret:
break
blurred_frame = cv2.GaussianBlur(frame,(21,21),0)
motion_contours = detect_motion(blurred_frame, background)
# Draw the selected object area
if selected_object_center:
cv2.circle(frame, selected_object_center, selected_object_radius, (0,255,0),2)
# Check if any contour intersects with the protected area
for contour in motion_contours:
if cv2.contourArea(contour)<500:
continue
(x, y, w, h)= cv2.boundingRect(contour)
center_of_contour =(x + w //2, y + h //2)
if selected_object_center and is_inside_circle(selected_object_center, selected_object_radius, center_of_contour):
alarm_triggered = True
break
if alarm_triggered:
cv2.putText(frame, "ALARM! Object touched!", (10,30), cv2.FONT_HERSHEY_SIMPLEX, 1,(0,0,255),2)
print("ALARM! Object touched!")
cv2.imshow('Frame', frame)
# Break the loop on 'q' key press
if cv2.waitKey(30) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Secrets Of Analytical Leaders Insights From Information Insiders

Authors: Wayne Eckerson

1st Edition

1935504347, 9781935504344

More Books

Students also viewed these Databases questions

Question

Outline the three steps in time management and in money management.

Answered: 1 week ago

Question

Write short notes on Interviews.

Answered: 1 week ago