Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

face_detection_cli.py 2.3 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
  1. # -*- coding: utf-8 -*-
  2. from __future__ import print_function
  3. import click
  4. import os
  5. import re
  6. import face_recognition.api as face_recognition
  7. import multiprocessing
  8. import sys
  9. import itertools
  10. def print_result(filename, location):
  11. top, right, bottom, left = location
  12. print("{},{},{},{},{}".format(filename, top, right, bottom, left))
  13. def test_image(image_to_check, model):
  14. unknown_image = face_recognition.load_image_file(image_to_check)
  15. face_locations = face_recognition.face_locations(unknown_image, number_of_times_to_upsample=0, model=model)
  16. for face_location in face_locations:
  17. print_result(image_to_check, face_location)
  18. def image_files_in_folder(folder):
  19. return [os.path.join(folder, f) for f in os.listdir(folder) if re.match(r'.*\.(jpg|jpeg|png)', f, flags=re.I)]
  20. def process_images_in_process_pool(images_to_check, number_of_cpus, model):
  21. if number_of_cpus == -1:
  22. processes = None
  23. else:
  24. processes = number_of_cpus
  25. # macOS will crash due to a bug in libdispatch if you don't use 'forkserver'
  26. context = multiprocessing
  27. if "forkserver" in multiprocessing.get_all_start_methods():
  28. context = multiprocessing.get_context("forkserver")
  29. pool = context.Pool(processes=processes)
  30. function_parameters = zip(
  31. images_to_check,
  32. itertools.repeat(model),
  33. )
  34. pool.starmap(test_image, function_parameters)
  35. @click.command()
  36. @click.argument('image_to_check')
  37. @click.option('--cpus', default=1, help='number of CPU cores to use in parallel. -1 means "use all in system"')
  38. @click.option('--model', default="hog", help='Which face detection model to use. Options are "hog" or "cnn".')
  39. def main(image_to_check, cpus, model):
  40. # Multi-core processing only supported on Python 3.4 or greater
  41. if (sys.version_info < (3, 4)) and cpus != 1:
  42. click.echo("WARNING: Multi-processing support requires Python 3.4 or greater. Falling back to single-threaded processing!")
  43. cpus = 1
  44. if os.path.isdir(image_to_check):
  45. if cpus == 1:
  46. [test_image(image_file, model) for image_file in image_files_in_folder(image_to_check)]
  47. else:
  48. process_images_in_process_pool(image_files_in_folder(image_to_check), cpus, model)
  49. else:
  50. test_image(image_to_check, model)
  51. if __name__ == "__main__":
  52. main()
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...