Check if the camera is opened or not using OpenCV-Python Last Updated : 03 Jan, 2023 Comments Improve Suggest changes 4 Likes Like Report OpenCV (Open Source Computer Vision) is a computer vision library that contains various functions to perform operations on Images or videos. OpenCV library can be used to perform multiple operations on videos. While writing code in Python using OpenCV, we may not be sure whether at the remote end camera is opened and working properly or not. The camera plays an essential role in areas such as Security and Video Surveillance System. In a real-time video monitoring system, to ensure that the camera is opened and working properly we have isOpened() of OpenCV. The idea behind the article is to check whether the camera is connected and if the camera is found to be disconnected then a mail will be sent to the Admin or the concerned person. 1. Check whether the camera is opened/connected or not. Approach: Importing necessary libraries(NumPy and OpenCV) Starting camera. Here, in VideoCapture()-0 denotes built-in webcam while 1 will denote the use of external webcams. If the camera is opened, we will loop over frames whereas in the other case, a message "Alert! Camera disconnected" will be printed on the terminal window. Below is the implementation. Python3 1== # Python program to check # whether the camera is opened # or not import numpy as np import cv2 cap = cv2.VideoCapture(0) while(cap.isOpened()): while True: ret, img = cap.read() cv2.imshow('img', img) if cv2.waitKey(30) & 0xff == ord('q'): break cap.release() cv2.destroyAllWindows() else: print("Alert ! Camera disconnected") 2. Sending Mail if camera is found to be disconnected/not opened. Approach: Import the necessary libraries(smtplib is the python library to send mails). Establish a connection with the server and login to the account. Specify the receiver's email address and message to be sent ("Alert! Camera disconnected!" in this case). Once the mail is sent, close the connection or quit the session. Below is the implementation. Python3 1== # Python program to send # the mail import smtplib conn = smtplib.SMTP('smtp.gmail.com', 587) conn.ehlo() conn.starttls() # Enter the sender's details conn.login('Enter sender \'s gmail address', 'Enter sender\'s password') conn.sendmail('Enter sender\'s gmail address', 'Enter Receiver\'s gmail address', 'Enter message to be sent') conn.quit() Create Quiz Comment S simranjenny84 Follow 4 Improve S simranjenny84 Follow 4 Improve Article Tags : Machine Learning OpenCV python Python-OpenCV Explore Machine Learning BasicsIntroduction to Machine Learning8 min readTypes of Machine Learning7 min readWhat is Machine Learning Pipeline?6 min readApplications of Machine Learning3 min readPython for Machine LearningMachine Learning with Python Tutorial5 min readNumPy Tutorial - Python Library3 min readPandas Tutorial4 min readData Preprocessing in Python4 min readEDA - Exploratory Data Analysis in Python6 min readFeature EngineeringWhat is Feature Engineering?5 min readIntroduction to Dimensionality Reduction4 min readFeature Selection Techniques in Machine Learning4 min readSupervised LearningSupervised Machine Learning7 min readLinear Regression in Machine learning14 min readLogistic Regression in Machine Learning10 min readDecision Tree in Machine Learning8 min readRandom Forest Algorithm in Machine Learning5 min readK-Nearest Neighbor(KNN) Algorithm8 min readSupport Vector Machine (SVM) Algorithm9 min readNaive Bayes Classifiers6 min readUnsupervised LearningWhat is Unsupervised Learning5 min readK means Clustering â Introduction6 min readHierarchical Clustering in Machine Learning6 min readDBSCAN Clustering in ML - Density based clustering6 min readApriori Algorithm6 min readFrequent Pattern Growth Algorithm5 min readECLAT Algorithm - ML5 min readPrincipal Component Analysis (PCA)7 min readModel Evaluation and TuningEvaluation Metrics in Machine Learning9 min readRegularization in Machine Learning5 min readCross Validation in Machine Learning5 min readHyperparameter Tuning5 min readUnderfitting and Overfitting in ML3 min readBias and Variance in Machine Learning6 min readAdvanced TechniquesReinforcement Learning9 min readSemi-Supervised Learning in ML5 min readSelf-Supervised Learning (SSL)6 min readEnsemble Learning8 min readMachine Learning PracticeMachine Learning Interview Questions and Answers15+ min read100+ Machine Learning Projects with Source Code5 min read Like