Skip to content

Commit 3f66b88

Browse files
committed
Implemented bounding box, and updated text overlay to be in corner of bounding box.
1 parent 0698ac9 commit 3f66b88

File tree

3 files changed

+49
-15
lines changed

3 files changed

+49
-15
lines changed

app.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def preprocess_image(image_bytes):
3737
face_img = cv2.resize(face_img, (64, 64))
3838
face_img = face_img.astype(np.float32)
3939
face_img = face_img[np.newaxis, np.newaxis, :, :] # shape: (1, 1, 64, 64)
40-
return face_img
40+
return face_img, faces[0]
4141

4242
def softmax(x):
4343
e_x = np.exp(x - np.max(x, axis=1, keepdims=True))
@@ -59,21 +59,17 @@ def detect_emotion():
5959
with open("static/debug_frame.jpg", "wb") as f:
6060
f.write(img_data)
6161

62-
input_img = preprocess_image(img_data)
62+
input_img,face_data = preprocess_image(img_data)
6363
if input_img is None:
6464
return jsonify({'error': 'Invalid image'}), 400
6565
debug_img = np.squeeze(input_img) * 255 # (64, 64) grayscale image
6666
debug_img = debug_img.astype(np.uint8)
6767
cv2.imwrite("static/debug_preprocessed.jpg", debug_img)
6868
inputs = {session.get_inputs()[0].name: input_img}
6969
raw_output = session.run(None, inputs)[0]
70-
print(raw_output)
7170
probs = softmax(raw_output)
7271
top_idx = np.argmax(probs)
73-
print(f"here is top_idx: {top_idx}")
74-
print(emotion_table)
75-
print(probs)
76-
return jsonify({'emotion': emotion_table[top_idx],'confidence': float(probs[0][top_idx]),'raw_probabilities': probs[0].tolist()})
72+
return jsonify({'face_data': face_data.tolist(), 'emotion': emotion_table[top_idx],'confidence': float(probs[0][top_idx]),'raw_probabilities': probs[0].tolist()})
7773

7874
except Exception as e:
7975
print("Error:", e)

static/script.js

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,45 @@ setInterval(() => {
3232
})
3333
.then(data => {
3434
overlay.textContent = data.emotion.charAt(0).toUpperCase() + data.emotion.slice(1);
35+
const faceData = data.face_data;
3536

36-
// restart the pop animation
37-
overlay.classList.remove('pop');
38-
void overlay.offsetWidth; // force reflow
39-
overlay.classList.add('pop');
37+
const container = document.querySelector(".video-wrapper");
38+
39+
const oldBoxes = container.querySelectorAll(".bounding-box");
40+
oldBoxes.forEach(box => box.remove());
41+
42+
43+
if (faceData && faceData.length > 0){
44+
const x = faceData[0];
45+
const y = faceData[1];
46+
const w = faceData[2];
47+
const h = faceData[3];
48+
49+
overlay.style.left = x + 'px';
50+
overlay.style.top = y + 'px';
51+
overlay.style.display = 'block';
52+
53+
const box = document.createElement("div");
54+
box.className = "bounding-box";
55+
56+
box.style.left = x + 'px';
57+
box.style.top = y + 'px';
58+
box.style.width = w + 'px';
59+
box.style.height = h + 'px';
60+
// Add the new box div inside the container
61+
container.appendChild(box);
62+
// restart the pop animation
63+
overlay.classList.remove('pop');
64+
void overlay.offsetWidth; // force reflow
65+
overlay.classList.add('pop');
66+
} else {
67+
overlay.style.display = 'none';
68+
}
4069

4170
})
4271
.catch(err => {
4372
analyzing = false;
4473
overlay.textContent = 'Error';
4574
console.error('Detection error:', err);
4675
});
47-
}, 1000);
76+
}, 100);

static/styles.css

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,23 @@ header p {
3131

3232
.overlay {
3333
position: absolute;
34-
bottom: 10px;
35-
left: 50%;
36-
transform: translateX(-50%);
34+
transform: translateY(-100%);
35+
margin-bottom: 5px;
3736
background: rgba(0,0,0,0.7);
3837
padding: 0.5rem 1rem;
3938
border-radius: 6px;
4039
font-size: 1.2rem;
4140
color: #00bcd4;
41+
z-index: 10;
42+
display: none;
43+
}
44+
45+
.bounding-box {
46+
position: absolute;
47+
48+
border: 2px solid #00FF00;
49+
50+
box-sizing: border-box;
4251
}
4352

4453
/* Mobile */

0 commit comments

Comments
 (0)