mrifat commited on
Commit
dc0ba93
·
verified ·
1 Parent(s): 2e65346

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -27
app.py CHANGED
@@ -4,39 +4,62 @@ import numpy as np
4
  import cv2
5
  from deepface import DeepFace
6
 
 
7
  st.set_page_config(page_title="Face Analyzer", layout="centered")
8
 
9
- st.title("🔍 AI Face Analyzer (Age, Gender, Emotion)")
10
- st.write("আপলোড করা ছবিতে মুখ বিশ্লেষণ করে বয়স, লিঙ্গ, আবেগ বের করা হয়।")
 
 
11
 
12
- uploaded_file = st.file_uploader("📤 একট ছবি আপলোড কর", type=["jpg", "png", "jpeg"])
13
 
14
- if uploaded_file:
15
- img = Image.open(uploaded_file)
16
- st.image(img, caption="আলোড করা ছবি", use_column_width=True)
 
 
17
 
18
- # Convert image to OpenCV format
19
- img_array = np.array(img.convert("RGB"))
20
- img_bgr = cv2.cvtColor(img_array, cv2.COLOR_RGB2BGR)
21
 
22
- # Analyze using DeepFace
23
- with st.spinner("Face বিশ্লেষণ হচ্ছে..."):
24
- try:
25
- # enforcement=False means it won't throw an error if no face is found
26
- results = DeepFace.analyze(img_bgr, actions=["age", "gender", "emotion"], enforce_detection=False)
27
 
28
- # Check if any face was detected
29
- if results and isinstance(results, list) and len(results) > 0:
30
- result = results[0] # Analyze the first face found
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
- st.subheader("📊 বিশ্লেষণ ফলাফল:")
33
- st.markdown(f"""
34
- - 👤 **বয়স (Age)**: {result['age']}
35
- - 🚻 **লিঙ্গ (Gender)**: {'Male' if result['gender'] == 'Man' else 'Female'}
36
- - 😊 **আবেগ (Emotion)**: {result['dominant_emotion'].capitalize()}
37
- """)
38
- else:
39
- st.warning("⚠️ এই ছবিতে কোনো মুখ খুঁজে পাওয়া যায়নি।")
40
 
41
- except Exception as e:
42
- st.error(f"❌ বিশ্লেষণ ব্যর্থ হয়েছে। একটি অ্য ছ���ি চেষকরুন। Error: {e}")
 
 
 
 
 
4
  import cv2
5
  from deepface import DeepFace
6
 
7
+ # --- পেজ কনফিগারেশন ---
8
  st.set_page_config(page_title="Face Analyzer", layout="centered")
9
 
10
+ # --- মূল ইন্টারফেস ---
11
+ st.title("🔍 AI Face Analyzer")
12
+ st.write("আপলোড করা ছবিতে মুখ বিশ্লেষণ করে বয়স, লিঙ্গ, এবং আবেগ বের করা হয়।")
13
+ st.write("---")
14
 
15
+ uploaded_file = st.file_uploader("📤 একটি ছবি আপলোড করুন", type=["jpg", "png", "jpeg"])
16
 
17
+ if uploaded_file is not None:
18
+ try:
19
+ # ছবিটি ওেন এবং িসপ্লে করা
20
+ image = Image.open(uploaded_file)
21
+ st.image(image, caption="আপলোড করা ছবি", use_column_width=True)
22
 
23
+ # ইমেজটিকে OpenCV ফরম্যাটে রূপান্তর করা
24
+ img_array = np.array(image.convert("RGB"))
25
+ img_bgr = cv2.cvtColor(img_array, cv2.COLOR_RGB2BGR)
26
 
27
+ st.write("---")
28
+ # DeepFace দিয়ে বিশ্লেষণ করা
29
+ with st.spinner("মুখ বিশ্লেষণ করা হচ্ছে... অনুগ্রহ করে অপেক্ষা করুন।"):
30
+ # enforce_detection=False দিলে মুখ খুঁজে না পেলে এরর দেবে না
31
+ results = DeepFace.analyze(img_path=img_bgr, actions=['age', 'gender', 'emotion'], enforce_detection=False)
32
 
33
+ # ফলাফল চেক এবং প্রদর্শন
34
+ if results and isinstance(results, list) and len(results) > 0:
35
+ # প্রথম মুখটির ফলাফল নেওয়া
36
+ result = results[0]
37
+
38
+ # জেন্ডার এবং ইমোশন অনুবাদ করা
39
+ gender = "পুরুষ (Male)" if result.get('dominant_gender') == 'Man' else "মহিলা (Female)"
40
+ emotion_map = {
41
+ 'angry': 'রাগান্বিত (Angry) 😠',
42
+ 'disgust': 'বিরক্ত (Disgust) 🤢',
43
+ 'fear': 'ভীত (Fear) 😨',
44
+ 'happy': 'খুশি (Happy) 😊',
45
+ 'sad': 'দুঃখিত (Sad) 😢',
46
+ 'surprise': 'অবাক (Surprise) 😮',
47
+ 'neutral': 'নিরপেক্ষ (Neutral) 😐'
48
+ }
49
+ emotion = emotion_map.get(result.get('dominant_emotion'), result.get('dominant_emotion').capitalize())
50
 
51
+ st.subheader("📊 বিশ্লেষণ ফলাফল:")
52
+ st.markdown(f"""
53
+ - **👤 আনুমানিক বয়স:** {result.get('age', 'N/A')}
54
+ - **🚻 লিঙ্গ:** {gender}
55
+ - **😊 প্রধান আবেগ:** {emotion}
56
+ """)
57
+ else:
58
+ st.warning("⚠️ দুঃখিত, এই ছবিতে কোনো মুখ খুঁজে পাওয়া যায়নি। অনুগ্রহ করে অন্য একটি ছবি চেষ্টা করুন।")
59
 
60
+ except Exception as e:
61
+ st.error(f"❌ একটি অরত্াশি সমসহয়েছে: {e}")
62
+ st.info("অ্যাপটি রিবুট করার চেষ্টা করুন অথবা একটি অন্য ছবি আপলোড করুন।")
63
+
64
+ else:
65
+ st.info("শুরু করতে, উপরের বক্স ব্যবহার করে একটি ছবি আপলোড করুন।")