Skip to content

Commit 88b6a88

Browse files
author
dehobitto
committed
MVP Done.
Added face detection, text, smoothing of detected rectangle so it's not shaking. Refactoring needed
1 parent a5bbb50 commit 88b6a88

File tree

1 file changed

+50
-4
lines changed

1 file changed

+50
-4
lines changed

Python/face_detector/main.py

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,76 @@
1-
import numpy as np
21
import cv2 as cv
3-
2+
import sys
43

54
def get_camera(camera_source_idx : int) -> cv.VideoCapture:
65
cap = cv.VideoCapture(camera_source_idx)
76

87
if not cap.isOpened():
98
print("Cannot open camera")
10-
exit()
9+
sys.exit()
1110

1211
return cap
1312

13+
def process_frame(frame):
14+
detect_faces(frame)
15+
return frame
16+
17+
TEXT_MARGIN = 5
18+
last_face = (0, 0)
19+
last_face_w, last_face_h = (100, 100)
20+
21+
def smooth(last_pos, cur_pos, alpha=0.2, threshold=2):
22+
dx = cur_pos[0] - last_pos[0]
23+
dy = cur_pos[1] - last_pos[1]
24+
25+
if abs(dx) < threshold:
26+
cur_pos = (last_pos[0], cur_pos[1])
27+
if abs(dy) < threshold:
28+
cur_pos = (cur_pos[0], last_pos[1])
29+
30+
x = int(alpha * cur_pos[0] + (1 - alpha) * last_pos[0])
31+
y = int(alpha * cur_pos[1] + (1 - alpha) * last_pos[1])
32+
return x, y
1433

1534

35+
def detect_faces(frame):
36+
global last_face
37+
global last_face_w, last_face_h
38+
frame_gray = cv.cvtColor(src=frame, code=cv.COLOR_BGR2GRAY)
39+
faces = face_cascade.detectMultiScale(image=frame_gray, scaleFactor=1.2, minNeighbors=7)
40+
41+
for (x, y, w, h) in faces:
42+
x, y = smooth(last_face, (x,y))
43+
w, h = smooth((last_face_w, last_face_h), (w, h))
44+
frame = cv.rectangle(
45+
img=frame,
46+
pt1=(x, y), pt2=(x + w, y + h),
47+
color=(255, 0, 0), thickness=2)
48+
add_text(frame, "Face", (x + w + TEXT_MARGIN, y - TEXT_MARGIN), h / 200)
49+
last_face = (x, y)
50+
last_face_w, last_face_h = w, h
51+
52+
def add_text(frame, text, point, font_scale = 1, font_face = cv.FONT_HERSHEY_DUPLEX, color = (255, 0, 0), thickness = 1):
53+
cv.putText(frame, text, point, font_face, font_scale, color, thickness)
54+
return frame
55+
56+
# TODO: List all cameras let him choose
1657
def main():
17-
#TODO: List all cameras let him choose
58+
global face_cascade
59+
60+
face_cascade = cv.CascadeClassifier(cv.data.haarcascades + "haarcascade_frontalface_default.xml")
1861
cap = get_camera(0)
1962

63+
# Main loop
2064
while True:
2165
res, frame = cap.read()
66+
frame = process_frame(frame)
2267

2368
if not res:
2469
print("Cannot read frame")
2570
break
2671

2772
cv.imshow('frame', frame)
73+
2874
if cv.waitKey(1) & 0xFF == ord('q'):
2975
break
3076

0 commit comments

Comments
 (0)