Spaces:
Sleeping
Sleeping
File size: 2,020 Bytes
7ec4ee8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# streamlit_app.py
import streamlit as st
import numpy as np
import pandas as pd
from keras.models import load_model
from PIL import Image, ImageDraw
import io
# Load the trained model
model = load_model('keypoint_model.h5')
def load_image(image):
image = Image.open(image).convert('L') # Convert to grayscale
image = image.resize((96, 96)) # Resize to match model input
image_array = np.array(image)
image_array = image_array / 255.0 # Normalize
return image_array.reshape(-1, 96, 96, 1) # Reshape for model input
def draw_keypoints(image, keypoints):
# Draw keypoints on the image
draw = ImageDraw.Draw(image)
for (x, y) in keypoints:
draw.ellipse((x - 3, y - 3, x + 3, y + 3), fill='red') # Draw a circle for each keypoint
return image
# Title of the app
st.title("Keypoint Prediction App")
# Upload an image
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
if uploaded_file is not None:
# Load and preprocess the image
image = load_image(uploaded_file)
# Display the uploaded image
original_image = Image.open(uploaded_file).convert('L').resize((96, 96)) # Convert and resize for displaying
st.image(original_image, caption='Uploaded Image.', use_column_width=True)
# Make predictions
if st.button("Predict"):
predictions = model.predict(image)
# Reshape predictions to (15, 2) for x and y coordinates
keypoints = predictions.reshape(-1, 2)
# Draw keypoints on the original image
keypoint_image = draw_keypoints(original_image.copy(), keypoints)
# Display the image with keypoints
st.image(keypoint_image, caption='Image with Predicted Keypoints', use_column_width=True)
# Display the keypoints
st.write("Predicted Keypoints:")
for i, (x, y) in enumerate(keypoints):
st.write(f"Keypoint {i+1}: (X: {x:.2f}, Y: {y:.2f})") |