File size: 4,622 Bytes
b069c78
 
 
 
 
 
941dfdb
 
 
 
 
 
b069c78
 
 
 
 
 
941dfdb
 
 
 
 
b069c78
 
941dfdb
 
b069c78
 
 
941dfdb
 
 
 
 
b069c78
941dfdb
 
 
b069c78
 
 
941dfdb
 
 
 
 
 
b069c78
 
941dfdb
 
 
 
 
 
 
 
 
b069c78
941dfdb
b069c78
941dfdb
b069c78
941dfdb
b069c78
941dfdb
 
 
 
 
b069c78
 
941dfdb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b069c78
941dfdb
 
 
b069c78
941dfdb
 
b069c78
941dfdb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b069c78
941dfdb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b069c78
 
 
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import streamlit as st
import cv2
import numpy as np
from PIL import Image
import math

from mediapipe.tasks import python
from mediapipe.tasks.python import vision

# -----------------------------
# CONFIG
# -----------------------------
st.set_page_config(
    page_title="Classroom Dimension Estimator",
    page_icon="📏",
    layout="wide"
)

POSE_MODEL_PATH = "pose_landmarker_lite.task"

# -----------------------------
# UI STYLE
# -----------------------------
st.markdown("""
    <style>
    .main { padding: 2rem; }
    .stAlert { margin-top: 1rem; }
    </style>
""", unsafe_allow_html=True)

# -----------------------------
# UTIL FUNCTIONS
# -----------------------------
def calculate_distance(p1, p2):
    return math.sqrt((p2[0]-p1[0])**2 + (p2[1]-p1[1])**2)

# -----------------------------
# ROOM DETECTION (unchanged)
# -----------------------------
def estimate_room_dimensions(image):
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    edges = cv2.Canny(gray, 50, 150)

    lines = cv2.HoughLinesP(edges, 1, np.pi/180,
                            threshold=100,
                            minLineLength=100,
                            maxLineGap=10)

    if lines is None:
        return None, None, image

    max_w, max_h = 0, 0
    out = image.copy()

    for l in lines:
        x1, y1, x2, y2 = l[0]
        cv2.line(out, (x1, y1), (x2, y2), (0, 255, 0), 2)

        length = calculate_distance((x1,y1), (x2,y2))
        angle = abs(math.degrees(math.atan2(y2-y1, x2-x1)))

        if angle < 45 or angle > 135:
            max_w = max(max_w, length)
        else:
            max_h = max(max_h, length)

    return max_w * 0.01, max_h * 0.01, out

# -----------------------------
# MEDIA PIPE TASKS (NEW)
# -----------------------------
def estimate_with_person_reference(image):

    base_options = python.BaseOptions(model_asset_path=POSE_MODEL_PATH)

    options = vision.PoseLandmarkerOptions(
        base_options=base_options,
        running_mode=vision.RunningMode.IMAGE,
        num_poses=1
    )

    with vision.PoseLandmarker.create_from_options(options) as landmarker:

        rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        mp_image = vision.Image(image_format=vision.ImageFormat.SRGB, data=rgb)

        result = landmarker.detect(mp_image)

        if not result.pose_landmarks:
            return None, image

        lm = result.pose_landmarks[0]

        h, w = image.shape[:2]

        # TASK API = index based landmarks
        nose = lm[0]
        ankle = lm[27]

        head = (int(nose.x * w), int(nose.y * h))
        foot = (int(ankle.x * w), int(ankle.y * h))

        pixel_dist = calculate_distance(head, foot)

        if pixel_dist == 0:
            return None, image

        ratio = 1.7 / pixel_dist

        annotated = image.copy()
        cv2.circle(annotated, head, 6, (0,0,255), -1)
        cv2.circle(annotated, foot, 6, (255,0,0), -1)

        return ratio, annotated

# -----------------------------
# STREAMLIT APP
# -----------------------------
def main():

    st.title("📏 Classroom Dimension Estimator")

    uploaded_file = st.file_uploader(
        "Upload classroom image",
        type=["jpg","jpeg","png"]
    )

    if uploaded_file:

        image = Image.open(uploaded_file)
        image_np = np.array(image)

        tab1, tab2 = st.tabs(["Image", "Results"])

        with tab1:
            st.image(image, use_column_width=True)

        with st.spinner("Processing..."):

            ratio, pose_img = estimate_with_person_reference(image_np)

            if ratio:
                st.success("Person detected → using scale reference")

                w, h, processed = estimate_room_dimensions(image_np)

                if w and h:
                    w *= ratio
                    h *= ratio

                    with tab1:
                        st.image(pose_img, caption="Pose detection")

                    with tab2:
                        st.metric("Width", f"{w:.2f} m")
                        st.metric("Height", f"{h:.2f} m")
                        st.metric("Area", f"{w*h:.2f} m²")

            else:
                st.warning("No person detected → fallback method")

                w, h, processed = estimate_room_dimensions(image_np)

                with tab2:
                    if w and h:
                        st.metric("Width", f"{w:.2f} m")
                        st.metric("Height", f"{h:.2f} m")
                        st.metric("Area", f"{w*h:.2f} m²")

        with tab1:
            st.image(processed, caption="Room detection")

if __name__ == "__main__":
    main()