Computer vision:Drowsiness Detection using YOLOv5



 


#!pip install torch==1.12.1+cpu torchvision==0.13.1+cpu torchaudio==0.12.1 --extra-index-url https://download.pytorch.org/whl/cpu

#!git clone https://github.com/ultralytics/yolov5

#!cd yolov5 & pip install -r requirements.txt


import os 
os.chdir(r"C:\Users\amb\Downloads\drownsenesyolov5")

import torch
from matplotlib import pyplot as plt
import numpy as np
import cv2

model = torch.hub.load('ultralytics/yolov5', 'yolov5s')

img = 'https://resources.stuff.co.nz/content/dam/images/1/k/q/r/d/c/image.related.StuffLandscapeSixteenByNine.1420x800.1kseqt.png/1501707394942.jpg'

results = model(img)
results.print()
results.render()


%matplotlib inline 
plt.imshow(np.squeeze(results.render()))
plt.show()

results.show()
results.xyxy
np.squeeze(results.render()).shape


cap = cv2.VideoCapture(0)
while cap.isOpened():
    ret, frame = cap.read()
    
    # Make detections 
    results = model(frame)
    
    cv2.imshow('YOLO', np.squeeze(results.render()))
    
    if cv2.waitKey(10)== ord('q'):
        break
cap.release()
cv2.destroyAllWindows()

import uuid   # Unique identifier
import os
import time

IMAGES_PATH = os.path.join('data', 'images') #/data/images
labels = ['awake', 'drowsy']
number_imgs = 20

cap = cv2.VideoCapture(0)
# Loop through labels
for label in labels:
    print('Collecting images for {}'.format(label))
    time.sleep(5)
    
    # Loop through image range
    for img_num in range(number_imgs):
        print('Collecting images for {}, image number {}'.format(label, img_num))
        
        # Webcam feed
        ret, frame = cap.read()
        
        # Naming out image path
        imgname = os.path.join(IMAGES_PATH, label+'.'+str(uuid.uuid1())+'.jpg')
        
        # Writes out image to file 
        cv2.imwrite(imgname, frame)
        
        # Render to the screen
        cv2.imshow('Image Collection', frame)
        
        # 2 second delay between captures
        time.sleep(3)
        
        if cv2.waitKey(10) == ord('q'):
            break
cap.release()
cv2.destroyAllWindows()

for label in labels:
    print('Collecting images for {}'.format(label))
    for img_num in range(number_imgs):
        print('Collecting images for {}, image number {}'.format(label, img_num))
        imgname = os.path.join(IMAGES_PATH, label+'.'+str(uuid.uuid1())+'.jpg')
        print(imgname) 

!cd yolov5 && python train.py --img 320 --batch 16 --epochs 50 --data dataset.yaml --weights yolov5s.pt --workers 2

model = torch.hub.load('ultralytics/yolov5', 'custom', path='yolov5/runs/train/exp20/weights/last.pt', force_reload=True)

img = os.path.join('data', 'images', 'awake-572bea22-6e6f-11ed-a895-8cdcd4d59021_jpg.rf.fda3b6c3f6b56d4d0726c67894d25117.jpg')
resul = model(img)
resul.print()
resul.show()

cap = cv2.VideoCapture(0)
while cap.isOpened():
    ret, frame = cap.read()
    
    # Make detections 
    results = model(frame)
    
    cv2.imshow('YOLO', np.squeeze(results.render()))
    
    if cv2.waitKey(10) & 0xFF == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()


#===================================#

if you faced any issue contact me via 

what's app : +201210894349

 or facebook






Comments

Popular posts from this blog