DCuong commited on
Commit
7872d4b
1 Parent(s): b64f1bc

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -0
app.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from huggingface_hub import hf_hub_download
2
+ from ultralytics import YOLO
3
+ from supervision import Detections
4
+ import cv2
5
+ import gradio as gr
6
+ from PIL import Image
7
+ import numpy as np
8
+
9
+ model_path = hf_hub_download(repo_id="arnabdhar/YOLOv8-Face-Detection", filename="model.pt")
10
+ model = YOLO(model_path)
11
+
12
+ def detect_faces(image):
13
+ output = model(image)
14
+ results = Detections.from_ultralytics(output[0])
15
+
16
+ im = np.array(image)
17
+ for i in results:
18
+ im = cv2.rectangle(im, (int(i[0][0]),int(i[0][1])), (int(i[0][2]),int(i[0][3])), (0,0,255), 2)
19
+
20
+ image_np = np.array(image)
21
+ gray_image = cv2.cvtColor(image_np, cv2.COLOR_RGB2GRAY)
22
+ face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
23
+ faces = face_cascade.detectMultiScale(gray_image, scaleFactor=1.1, minNeighbors=5, minSize=(5, 5))
24
+ for (x, y, w, h) in faces:
25
+ cv2.rectangle(image_np, (x, y), (x+w, y+h), (0, 255, 0), 2)
26
+
27
+ return (image_np,im)
28
+
29
+ interface = gr.Interface(
30
+ fn=detect_faces,
31
+ inputs="image",
32
+ outputs=["image","image"],
33
+ title="Face Detection Deep Learning",
34
+ description="Upload an image, and the model will detect faces and draw bounding boxes around them.",
35
+ )
36
+ interface.launch()