Ahmadkhan12 commited on
Commit
1effd41
·
verified ·
1 Parent(s): d6428bd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -33
app.py CHANGED
@@ -18,43 +18,63 @@ def detect_emotion(face_landmarks):
18
  A simple mock-up function for detecting emotions based on landmarks.
19
  Replace with a more sophisticated model as needed.
20
  """
21
- # Example: Arbitrarily assign "Happy" if eyes are close together
22
  if face_landmarks:
 
23
  return "Happy"
24
  return "Neutral"
25
 
 
 
 
 
 
 
 
 
26
  # Process the uploaded image
27
  if uploaded_file is not None:
28
- image = Image.open(uploaded_file)
29
- image_np = np.array(image)
30
-
31
- # Convert image to RGB
32
- rgb_image = cv2.cvtColor(image_np, cv2.COLOR_BGR2RGB)
33
-
34
- # Detect faces in the image
35
- face_locations = face_recognition.face_locations(rgb_image)
36
- face_landmarks_list = face_recognition.face_landmarks(rgb_image)
37
-
38
- if face_locations:
39
- for face_location, face_landmarks in zip(face_locations, face_landmarks_list):
40
- # Draw a rectangle around the face
41
- top, right, bottom, left = face_location
42
- cv2.rectangle(image_np, (left, top), (right, bottom), (0, 255, 0), 2)
43
-
44
- # Detect emotion based on landmarks
45
- emotion = detect_emotion(face_landmarks)
46
-
47
- # Display emotion above the face
48
- cv2.putText(
49
- image_np,
50
- emotion,
51
- (left, top - 10),
52
- cv2.FONT_HERSHEY_SIMPLEX,
53
- 0.9,
54
- (255, 0, 0),
55
- 2,
56
- )
57
-
58
- st.image(image_np, caption="Processed Image", use_column_width=True)
59
  else:
60
- st.warning("No faces detected in the image.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  A simple mock-up function for detecting emotions based on landmarks.
19
  Replace with a more sophisticated model as needed.
20
  """
 
21
  if face_landmarks:
22
+ # Example: Assign "Happy" if eyes are close together
23
  return "Happy"
24
  return "Neutral"
25
 
26
+ # Resize image to reduce memory usage
27
+ def resize_image(image, max_size=(800, 800)):
28
+ """
29
+ Resizes the image to the specified maximum size while maintaining aspect ratio.
30
+ """
31
+ image.thumbnail(max_size, Image.ANTIALIAS)
32
+ return image
33
+
34
  # Process the uploaded image
35
  if uploaded_file is not None:
36
+ # Check file size to prevent loading large images
37
+ if uploaded_file.size > 10 * 1024 * 1024: # 10 MB limit
38
+ st.error("File too large. Please upload an image smaller than 10 MB.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  else:
40
+ # Open and resize the image
41
+ image = Image.open(uploaded_file)
42
+ image = resize_image(image)
43
+
44
+ # Convert image to numpy array
45
+ image_np = np.array(image)
46
+
47
+ # Convert image to RGB (ensure compatibility with face_recognition)
48
+ if len(image_np.shape) == 3 and image_np.shape[2] == 4: # RGBA to RGB
49
+ image_np = cv2.cvtColor(image_np, cv2.COLOR_RGBA2RGB)
50
+ elif len(image_np.shape) == 3 and image_np.shape[2] == 3: # BGR to RGB
51
+ image_np = cv2.cvtColor(image_np, cv2.COLOR_BGR2RGB)
52
+
53
+ # Detect faces and landmarks
54
+ face_locations = face_recognition.face_locations(image_np)
55
+ face_landmarks_list = face_recognition.face_landmarks(image_np)
56
+
57
+ if face_locations:
58
+ for face_location, face_landmarks in zip(face_locations, face_landmarks_list):
59
+ # Draw a rectangle around the face
60
+ top, right, bottom, left = face_location
61
+ cv2.rectangle(image_np, (left, top), (right, bottom), (0, 255, 0), 2)
62
+
63
+ # Detect emotion based on landmarks
64
+ emotion = detect_emotion(face_landmarks)
65
+
66
+ # Display emotion above the face
67
+ cv2.putText(
68
+ image_np,
69
+ emotion,
70
+ (left, top - 10),
71
+ cv2.FONT_HERSHEY_SIMPLEX,
72
+ 0.9,
73
+ (255, 0, 0),
74
+ 2,
75
+ )
76
+
77
+ # Display the processed image
78
+ st.image(image_np, caption="Processed Image", use_column_width=True)
79
+ else:
80
+ st.warning("No faces detected in the image.")