Optimization for Detection and Recognition in Images and Videos