ABB-Automata / app.py
Krrish-shetty's picture
Upload app.py
1656d34 verified
Raw
History Blame Contribute Delete
5.47 kB
import cv2
import os
import time
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
from email.mime.text import MIMEText
from zipfile import ZipFile
import streamlit as st
# Configuration for Gmail SMTP
EMAIL_SENDER = 'kshetty200319@gmail.com' # Sender's email
EMAIL_PASSWORD = 'kdpo givc nwoi mnhl' # App-specific password
EMAIL_RECEIVER = 'kshetty200319@gmail.com' # Receiver's email
SMTP_SERVER = 'smtp.gmail.com'
SMTP_PORT = 587
# Paths
IMAGE_FOLDER = 'captured_images/'
VIDEO_PATH = 'captured_video.mp4'
COMPRESSED_IMAGES_PATH = 'compressed_images.zip'
# Create directory for images
os.makedirs(IMAGE_FOLDER, exist_ok=True)
def capture_images_and_video():
cap = cv2.VideoCapture(0)
if not cap.isOpened():
st.error("Could not open video capture.")
return False
cv2.namedWindow('Camera Feed', cv2.WINDOW_NORMAL)
try:
# Capture images
for i in range(5):
start_time = time.time()
while time.time() - start_time < 3: # Delay with live feed
ret, frame = cap.read()
if not ret:
st.error("Could not read frame.")
return False
cv2.imshow('Camera Feed', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
st.warning("Operation canceled by user.")
return False
# Save the captured image
cv2.imwrite(f'{IMAGE_FOLDER}image_{i}.jpg', frame)
# Record video
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter(VIDEO_PATH, fourcc, 20.0, (640, 480))
start_time = time.time()
while time.time() - start_time < 15: # Record for 15 seconds
ret, frame = cap.read()
if not ret:
st.error("Could not read frame.")
return False
out.write(frame)
cv2.imshow('Camera Feed', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
st.warning("Operation canceled by user.")
return False
out.release()
except Exception as e:
st.error(str(e))
return False
finally:
cap.release()
cv2.destroyAllWindows()
return True
def compress_files():
try:
with ZipFile(COMPRESSED_IMAGES_PATH, 'w') as zipf:
for root, _, files in os.walk(IMAGE_FOLDER):
for file in files:
zipf.write(os.path.join(root, file),
os.path.relpath(os.path.join(root, file), IMAGE_FOLDER))
except Exception as e:
st.error(f"Failed to compress images: {e}")
return False
return True
def send_email(sales_order_number):
msg = MIMEMultipart()
msg['From'] = EMAIL_SENDER
msg['To'] = EMAIL_RECEIVER
msg['Subject'] = f'Captured images and video for Sales Order #{sales_order_number}'
body = (f'Dear sir,\n\nPlease find the captured images and video for '
f'Sales Order #{sales_order_number} attached.\n\nThank you.\n\nRegards,\nAutomation Team')
msg.attach(MIMEText(body, 'plain'))
try:
with open(COMPRESSED_IMAGES_PATH, 'rb') as file:
part = MIMEBase('application', 'zip')
part.set_payload(file.read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', f'attachment; filename={COMPRESSED_IMAGES_PATH}')
msg.attach(part)
with open(VIDEO_PATH, 'rb') as file:
part = MIMEBase('application', 'octet-stream')
part.set_payload(file.read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', f'attachment; filename={VIDEO_PATH}')
msg.attach(part)
with smtplib.SMTP(SMTP_SERVER, SMTP_PORT) as server:
server.starttls()
server.login(EMAIL_SENDER, EMAIL_PASSWORD)
server.send_message(msg)
st.success("Email sent successfully!")
except Exception as e:
st.error(f"Failed to send email: {e}")
return False
return True
def clean_up():
try:
if os.path.exists(VIDEO_PATH):
os.remove(VIDEO_PATH)
if os.path.exists(COMPRESSED_IMAGES_PATH):
os.remove(COMPRESSED_IMAGES_PATH)
if os.path.exists(IMAGE_FOLDER):
for file in os.listdir(IMAGE_FOLDER):
os.remove(os.path.join(IMAGE_FOLDER, file))
os.rmdir(IMAGE_FOLDER)
except Exception as e:
st.error(f"Failed to clean up files: {e}")
# Streamlit UI
st.title("ABB Automata")
sales_order_number = st.text_input("Enter Sales Order Number:")
if st.button("Start Automation"):
if not sales_order_number:
st.error("Please enter a Sales Order Number.")
else:
st.info("Capturing images and video...")
if capture_images_and_video():
st.info("Compressing files...")
if compress_files():
st.info("Sending email...")
if send_email(sales_order_number):
st.info("Cleaning up temporary files...")
clean_up()
st.success("Automation process completed successfully!")