Skip to content

Commit a2efe7c

Browse files
2 parents ef0aacb + b1e0240 commit a2efe7c

File tree

7 files changed

+132
-30
lines changed

7 files changed

+132
-30
lines changed

.github/workflows/label.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ jobs:
1414
runs-on: ubuntu-latest
1515

1616
steps:
17-
- uses: actions/labeler@v2
17+
- uses: actions/labeler@v3
1818
with:
1919
repo-token: "${{ secrets.GITHUB_TOKEN }}"
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
2+
import cv2
3+
import time
4+
import numpy as np
5+
6+
## Preparation for writing the ouput video
7+
fourcc = cv2.VideoWriter_fourcc(*'XVID')
8+
out = cv2.VideoWriter('output.avi', fourcc, 20.0, (640, 480))
9+
10+
##reading from the webcam
11+
cap = cv2.VideoCapture(0)
12+
13+
## Allow the system to sleep for 3 seconds before the webcam starts
14+
time.sleep(3)
15+
count = 0
16+
background = 0
17+
18+
## Capture the background in range of 60
19+
for i in range(60):
20+
ret, background = cap.read()
21+
background = np.flip(background, axis=1)
22+
23+
## Read every frame from the webcam, until the camera is open
24+
while (cap.isOpened()):
25+
ret, img = cap.read()
26+
if not ret:
27+
break
28+
count += 1
29+
img = np.flip(img, axis=1)
30+
31+
## Convert the color space from BGR to HSV
32+
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
33+
34+
## Generat masks to detect red color
35+
lower_red = np.array([0, 120, 50])
36+
upper_red = np.array([10, 255,255])
37+
mask1 = cv2.inRange(hsv, lower_red, upper_red)
38+
39+
lower_red = np.array([170, 120, 70])
40+
upper_red = np.array([180, 255, 255])
41+
mask2 = cv2.inRange(hsv, lower_red, upper_red)
42+
43+
mask1 = mask1 + mask2
44+
45+
## Open and Dilate the mask image
46+
mask1 = cv2.morphologyEx(mask1, cv2.MORPH_OPEN, np.ones((3, 3), np.uint8))
47+
mask1 = cv2.morphologyEx(mask1, cv2.MORPH_DILATE, np.ones((3, 3), np.uint8))
48+
49+
## Create an inverted mask to segment out the red color from the frame
50+
mask2 = cv2.bitwise_not(mask1)
51+
52+
## Segment the red color part out of the frame using bitwise and with the inverted mask
53+
res1 = cv2.bitwise_and(img, img, mask=mask2)
54+
55+
## Create image showing static background frame pixels only for the masked region
56+
res2 = cv2.bitwise_and(background, background, mask=mask1)
57+
58+
## Generating the final output and writing
59+
finalOutput = cv2.addWeighted(res1, 1, res2, 1, 0)
60+
out.write(finalOutput)
61+
cv2.imshow("magic", finalOutput)
62+
cv2.waitKey(1)
63+
64+
65+
cap.release()
66+
out.release()
67+
cv2.destroyAllWindows()
68+
69+
#------------------------
70+
#colors code
71+
72+
#skin color
73+
#lower_red = np.array([0, 0, 70])
74+
#upper_red = np.array([100, 255,255])
75+
# mask1 = cv2.inRange(hsv, lower_red, upper_red)
76+
77+
# lower_red = np.array([170, 120, 70])
78+
# upper_red = np.array([180, 255, 255])
79+
80+
#-----------------------

7. Face Detection/face detection.py

Lines changed: 0 additions & 29 deletions
This file was deleted.

7.FaceDetection/face_detection.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import cv2
2+
3+
# Path
4+
face_path = '7.FaceDetection/haarcascade_frontalface_default.xml'
5+
eye_path = '7.FaceDetection/haarcascade_eye.xml'
6+
face_cascade = cv2.CascadeClassifier(face_path)
7+
eye_cascade = cv2.CascadeClassifier(eye_path)
8+
9+
10+
def detectedFace(img):
11+
img = cv2.imread(img)
12+
13+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
14+
15+
faces = face_cascade.detectMultiScale(gray, 1.4, 5)
16+
17+
for face in faces:
18+
x, y, width, height = face
19+
# draw a rectangle for detection
20+
cv2.rectangle(
21+
img,
22+
(x, y),
23+
(x + width, y + height),
24+
(0, 0, 255),
25+
1,
26+
)
27+
roi_gray = gray[y:y+height, x:x+width]
28+
roi_color = img[y:y+height, x:x+width]
29+
30+
eyes = eye_cascade.detectMultiScale(roi_gray)
31+
for (ex, ey, ew, eh) in eyes:
32+
cv2.rectangle(
33+
roi_color,
34+
(ex, ey),
35+
(ex+ew, ey+eh),
36+
(0, 255, 0),
37+
2,
38+
)
39+
40+
cv2.imshow('Face Detection', img)
41+
k = cv2.waitKey(0) & 0xFF
42+
43+
if k == 27:
44+
cv2.destroyAllWindows()
45+
elif k == ord('s'):
46+
cv2.imwrite('Media/face-detected.jpeg', img)
47+
cv2.destroyAllWindows()
48+
49+
50+
path_img = './Media/face-001.jpg'
51+
face = detectedFace(path_img)
File renamed without changes.

0 commit comments

Comments
 (0)