FishKIM commited on
Commit
0d35d61
ยท
verified ยท
1 Parent(s): 2a29151

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -9
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
- # ๐Ÿ“ธ PIL.Image๋ฅผ numpy๋กœ ๋ณ€ํ™˜
 
 
 
 
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
- "๐Ÿ“ฑ Mobile users: tap the button to take a photo. On Chrome, you can switch to the back camera by tapping the camera icon "
81
- "(if available). Internet Explorer is not supported โ€” use Chrome or Safari."
 
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()