cbensimon HF staff commited on
Commit
2a17845
1 Parent(s): 9d9afac

Avoid `IndexError` when no face is detected

Browse files

Currently the Space sometimes errors on :
``` Python
Traceback (most recent call last):
File "/home/user/.local/lib/python3.10/site-packages/spaces/zero/wrappers.py", line 164, in thread_wrapper
res = future.result()
File "/usr/local/lib/python3.10/concurrent/futures/_base.py", line 451, in result
return self.__get_result()
File "/usr/local/lib/python3.10/concurrent/futures/_base.py", line 403, in __get_result
raise self._exception
File "/usr/local/lib/python3.10/concurrent/futures/thread.py", line 58, in run
result = self.fn(*self.args, **self.kwargs)
File "/home/user/app/app.py", line 61, in generate_image
faceid_embed = torch.from_numpy(faces[0].normed_embedding).unsqueeze(0)
IndexError: list index out of range
```

This change should display an error modal instead

Files changed (1) hide show
  1. app.py +3 -1
app.py CHANGED
@@ -55,9 +55,11 @@ def generate_image(images, prompt, negative_prompt, preserve_face_structure, fac
55
 
56
  faceid_all_embeds = []
57
  first_iteration = True
58
- for image in images:
59
  face = cv2.imread(image)
60
  faces = app.get(face)
 
 
61
  faceid_embed = torch.from_numpy(faces[0].normed_embedding).unsqueeze(0)
62
  faceid_all_embeds.append(faceid_embed)
63
  if(first_iteration and preserve_face_structure):
 
55
 
56
  faceid_all_embeds = []
57
  first_iteration = True
58
+ for i, image in enumerate(images):
59
  face = cv2.imread(image)
60
  faces = app.get(face)
61
+ if len(faces) == 0:
62
+ raise gr.Error(f"No face detected in image number {i+1}")
63
  faceid_embed = torch.from_numpy(faces[0].normed_embedding).unsqueeze(0)
64
  faceid_all_embeds.append(faceid_embed)
65
  if(first_iteration and preserve_face_structure):