Update app.py
Browse files
app.py
CHANGED
@@ -42,19 +42,21 @@ def calculate_angle(a, b, c):
|
|
42 |
angle = 360 - angle
|
43 |
return angle
|
44 |
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
# stage='mid'#it checks if we our hand is UP or DOWN
|
49 |
|
50 |
# Detection Queue
|
51 |
result_queue: "queue.Queue[List[Detection]]" = queue.Queue()
|
52 |
|
53 |
def video_frame_callback(frame: av.VideoFrame) -> av.VideoFrame:
|
54 |
-
counterL
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
|
|
|
|
58 |
image = frame.to_ndarray(format="bgr24")
|
59 |
h, w = image.shape[:2]
|
60 |
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
@@ -92,40 +94,40 @@ def video_frame_callback(frame: av.VideoFrame) -> av.VideoFrame:
|
|
92 |
|
93 |
#Visualize of left leg
|
94 |
cv2.putText(image, str(angleHipL),tuple(np.multiply(angleHipL, [640, 480]).astype(int)),cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2, cv2.LINE_AA)
|
95 |
-
|
96 |
-
# # Squat logic
|
97 |
-
# if 80 < angleKneeL < 110 and 29 < angleHipL < 40:
|
98 |
-
# cv2.putText(image, "Squat Detected!", (300, 100), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 255, 0), 3)
|
99 |
-
# else:
|
100 |
-
# if angleHipL < 29:
|
101 |
-
# cv2.putText(image, "Lean Forward!", (300, 200), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255), 3)
|
102 |
-
# elif angleHipL > 45:
|
103 |
-
# cv2.putText(image, "Lean Backward!", (300, 200), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255), 3)
|
104 |
-
# if angleKneeL < 80:
|
105 |
-
# cv2.putText(image, "Squat Too Deep!", (300, 250), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255), 3)
|
106 |
-
# elif angleKneeL > 110:
|
107 |
-
# cv2.putText(image, "Lower Your Hips!", (300, 300), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255), 3)
|
108 |
|
109 |
|
110 |
-
|
111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
112 |
if 10 < angleHipL < 18:
|
113 |
cv2.rectangle(image, (310, 180), (450, 220), (0, 0, 0), -1)
|
114 |
cv2.putText(image,f"Bend Forward",(320,200),cv2.FONT_HERSHEY_SIMPLEX,1,(150,120,255),1,cv2.LINE_AA)
|
115 |
-
|
116 |
-
# 2. Lean Backward Warning
|
117 |
if angleHipL > 45:
|
118 |
cv2.rectangle(image, (310, 180), (450, 220), (0, 0, 0), -1)
|
119 |
cv2.putText(image,f"Bend Backward",(320,200),cv2.FONT_HERSHEY_SIMPLEX,1,(80,120,255),1,cv2.LINE_AA)
|
|
|
120 |
|
121 |
-
# # stage 2
|
122 |
|
123 |
-
# # Incorrect movements
|
124 |
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
|
|
|
|
|
|
|
|
|
|
129 |
|
130 |
|
131 |
# # 3. Knees not low enough and not completed the squat
|
@@ -142,11 +144,13 @@ def video_frame_callback(frame: av.VideoFrame) -> av.VideoFrame:
|
|
142 |
# incorrect +=1
|
143 |
# stage='up'
|
144 |
|
145 |
-
# stage 4
|
146 |
-
if (80 < angleKneeL < 110):
|
147 |
-
|
148 |
-
|
149 |
-
#
|
|
|
|
|
150 |
|
151 |
|
152 |
|
@@ -193,224 +197,3 @@ webrtc_streamer(
|
|
193 |
video_frame_callback=video_frame_callback,
|
194 |
async_processing=True,
|
195 |
)
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
# import logging
|
228 |
-
# import cv2
|
229 |
-
# import numpy as np
|
230 |
-
# import streamlit as st
|
231 |
-
# from streamlit_webrtc import WebRtcMode, webrtc_streamer
|
232 |
-
# from cvzone.HandTrackingModule import HandDetector
|
233 |
-
# from cvzone.SelfiSegmentationModule import SelfiSegmentation
|
234 |
-
# import os
|
235 |
-
# import time
|
236 |
-
# import av
|
237 |
-
# import queue
|
238 |
-
# from typing import List, NamedTuple
|
239 |
-
# from sample_utils.turn import get_ice_servers
|
240 |
-
|
241 |
-
# logger = logging.getLogger(__name__)
|
242 |
-
|
243 |
-
# # Streamlit settings
|
244 |
-
# st.set_page_config(page_title="Virtual Keyboard", layout="wide")
|
245 |
-
# st.title("Interactive Virtual Keyboard")
|
246 |
-
# st.subheader('''Turn on the webcam and use hand gestures to interact with the virtual keyboard.
|
247 |
-
# Use 'a' and 'd' from the keyboard to change the background.''')
|
248 |
-
|
249 |
-
# # Initialize modules
|
250 |
-
# detector = HandDetector(maxHands=1, detectionCon=0.8)
|
251 |
-
# segmentor = SelfiSegmentation()
|
252 |
-
|
253 |
-
# # Define virtual keyboard layout
|
254 |
-
# keys = [["Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P"],
|
255 |
-
# ["A", "S", "D", "F", "G", "H", "J", "K", "L", ";"],
|
256 |
-
# ["Z", "X", "C", "V", "B", "N", "M", ",", ".", "/"]]
|
257 |
-
|
258 |
-
# class Button:
|
259 |
-
# def __init__(self, pos, text, size=[100, 100]):
|
260 |
-
# self.pos = pos
|
261 |
-
# self.size = size
|
262 |
-
# self.text = text
|
263 |
-
|
264 |
-
# class Detection(NamedTuple):
|
265 |
-
# label: str
|
266 |
-
# score: float
|
267 |
-
# box: np.ndarray
|
268 |
-
|
269 |
-
# # result_queue: "queue.Queue[List[Detection]]" = queue.Queue()
|
270 |
-
|
271 |
-
# listImg = os.listdir('model/street') if os.path.exists('model/street') else []
|
272 |
-
# if not listImg:
|
273 |
-
# st.error("Error: 'street' directory is missing or empty. Please add background images.")
|
274 |
-
# st.stop()
|
275 |
-
# else:
|
276 |
-
# imgList = [cv2.imread(f'model/street/{imgPath}') for imgPath in listImg if cv2.imread(f'model/street/{imgPath}') is not None]
|
277 |
-
|
278 |
-
# indexImg = 0
|
279 |
-
# prev_key_time = [time.time()] * 2
|
280 |
-
# output_text = ""
|
281 |
-
|
282 |
-
# if "output_text" not in st.session_state:
|
283 |
-
# st.session_state["output_text"] = ""
|
284 |
-
|
285 |
-
|
286 |
-
# # def video_frame_callback(frame: av.VideoFrame) -> av.VideoFrame:
|
287 |
-
# # img = frame.to_ndarray(format="bgr24")
|
288 |
-
# # hands, img = detector.findHands(img, flipType=False)
|
289 |
-
|
290 |
-
# # # Render hand detection results
|
291 |
-
|
292 |
-
# # if hands:
|
293 |
-
# # hand = hands[0]
|
294 |
-
# # bbox = hand["bbox"]
|
295 |
-
# # cv2.rectangle(img, (bbox[0], bbox[1]), (bbox[0]+bbox[2], bbox[1]+bbox[3]), (255, 0, 0), 2)
|
296 |
-
|
297 |
-
# # cv2.putText(img, 'OpenCV', (50,50), font,
|
298 |
-
# # fontScale, color, thickness, cv2.LINE_AA)
|
299 |
-
# # cv2.putText(img, 'OpenCV', (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 2, (255, 255, 255), 1, cv2.LINE_AA)
|
300 |
-
|
301 |
-
# # result_queue.put(hands)
|
302 |
-
|
303 |
-
# # return av.VideoFrame.from_ndarray(img, format="bgr24")
|
304 |
-
|
305 |
-
|
306 |
-
# result_queue: "queue.Queue[List[Detection]]" = queue.Queue()
|
307 |
-
|
308 |
-
|
309 |
-
# def video_frame_callback(frame: av.VideoFrame) -> av.VideoFrame:
|
310 |
-
# image = frame.to_ndarray(format="bgr24")
|
311 |
-
|
312 |
-
# # Run inference
|
313 |
-
# blob = cv2.dnn.blobFromImage(
|
314 |
-
# cv2.resize(image, (300, 300)), 0.007843, (300, 300), 127.5
|
315 |
-
# )
|
316 |
-
# net.setInput(blob)
|
317 |
-
# output = net.forward()
|
318 |
-
|
319 |
-
# h, w = image.shape[:2]
|
320 |
-
|
321 |
-
# # Convert the output array into a structured form.
|
322 |
-
# output = output.squeeze() # (1, 1, N, 7) -> (N, 7)
|
323 |
-
# output = output[output[:, 2] >= score_threshold]
|
324 |
-
# detections = [
|
325 |
-
# Detection(
|
326 |
-
# class_id=int(detection[1]),
|
327 |
-
# label=CLASSES[int(detection[1])],
|
328 |
-
# score=float(detection[2]),
|
329 |
-
# box=(detection[3:7] * np.array([w, h, w, h])),
|
330 |
-
# )
|
331 |
-
# for detection in output
|
332 |
-
# ]
|
333 |
-
|
334 |
-
# # Render bounding boxes and captions
|
335 |
-
# for detection in detections:
|
336 |
-
# caption = f"{detection.label}: {round(detection.score * 100, 2)}%"
|
337 |
-
# color = COLORS[detection.class_id]
|
338 |
-
# xmin, ymin, xmax, ymax = detection.box.astype("int")
|
339 |
-
|
340 |
-
# cv2.rectangle(image, (xmin, ymin), (xmax, ymax), color, 2)
|
341 |
-
# cv2.putText(
|
342 |
-
# image,
|
343 |
-
# caption,
|
344 |
-
# (xmin, ymin - 15 if ymin - 15 > 15 else ymin + 15),
|
345 |
-
# cv2.FONT_HERSHEY_SIMPLEX,
|
346 |
-
# 0.5,
|
347 |
-
# color,
|
348 |
-
# 2,
|
349 |
-
# )
|
350 |
-
|
351 |
-
# result_queue.put(detections)
|
352 |
-
|
353 |
-
# return av.VideoFrame.from_ndarray(image, format="bgr24")
|
354 |
-
|
355 |
-
|
356 |
-
# # def video_frame_callback(frame: av.VideoFrame) -> av.VideoFrame:
|
357 |
-
# # global indexImg, output_text
|
358 |
-
|
359 |
-
# # img = frame.to_ndarray(format="bgr24")
|
360 |
-
# # imgOut = segmentor.removeBG(img, imgList[indexImg])
|
361 |
-
# # hands, imgOut = detector.findHands(imgOut, flipType=False)
|
362 |
-
|
363 |
-
# # buttonList = [Button([30 + col * 105, 30 + row * 120], key) for row, line in enumerate(keys) for col, key in enumerate(line)]
|
364 |
-
|
365 |
-
# # detections = []
|
366 |
-
# # if hands:
|
367 |
-
# # for i, hand in enumerate(hands):
|
368 |
-
# # lmList = hand['lmList']
|
369 |
-
# # bbox = hand['bbox']
|
370 |
-
# # label = "Hand"
|
371 |
-
# # score = hand['score']
|
372 |
-
# # box = np.array([bbox[0], bbox[1], bbox[0] + bbox[2], bbox[1] + bbox[3]])
|
373 |
-
# # detections.append(Detection(label=label, score=score, box=box))
|
374 |
-
|
375 |
-
# # if lmList:
|
376 |
-
# # x4, y4 = lmList[4][0], lmList[4][1]
|
377 |
-
# # x8, y8 = lmList[8][0], lmList[8][1]
|
378 |
-
# # distance = np.sqrt((x8 - x4) ** 2 + (y8 - y4) ** 2)
|
379 |
-
# # click_threshold = 10
|
380 |
-
|
381 |
-
# # for button in buttonList:
|
382 |
-
# # x, y = button.pos
|
383 |
-
# # w, h = button.size
|
384 |
-
# # if x < x8 < x + w and y < y8 < y + h:
|
385 |
-
# # cv2.rectangle(imgOut, button.pos, (x + w, y + h), (0, 255, 160), -1)
|
386 |
-
# # cv2.putText(imgOut, button.text, (x + 20, y + 70), cv2.FONT_HERSHEY_PLAIN, 5, (255, 255, 255), 3)
|
387 |
-
|
388 |
-
# # if (distance / np.sqrt(bbox[2] ** 2 + bbox[3] ** 2)) * 100 < click_threshold:
|
389 |
-
# # if time.time() - prev_key_time[i] > 2:
|
390 |
-
# # prev_key_time[i] = time.time()
|
391 |
-
# # if button.text != 'BS' and button.text != 'SPACE':
|
392 |
-
# # output_text += button.text
|
393 |
-
# # elif button.text == 'BS':
|
394 |
-
# # output_text = output_text[:-1]
|
395 |
-
# # else:
|
396 |
-
# # output_text += ' '
|
397 |
-
|
398 |
-
# # result_queue.put(detections)
|
399 |
-
# # st.session_state["output_text"] = output_text
|
400 |
-
# # return av.VideoFrame.from_ndarray(imgOut, format="bgr24")
|
401 |
-
|
402 |
-
|
403 |
-
|
404 |
-
# webrtc_streamer(
|
405 |
-
# key="virtual-keyboard",
|
406 |
-
# mode=WebRtcMode.SENDRECV,
|
407 |
-
# rtc_configuration={"iceServers": get_ice_servers(), "iceTransportPolicy": "relay"},
|
408 |
-
# media_stream_constraints={"video": True, "audio": False},
|
409 |
-
# video_frame_callback=video_frame_callback,
|
410 |
-
# async_processing=True,
|
411 |
-
# )
|
412 |
-
|
413 |
-
# st.subheader("Output Text")
|
414 |
-
# st.text_area("Live Input:", value=st.session_state["output_text"], height=200)
|
415 |
-
|
416 |
-
|
|
|
42 |
angle = 360 - angle
|
43 |
return angle
|
44 |
|
45 |
+
counterL=0#Counter checks for number of curls
|
46 |
+
correct=0
|
47 |
+
incorrect=0
|
|
|
48 |
|
49 |
# Detection Queue
|
50 |
result_queue: "queue.Queue[List[Detection]]" = queue.Queue()
|
51 |
|
52 |
def video_frame_callback(frame: av.VideoFrame) -> av.VideoFrame:
|
53 |
+
global counterL, correct, incorrect, stage #The change made
|
54 |
+
# Initialize stage if not defined
|
55 |
+
if 'stage' not in globals():
|
56 |
+
stage = 'up'
|
57 |
+
correct = 0
|
58 |
+
incorrect = 0
|
59 |
+
|
60 |
image = frame.to_ndarray(format="bgr24")
|
61 |
h, w = image.shape[:2]
|
62 |
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
|
|
94 |
|
95 |
#Visualize of left leg
|
96 |
cv2.putText(image, str(angleHipL),tuple(np.multiply(angleHipL, [640, 480]).astype(int)),cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2, cv2.LINE_AA)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
|
98 |
|
99 |
+
|
100 |
+
|
101 |
+
# Update squat stage and count correct reps
|
102 |
+
if angleKneeL > 110 and stage == 'down':
|
103 |
+
stage = 'up'
|
104 |
+
if 18 < angleHipL < 40:
|
105 |
+
correct += 1
|
106 |
+
|
107 |
+
if 80 < angleKneeL < 110 and stage == 'up':
|
108 |
+
stage = 'down'
|
109 |
+
|
110 |
+
# Display feedback messages
|
111 |
if 10 < angleHipL < 18:
|
112 |
cv2.rectangle(image, (310, 180), (450, 220), (0, 0, 0), -1)
|
113 |
cv2.putText(image,f"Bend Forward",(320,200),cv2.FONT_HERSHEY_SIMPLEX,1,(150,120,255),1,cv2.LINE_AA)
|
114 |
+
|
|
|
115 |
if angleHipL > 45:
|
116 |
cv2.rectangle(image, (310, 180), (450, 220), (0, 0, 0), -1)
|
117 |
cv2.putText(image,f"Bend Backward",(320,200),cv2.FONT_HERSHEY_SIMPLEX,1,(80,120,255),1,cv2.LINE_AA)
|
118 |
+
|
119 |
|
|
|
120 |
|
|
|
121 |
|
122 |
+
|
123 |
+
# # # stage 2
|
124 |
+
|
125 |
+
# # # Incorrect movements
|
126 |
+
|
127 |
+
# # 3. Knees not low enough
|
128 |
+
# if 110 < angleKneeL < 130:
|
129 |
+
# cv2.rectangle(image, (220, 40), (450, 80), (0, 0, 0), -1)
|
130 |
+
# cv2.putText(image,f"Lower Your Hips",(230,60),cv2.FONT_HERSHEY_SIMPLEX,1,(255,255,255),1,cv2.LINE_AA)
|
131 |
|
132 |
|
133 |
# # 3. Knees not low enough and not completed the squat
|
|
|
144 |
# incorrect +=1
|
145 |
# stage='up'
|
146 |
|
147 |
+
# # stage 4
|
148 |
+
# if (80 < angleKneeL < 110) and stage=='mid':
|
149 |
+
# if (18 < angleHipL < 40): # Valid "down" position
|
150 |
+
# correct+=1
|
151 |
+
# stage='up'
|
152 |
+
# if (angleKneeL>110):
|
153 |
+
# stage='mid'
|
154 |
|
155 |
|
156 |
|
|
|
197 |
video_frame_callback=video_frame_callback,
|
198 |
async_processing=True,
|
199 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|