Skip to content

Commit 2019941

Browse files
committed
Added realtime face detection
1 parent fc06911 commit 2019941

File tree

3 files changed

+33381
-0
lines changed

3 files changed

+33381
-0
lines changed

Diff for: AI Projects/Real-time face detector/FaceDetector.py

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import cv2
2+
from random import randrange
3+
4+
#loading some pretrained data into our badass app!
5+
trained_face_data = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
6+
7+
#Yeah thats how you import an image, think of image as a 2D array
8+
#img = cv2.imread('abm3.jpeg')
9+
10+
11+
"""
12+
#converting image to grayscale
13+
grayscaled_img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
14+
15+
#Detect the face by presenting it to your algorithm
16+
face_coordinates = trained_face_data.detectMultiScale(grayscaled_img)
17+
#detectMultiScale is the function that detects the image reguardless of if its scale ie size, this returns the coordinates of the rectangle that shows us the postion of the face
18+
19+
print(face_coordinates)
20+
21+
#draw rectangle around the face
22+
for (x,y,w,h) in face_coordinates:
23+
cv2.rectangle(img,(x,y),(x+w,y+h),(randrange(256),randrange(256),randrange(256)),5)
24+
25+
#to show the image ("the name of the window showing it",file)
26+
cv2.imshow('Face Detector',img)
27+
28+
#write this code otherwise the window will just open and close in a brink of a second
29+
cv2.waitKey()
30+
"""
31+
32+
#Now its time for face detection in videos
33+
34+
#Capture video in real time , 0 here tells you its the default webcam
35+
webcam = cv2.VideoCapture(0)
36+
37+
#we want it to run until we close the webcam
38+
39+
while True:
40+
#Reading the current frame
41+
successful_frame_read, frame = webcam.read() #first variable is a boolean telling if capture was success or not and the second one actually has the image
42+
43+
#convert to grayscale
44+
gframe = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
45+
46+
#Detect the face in frame
47+
detect_face = trained_face_data.detectMultiScale(gframe)
48+
49+
for (x,y,w,h) in detect_face:
50+
cv2.rectangle(frame,(x,y),(w+x,h+y),(randrange(256),randrange(256),randrange(256)),5)
51+
cv2.imshow("Real Time Face", frame)
52+
53+
#refreshing the frame every 1ms
54+
key= cv2.waitKey(1)
55+
#the app will stop when q or Q is pressed
56+
if key==81 or key ==113:
57+
break
58+
59+
webcam.release()
60+
print("Code Completed KING!👑")

Diff for: AI Projects/Real-time face detector/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Face Detection With OpenCV😄
2+
3+
This is my very first project on AI and ML with Python, This program uses the **haarcascade** algorithm, to detect the face in real time, We're using the trained data provided by OpenCV in their repostiory, in for testing process.
4+
5+
That's All! Have a nice day!🐍
6+
7+
### Kamui!

0 commit comments

Comments
 (0)