mischeiwiller commited on
Commit
05c65ac
·
verified ·
1 Parent(s): ea6e321

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -34
app.py CHANGED
@@ -6,38 +6,25 @@ import kornia as K
6
  from kornia.core import Tensor
7
  from kornia.contrib import FaceDetector, FaceDetectorResult, FaceKeypoint
8
 
9
-
10
-
11
-
12
  def draw_keypoint(img: np.ndarray, det: FaceDetectorResult, kpt_type: FaceKeypoint) -> np.ndarray:
13
  kpt = det.get_keypoint(kpt_type).int().tolist()
14
  return cv2.circle(img, kpt, 2, (255, 0, 0), 2)
15
 
16
-
17
-
18
  def detect(img_raw):
19
-
20
  # preprocess
21
  if img_raw is not None and len(img_raw.shape) == 3:
22
- img = K.image_to_tensor(img_raw, keepdim=False)
23
  img = K.color.bgr_to_rgb(img.float())
24
-
25
-
26
  # create the detector and find the faces !
27
  face_detection = FaceDetector()
28
-
29
  with torch.no_grad():
30
  dets = face_detection(img)
31
  dets = [FaceDetectorResult(o) for o in dets[0]]
32
-
33
  img_vis = img_raw.copy()
34
-
35
  vis_threshold = 0.8
36
-
37
  for b in dets:
38
  if b.score < vis_threshold:
39
  continue
40
-
41
  # Draw face bounding box
42
  img_vis = cv2.rectangle(img_vis, b.top_left.int().tolist(), b.bottom_right.int().tolist(), (0, 255, 0), 4)
43
  # Draw Keypoints
@@ -46,30 +33,26 @@ def detect(img_raw):
46
  img_vis = draw_keypoint(img_vis, b, FaceKeypoint.NOSE)
47
  img_vis = draw_keypoint(img_vis, b, FaceKeypoint.MOUTH_LEFT)
48
  img_vis = draw_keypoint(img_vis, b, FaceKeypoint.MOUTH_RIGHT)
49
-
50
  return img_vis
51
 
52
-
53
-
54
  title = "Kornia Face Detection"
55
  description = "<p style='text-align: center'>This is a Gradio demo for Kornia's Face Detection.</p><p style='text-align: center'>To use it, simply upload your image, or click one of the examples to load them</p>"
56
  article = "<p style='text-align: center'><a href='https://kornia.readthedocs.io/en/latest/' target='_blank'>Kornia Docs</a> | <a href='https://github.com/kornia/kornia' target='_blank'>Kornia Github Repo</a> | <a href='https://kornia.readthedocs.io/en/latest/applications/face_detection.html' target='_blank'>Kornia Face Detection Tutorial</a></p>"
57
-
58
  examples = ['sample.jpg']
59
 
60
-
61
- face = gr.Interface(
62
- detect,
63
- gr.inputs.Image(type="numpy"),
64
- gr.Image(type="pil", interactive=False),
65
- examples=examples,
66
- title=title,
67
- description=description,
68
- article=article,
69
- live=True,
70
- allow_flagging="never"
71
- )
72
-
73
-
74
- face.launch()
75
-
 
6
  from kornia.core import Tensor
7
  from kornia.contrib import FaceDetector, FaceDetectorResult, FaceKeypoint
8
 
 
 
 
9
  def draw_keypoint(img: np.ndarray, det: FaceDetectorResult, kpt_type: FaceKeypoint) -> np.ndarray:
10
  kpt = det.get_keypoint(kpt_type).int().tolist()
11
  return cv2.circle(img, kpt, 2, (255, 0, 0), 2)
12
 
 
 
13
  def detect(img_raw):
 
14
  # preprocess
15
  if img_raw is not None and len(img_raw.shape) == 3:
16
+ img = K.utils.image_to_tensor(img_raw, keepdim=False)
17
  img = K.color.bgr_to_rgb(img.float())
 
 
18
  # create the detector and find the faces !
19
  face_detection = FaceDetector()
 
20
  with torch.no_grad():
21
  dets = face_detection(img)
22
  dets = [FaceDetectorResult(o) for o in dets[0]]
 
23
  img_vis = img_raw.copy()
 
24
  vis_threshold = 0.8
 
25
  for b in dets:
26
  if b.score < vis_threshold:
27
  continue
 
28
  # Draw face bounding box
29
  img_vis = cv2.rectangle(img_vis, b.top_left.int().tolist(), b.bottom_right.int().tolist(), (0, 255, 0), 4)
30
  # Draw Keypoints
 
33
  img_vis = draw_keypoint(img_vis, b, FaceKeypoint.NOSE)
34
  img_vis = draw_keypoint(img_vis, b, FaceKeypoint.MOUTH_LEFT)
35
  img_vis = draw_keypoint(img_vis, b, FaceKeypoint.MOUTH_RIGHT)
 
36
  return img_vis
37
 
 
 
38
  title = "Kornia Face Detection"
39
  description = "<p style='text-align: center'>This is a Gradio demo for Kornia's Face Detection.</p><p style='text-align: center'>To use it, simply upload your image, or click one of the examples to load them</p>"
40
  article = "<p style='text-align: center'><a href='https://kornia.readthedocs.io/en/latest/' target='_blank'>Kornia Docs</a> | <a href='https://github.com/kornia/kornia' target='_blank'>Kornia Github Repo</a> | <a href='https://kornia.readthedocs.io/en/latest/applications/face_detection.html' target='_blank'>Kornia Face Detection Tutorial</a></p>"
 
41
  examples = ['sample.jpg']
42
 
43
+ with gr.Blocks(title=title) as demo:
44
+ gr.Markdown(f"# {title}")
45
+ gr.Markdown(description)
46
+
47
+ with gr.Row():
48
+ input_image = gr.Image(type="numpy", label="Input Image")
49
+ output_image = gr.Image(type="numpy", label="Detected Faces")
50
+
51
+ gr.Examples(examples, inputs=input_image)
52
+
53
+ input_image.change(fn=detect, inputs=input_image, outputs=output_image)
54
+
55
+ gr.Markdown(article)
56
+
57
+ if __name__ == "__main__":
58
+ demo.launch()