Object Detection Tutorial (YOLO) : Description
Object Detection Tutorial (YOLO) : Description
Description
In this tutorial we will go step by step on how to run state of the art object detection CNN (YOLO) using
open source projects and TensorFlow, YOLO is a R-CNN network for detecting objects and proposing
bounding boxes on them. It has more a lot of variations and configurations. We will focus on using the
Tiny, 80 classes COCO one. YOLO is pretty lightweight and it achieves around 1FPS on Euclid. Impressive!
https://pjreddie.com/darknet/yolo/ https://pjreddie.com/media/files/papers/yolo_1.pdf
1. Installing TensorFlow
Install TensorFlow using PIP : $ sudo pip install tensorflow
Note: if you don’t have pip yet install it using: $ sudo apt-get install python-pip
2. Installing DarkFlow
DarkFlow is a network builder adapted from Darknet, it allows building TensorFlow networks from cfg
files and loading pre trained weights. We will use it to run YOLO.
class EuclidObjectRecognizer():
def __init__(self):
self.detectionImage = self.image = None
self.lastTime = time.time()
self.elapsedTime = 1
self.boxes = []
script_path = os.path.dirname(os.path.realpath(__file__))
self.tfnet = TFNet(self.options)
self.colors = self.tfnet.meta['colors']
self.classesColorMap = {}
# Start ROS
rospy.init_node("object_recognizer", anonymous=True)
self.bridge = CvBridge()
self.imagePub = rospy.Publisher("/euclid/object/live", Image)
self.imageSub = rospy.Subscriber(
"/camera/color/image_raw", Image, self.newColorImage)
self.rate = rospy.Rate(10)
while self.image == None:
self.rate.sleep()
self.liveThread = threading.Thread(target=self.liveRecognitionThread)
self.liveThread.start()
self.mainThread()
def mainThread(self):
h, w, _ = self.image.shape
while not rospy.is_shutdown():
self.detectionImage = self.image.copy()
for bbox in self.boxes:
left, top, right, bot, label = bbox['topleft']['x'], bbox['topleft'][
'y'], bbox['bottomright']['x'], bbox['bottomright']['y'],
bbox['label']
color = self.getClassColor(label)
self.imagePub.publish(self.bridge.cv2_to_imgmsg(self.detectionImage,
"bgr8"))
self.rate.sleep()
def liveRecognitionThread(self):
print "Starting live recognition"
while not rospy.is_shutdown():
self.lastTime = time.time()
self.boxes = self.tfnet.return_predict(self.image)
self.elapsedTime = time.time() - self.lastTime
print "Live recognition Stopped"
if __name__ == "__main__":
try:
recgonizer = EuclidObjectRecognizer()
except Exception as e:
print e
rospy.signal_shutdown()
Change self.options to point to the configuration and weight files you download.
Note: I recommend saving the weights and configuration files in the same directory as the
script or to change the script_path variable to point to that directory.
Note: The classes names should be in the same directory as the configuration file.
$ python yolo_demo.py
4. Time to play
Now you can try different weights and configurations, or simply start building an application using this
amazing capability.
Some ideas:
f. Use depth to do tracking on the bounding boxes to get better results and FPS
g. Use the depth camera to calculate the distance of objects from Euclid
h. Use object detection for scene understanding and reasoning.