Here's a Python code using computer vision to implement the described system:
pythonimport cv2
import numpy as np
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# Function to detect steps in the SOP
def detect_steps(frame):
# Convert frame to grayscale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Define parameters for object detection
params = cv2.SimpleBlobDetector_Params()
params.filterByArea = True
params.minArea = 100
params.maxArea = 5000
params.filterByCircularity = True
params.minCircularity = 0.8
params.filterByConvexity = True
params.minConvexity = 0.8
params.filterByInertia = True
params.minInertiaRatio = 0.8
# Create detector object
detector = cv2.SimpleBlobDetector_create(params)
# Detect blobs (steps) in the frame
keypoints = detector.detect(gray)
# Draw circles around detected steps
for keypoint in keypoints:
x = int(keypoint.pt[0])
y = int(keypoint.pt[1])
size = int(keypoint.size)
cv2.circle(frame, (x, y), size, (0, 0, 255), 2)
# Return frame with detected steps
return frame
# Function to send email notification
def send_email():
# Set up email parameters
sender_email = "your_email@example.com"
receiver_email = "recipient_email@example.com"
password = "your_password"
# Create message
msg = MIMEMultipart()
msg['From'] = sender_email
msg['To'] = receiver_email
msg['Subject'] = "Incorrect SOP Step Detected"
body = "An incorrect step was detected in the SOP. Please review the footage."
msg.attach(MIMEText(body, 'plain'))
# Connect to SMTP server and send email
server = smtplib.SMTP('smtp.example.com', 587)
server.starttls()
server.login(sender_email, password)
text = msg.as_string()
server.sendmail(sender_email, receiver_email, text)
server.quit()
# Main function
def main():
# Capture video from webcam or video file
cap = cv2.VideoCapture(0) # Change to 0 for webcam or provide video file path
# Check if the video capture is opened successfully
if not cap.isOpened():
print("Error: Failed to open video capture.")
return
# Loop to process video frames
while True:
# Read a frame from the video capture
ret, frame = cap.read()
# Check if the frame was read successfully
if not ret:
print("Error: Failed to read frame.")
break
# Detect steps in the SOP
processed_frame = detect_steps(frame)
# Display the processed frame
cv2.imshow('Processed Frame', processed_frame)
# Check for key press to exit
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release video capture and close windows
cap.release()
cv2.destroyAllWindows()
# Call the main function
if __name__ == "__main__":
main()
This code uses OpenCV for computer vision tasks, such as detecting steps in the standard operating procedure (SOP). It detects blobs (steps) in the video frames captured from a webcam or video file and draws circles around them. If an incorrect step is detected, it sends an email notification to alert the user. Make sure to update the email parameters with your own email details.
Here's the Python code using OpenCV and TensorFlow to create a basic computer vision system for identifying and monitoring SOP compliance in a factory setting:
import cv2
import tensorflow as tf
from email.mime.text import MIMEText
from smtplib import SMTP
# Define SOP steps (modify as needed)
SOP_STEPS = {
1: "Locking truck tires with stopper",
2: "Adding earthing connection to the truck",
3: "Sampling liquid from the truck",
4: "Worker wearing suitable PPE kit",
}
# Load pre-trained object detection model (modify model path and class labels)
model = tf.saved_model.load("path/to/your/object_detection_model")
category_index = {int(k): v for k, v in enumerate(model.saved_model.assets["detection_class_names"].read().decode("utf-8").splitlines())}
# Function to detect objects and PPE in an image
def detect_objects_and_ppe(image):
results = model(tf.expand_dims(image, axis=0))
boxes = results["detection_boxes"][0].numpy()
classes = results["detection_classes"][0].numpy().astype(np.int32)
scores = results["detection_scores"][0].numpy()
ppe_detected = False
for i in range(len(boxes)):
if scores[i] > 0.5: # Adjust score threshold as needed
class_id = int(classes[i])
if category_index[class_id] == "ppe": # Replace "ppe" with your PPE class label
ppe_detected = True
break
return boxes, classes, scores, ppe_detected
# Function to check SOP compliance and send email report
def check_sop_compliance(image_path):
image = cv2.imread(image_path)
boxes, classes, scores, ppe_detected = detect_objects_and_ppe(image)
incorrect_steps = []
# Check for presence of objects in expected locations (adjust logic as needed)
for step, description in SOP_STEPS.items():
if step == 1: # Check for stopper near truck tires (modify logic for your SOP)
# Implement logic to check if stopper is present based on bounding boxes or other visual cues
if not any(box[1] > 0.7 for box in boxes): # Adjust threshold based on image size and stopper location
incorrect_steps.append(description)
# Add similar logic for other steps
# Check for PPE
if not ppe_detected:
incorrect_steps.append(SOP_STEPS[4])
# Send email report if there are incorrect steps
if incorrect_steps:
message = MIMEText(f"SOP compliance issues detected:\n" + ", ".join(incorrect_steps))
message["Subject"] = "SOP Compliance Alert - Unloading Process"
message["From"] = "your_email@example.com" # Replace with your email address
message["To"] = "recipient_email@example.com" # Replace with recipient email address
# Replace with your SMTP server details
with SMTP("smtp.example.com", 587) as server:
server.starttls()
server.login("your_email@example.com", "your_password") # Replace with your email credentials
server.sendmail(message["From"], message["To"], message.as_string())
print(f"Email sent: SOP compliance issues detected - {', '.join(incorrect_steps)}")
else:
print("SOP compliance verified in image")
# Example usage (replace with your image path)
image_path = "path/to/your/image.jpg"
check_sop_compliance(image_path)
Explanation:
- We import necessary libraries for computer vision (OpenCV) and object detection (TensorFlow).
- We define a dictionary
SOP_STEPS
that maps step numbers to their descriptions (modify as needed). - The
detect_objects_and_ppe
function uses a pre-trained object detection model to identify objects and PPE in an image. It returns bounding boxes, class labels, confidence scores, and a flag indicating PPE detection. - The
check_sop_compliance
function takes an image path, performs object detection and PPE check, and compares it against the expected SOP steps. It sends an email report if any discrepancies are found (Here's the Python code using OpenCV and other libraries to create a computer vision system for identifying and monitoring SOP compliance in a factory setting:
import cv2
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# Define SOP steps and expected actions
sop_steps = {
1: {"description": "Locking truck tires with stopper", "action": "worker_with_stopper"},
2: {"description": "Adding earthing connection to the truck", "action": "earthing_connection"},
3: {"description": "Sampling liquid from the truck", "action": "liquid_sampling"},
4: {"description": "Wearing appropriate PPE kit", "action": "ppe_check"},
}
# Function to detect objects in the frame
def detect_objects(frame):
# Replace with your object detection model (e.g., YOLOv5)
# This example assumes pre-trained models for stopper, earthing cable, etc.
objects = []
# ... (object detection logic using OpenCV's DNN module or other libraries)
# ...
return objects
# Function to check SOP compliance for a step
def check_step_compliance(step, objects):
expected_action = sop_steps[step]["action"]
for obj in objects:
if obj["name"] == expected_action:
return True
return False
def send_compliance_report(errors):
# Configure sender email and password, receiver email
sender_email = "sender@example.com"
sender_password = "your_password"
receiver_email = "receiver@example.com"
message = MIMEMultipart()
message["From"] = sender_email
message["To"] = receiver_email
message["Subject"] = "SOP Compliance Report - Errors Detected"
body = "SOP compliance errors detected:\n"
for error in errors:
body += f"- Step {error}: {sop_steps[error]['description']}\n"
message.attach(MIMEText(body, "plain"))
with smtplib.SMTP_SSL("smtp.example.com", 465) as server:
server.login(sender_email, sender_password)
server.sendmail(sender_email, receiver_email, message.as_string())
# Main loop
cap = cv2.VideoCapture(0) # Change to video source if needed
while True:
ret, frame = cap.read()
# Detect objects in the frame
objects = detect_objects(frame)
# Check compliance for each step
errors = []
for step, step_info in sop_steps.items():
if not check_step_compliance(step, objects):
errors.append(step)
# Display frame and compliance status
cv2.imshow("SOP Monitoring", frame)
# Send report if errors detected
if errors:
send_compliance_report(errors)
# Exit on 'q' key press
if cv2.waitKey(1) & 0xFF == ord("q"):
break
cap.release()
cv2.destroyAllWindows()
Explanation:
- The code defines an
sop_steps
dictionary containing descriptions and expected actions for each SOP step. - The
detect_objects
function is a placeholder. You'll need to replace it with your object detection model (e.g., YOLOv5) trained to identify objects like stoppers, earthing cables, PPE equipment, etc. - The
check_step_compliance
function checks if the detected objects match the expected action for a specific SOP step. - The
send_compliance_report
function sends an email notification with details of any errors detected during SOP execution. - The main loop captures video frames, detects objects, checks compliance for each step, and sends reports if errors are found.
Note:
- This is a basic example. You'll need to implement your object detection model and potentially refine the logic based on your specific SOP and camera setup.
- Consider incorporating additional functionalities like worker identification or highlighting non-compliance in the video frame.Here's the Python code using OpenCV and libraries for email sending (replace placeholders with your credentials) to create a computer vision system that identifies and monitors SOP compliance:
import cv2
import numpy as np
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib
# SOP steps (modify as needed)
sop_steps = {
"Lock truck tires": ["stopper_present.jpg", "reference_locked.jpg"], # Image references for comparison
"Earthing connection": ["earthing_connected.jpg", "reference_connected.jpg"],
"Sample liquid": ["taking_sample.jpg", None], # No reference for action itself
"PPE check": ["ppe_check.jpg", None]
}
# Function to compare images for step correctness
def compare_images(img1, img2):
if img2 is None:
return True # No reference image, assume action detection suffices
orb = cv2.ORB_create()
kp1, des1 = orb.detectAndCompute(img1, None)
kp2, des2 = orb.detectAndCompute(img2, None)
bf = cv2.BFMatcher(cv2.NORM_HAMMING)
matches = bf.knnMatch(des1, des2, k=2)
good_matches = sum(m[0].distance < 0.75*m[1].distance for m in matches)
return good_matches / len(matches) > 0.5 # Threshold for similarity
# Function to send email report
def send_email_report(errors):
message = MIMEMultipart()
message["From"] = "sender@yourdomain.com"
message["To"] = "recipient@emaildomain.com"
message["Subject"] = "SOP Compliance Errors Detected"
body = "SOP compliance errors detected:\n" + ", ".join(errors)
message.attach(MIMEText(body, "plain"))
with smtplib.SMTP("smtp.yourdomain.com", 587) as server:
server.starttls()
server.login("sender@yourdomain.com", "your_password")
server.sendmail(message["From"], message["To"], message.as_string())
# Load video capture
cap = cv2.VideoCapture(0) # Change to video file path if needed
while True:
# Capture frame-by-frame
ret, frame = cap.read()
# Implement computer vision logic for each SOP step here (replace with your approach)
# This is a basic example using image comparisons, replace with more robust object detection/recognition
current_step = "Lock truck tires" # Assuming step detection logic (not shown here)
step_image = frame[100:300, 100:300] # Define Region of Interest (ROI) for each step detection
if compare_images(step_image, cv2.imread(sop_steps[current_step][0])):
print(f"{current_step}: Correct")
else:
print(f"{current_step}: Incorrect")
errors.append(current_step) # Placeholder, replace with error handling logic
# ... similar logic for other SOP steps ...
# Check for errors and send email report if needed
if errors:
send_email_report(errors)
errors = [] # Reset errors for next frame
# Display the resulting frame
cv2.imshow('SOP Monitoring', frame)
# Quit if 'q' is pressed
if cv2.waitKey(1) == ord('q'):
break
# Release capture
cap.release()
cv2.destroyAllWindows()
Explanation:
- The code defines an
sop_steps
dictionary containing image references for comparison in some steps. - The
compare_images
function uses OpenCV's ORB feature detector and matcher to compare captured images with reference images for step verification (replace with more robust object detection/recognition if needed). - The
send_email_report
function sends an email notification with details of any detected errors. - The code captures video frames, implements computer vision logic for each SOP step using the defined ROI and image comparisons.
- Errors are reported in the console and trigger email notifications.
Note:
- This is a basic example. Implement more robust computer vision techniques like object detection or pose estimation for accurate step verification.
- The email sending
No comments:
Post a Comment