Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -27,7 +27,6 @@ def detect_face_opencv(pil_image):
|
|
| 27 |
(x, y, w, h) = max(faces, key=lambda b: b[2]*b[3])
|
| 28 |
return pil_image.crop((x, y, x+w, y+h))
|
| 29 |
|
| 30 |
-
# Grad-CAM visualization (Xception only)
|
| 31 |
def grad_cam(model, img, size, preprocess_func):
|
| 32 |
img_resized = img.resize(size)
|
| 33 |
x = img_to_array(img_resized)
|
|
@@ -39,8 +38,9 @@ def grad_cam(model, img, size, preprocess_func):
|
|
| 39 |
with tf.GradientTape() as tape:
|
| 40 |
conv_outputs, predictions = grad_model(x_tensor)
|
| 41 |
loss = predictions[:, 0]
|
| 42 |
-
grads = tape.gradient(loss, conv_outputs)
|
| 43 |
-
cam =
|
|
|
|
| 44 |
cam = np.maximum(cam, 0)
|
| 45 |
cam /= cam.max() if cam.max() != 0 else 1
|
| 46 |
heatmap = cv2.resize(cam, size)
|
|
@@ -53,6 +53,7 @@ def grad_cam(model, img, size, preprocess_func):
|
|
| 53 |
superimposed = cv2.addWeighted(img_np, 0.6, heatmap, 0.4, 0)
|
| 54 |
return Image.fromarray(cv2.cvtColor(superimposed, cv2.COLOR_BGR2RGB))
|
| 55 |
|
|
|
|
| 56 |
# Preprocessing helper
|
| 57 |
def preprocess(img, size, func):
|
| 58 |
img = img.resize(size)
|
|
|
|
| 27 |
(x, y, w, h) = max(faces, key=lambda b: b[2]*b[3])
|
| 28 |
return pil_image.crop((x, y, x+w, y+h))
|
| 29 |
|
|
|
|
| 30 |
def grad_cam(model, img, size, preprocess_func):
|
| 31 |
img_resized = img.resize(size)
|
| 32 |
x = img_to_array(img_resized)
|
|
|
|
| 38 |
with tf.GradientTape() as tape:
|
| 39 |
conv_outputs, predictions = grad_model(x_tensor)
|
| 40 |
loss = predictions[:, 0]
|
| 41 |
+
grads = tape.gradient(loss, conv_outputs)
|
| 42 |
+
cam = tf.reduce_mean(grads, axis=-1).numpy()[0]
|
| 43 |
+
|
| 44 |
cam = np.maximum(cam, 0)
|
| 45 |
cam /= cam.max() if cam.max() != 0 else 1
|
| 46 |
heatmap = cv2.resize(cam, size)
|
|
|
|
| 53 |
superimposed = cv2.addWeighted(img_np, 0.6, heatmap, 0.4, 0)
|
| 54 |
return Image.fromarray(cv2.cvtColor(superimposed, cv2.COLOR_BGR2RGB))
|
| 55 |
|
| 56 |
+
|
| 57 |
# Preprocessing helper
|
| 58 |
def preprocess(img, size, func):
|
| 59 |
img = img.resize(size)
|