diff --git a/capture_img.py b/capture_img.py new file mode 100644 index 0000000..bb666d6 --- /dev/null +++ b/capture_img.py @@ -0,0 +1,113 @@ +""" + This is a python script to capture images from the camera + and save them to the local directory in an interval of every 5 seconds. +""" + +import cv2 +import time +from datetime import datetime +import os +import logging + + +def create_dir(**kwargs) -> None: + """ + This function creates a new directory if it does not exist + the name of directory is images + """ + import os + + if kwargs is not None and "path" in kwargs: + path = kwargs["path"] + else: + path = "images" + + format = "%(asctime)s [%(levelname)s] %(message)s" + logging.basicConfig( + level=logging.INFO, + format=format, + handlers=[logging.FileHandler("pipeline-1-capture-img.log")], + ) + + if not os.path.exists(path): + try: + os.mkdir(path) + logging.log(level=logging.INFO, msg=f"Directory {path} Created") + except Exception as e: + logging.log(level=logging.ERROR, msg=e) + logging.log(level=logging.ERROR, msg=f"Unable to create directory {path}") + else: + logging.log(level=logging.INFO, msg=f"{path} directory already exists") + + +def capture_img(**kwargs) -> None: + """ + This function captures images from the camera and saves them to the local directory + in an interval of every 5 seconds. + """ + + cap = cv2.VideoCapture(0) + + if not cap.isOpened(): + logging.log(level=logging.ERROR, msg="Unable to read camera feed") + + # Default resolutions of the frame are obtained.The default resolutions are system dependent. + # We convert the resolutions from float to integer. + + if not os.path.exists(os.path.join(os.getcwd(), "images")): + logging.log( + level=logging.FATAL, + msg="images directory not found, restart the script to create the directory", + ) + exit(1) + else: + logging.log(level=logging.INFO, msg="images directory found") + + queue = None + if kwargs is not None and "queue" in kwargs: + queue = kwargs["queue"] + + while True: + ret, frame = cap.read() + + if ret == True: + timestamp = datetime.now().strftime("%Y_%m_%d_%H_%M_%S") + filename = f"{os.getcwd()}/images/img_{timestamp}.jpg" + + cv2.imwrite(filename, frame) + time.sleep(0.2) + + if queue is not None: + queue.put(filename) + + logging.log( + level=logging.INFO, + msg=f"Image saved named img_{timestamp}.jpg in the directory images", + ) + + # Press Q on keyboard to stop recording + if cv2.waitKey(1) & 0xFF == ord("q"): + break + + time.sleep(5) + + else: + break + + cap.release() + cv2.destroyAllWindows() + + +if __name__ == "__main__": + format = "%(asctime)s [%(levelname)s] %(message)s" + logging.basicConfig( + level=logging.INFO, + format=format, + handlers=[logging.FileHandler("pipeline-1.log")], + ) + + logging.log(level=logging.INFO, msg="Starting the pipeline") + pipeline = (create_dir, capture_img) + + for func in pipeline: + func() diff --git a/classifier/img.png b/classifier/img.png new file mode 100644 index 0000000..7e6cfaf Binary files /dev/null and b/classifier/img.png differ diff --git a/classifier/model.h5 b/classifier/model.h5 new file mode 100644 index 0000000..52d7ec4 Binary files /dev/null and b/classifier/model.h5 differ diff --git a/classifier/model.py b/classifier/model.py new file mode 100644 index 0000000..09fb244 --- /dev/null +++ b/classifier/model.py @@ -0,0 +1,32 @@ +import tensorflow as tf +import cv2 +import numpy as np +from time import perf_counter + + +def predict_monitor(model, img): + start = perf_counter() + img = np.array(img) + + img = img.astype('float32') / 255.0 + img = cv2.resize(img, (224, 224)) + x = np.expand_dims(img, axis=0) + preds = model.predict(x) + k = np.argmax(preds) + print(perf_counter() - start) + if k == 0: + return "1" + elif k == 1: + return "2" + elif k == 2: + return "3" + elif k == 3: + return "4" + +if __name__ == "__main__": + model = tf.keras.models.load_model('model.h5') + + img = cv2.imread('img.png') + + print(predict_monitor(model, img)) + diff --git a/model_yolo/best.pt b/model_yolo/best.pt new file mode 100644 index 0000000..95fa39f Binary files /dev/null and b/model_yolo/best.pt differ diff --git a/model_yolo/medicakolkata_rcu_mon--8_2022_5_18_17_5_38.jpeg b/model_yolo/medicakolkata_rcu_mon--8_2022_5_18_17_5_38.jpeg new file mode 100644 index 0000000..74f7843 Binary files /dev/null and b/model_yolo/medicakolkata_rcu_mon--8_2022_5_18_17_5_38.jpeg differ