Skip to content

Commit 97e5bfc

Browse files
committed
first commit
1 parent c3b8694 commit 97e5bfc

File tree

10 files changed

+40154
-1
lines changed

10 files changed

+40154
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,4 @@ cython_debug/
158158
# and can be added to the global gitignore or merged into this file. For a more nuclear
159159
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
160160
#.idea/
161+
.DS_Store

README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,19 @@
1-
# face_detection
1+
# Face Detection
2+
3+
### To run the app follow the steps:
4+
5+
```
6+
python3 -m venv venv
7+
source venv/bin/activate
8+
pip3 install -r requirements.txt
9+
python3 face_detection_webcam.py
10+
```
11+
12+
or
13+
14+
```
15+
python3 -m venv venv
16+
source venv/bin/activate
17+
pip3 install -r requirements.txt
18+
python3 face_detection_image.py
19+
```

face_detection_image.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import cv2
2+
import os
3+
4+
5+
def face_detection(casc_model, image_gray_scale):
6+
# Detection frontal faces
7+
faces_detected = casc_model.detectMultiScale(
8+
image_gray_scale,
9+
scaleFactor=1.5,
10+
minNeighbors=7,
11+
minSize=(30, 30),
12+
flags = cv2.CASCADE_SCALE_IMAGE
13+
)
14+
15+
qtd_faces = len(faces_detected)
16+
17+
if qtd_faces > 0:
18+
print("Detected {0} faces!".format(qtd_faces))
19+
20+
return faces_detected, qtd_faces
21+
22+
23+
def draw_rectangle(faces, image):
24+
25+
for (x, y, w, h) in faces:
26+
cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
27+
28+
29+
casc_path_frontal_face = "xml/haarcascade_frontalface_default.xml"
30+
frontal_face_casc = cv2.CascadeClassifier(casc_path_frontal_face)
31+
32+
files_in_path = os.listdir("images/")
33+
images = [image for image in files_in_path
34+
if image.endswith(".jpeg")]
35+
36+
for image in images:
37+
image = cv2.imread(f"images/{ image }")
38+
39+
image_gray_scale = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
40+
41+
frontal_faces, qtd_faces = face_detection(frontal_face_casc, image_gray_scale)
42+
draw_rectangle(frontal_faces, image)
43+
44+
cv2.imshow(f'{ qtd_faces } face(s) detected', image)
45+
cv2.waitKey(0)

face_detection_webcam.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import cv2
2+
3+
casc_path_frontal_face = "xml/haarcascade_frontalface_default.xml"
4+
frontal_face_casc = cv2.CascadeClassifier(casc_path_frontal_face)
5+
6+
video_capture = cv2.VideoCapture(0)
7+
8+
def face_detection(casc_model, img_gray_scale):
9+
# Detection frontal faces
10+
faces_detected = casc_model.detectMultiScale(
11+
img_gray_scale,
12+
scaleFactor=1.5,
13+
minNeighbors=7,
14+
minSize=(30, 30),
15+
flags = cv2.CASCADE_SCALE_IMAGE
16+
)
17+
18+
qtd_faces = len(faces_detected)
19+
20+
if qtd_faces > 0:
21+
print("Detected {0} faces!".format(qtd_faces))
22+
23+
return faces_detected
24+
25+
def draw_rectangle(faces):
26+
27+
for (x, y, w, h) in faces:
28+
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
29+
30+
while True:
31+
ret, frame = video_capture.read()
32+
33+
img_gray_scale = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
34+
35+
frontal_faces = face_detection(frontal_face_casc, img_gray_scale)
36+
draw_rectangle(frontal_faces)
37+
38+
cv2.imshow('face detection', frame)
39+
40+
if cv2.waitKey(1) & 0xFF == ord('q'):
41+
break
42+
43+
video_capture.release()
44+
cv2.destroyAllWindows()

images/pessoas.jpeg

51.1 KB
Loading

images/pessoas_2.jpeg

592 KB
Loading

images/pessoas_3.jpeg

49.7 KB
Loading

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
numpy==1.26.3
2+
opencv-python==4.9.0.80

0 commit comments

Comments
 (0)