YOLO — Object Detection
The State of the Art in Object Detection Framework! #PyVisionSeries — Episode #07
This is a brief overview and introduction to the YOLO object detection algorithm.So this is one of the state of the art image detection algorithms, and it's known as YOLO, which stands for you only look once YOLO can view an image and then draw bounding boxes over what it perceives as identified classes.Welcome!
Here is the structure you will need to mount:
Let’s see how to use the state of the art in object detection!
Please make sure to load this file, yolo.h5, an already trained YOLO model, and put this file in the directory as indicated above and below :)
You will need to download this large weights file (over 200MB):
yalo.v5CODE SOURCE: https://github.com/xiaochus/YOLOv3
Quick start
Throw this here:
/YOLO/data/
REFERENCE (for original YOLOv3):
@article{YOLOv3,
title={YOLOv3: An Incremental Improvement},
author={J Redmon, A Farhadi },
year={2018}
To start off import opencv, tensorflow and keras libs:
!pip3 install opencv-python
Now tensorflow:
!pip3 install tensorflow
And keras:
!pip3 install keras
01 step # Import others libs
import os
import time
import cv2
import numpy as np
from model.yolo_model import YOLO
02 step # Preparing the methods to be used
def process_image(img):
image = cv2.resize(img, (416, 416),interpolation=cv2.INTER_CUBIC)
image = np.array(image, dtype='float32')
image /= 255.
image = np.expand_dims(image, axis=0) return image
Get classes:
def get_classes(file):
with open(file) as f:
class_names = f.readlines()
class_names = [c.strip() for c in class_names]return class_names
Draw:
def draw(image, boxes, scores, classes, all_classes):
for box, score, cl in zip(boxes, scores, classes):
x, y, w, h = box
top = max(0, np.floor(x + 0.5).astype(int))
left = max(0, np.floor(y + 0.5).astype(int))
right = min(image.shape[1], np.floor(x + w + 0.5).astype(int))
bottom = min(image.shape[0], np.floor(y + h + 0.5).astype(int))
cv2.rectangle(image, (top, left), (right, bottom), (255, 0, 0),
2)
cv2.putText(image, '{0} {1:.2f}'.format(all_classes[cl], score),
(top, left - 6),cv2.FONT_HERSHEY_SIMPLEX,0.6, (0, 0, 255),
1,cv2.LINE_AA)
print('class: {0}, score: {1:.2f}'.format(all_classes[cl],
score))
print('box coordinate x,y,w,h: {0}'.format(box))print()
Detecting:
def detect_image(image, yolo, all_classes): pimage = process_image(image)
start = time.time()
boxes, classes, scores = yolo.predict(pimage, image.shape)
end = time.time() print('time: {0:.2f}s'.format(end - start)) if boxes is not None:
draw(image, boxes, scores, classes, all_classes) return image
For videos(in case you’ll need:)
def detect_video(video, yolo, all_classes):
video_path = os.path.join("videos", "test", video)
camera = cv2.VideoCapture(video_path)
cv2.namedWindow("detection", cv2.WINDOW_AUTOSIZE) # Prepare for saving the detected video
sz = (int(camera.get(cv2.CAP_PROP_FRAME_WIDTH)),
int(camera.get(cv2.CAP_PROP_FRAME_HEIGHT))) fourcc = cv2.VideoWriter_fourcc(*'mpeg')
vout = cv2.VideoWriter()
vout.open(os.path.join("videos", "res", video), fourcc, 20, sz,
True)while True:
res, frame = camera.read()
if not res:
break image = detect_image(frame, yolo, all_classes)
cv2.imshow("detection", image)
# Save the video frame by frame
vout.write(image)
if cv2.waitKey(110) & 0xff == 27:
breakvout.release()
camera.release()
Loading YOLO:
yolo = YOLO(0.6, 0.5)
file = 'data/coco_classes.txt'
all_classes = get_classes(file)WARNING:tensorflow:No training configuration found in the save file, so the model was *not* compiled. Compile it manually.
Detecting Images
f = 'spoon_knife.jpg'
# f = 'carrot_apple.jpg'
# f = 'bus_traffic_light.jpg'
# f = 'central_market.jpg'
# f = 'friend_groups.jpg'
# f = 'fruit_basket.jpg'
# f = 'house_kitchen.jpg'
# f = 'spoon_knife.jpg'
# f = 'tv_laptop.jpg'
# f = 'vegetable.jpg'
# f = 'woman_on_track.jpg'
path = 'images/test/'+f
image = cv2.imread(path)
image = detect_image(image, yolo, all_classes)
cv2.imwrite('images/res/' + f, image)1/1 [==============================] - 1s 1s/step
time: 1.33s
class: fork, score: 0.92
box coordinate x,y,w,h: [187.73759604 19.22580004 36.17203832 347.9186058 ]
class: knife, score: 0.87
box coordinate x,y,w,h: [230.02996445 27.17533112 26.21091604 358.97622108]
class: knife, score: 0.77
box coordinate x,y,w,h: [240.34616947 1.77932978 25.62791705 385.19451618]
class: spoon, score: 0.84
box coordinate x,y,w,h: [124.01204109 116.89527035 53.72431278 232.19351768]True
That all folks!
print("That's it! Thank you once again!\nI hope will be helpful.")That's it! Thank you once again!
I hope will be helpful.
Folow the images tested by us:
Note that not all objects were detected.
This technique is evolving!I think we got the message across.To run on your win 10 platform download all the files.
Then go to that site and download yalo.v5 and upload it to the directory as shown on this image.
👉Jupiter notebook link :)
👉Github (YALO/)
Credits & References:
Jose Portilla — Python for Computer Vision with OpenCV and Deep Learning — Learn the latest techniques in computer vision with Python, OpenCV, and Deep Learning!
https://ieeexplore.ieee.org/abstract/document/8343709
https://iaexpert.academy/2020/10/13/deteccao-de-objetos-com-yolo-uma-abordagem-moderna/
YOLOv4 Paper — https://arxiv.org/abs/2004.10934
YOLO: Real-Time Object Detection — https://pjreddie.com/darknet/yolo/
Posts Related:
00 Episode# Hi Python Computer Vision — PIL! — An Intro To Python Imaging Library #PyVisionSeries
01 Episode# Jupyter-lab — Python — OpenCV — Image Processing Exercises #PyVisionSeries
02 Episode# OpenCV — Image Basics — Create Image From Scratch #PyVisionSeries
03 Episode# OpenCV — Morphological Operations — How To Erode, Dilate, Edge Detect w/ Gradient #PyVisionSeries
04 Episode# OpenCV — Histogram Equalization — HOW TO Equalize Histograms Of Images — #PyVisionSeries
05 Episode# OpenCV — Resize an image — How To Resize Without Distortion — #PyVisionSeries
07 Episode# YOLO — Object Detection — The state of the art in object detection Framework!
08 Episode# OpenCV — HaashCascate — Object Detection — Viola–Jones object detection framework — #PyVisionSeries