I haven’t tested this but I believe you would simply change some of the code close to the end of the snippet you posted.
if (count(get_posts("post_type=attachment&post_parent={$post_id}"))>5)
$file['error'] = "Sorry, you cannot upload more than five (5) images.";
Let me know if this doesn’t address your needs.