arshtech commited on
Commit
9381ccb
·
verified ·
1 Parent(s): 3ce4e8b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -32
app.py CHANGED
@@ -3,7 +3,8 @@ import imutils
3
  import gradio as gr
4
  import numpy as np
5
 
6
- face_detector = cv2.CascadeClassifier("haar_cascades/haarcascade_frontalface_default.xml")
 
7
 
8
  def detect_faces(img, size, neighbours, scale):
9
  frame = np.array(img)
@@ -12,39 +13,26 @@ def detect_faces(img, size, neighbours, scale):
12
  gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
13
 
14
  faceRects = face_detector.detectMultiScale(
15
- gray, scaleFactor=1.1, minNeighbors=5)
 
16
 
17
  box_data = []
 
18
 
19
- class_labels = {
20
- 0: "face"
21
- }
22
-
23
- for (x,y,w,h) in faceRects:
24
- frame = cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)
25
-
26
- midX = int(x+w/2)
27
- midY = int(y+h/2)
28
  box = {
29
- "position": {
30
- "middle": [midX, midY],
31
- "width": float(w),
32
- "height": float(h)
33
- },
34
- "domain" : "pixel",
35
- "class_id" : 0
36
- }
37
- box_data.append(box)
38
-
39
- predictions = {"predictions": {
40
- "box_data": box_data,
41
- "class_labels": class_labels
42
- }
43
  }
 
44
 
45
- re_im =cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
46
  return re_im
47
-
48
  image = gr.components.Image()
49
  out_im = gr.components.Image()
50
 
@@ -52,13 +40,10 @@ size_slider = gr.components.Slider(minimum=5, maximum=50, value=30, step=5, labe
52
  neighbour_slider = gr.components.Slider(minimum=1, maximum=20, value=5, step=1, label="Min Number of Neighbours")
53
  scale_slider = gr.components.Slider(minimum=1.1, maximum=2.0, value=1.3, step=0.1, label="Scale Factor")
54
 
55
- description = """Face Detection with Haar Cascades using OpenCV"""
56
-
57
-
58
  Iface = gr.Interface(
59
  fn=detect_faces,
60
  inputs=[image, size_slider, neighbour_slider, scale_slider],
61
  outputs=out_im,
62
- #examples=[["data/9_Press_Conference_Press_Conference_9_86.jpg"], ["data/12_Group_Group_12_Group_Group_12_39.jpg"], ["data/31_Waiter_Waitress_Waiter_Waitress_31_55.jpg"]],
63
  title="Haar Cascade Object Detection",
64
- ).launch()
 
 
3
  import gradio as gr
4
  import numpy as np
5
 
6
+ # FIXED LINE
7
+ face_detector = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
8
 
9
  def detect_faces(img, size, neighbours, scale):
10
  frame = np.array(img)
 
13
  gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
14
 
15
  faceRects = face_detector.detectMultiScale(
16
+ gray, scaleFactor=scale, minNeighbors=int(neighbours), minSize=(int(size), int(size))
17
+ )
18
 
19
  box_data = []
20
+ class_labels = {0: "face"}
21
 
22
+ for (x, y, w, h) in faceRects:
23
+ frame = cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
24
+ midX = int(x + w / 2)
25
+ midY = int(y + h / 2)
 
 
 
 
 
26
  box = {
27
+ "position": {"middle": [midX, midY], "width": float(w), "height": float(h)},
28
+ "domain": "pixel",
29
+ "class_id": 0
 
 
 
 
 
 
 
 
 
 
 
30
  }
31
+ box_data.append(box)
32
 
33
+ re_im = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
34
  return re_im
35
+
36
  image = gr.components.Image()
37
  out_im = gr.components.Image()
38
 
 
40
  neighbour_slider = gr.components.Slider(minimum=1, maximum=20, value=5, step=1, label="Min Number of Neighbours")
41
  scale_slider = gr.components.Slider(minimum=1.1, maximum=2.0, value=1.3, step=0.1, label="Scale Factor")
42
 
 
 
 
43
  Iface = gr.Interface(
44
  fn=detect_faces,
45
  inputs=[image, size_slider, neighbour_slider, scale_slider],
46
  outputs=out_im,
 
47
  title="Haar Cascade Object Detection",
48
+ description="Face Detection with Haar Cascades using OpenCV"
49
+ ).launch()