├── README.md ├── coco.names ├── doc ├── detector1.gif ├── detector1.jpg ├── detector1.mp4 ├── detector1.wmv ├── detector2.gif ├── detector2.jpg ├── detector2.mp4 ├── detector2.wmv └── webcam_detector.jpg ├── real_time_yolo_detector1.py ├── real_time_yolo_detector2.py ├── real_time_yolo_webcam.py ├── src_room.jpg ├── uk.mp4 └── usa-street.mp4 /README.md: -------------------------------------------------------------------------------- 1 | ## YOLO Real Time Object Detection 2 | 3 | This project implements a real time object detection via video, webcam and image detection using YOLO algorithm. YOLO is a object detection algorithm which stand for You Only Look Once. I've implemented the algorithm from scratch in Python using pre-trained weights. YOLOv3 was published in research paper: YOLOv3: An Incremental Improvement: Joseph Redmon, Ali Farhadi It's originally implemented in YOLOv3. 4 | 5 | COCO dataset is used for training. 6 | 7 | Real time detection can be use via command prompt or GUI. 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 |
A USA Real-Time Road Detection
A UK Real-Time Road Detection
A Real-Time Webcam Detection
31 | 32 | Yolo is a deep learning algorythm which came out on may 2016 and it became quickly so popular because it’s so fast compared with the previous deep learning algorythm. 33 | With yolo we can detect real time objects at a relatively high speed. With a GPU we would be able to process over 45 frames/second while with a CPU around a frame per second. 34 | 35 | OpenCV dnn module supports running inference on pre-trained deep learning models from popular frameworks like Caffe, Torch and TensorFlow. 36 | 37 | ## Requirement 38 | 42 | 43 | ## Quick start 44 | 50 | 51 | ## Dependencies 52 | 56 | 57 | ## Install dependencies 58 |

pip install numpy opencv-python

59 | 60 | ## How to use? 61 |
    62 |
  1. Clone the repository
  2. 63 |

    git clone https://github.com/muhammadshiraz/YOLO-Real-Time-Object-Detection.git

    64 |
65 |
    66 |
  1. Move to the directory
  2. 67 |

    cd YOLO-Real-Time-Object-Detection

    68 |
69 |
    70 |
  1. To view the UK Real-Time Road Detection
  2. 71 |

    python real_time_yolo_detector1.py

    72 |
73 |
    74 |
  1. To view the USA Real-Time Road Detection
  2. 75 |

    python real_time_yolo_detector2.py

    76 |
77 |
    78 |
  1. To use in real-time on webcam
  2. 79 |

    python real_time_yolo_webcam.py

    80 |
