Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃煛 Feat: Classification of gender and emotion together with Class Structure #3

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified __pycache__/faceProcess.cpython-37.pyc
Binary file not shown.
Binary file modified __pycache__/genderClassification.cpython-37.pyc
Binary file not shown.
16 changes: 14 additions & 2 deletions faceProcess.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,19 @@ def run(self):
color = (245, 215, 130)
resized = self.frame[y - 20:y1 + 30, x - 10:x1 + 10]
cv2.rectangle(self.frame, (x, y), (x1, y1), color, 2)
gender_result = GenderClassification(resized, x, y, x1,
y1).predict()
try:
gender_result = GenderClassification(
resized, x, y, x1, y1).predict()
except:
continue

cv2.rectangle(self.frame, (x + 20, y1 + 20),
(x + 170, y1 + 55), gender_result['color'], -1)
cv2.line(self.frame, (x, y1), (x + 20, y1 + 20),
gender_result['color'],
thickness=2)
cv2.putText(self.frame, '{}'.format(gender_result['label']),
(x + 25, y1 + 45), cv2.FONT_HERSHEY_SIMPLEX, 0.8,
(255, 255, 255), 2, cv2.LINE_AA)

return self.frame
45 changes: 26 additions & 19 deletions genderClassificationWithDLIB.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
ap.add_argument("-vw", "--isVideoWriter", type=bool, default=False)
args = vars(ap.parse_args())


def shapePoints(shape):
coords = np.zeros((68, 2), dtype="int")
for i in range(0, 68):
Expand All @@ -32,18 +33,25 @@ def rectPoints(rect):
genderClassifier = load_model(genderModelPath, compile=False)
genderTargetSize = genderClassifier.input_shape[1:3]

genders = {
0: { "label": "Female", "color": (245,215,130) },
1: { "label": "Male", "color": (148,181,192) },
genders = {
0: {
"label": "Female",
"color": (245, 215, 130)
},
1: {
"label": "Male",
"color": (148, 181, 192)
},
}

cap = cv2.VideoCapture(0)

if args["isVideoWriter"] == True:
fourrcc = cv2.VideoWriter_fourcc("M","J","P","G")
fourrcc = cv2.VideoWriter_fourcc("M", "J", "P", "G")
capWidth = int(cap.get(3))
capHeight = int(cap.get(4))
videoWrite = cv2.VideoWriter("output.avi", fourrcc, 22, (capWidth, capHeight))
videoWrite = cv2.VideoWriter("output.avi", fourrcc, 22,
(capWidth, capHeight))

while True:
ret, frame = cap.read()
Expand All @@ -56,38 +64,37 @@ def rectPoints(rect):
shape = predictor(frame, rect)
points = shapePoints(shape)
(x, y, w, h) = rectPoints(rect)
resized = frame[y-20: y+h+30, x-10:x+w+10]
resized = frame[y - 20:y + h + 30, x - 10:x + w + 10]
cv2.imshow("resized: ", resized)
try:
frame_resize = cv2.resize(resized, genderTargetSize)
except:
continue

frame_resize = frame_resize.astype('float32')
frame_scaled = frame_resize/255.0
frame_reshape = np.reshape(frame_scaled,(1, 100, 100 ,3))
frame_scaled = frame_resize / 255.0
frame_reshape = np.reshape(frame_scaled, (1, 100, 100, 3))
frame_vstack = np.vstack([frame_reshape])
gender_prediction = genderClassifier.predict(frame_vstack)
gender_probability = np.max(gender_prediction)
color = (255,255,255)
if(gender_probability > 0.6):
gender_label = np.argmax(gender_prediction)
gender_result = genders[gender_label]["label"]
color = genders[gender_label]["color"]
cv2.putText(frame, gender_result , (x+5, y+h-5),
cv2.FONT_HERSHEY_SIMPLEX, 1 , color, 2, cv2.LINE_AA)
cv2.rectangle(frame, (x, y), (x + w, y + h), color, 2)
color = (255, 255, 255)
if (gender_probability > 0.6):
gender_label = np.argmax(gender_prediction)
gender_result = genders[gender_label]["label"]
color = genders[gender_label]["color"]
cv2.putText(frame, gender_result, (x + 5, y + h - 5),
cv2.FONT_HERSHEY_SIMPLEX, 1, color, 2, cv2.LINE_AA)
cv2.rectangle(frame, (x, y), (x + w, y + h), color, 2)
else:
cv2.rectangle(frame, (x, y), (x + w, y + h), color, 2)
cv2.rectangle(frame, (x, y), (x + w, y + h), color, 2)

if args["isVideoWriter"] == True:
videoWrite.write(frame)

cv2.imshow("Gender Classification", frame)
k = cv2.waitKey(1) & 0xFF
if k == 27:
break

break

cap.release()
if args["isVideoWriter"] == True:
Expand Down