Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -13,6 +13,30 @@ def load_model_and_mtcnn(model_path):
|
|
13 |
mtcnn = MTCNN(keep_all=True, device=device)
|
14 |
return model, device, mtcnn
|
15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
# Function to preprocess the image and return both the tensor and the final PIL image for display
|
17 |
def preprocess_image(image, mtcnn, device):
|
18 |
processed_image = image # Initialize with the original image
|
@@ -56,9 +80,14 @@ uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png
|
|
56 |
if uploaded_file is not None:
|
57 |
image = Image.open(uploaded_file).convert("RGB")
|
58 |
st.image(image, caption='Uploaded Image', use_column_width=True)
|
|
|
|
|
59 |
image_tensor, final_image = preprocess_image(image, mtcnn, device)
|
60 |
-
predicted_class, probabilities = predict(
|
61 |
|
62 |
st.write(f"Predicted class: {predicted_class.item()}")
|
63 |
# Display the final processed image
|
64 |
-
|
|
|
|
|
|
|
|
13 |
mtcnn = MTCNN(keep_all=True, device=device)
|
14 |
return model, device, mtcnn
|
15 |
|
16 |
+
def detect_and_process_skin(image_bytes):
|
17 |
+
"""Detects faces in an image, crops the skin region, and returns it as an image object."""
|
18 |
+
# Load image from bytes
|
19 |
+
img = Image.open(io.BytesIO(image_bytes))
|
20 |
+
img_np = np.array(img)
|
21 |
+
img_rgb = cv2.cvtColor(img_np, cv2.COLOR_BGR2RGB)
|
22 |
+
|
23 |
+
# Detect faces in the image
|
24 |
+
detections = mtcnn.detect_faces(img_rgb)
|
25 |
+
|
26 |
+
# Check if any faces were detected
|
27 |
+
if detections:
|
28 |
+
x, y, width, height = detections[0]['box']
|
29 |
+
|
30 |
+
# Crop the face region
|
31 |
+
face_img_np = img_np[y:y+height, x:x+width]
|
32 |
+
|
33 |
+
# Convert to PIL Image for return
|
34 |
+
pil_img = Image.fromarray(face_img_np)
|
35 |
+
return pil_img
|
36 |
+
else:
|
37 |
+
# Return original image if no face was detected
|
38 |
+
return img
|
39 |
+
|
40 |
# Function to preprocess the image and return both the tensor and the final PIL image for display
|
41 |
def preprocess_image(image, mtcnn, device):
|
42 |
processed_image = image # Initialize with the original image
|
|
|
80 |
if uploaded_file is not None:
|
81 |
image = Image.open(uploaded_file).convert("RGB")
|
82 |
st.image(image, caption='Uploaded Image', use_column_width=True)
|
83 |
+
image1 = image.getvalue()
|
84 |
+
image_ten = detect_and_process_skin(image1)
|
85 |
image_tensor, final_image = preprocess_image(image, mtcnn, device)
|
86 |
+
predicted_class, probabilities = predict(image_ten, model, device)
|
87 |
|
88 |
st.write(f"Predicted class: {predicted_class.item()}")
|
89 |
# Display the final processed image
|
90 |
+
# st.image(final_image, caption='Processed Image', use_column_width=True)
|
91 |
+
img_bytes = io.BytesIO()
|
92 |
+
detect_and_process_skin(image1.getvalue()).save(img_bytes, format='JPEG')
|
93 |
+
st.image(img_bytes.getvalue(), width=250, caption="Processed Image")
|