Answered step by step
Verified Expert Solution
Link Copied!

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 cv2
import numpy as np
# Initialize the camera
cap = cv2.VideoCapture(0) # using the default camera
# Initialization of variables
selected_object = None
alarm_triggered = False
# Function to handle mouse clicks and select object via edge detection
def select_object(event, x, y, flags, param):
global selected_object
if event == cv2.EVENT_LBUTTONDOWN:
ret, ref_frame = cap.read()
if ret:
ref_frame_gray = cv2.cvtColor(ref_frame, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(ref_frame_gray, 100,200)
contours, _= cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
# Check if contour is circular
perimeter = cv2.arcLength(cnt, True)
area = cv2.contourArea(cnt)
circularity =4* np.pi * area /(perimeter * perimeter)
if circularity >0.8: # Adjust this threshold as needed
selected_object = cnt
break
# Function to check if a point is inside the selected object
def is_inside_object(point):
global selected_object
return cv2.pointPolygonTest(selected_object, tuple(point), False)>=0 if selected_object is not None else False
# Function to detect motion in the video frames
def detect_motion(frame):
gray = cv2.cvtColor(frame, 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
# Set up the window and mouse callback
cv2.namedWindow('Frame')
cv2.setMouseCallback('Frame', select_object)
# Main program loop
while True:
ret, frame = cap.read()
if not ret:
break
motion_contours = detect_motion(frame)
if selected_object is not None:
cv2.drawContours(frame,[selected_object],-1,(0,255,0),2) # Draw the selected object
for contour in motion_contours:
(x, y, w, h)= cv2.boundingRect(contour)
center_of_contour =(x + w//2, y + h//2)
if selected_object is not None and is_inside_object(center_of_contour):
alarm_triggered = True
if alarm_triggered:
cv2.putText(frame, "ALARM! Object touched!", (10,30), cv2.FONT_HERSHEY_SIMPLEX, 1,(0,0,255),2)
print("ALARM! Object touched!")
alarm_triggered = False # Reset the alarm trigger
cv2.imshow('Frame', frame)
# Exit the loop on 'q' key press
if cv2.waitKey(30) & 0xFF == ord('q'):
break
# Cleanup resources
cap.release()
cv2.destroyAllWindows()
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

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

How do I feel just before I give in to my bad habit?

Answered: 1 week ago

Question

How does the EEOC interpret the national origin guidelines?

Answered: 1 week ago

Question

What is the purpose of the OFCCP?

Answered: 1 week ago