Update app.py
Browse files
app.py
CHANGED
@@ -152,12 +152,36 @@ else: # Emotion Detection
|
|
152 |
cv2.putText(frame, result_text, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
|
153 |
return frame, result_text
|
154 |
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
161 |
|
162 |
elif mode == "Camera":
|
163 |
cap = cv2.VideoCapture(0)
|
|
|
152 |
cv2.putText(frame, result_text, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
|
153 |
return frame, result_text
|
154 |
|
155 |
+
if mode == "Image":
|
156 |
+
uploaded_image = st.file_uploader("Upload an Image", type=["jpg", "jpeg", "png"])
|
157 |
+
name_for_image = st.text_input("Enter Name (optional, for registration):")
|
158 |
+
register_image_button = st.button("Register Image")
|
159 |
+
|
160 |
+
if uploaded_image:
|
161 |
+
image = np.array(Image.open(uploaded_image))
|
162 |
+
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
163 |
+
faces = face_cascade.detectMultiScale(gray_image, scaleFactor=1.1, minNeighbors=5, minSize=(100, 100))
|
164 |
+
|
165 |
+
if len(faces) > 0:
|
166 |
+
for (x, y, w, h) in faces:
|
167 |
+
face_roi = gray_image[y:y + h, x:x + w]
|
168 |
+
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2) # Annotate detected face
|
169 |
+
|
170 |
+
# Save face if user provides a name
|
171 |
+
if register_image_button and name_for_image:
|
172 |
+
person_dir = os.path.join(KNOWN_FACES_DIR, name_for_image)
|
173 |
+
if not os.path.exists(person_dir):
|
174 |
+
os.makedirs(person_dir)
|
175 |
+
face_filename = os.path.join(person_dir, f"{name_for_image}_{datetime.now().strftime('%Y%m%d_%H%M%S')}.jpg")
|
176 |
+
cv2.imwrite(face_filename, face_roi)
|
177 |
+
st.success(f"Face for {name_for_image} has been saved successfully!")
|
178 |
+
label_ids_rev = train_recognizer() # Retrain recognizer after adding new face
|
179 |
+
|
180 |
+
else:
|
181 |
+
st.warning("No face detected in the uploaded image. Please try another image.")
|
182 |
+
|
183 |
+
st.image(image, caption="Processed Image with Face Annotations")
|
184 |
+
|
185 |
|
186 |
elif mode == "Camera":
|
187 |
cap = cv2.VideoCapture(0)
|