0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

pythonでの部品管理テスト②

Posted at

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()

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?