Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
CursedPrograms committed Feb 9, 2024
1 parent cb06894 commit f588ea1
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ chmod +x setup.sh
deepface==0.0.81
opencv-python==4.8.1.78
Pillow==8.3.2
flask==3.0.0
```
- [GloriosaAI Repository](https://github.com/CursedPrograms/GloriosaAI)
- [Detect-Face Repository](https://github.com/CursedPrograms/Detect-Face)
Expand Down
75 changes: 75 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
from flask import Flask, render_template, Response
import cv2
from deepface import DeepFace
import webbrowser

app = Flask(__name__)

def extract_gender_and_age(frame, face_cascade):
try:
# Convert the frame to RGB (required by DeepFace)
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

# Use Haar Cascade to detect faces
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray_frame, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))

for (x, y, w, h) in faces:
# Extract face region
face_roi = frame[y:y+h, x:x+w]

# Use DeepFace to analyze gender and age
result = DeepFace.analyze(face_roi, actions=['gender', 'age'])

gender_stats = result[0]['gender']
age = int(result[0]['age'])
print("Predicted Gender:", gender_stats)
print("Predicted Age:", age)

# Select the dominant gender label
gender = max(gender_stats, key=gender_stats.get)

# Display gender and age information on the frame
cv2.putText(frame, f"Gender: {gender} - {gender_stats[gender]:.2f}%", (x, y - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1, cv2.LINE_AA)
cv2.putText(frame, f"Age: {age} years", (x, y + h + 20),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1, cv2.LINE_AA)

# Draw rectangle around the face
cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)

return frame

except Exception as e:
print(f"Error: {e}")
return frame

def generate_frames():
cap = cv2.VideoCapture(0)
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')

while True:
success, frame = cap.read()
if not success:
break

frame = extract_gender_and_age(frame, face_cascade)

ret, buffer = cv2.imencode('.jpg', frame)
frame = buffer.tobytes()

yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')

@app.route('/')
def index():
return render_template('index.html')

@app.route('/video_feed')
def video_feed():
return Response(generate_frames(),
mimetype='multipart/x-mixed-replace; boundary=frame')

if __name__ == "__main__":
webbrowser.open('http://127.0.0.1:5000/')
app.run(debug=True, use_reloader=False)
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
deepface==0.0.81
opencv-python==4.8.1.78
Pillow==8.3.2
flask==3.0.0
15 changes: 15 additions & 0 deletions run-server.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
@echo off

set "VENV_DIR=psdenv"

rem
if not exist "%VENV_DIR%" (
rem
python -m venv "%VENV_DIR%"
)

rem
call "%VENV_DIR%\Scripts\activate" && python app.py

rem
pause
1 change: 1 addition & 0 deletions static/css/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import url('https://cursedprograms.github.io/cursedentertainment/styles/main-style.css');
22 changes: 22 additions & 0 deletions templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!DOCTYPE html>
<html>
<head>
<title>Gender-Age-ID</title>
<meta charset="UTF-8">
<link href="https://cursedprograms.github.io/cursedentertainment/cursedFavicon.png" rel="icon" type="image/x-icon" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
</head>
<body>
<h1>Gender-Age-ID</h1>
<div class="content">
<img src="{{ url_for('video_feed') }}" alt="Webcam Feed">
</div>
<div class="icons_container">
<iframe src="https://cursedprograms.github.io/cursedentertainment/iframes/social-media-icons/social-media-icons.html"
frameborder="0"></iframe>
</div>
<footer id="dynamicFooter"></footer>
<script src="https://cursedprograms.github.io/cursedentertainment/scripts/footer.js"></script>
</body>
</html>

0 comments on commit f588ea1

Please sign in to comment.