An Adaptive Unified Algorithm for Both Detection and Recognition