|
| 1 | +from io import BufferedReader, BytesIO |
| 2 | + |
| 3 | +import cv2 |
| 4 | +import mediapipe as mp |
| 5 | +import numpy as np |
| 6 | +import streamlit as st |
| 7 | +from dotenv import load_dotenv |
| 8 | + |
| 9 | +load_dotenv() |
| 10 | + |
| 11 | +with st.sidebar: |
| 12 | + # https://chuoling.github.io/mediapipe/solutions/pose.html#min_detection_confidence |
| 13 | + min_detection_confidence = st.number_input( |
| 14 | + label="Minimum detection confidence", |
| 15 | + min_value=0.0, |
| 16 | + max_value=1.0, |
| 17 | + value=0.5, |
| 18 | + key="min_detection_confidence", |
| 19 | + ) |
| 20 | + # https://chuoling.github.io/mediapipe/solutions/pose.html#model_complexity |
| 21 | + model_complexity = st.selectbox( |
| 22 | + label="Model complexity", |
| 23 | + options=[ |
| 24 | + 0, |
| 25 | + 1, |
| 26 | + 2, |
| 27 | + ], |
| 28 | + index=1, |
| 29 | + key="model_complexity", |
| 30 | + ) |
| 31 | + |
| 32 | + |
| 33 | +st.title("Pose estimation") |
| 34 | + |
| 35 | +st.info("Upload an image and AI will estimate the pose.") |
| 36 | + |
| 37 | +uploaded_file = st.file_uploader( |
| 38 | + "Upload an image", |
| 39 | + type=( |
| 40 | + "jpg", |
| 41 | + "jpeg", |
| 42 | + "png", |
| 43 | + "gif", |
| 44 | + "bmp", |
| 45 | + "tiff", |
| 46 | + ), |
| 47 | +) |
| 48 | +if uploaded_file is not None: |
| 49 | + button = st.button("Estimate pose") |
| 50 | + |
| 51 | +row1_left, row1_right = st.columns(2) |
| 52 | +with row1_left: |
| 53 | + if uploaded_file: |
| 54 | + st.image( |
| 55 | + uploaded_file, |
| 56 | + use_column_width=True, |
| 57 | + caption="Input image", |
| 58 | + ) |
| 59 | + |
| 60 | +with row1_right: |
| 61 | + if uploaded_file and button: |
| 62 | + with st.spinner("Thinking..."): |
| 63 | + mp_pose = mp.solutions.pose |
| 64 | + mp_drawing = mp.solutions.drawing_utils |
| 65 | + |
| 66 | + pose = mp_pose.Pose( |
| 67 | + static_image_mode=True, |
| 68 | + min_detection_confidence=min_detection_confidence, |
| 69 | + model_complexity=model_complexity, |
| 70 | + ) |
| 71 | + |
| 72 | + bytes_data = uploaded_file.getvalue() |
| 73 | + cv2_img = cv2.imdecode(np.frombuffer(bytes_data, np.uint8), cv2.IMREAD_COLOR) |
| 74 | + img = cv2.cvtColor(cv2_img, cv2.COLOR_BGR2RGB) |
| 75 | + results = pose.process(img) |
| 76 | + output_img = img.copy() |
| 77 | + mp_drawing.draw_landmarks( |
| 78 | + output_img, |
| 79 | + results.pose_landmarks, |
| 80 | + mp_pose.POSE_CONNECTIONS, |
| 81 | + landmark_drawing_spec=mp_drawing.DrawingSpec(color=(255, 0, 0), thickness=2, circle_radius=10), |
| 82 | + connection_drawing_spec=mp_drawing.DrawingSpec(color=(255, 0, 0), thickness=10), |
| 83 | + ) |
| 84 | + ret, enco_img = cv2.imencode(".png", cv2.cvtColor(output_img, cv2.COLOR_BGR2RGB)) |
| 85 | + BytesIO_img = BytesIO(enco_img.tostring()) |
| 86 | + BufferedReader_img = BufferedReader(BytesIO_img) |
| 87 | + |
| 88 | + # Output |
| 89 | + st.image( |
| 90 | + image=output_img, |
| 91 | + use_column_width=True, |
| 92 | + caption="Output image", |
| 93 | + ) |
| 94 | + st.download_button( |
| 95 | + label="Download", |
| 96 | + data=BufferedReader_img, |
| 97 | + file_name="output.png", |
| 98 | + mime="image/png", |
| 99 | + ) |
0 commit comments