|
| 1 | +import numpy as np |
| 2 | +import random as rn |
| 3 | +import tensorflow as tf |
| 4 | +from tensorflow import keras |
| 5 | +from tensorflow.keras import layers, models, optimizers |
| 6 | +from tensorflow.keras.models import Sequential |
| 7 | +from tensorflow.keras.applications import VGG16 |
| 8 | +import PIL |
| 9 | +from PIL import Image |
| 10 | +import cv2 |
| 11 | +import os |
| 12 | + |
| 13 | +import classification |
| 14 | +from playsound import playsound |
| 15 | + |
| 16 | + |
| 17 | +if __name__ == "__main__": |
| 18 | + |
| 19 | + count = 0 |
| 20 | + num_image = 0 |
| 21 | + flag_safe = 0 |
| 22 | + flag_dist = 0 |
| 23 | + flag_phone = 0 |
| 24 | + |
| 25 | + print("System is going to start----->\n") |
| 26 | + |
| 27 | + |
| 28 | + print(".......\n") |
| 29 | + |
| 30 | + mac_cam = cv2.VideoCapture(0) |
| 31 | + vgg_model = tf.keras.models.load_model('model/model.h5') |
| 32 | + |
| 33 | + while(True): |
| 34 | + |
| 35 | + retrieve, frames = mac_cam.read() |
| 36 | + cv2.imshow('Try2Catch', frames) |
| 37 | + count = count + 1 |
| 38 | + if count == 20: |
| 39 | + |
| 40 | + cv2.imwrite("image/"+str(num_image)+".png",frames) |
| 41 | + count = 0 |
| 42 | + num_image = num_image + 1 |
| 43 | + |
| 44 | + if cv2.waitKey(20) & 0xFF == ord('q'): |
| 45 | + break |
| 46 | + |
| 47 | + if len(os.listdir("image")) == 10: |
| 48 | + for image in os.listdir("image"): |
| 49 | + result = classification.classification(vgg_model,"image/"+image) |
| 50 | + print("Result: " + result) |
| 51 | + if result == "Phone": |
| 52 | + flag_phone = flag_phone + 1 |
| 53 | + elif result == "Distraction": |
| 54 | + flag_dist = flag_dist + 1 |
| 55 | + else: |
| 56 | + flag_safe = flag_safe + 1 |
| 57 | + os.remove("image/"+image) |
| 58 | + num_image = 0 |
| 59 | + |
| 60 | + if max(flag_phone, flag_dist, flag_safe) == flag_dist: |
| 61 | + playsound('audio/dist.mp3') |
| 62 | + elif max(flag_phone, flag_dist, flag_safe) == flag_phone: |
| 63 | + playsound('audio/phone.mp3') |
| 64 | + elif max(flag_phone, flag_dist, flag_safe) == flag_safe: |
| 65 | + playsound('audio/safe.mp3') |
| 66 | + |
| 67 | + flag_safe = 0 |
| 68 | + flag_phone = 0 |
| 69 | + flag_dist = 0 |
| 70 | + |
| 71 | + |
0 commit comments