Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -17,10 +17,15 @@ pose = mp_pose.Pose(static_image_mode=False, min_detection_confidence=0.5)
|
|
| 17 |
mp_drawing = mp.solutions.drawing_utils
|
| 18 |
|
| 19 |
def process_frame(frame):
|
| 20 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
if isinstance(frame, Image.Image):
|
| 22 |
frame = np.array(frame)
|
| 23 |
|
|
|
|
| 24 |
try:
|
| 25 |
image_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 26 |
except Exception as e:
|
|
@@ -28,10 +33,12 @@ def process_frame(frame):
|
|
| 28 |
|
| 29 |
results = pose.process(image_rgb)
|
| 30 |
|
|
|
|
| 31 |
if results.pose_landmarks:
|
| 32 |
mp_drawing.draw_landmarks(frame, results.pose_landmarks, mp_pose.POSE_CONNECTIONS)
|
| 33 |
|
| 34 |
try:
|
|
|
|
| 35 |
resized = cv2.resize(image_rgb, (128, 128))
|
| 36 |
input_data = resized.flatten().reshape(1, -1)
|
| 37 |
prediction_proba = model.predict_proba(input_data)[0]
|
|
@@ -42,7 +49,7 @@ def process_frame(frame):
|
|
| 42 |
except Exception as e:
|
| 43 |
return frame, f"Prediction error: {e}", None
|
| 44 |
|
| 45 |
-
#
|
| 46 |
try:
|
| 47 |
fig, ax = plt.subplots()
|
| 48 |
ax.bar(action_labels.values(), prediction_proba, color='skyblue')
|
|
@@ -58,18 +65,18 @@ def process_frame(frame):
|
|
| 58 |
graph_img = None
|
| 59 |
label += f" (Graph error: {e})"
|
| 60 |
|
| 61 |
-
#
|
| 62 |
try:
|
| 63 |
cv2.putText(frame, label, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,255,0), 2)
|
| 64 |
except:
|
| 65 |
-
pass
|
| 66 |
|
| 67 |
return frame, label, graph_img
|
| 68 |
|
| 69 |
-
# Gradio ์ธํฐํ์ด์ค
|
| 70 |
demo = gr.Interface(
|
| 71 |
fn=process_frame,
|
| 72 |
-
inputs=gr.Image(source="webcam", streaming=False),
|
| 73 |
outputs=[
|
| 74 |
gr.Image(label="Pose + Prediction"),
|
| 75 |
gr.Textbox(label="Predicted Action"),
|
|
@@ -77,10 +84,11 @@ demo = gr.Interface(
|
|
| 77 |
],
|
| 78 |
title="Pose Action Classifier",
|
| 79 |
description=(
|
| 80 |
-
"๐ฑ
|
| 81 |
-
"
|
|
|
|
| 82 |
)
|
| 83 |
)
|
| 84 |
|
| 85 |
if __name__ == "__main__":
|
| 86 |
-
demo.launch()
|
|
|
|
| 17 |
mp_drawing = mp.solutions.drawing_utils
|
| 18 |
|
| 19 |
def process_frame(frame):
|
| 20 |
+
# 1. None ์ฒดํฌ
|
| 21 |
+
if frame is None:
|
| 22 |
+
return None, "Error: No image received from camera", None
|
| 23 |
+
|
| 24 |
+
# 2. PIL.Image โ numpy ๋ณํ
|
| 25 |
if isinstance(frame, Image.Image):
|
| 26 |
frame = np.array(frame)
|
| 27 |
|
| 28 |
+
# 3. RGB ๋ณํ ์์ธ ์ฒ๋ฆฌ
|
| 29 |
try:
|
| 30 |
image_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 31 |
except Exception as e:
|
|
|
|
| 33 |
|
| 34 |
results = pose.process(image_rgb)
|
| 35 |
|
| 36 |
+
# ๊ด์ ์๊ฐํ
|
| 37 |
if results.pose_landmarks:
|
| 38 |
mp_drawing.draw_landmarks(frame, results.pose_landmarks, mp_pose.POSE_CONNECTIONS)
|
| 39 |
|
| 40 |
try:
|
| 41 |
+
# ๋ชจ๋ธ ์
๋ ฅ ์ ์ฒ๋ฆฌ
|
| 42 |
resized = cv2.resize(image_rgb, (128, 128))
|
| 43 |
input_data = resized.flatten().reshape(1, -1)
|
| 44 |
prediction_proba = model.predict_proba(input_data)[0]
|
|
|
|
| 49 |
except Exception as e:
|
| 50 |
return frame, f"Prediction error: {e}", None
|
| 51 |
|
| 52 |
+
# ์์ธก ๊ทธ๋ํ ์์ฑ
|
| 53 |
try:
|
| 54 |
fig, ax = plt.subplots()
|
| 55 |
ax.bar(action_labels.values(), prediction_proba, color='skyblue')
|
|
|
|
| 65 |
graph_img = None
|
| 66 |
label += f" (Graph error: {e})"
|
| 67 |
|
| 68 |
+
# ์์ธก ํ
์คํธ ์ด๋ฏธ์ง์ ํ์
|
| 69 |
try:
|
| 70 |
cv2.putText(frame, label, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,255,0), 2)
|
| 71 |
except:
|
| 72 |
+
pass
|
| 73 |
|
| 74 |
return frame, label, graph_img
|
| 75 |
|
| 76 |
+
# Gradio ์ธํฐํ์ด์ค ๊ตฌ์ฑ
|
| 77 |
demo = gr.Interface(
|
| 78 |
fn=process_frame,
|
| 79 |
+
inputs=gr.Image(source="webcam", streaming=False), # โ
์ฌ์ง ์ดฌ์ ๋ฒํผ ํ์ฑํ!
|
| 80 |
outputs=[
|
| 81 |
gr.Image(label="Pose + Prediction"),
|
| 82 |
gr.Textbox(label="Predicted Action"),
|
|
|
|
| 84 |
],
|
| 85 |
title="Pose Action Classifier",
|
| 86 |
description=(
|
| 87 |
+
"๐ฑ On mobile, tap the button to take a photo. "
|
| 88 |
+
"You can switch to the back camera by tapping the camera icon. "
|
| 89 |
+
"Internet Explorer is not supported โ use Chrome or Safari."
|
| 90 |
)
|
| 91 |
)
|
| 92 |
|
| 93 |
if __name__ == "__main__":
|
| 94 |
+
demo.launch()
|