import cv2
import numpy as np
import time
from tkinter import simpledialog
import tkinter as tk
def preprocess_image(image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
return blurred
def detect_changes(image1, image2, points, threshold=30, area_size=20):
diff = cv2.absdiff(image1, image2)
_, thresh = cv2.threshold(diff, threshold, 255, cv2.THRESH_BINARY)
changes = {}
for pt, tool_name in points.items():
x, y = pt
roi = thresh[max(0, y - area_size):y + area_size, max(0, x - area_size):x + area_size]
if cv2.countNonZero(roi) > 0:
changes[pt] = tool_name
return changes
def ask_tool_name(x, y):
root = tk.Tk()
root.withdraw() # Hide the main window
tool_name = simpledialog.askstring("Tool Name", "Enter the name of the tool:", parent=root)
root.destroy()
return tool_name
def click_event(event, x, y, flags, params):
if event == cv2.EVENT_LBUTTONDOWN:
tool_name = ask_tool_name(x, y)
if tool_name:
points[(x, y)] = tool_name
print(f"Selected Point: ({x}, {y}) - Tool: {tool_name}")
points = {}
cap = cv2.VideoCapture(0)
cv2.namedWindow("Camera Feed")
cv2.setMouseCallback("Camera Feed", click_event)
ret, last_frame = cap.read()
last_frame = preprocess_image(last_frame)
last_check = time.time()
while True:
ret, frame = cap.read()
if not ret:
break
processed_frame = preprocess_image(frame)
if time.time() - last_check > 60 and points:
changes = detect_changes(last_frame, processed_frame, points)
for pt, tool_name in changes.items():
print(f"{tool_name} is missing at {pt}")
cv2.putText(frame, f"{tool_name} is missing!", pt, cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
last_check = time.time()
last_frame = processed_frame
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cv2.imshow("Camera Feed", frame)
cap.release()
cv2.destroyAllWindows()