LBLong commited on
Commit
83af7df
1 Parent(s): b113bd5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -5
app.py CHANGED
@@ -10,21 +10,44 @@ model_path = hf_hub_download(repo_id="arnabdhar/YOLOv8-Face-Detection", filename
10
  model = YOLO(model_path)
11
 
12
  def detect_faces(image):
13
-
 
 
14
  output = model(image)
15
  results = Detections.from_ultralytics(output[0])
16
 
17
  im = np.array(image)
18
  for i in results:
19
- im = cv2.rectangle(im, (int(i[0][0]),int(i[0][1])), (int(i[0][2]),int(i[0][3])), (0,0,255), 2)
20
 
21
  image_np = np.array(image)
22
  gray_image = cv2.cvtColor(image_np, cv2.COLOR_RGB2GRAY)
23
- face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
24
- faces = face_cascade.detectMultiScale(gray_image, scaleFactor=1.1, minNeighbors=5, minSize=(5, 5))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  for (x, y, w, h) in faces:
26
  cv2.rectangle(image_np, (x, y), (x+w, y+h), (0, 255, 0), 2)
27
-
 
 
28
  return (image_np,im)
29
 
30
  interface = gr.Interface(
 
10
  model = YOLO(model_path)
11
 
12
  def detect_faces(image):
13
+
14
+ print(type(image))
15
+
16
  output = model(image)
17
  results = Detections.from_ultralytics(output[0])
18
 
19
  im = np.array(image)
20
  for i in results:
21
+ im = cv2.rectangle(im, (int(i[0][0]),int(i[0][1])), (int(i[0][2]),int(i[0][3])), (255,0,0), 2)
22
 
23
  image_np = np.array(image)
24
  gray_image = cv2.cvtColor(image_np, cv2.COLOR_RGB2GRAY)
25
+
26
+ face_cascade_face_1 = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
27
+ face_cascade_face_2 = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_alt.xml")
28
+ face_cascade_face_3 = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_alt2.xml")
29
+
30
+ faces1 = face_cascade_face_1.detectMultiScale(gray_image, scaleFactor=1.1, minNeighbors=5, minSize=(5, 5))
31
+ faces2 = face_cascade_face_2.detectMultiScale(gray_image, scaleFactor=1.1, minNeighbors=5, minSize=(5, 5))
32
+ faces3 = face_cascade_face_3.detectMultiScale(gray_image, scaleFactor=1.1, minNeighbors=5, minSize=(5, 5))
33
+
34
+ if len(faces1) <= len(faces2):
35
+ if len(faces2) < len(faces3):
36
+ faces = faces3
37
+ else:
38
+ faces = faces2
39
+ else:
40
+ faces = faces1
41
+ print(len(faces1),len(faces2),len(faces3))
42
+
43
+ face_cascade_eye = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_eye.xml")
44
+ eyes = face_cascade_eye.detectMultiScale(gray_image, scaleFactor=1.1, minNeighbors=5, minSize=(5, 5))
45
+
46
  for (x, y, w, h) in faces:
47
  cv2.rectangle(image_np, (x, y), (x+w, y+h), (0, 255, 0), 2)
48
+ for (x, y, w, h) in eyes:
49
+ cv2.rectangle(image_np, (x, y), (x+w, y+h), (0, 0, 255), 2)
50
+
51
  return (image_np,im)
52
 
53
  interface = gr.Interface(