jeffin07 commited on
Commit
9e40f68
1 Parent(s): 5ef5701

face detection app initial commit

Browse files
Files changed (2) hide show
  1. app.py +73 -0
  2. sample.jpg +0 -0
app.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import cv2
3
+ import numpy as np
4
+ import torch
5
+ 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
+
34
+
35
+ img_vis = img_raw.copy()
36
+
37
+ vis_threshold = 0.8
38
+
39
+ for b in dets:
40
+ if b.score < vis_threshold:
41
+ continue
42
+
43
+ # draw face bounding box
44
+ img_vis = cv2.rectangle(img_vis, b.top_left.int().tolist(), b.bottom_right.int().tolist(), (0, 255, 0), 4)
45
+
46
+
47
+ return img_vis
48
+
49
+
50
+
51
+
52
+ title = "Kornia Face Detection"
53
+ 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>"
54
+ 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>"
55
+
56
+ examples = ['sample.jpg']
57
+
58
+
59
+ face = gr.Interface(
60
+ detect,
61
+ gr.inputs.Image(type="numpy"),
62
+ gr.Image(type="pil", interactive=False),
63
+ examples=examples,
64
+ title=title,
65
+ description=description,
66
+ article=article,
67
+ live=True,
68
+ allow_flagging="never"
69
+ )
70
+
71
+
72
+ face.launch()
73
+
sample.jpg ADDED