suhail0318 commited on
Commit
30a754d
·
verified ·
1 Parent(s): d8e6fe6

Update face1.py

Browse files
Files changed (1) hide show
  1. face1.py +76 -76
face1.py CHANGED
@@ -1,77 +1,77 @@
1
- import streamlit as st
2
- import cv2
3
- import numpy as np
4
- from deepface import DeepFace
5
-
6
- st.set_page_config(
7
- page_title="✨ Age & Gender Predictor",
8
- page_icon=":sparkles:",
9
- layout="centered",
10
- )
11
-
12
- st.title("✨ Age & Gender Predictor")
13
- st.write(
14
- """
15
- Welcome to the future of facial analysis!
16
- **Upload your photo** and let our cutting-edge AI reveal your age and gender with impressive precision.
17
-
18
- **No data is stored**.
19
- """
20
- )
21
-
22
- uploaded_file = st.file_uploader("Upload an image...", type=["jpg", "jpeg", "png"])
23
-
24
- if uploaded_file is not None:
25
- file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
26
- image = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
27
-
28
- image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
29
-
30
- st.image(image_rgb, caption="Your Uploaded Image", use_container_width=True)
31
-
32
- with st.spinner("Analyzing your image with advanced AI models..."):
33
- try:
34
- results = DeepFace.analyze(
35
- image,
36
- actions=['age', 'gender'],
37
- detector_backend='retinaface',
38
- enforce_detection=True,
39
- align=True
40
- )
41
-
42
- if isinstance(results, list):
43
- results = sorted(results, key=lambda x: x['face_confidence'], reverse=True)
44
- main_result = results[0]
45
- else:
46
- main_result = results
47
-
48
- st.success("Analysis complete! Here's what we found:")
49
- st.write("## Detailed Results")
50
-
51
- age = main_result['age']
52
- st.write(f"**Predicted Age:** {age} years")
53
-
54
- gender = main_result['gender']
55
- dominant_gender = gender if isinstance(gender, str) else max(gender, key=gender.get)
56
- confidence = gender[dominant_gender] if isinstance(gender, dict) else None
57
-
58
- if confidence:
59
- st.write(f"**Predicted Gender:** {dominant_gender} ({confidence:.2f}% confidence)")
60
- else:
61
- st.write(f"**Predicted Gender:** {dominant_gender}")
62
-
63
- except Exception as e:
64
- st.error(f"Analysis failed: {str(e)}")
65
- st.info(
66
- "For best results, please try the following tips:\n"
67
- "- Use a well-lit, clear photo\n"
68
- "- Ensure your face is fully visible\n"
69
- "- Avoid low-resolution images"
70
- )
71
-
72
- st.markdown("---")
73
- st.markdown(
74
- """
75
- **Powered by DeepFace & RetinaFace**
76
- """
77
  )
 
1
+ import streamlit as st
2
+ import cv2
3
+ import numpy as np
4
+ from deepface import DeepFace
5
+
6
+ st.set_page_config(
7
+ page_title="✨ Age & Gender Predictor",
8
+ page_icon=":sparkles:",
9
+ layout="centered",
10
+ )
11
+
12
+ st.title("✨ Age & Gender Predictor")
13
+ st.write(
14
+ """
15
+ Welcome to the future of facial analysis!
16
+ **Upload your photo** and let our cutting-edge AI reveal your age and gender with impressive precision.
17
+
18
+ **No data is stored**.
19
+ """
20
+ )
21
+
22
+ uploaded_file = st.file_uploader("Upload an image...", type=["jpg", "jpeg", "png"])
23
+
24
+ if uploaded_file is not None:
25
+ file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
26
+ image = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
27
+
28
+ image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
29
+
30
+ st.image(image_rgb, caption="Your Uploaded Image", use_container_width=True)
31
+
32
+ with st.spinner("Analyzing your image with advanced AI models..."):
33
+ try:
34
+ results = DeepFace.analyze(
35
+ image,
36
+ actions=['age', 'gender'],
37
+ detector_backend='retinaface',
38
+ enforce_detection=True,
39
+ align=True
40
+ )
41
+
42
+ if isinstance(results, list):
43
+ results = sorted(results, key=lambda x: x['face_confidence'], reverse=True)
44
+ main_result = results[0]
45
+ else:
46
+ main_result = results
47
+
48
+ st.success("Analysis complete! Here's what we found:")
49
+ st.write("## Detailed Results")
50
+
51
+ age = main_result['age']
52
+ st.write(f"**Predicted Age:** {age} years")
53
+
54
+ gender = main_result['gender']
55
+ dominant_gender = gender if isinstance(gender, str) else max(gender, key=gender.get)
56
+ confidence = gender[dominant_gender] if isinstance(gender, dict) else None
57
+
58
+ if confidence:
59
+ st.write(f"**Predicted Gender:** {dominant_gender} ({confidence:.2f}% confidence)")
60
+ else:
61
+ st.write(f"**Predicted Gender:** {dominant_gender}")
62
+
63
+ except Exception as e:
64
+ st.error(f"Analysis failed: {str(e)}")
65
+ st.info(
66
+ "For best results, please try the following tips:\n"
67
+ "- Use a well-lit, clear photo\n"
68
+ "- Ensure your face is fully visible\n"
69
+ "- Avoid low-resolution images"
70
+ )
71
+
72
+ st.markdown("---")
73
+ st.markdown(
74
+ """
75
+ **Powered by DeepFace & RetinaFace**
76
+ """
77
  )