Computer vision with Yolo and Webcam :: Advanced Analytics (BANA 4373)

Computer vision with Yolo and Webcam

Binder

This project uses the Yolo trained model for object recognitition. It uses the webcam video feed to apply the model in real time.

This version won’t work properly on myBinder. You need to run this on your local computer so it connects properly to your webcam.

! pip install -r requirements.txt
import wget

url = "https://github.com/OlafenwaMoses/ImageAI/releases/download/1.0/yolo.h5"
modelpath = wget.download(url)

from imageai import Detection
yolo = Detection.ObjectDetection()
yolo.setModelTypeAsYOLOv3()
yolo.setModelPath(modelpath)
yolo.loadModel()

import cv2
cam = cv2.VideoCapture(1) #0=front-cam, 1=back-cam
cam.set(cv2.CAP_PROP_FRAME_WIDTH, 1300)
cam.set(cv2.CAP_PROP_FRAME_HEIGHT, 1500)
True
while True:
    ## read frames
    ret, img = cam.read()
    ## predict yolo
    img, preds = yolo.detectCustomObjectsFromImage(input_image=img, 
                      custom_objects=None, input_type="array",
                      output_type="array",
                      minimum_percentage_probability=70,
                      display_percentage_probability=False,
                      display_object_name=True)
    ## display predictions
    cv2.imshow("", img)
    ## press q or Esc to quit    
    if (cv2.waitKey(1) & 0xFF == ord("q")) or (cv2.waitKey(1)==27):
        break
## close camera
cam.release()
cv2.destroyAllWindows()