nbeuchat commited on
Commit
80d88e9
1 Parent(s): 0959acc

fix image resizing and update article info

Browse files
Files changed (1) hide show
  1. app.py +24 -4
app.py CHANGED
@@ -1,8 +1,10 @@
1
  import gradio as gr
 
 
 
2
  from actors_matching.api import analyze_image, load_annoy_index
3
  from pathlib import Path
4
 
5
-
6
  annoy_index, actors_mapping = load_annoy_index()
7
 
8
 
@@ -29,8 +31,22 @@ def get_best_matches(image, n_matches: int):
29
  return analyze_image(image, annoy_index=annoy_index, n_matches=n_matches)
30
 
31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  def find_matching_actors(input_img, title, n_matches: int = 10):
33
- best_matches_list = get_best_matches(input_img, n_matches=n_matches)
 
34
 
35
  # TODO: allow looping through characters
36
  if best_matches_list:
@@ -55,9 +71,13 @@ iface = gr.Interface(
55
  title="Which actor or actress looks like you?",
56
  description="""Who is the best person to play a movie about you? Upload a picture and find out!
57
  Or maybe you'd like to know who would best interpret your favorite historical character?
58
- Give it a shot or try one of the sample images below.\nPlease read below for more information on biases
 
 
 
 
59
  and limitations of the tool!""",
60
- article=Path("README.md").read_text(),
61
  inputs=[
62
  gr.inputs.Image(shape=None, label="Your image"),
63
  gr.inputs.Textbox(
1
  import gradio as gr
2
+ import PIL
3
+ import numpy as np
4
+ import re
5
  from actors_matching.api import analyze_image, load_annoy_index
6
  from pathlib import Path
7
 
 
8
  annoy_index, actors_mapping = load_annoy_index()
9
 
10
 
31
  return analyze_image(image, annoy_index=annoy_index, n_matches=n_matches)
32
 
33
 
34
+ def resize_image_keep_ratio(input_image: np.array, size: tuple):
35
+ resized_image = PIL.Image.fromarray(input_image)
36
+ resized_image.thumbnail(size, PIL.Image.ANTIALIAS)
37
+ return np.array(resized_image)
38
+
39
+
40
+ def get_article_text():
41
+ article = Path("README.md").read_text()
42
+ # Remove the HuggingFace Space app information from the README
43
+ article = re.sub(r"^---.+---\s+", "", article, flags=re.MULTILINE + re.DOTALL)
44
+ return article
45
+
46
+
47
  def find_matching_actors(input_img, title, n_matches: int = 10):
48
+ resized_image = resize_image_keep_ratio(input_img, (512, 512))
49
+ best_matches_list = get_best_matches(resized_image, n_matches=n_matches)
50
 
51
  # TODO: allow looping through characters
52
  if best_matches_list:
71
  title="Which actor or actress looks like you?",
72
  description="""Who is the best person to play a movie about you? Upload a picture and find out!
73
  Or maybe you'd like to know who would best interpret your favorite historical character?
74
+ Give it a shot or try one of the sample images below.
75
+
76
+ Built with ❤️ using great open-source libraries such as dlib, face_recognition and Annoy.
77
+
78
+ Please read below for more information on biases
79
  and limitations of the tool!""",
80
+ article=get_article_text(),
81
  inputs=[
82
  gr.inputs.Image(shape=None, label="Your image"),
83
  gr.inputs.Textbox(