-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintellirack.py
More file actions
181 lines (114 loc) · 3.78 KB
/
Copy pathintellirack.py
File metadata and controls
181 lines (114 loc) · 3.78 KB
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
175
176
177
178
179
180
181
import streamlit as st
import cv2
import mediapipe as mp
import numpy as np
import time
from streamlit_webrtc import webrtc_streamer, VideoProcessorBase
st.set_page_config(layout="wide")
st.title("🧠 IntelliRack Smart Shelf Analytics")
# ----------------------------
# Metrics
# ----------------------------
if "footfall" not in st.session_state:
st.session_state.footfall = 0
if "pickup" not in st.session_state:
st.session_state.pickup = 0
if "interest" not in st.session_state:
st.session_state.interest = 0
if "dwell_start" not in st.session_state:
st.session_state.dwell_start = None
# ----------------------------
# MediaPipe Init
# ----------------------------
mp_hands = mp.solutions.hands
mp_draw = mp.solutions.drawing_utils
# ----------------------------
# Video Processor
# ----------------------------
class IntelliRackProcessor(VideoProcessorBase):
def __init__(self):
self.hands = mp_hands.Hands(
max_num_hands=2,
min_detection_confidence=0.6,
min_tracking_confidence=0.6
)
self.last_hand = False
self.hand_frames = 0
self.motion_bg = None
def detect_motion(self, frame):
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray,(21,21),0)
if self.motion_bg is None:
self.motion_bg = blur
return False
diff = cv2.absdiff(self.motion_bg, blur)
thresh = cv2.threshold(diff,25,255,cv2.THRESH_BINARY)[1]
movement = np.sum(thresh)
return movement > 400000
def recv(self, frame):
img = frame.to_ndarray(format="bgr24")
rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
results = self.hands.process(rgb)
# ----------------
# Footfall Detection
# ----------------
if self.detect_motion(img):
if st.session_state.dwell_start is None:
st.session_state.footfall += 1
st.session_state.dwell_start = time.time()
# ----------------
# Dwell Time
# ----------------
if st.session_state.dwell_start is not None:
dwell = time.time() - st.session_state.dwell_start
cv2.putText(
img,
f"Dwell {int(dwell)}s",
(20,40),
cv2.FONT_HERSHEY_SIMPLEX,
1,
(0,255,0),
2
)
if dwell > 10:
st.session_state.interest += 1
st.session_state.dwell_start = None
# ----------------
# Hand Detection
# ----------------
hand_detected = False
if results.multi_hand_landmarks:
hand_detected = True
for hand_landmarks in results.multi_hand_landmarks:
mp_draw.draw_landmarks(
img,
hand_landmarks,
mp_hands.HAND_CONNECTIONS
)
# ----------------
# Pickup Logic
# ----------------
if hand_detected:
self.hand_frames += 1
else:
self.hand_frames = 0
if self.hand_frames > 15:
st.session_state.pickup += 1
st.session_state.interest += 1
self.hand_frames = 0
return img
# ----------------------------
# Layout
# ----------------------------
col1, col2 = st.columns([3,1])
with col1:
webrtc_streamer(
key="rack",
video_processor_factory=IntelliRackProcessor,
media_stream_constraints={"video": True, "audio": False}
)
with col2:
st.subheader("📊 Live Metrics")
st.metric("Footfall", st.session_state.footfall)
st.metric("Pickup", st.session_state.pickup)
st.metric("Interest", st.session_state.interest)