81 | 82 | ## Graphical User Interface: 83 | #### A USA Real-Time Road Detection 84 | 85 | 86 | #### A UK Real-Time Road Detection 87 | 88 | 89 | #### A Real-Time Webcam Detection 90 | 91 | -------------------------------------------------------------------------------- /coco.names: -------------------------------------------------------------------------------- 1 | person 2 | bicycle 3 | car 4 | motorbike 5 | aeroplane 6 | bus 7 | train 8 | truck 9 | boat 10 | traffic light 11 | fire hydrant 12 | stop sign 13 | parking meter 14 | bench 15 | bird 16 | cat 17 | dog 18 | horse 19 | sheep 20 | cow 21 | elephant 22 | bear 23 | zebra 24 | giraffe 25 | backpack 26 | umbrella 27 | handbag 28 | tie 29 | suitcase 30 | frisbee 31 | skis 32 | snowboard 33 | sports ball 34 | kite 35 | baseball bat 36 | baseball glove 37 | skateboard 38 | surfboard 39 | tennis racket 40 | bottle 41 | wine glass 42 | cup 43 | fork 44 | knife 45 | spoon 46 | bowl 47 | banana 48 | apple 49 | sandwich 50 | orange 51 | broccoli 52 | carrot 53 | hot dog 54 | pizza 55 | donut 56 | cake 57 | chair 58 | sofa 59 | pottedplant 60 | bed 61 | diningtable 62 | toilet 63 | tvmonitor 64 | laptop 65 | mouse 66 | remote 67 | keyboard 68 | cell phone 69 | microwave 70 | oven 71 | toaster 72 | sink 73 | refrigerator 74 | book 75 | clock 76 | vase 77 | scissors 78 | teddy bear 79 | hair drier 80 | toothbrush -------------------------------------------------------------------------------- /doc/detector1.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/muhammadshiraz/YOLO-Real-Time-Object-Detection/79327114fa318892da1ef21cb91f3a63ed0b7100/doc/detector1.gif -------------------------------------------------------------------------------- /doc/detector1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/muhammadshiraz/YOLO-Real-Time-Object-Detection/79327114fa318892da1ef21cb91f3a63ed0b7100/doc/detector1.jpg -------------------------------------------------------------------------------- /doc/detector1.mp4: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/muhammadshiraz/YOLO-Real-Time-Object-Detection/79327114fa318892da1ef21cb91f3a63ed0b7100/doc/detector1.mp4 -------------------------------------------------------------------------------- /doc/detector1.wmv: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/muhammadshiraz/YOLO-Real-Time-Object-Detection/79327114fa318892da1ef21cb91f3a63ed0b7100/doc/detector1.wmv -------------------------------------------------------------------------------- /doc/detector2.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/muhammadshiraz/YOLO-Real-Time-Object-Detection/79327114fa318892da1ef21cb91f3a63ed0b7100/doc/detector2.gif -------------------------------------------------------------------------------- /doc/detector2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/muhammadshiraz/YOLO-Real-Time-Object-Detection/79327114fa318892da1ef21cb91f3a63ed0b7100/doc/detector2.jpg -------------------------------------------------------------------------------- /doc/detector2.mp4: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/muhammadshiraz/YOLO-Real-Time-Object-Detection/79327114fa318892da1ef21cb91f3a63ed0b7100/doc/detector2.mp4 -------------------------------------------------------------------------------- /doc/detector2.wmv: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/muhammadshiraz/YOLO-Real-Time-Object-Detection/79327114fa318892da1ef21cb91f3a63ed0b7100/doc/detector2.wmv -------------------------------------------------------------------------------- /doc/webcam_detector.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/muhammadshiraz/YOLO-Real-Time-Object-Detection/79327114fa318892da1ef21cb91f3a63ed0b7100/doc/webcam_detector.jpg -------------------------------------------------------------------------------- /real_time_yolo_detector1.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import time 4 | 5 | # Load Yolo 6 | net = cv2.dnn.readNet("weights/yolov3-tiny.weights", "cfg/yolov3-tiny.cfg") 7 | classes = [] 8 | with open("coco.names", "r") as f: 9 | classes = [line.strip() for line in f.readlines()] 10 | layer_names = net.getLayerNames() 11 | output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()] 12 | colors = np.random.uniform(0, 255, size=(len(classes), 3)) 13 | 14 | # Loading image 15 | cap = cv2.VideoCapture("uk.mp4") 16 | 17 | 18 | font = cv2.FONT_HERSHEY_PLAIN 19 | starting_time = time.time() 20 | frame_id = 0 21 | while True: 22 | _, frame = cap.read() 23 | frame_id += 1 24 | 25 | height, width, channels = frame.shape 26 | 27 | # Detecting objects 28 | blob = cv2.dnn.blobFromImage(frame, 0.00392, (416, 416), (0, 0, 0), True, crop=False) 29 | 30 | net.setInput(blob) 31 | outs = net.forward(output_layers) 32 | 33 | # Showing informations on the screen 34 | class_ids = [] 35 | confidences = [] 36 | boxes = [] 37 | for out in outs: 38 | for detection in out: 39 | scores = detection[5:] 40 | class_id = np.argmax(scores) 41 | confidence = scores[class_id] 42 | if confidence > 0.2: 43 | # Object detected 44 | center_x = int(detection[0] * width) 45 | center_y = int(detection[1] * height) 46 | w = int(detection[3] * width) 47 | h = int(detection[3] * height) 48 | 49 | # Rectangle coordinates 50 | x = int(center_x - w / 1.8) 51 | y = int(center_y - h / 1.8) 52 | 53 | boxes.append([x, y, w, h]) 54 | confidences.append(float(confidence)) 55 | class_ids.append(class_id) 56 | 57 | indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.4, 0.3) 58 | 59 | for i in range(len(boxes)): 60 | if i in indexes: 61 | x, y, w, h = boxes[i] 62 | label = str(classes[class_ids[i]]) 63 | confidence = confidences[i] 64 | color = colors[class_ids[i]] 65 | cv2.rectangle(frame, (x, y), (x + w, y + h), color, 2) 66 | cv2.putText(frame, label + " " + str(round(confidence, 2)), (x, y + 30), font, 2, color, 2) 67 | 68 | 69 | 70 | elapsed_time = time.time() - starting_time 71 | fps = frame_id / elapsed_time 72 | cv2.putText(frame, "FPS: " + str(round(fps, 2)), (10, 50), font, 2, (0, 0, 0), 3) 73 | cv2.imshow("Image", frame) 74 | key = cv2.waitKey(1) 75 | if key == 27: 76 | break 77 | 78 | cap.release() 79 | cv2.destroyAllWindows() -------------------------------------------------------------------------------- /real_time_yolo_detector2.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import time 4 | 5 | # Load Yolo 6 | net = cv2.dnn.readNet("weights/yolov3-tiny.weights", "cfg/yolov3-tiny.cfg") 7 | classes = [] 8 | with open("coco.names", "r") as f: 9 | classes = [line.strip() for line in f.readlines()] 10 | layer_names = net.getLayerNames() 11 | output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()] 12 | colors = np.random.uniform(0, 255, size=(len(classes), 3)) 13 | 14 | # Loading image 15 | cap = cv2.VideoCapture("usa-street.mp4") 16 | 17 | 18 | font = cv2.FONT_HERSHEY_PLAIN 19 | starting_time = time.time() 20 | frame_id = 0 21 | while True: 22 | _, frame = cap.read() 23 | frame_id += 1 24 | 25 | height, width, channels = frame.shape 26 | 27 | # Detecting objects 28 | blob = cv2.dnn.blobFromImage(frame, 0.00392, (416, 416), (0, 0, 0), True, crop=False) 29 | 30 | net.setInput(blob) 31 | outs = net.forward(output_layers) 32 | 33 | # Showing informations on the screen 34 | class_ids = [] 35 | confidences = [] 36 | boxes = [] 37 | for out in outs: 38 | for detection in out: 39 | scores = detection[5:] 40 | class_id = np.argmax(scores) 41 | confidence = scores[class_id] 42 | if confidence > 0.2: 43 | # Object detected 44 | center_x = int(detection[0] * width) 45 | center_y = int(detection[1] * height) 46 | w = int(detection[3] * width) 47 | h = int(detection[3] * height) 48 | 49 | # Rectangle coordinates 50 | x = int(center_x - w / 1.8) 51 | y = int(center_y - h / 1.8) 52 | 53 | boxes.append([x, y, w, h]) 54 | confidences.append(float(confidence)) 55 | class_ids.append(class_id) 56 | 57 | indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.4, 0.3) 58 | 59 | for i in range(len(boxes)): 60 | if i in indexes: 61 | x, y, w, h = boxes[i] 62 | label = str(classes[class_ids[i]]) 63 | confidence = confidences[i] 64 | color = colors[class_ids[i]] 65 | cv2.rectangle(frame, (x, y), (x + w, y + h), color, 2) 66 | cv2.putText(frame, label + " " + str(round(confidence, 2)), (x, y + 30), font, 2, color, 2) 67 | 68 | 69 | 70 | elapsed_time = time.time() - starting_time 71 | fps = frame_id / elapsed_time 72 | cv2.putText(frame, "FPS: " + str(round(fps, 2)), (10, 50), font, 2, (0, 0, 0), 3) 73 | cv2.imshow("Image", frame) 74 | key = cv2.waitKey(1) 75 | if key == 27: 76 | break 77 | 78 | cap.release() 79 | cv2.destroyAllWindows() -------------------------------------------------------------------------------- /real_time_yolo_webcam.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | # Load Yolo 5 | net = cv2.dnn.readNet("weights/yolov3.weights", "cfg/yolov3.cfg") 6 | classes = [] 7 | with open("coco.names", "r") as f: 8 | classes = [line.strip() for line in f.readlines()] 9 | layer_names = net.getLayerNames() 10 | output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()] 11 | colors = np.random.uniform(0, 255, size=(len(classes), 3)) 12 | 13 | # Loading image 14 | img = cv2.imread("src_room.jpg") 15 | img = cv2.resize(img, None, fx=0.8, fy=0.7) 16 | height, width, channels = img.shape 17 | 18 | # Detecting objects 19 | blob = cv2.dnn.blobFromImage(img, 0.00392, (416, 416), (0, 0, 0), True, crop=False) 20 | 21 | net.setInput(blob) 22 | outs = net.forward(output_layers) 23 | 24 | # Showing informations on the screen 25 | class_ids = [] 26 | confidences = [] 27 | boxes = [] 28 | for out in outs: 29 | for detection in out: 30 | scores = detection[5:] 31 | class_id = np.argmax(scores) 32 | confidence = scores[class_id] 33 | if confidence > 0.5: 34 | # Object detected 35 | center_x = int(detection[0] * width) 36 | center_y = int(detection[1] * height) 37 | w = int(detection[2] * width) 38 | h = int(detection[3] * height) 39 | 40 | # Rectangle coordinates 41 | x = int(center_x - w / 2) 42 | y = int(center_y - h / 2) 43 | 44 | boxes.append([x, y, w, h]) 45 | confidences.append(float(confidence)) 46 | class_ids.append(class_id) 47 | 48 | indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4) 49 | print(indexes) 50 | font = cv2.FONT_HERSHEY_PLAIN 51 | for i in range(len(boxes)): 52 | if i in indexes: 53 | x, y, w, h = boxes[i] 54 | label = str(classes[class_ids[i]]) 55 | color = colors[i] 56 | cv2.rectangle(img, (x, y), (x + w, y + h), color, 1) 57 | cv2.putText(img, label, (x, y + 30), font, 3, color, 2) 58 | 59 | 60 | cv2.imshow("Image", img) 61 | cv2.waitKey(0) 62 | cv2.destroyAllWindows() -------------------------------------------------------------------------------- /src_room.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/muhammadshiraz/YOLO-Real-Time-Object-Detection/79327114fa318892da1ef21cb91f3a63ed0b7100/src_room.jpg -------------------------------------------------------------------------------- /uk.mp4: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/muhammadshiraz/YOLO-Real-Time-Object-Detection/79327114fa318892da1ef21cb91f3a63ed0b7100/uk.mp4 -------------------------------------------------------------------------------- /usa-street.mp4: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/muhammadshiraz/YOLO-Real-Time-Object-Detection/79327114fa318892da1ef21cb91f3a63ed0b7100/usa-street.mp4 --------------------------------------------------------------------------------