Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -45,6 +45,22 @@ class AirDrawingApp:
|
|
| 45 |
margin: 5px 0;
|
| 46 |
text-align: center;
|
| 47 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
.stButton button {
|
| 49 |
background: linear-gradient(45deg, #FF6B6B, #4ECDC4);
|
| 50 |
color: white;
|
|
@@ -88,6 +104,9 @@ class AirDrawingApp:
|
|
| 88 |
if 'is_drawing' not in st.session_state:
|
| 89 |
st.session_state.is_drawing = False
|
| 90 |
|
|
|
|
|
|
|
|
|
|
| 91 |
if 'current_color' not in st.session_state:
|
| 92 |
st.session_state.current_color = (255, 0, 0) # Red
|
| 93 |
|
|
@@ -106,7 +125,6 @@ class AirDrawingApp:
|
|
| 106 |
middle_tip = landmarks.landmark[self.mp_hands.HandLandmark.MIDDLE_FINGER_TIP]
|
| 107 |
ring_tip = landmarks.landmark[self.mp_hands.HandLandmark.RING_FINGER_TIP]
|
| 108 |
pinky_tip = landmarks.landmark[self.mp_hands.HandLandmark.PINKY_TIP]
|
| 109 |
-
thumb_tip = landmarks.landmark[self.mp_hands.HandLandmark.THUMB_TIP]
|
| 110 |
|
| 111 |
# Get PIP joints (for finger extended detection)
|
| 112 |
index_pip = landmarks.landmark[self.mp_hands.HandLandmark.INDEX_FINGER_PIP]
|
|
@@ -125,25 +143,22 @@ class AirDrawingApp:
|
|
| 125 |
|
| 126 |
# Determine drawing state
|
| 127 |
if extended_fingers == 1 and index_extended:
|
| 128 |
-
# Only index finger extended -
|
| 129 |
-
|
| 130 |
-
status = "
|
| 131 |
elif extended_fingers == 0:
|
| 132 |
# Fist - clear mode
|
| 133 |
-
|
| 134 |
-
status = "β
|
| 135 |
-
st.session_state.drawing_points = []
|
| 136 |
-
st.session_state.last_point = None
|
| 137 |
else:
|
| 138 |
-
# Other gestures - not
|
| 139 |
-
|
| 140 |
status = "π Show only index finger to draw"
|
| 141 |
-
st.session_state.last_point = None
|
| 142 |
|
| 143 |
# Get index finger tip position
|
| 144 |
finger_tip = (int(index_tip.x * w), int(index_tip.y * h))
|
| 145 |
|
| 146 |
-
return
|
| 147 |
|
| 148 |
def update_canvas(self, current_point):
|
| 149 |
"""Update the canvas with new drawing points"""
|
|
@@ -172,6 +187,12 @@ class AirDrawingApp:
|
|
| 172 |
st.session_state.last_point = None
|
| 173 |
st.session_state.is_drawing = False
|
| 174 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
def process_frame(self, frame):
|
| 176 |
"""Process camera frame and detect hand gestures"""
|
| 177 |
# Convert BGR to RGB
|
|
@@ -181,7 +202,7 @@ class AirDrawingApp:
|
|
| 181 |
results = self.hands.process(rgb_frame)
|
| 182 |
|
| 183 |
status = "π Show your hand to the camera"
|
| 184 |
-
|
| 185 |
finger_tip = None
|
| 186 |
|
| 187 |
if results.multi_hand_landmarks:
|
|
@@ -196,14 +217,20 @@ class AirDrawingApp:
|
|
| 196 |
)
|
| 197 |
|
| 198 |
# Detect gesture and get finger tip
|
| 199 |
-
|
| 200 |
|
| 201 |
# Draw finger tip circle
|
| 202 |
if finger_tip:
|
| 203 |
-
|
| 204 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
|
| 206 |
-
return frame,
|
| 207 |
|
| 208 |
def run(self):
|
| 209 |
# Sidebar controls
|
|
@@ -211,18 +238,31 @@ class AirDrawingApp:
|
|
| 211 |
|
| 212 |
st.sidebar.markdown("""
|
| 213 |
### How to Draw:
|
| 214 |
-
1. **
|
| 215 |
-
2. **
|
| 216 |
-
3. **
|
| 217 |
-
4. **
|
| 218 |
-
5. **
|
| 219 |
-
|
| 220 |
-
### Gestures:
|
| 221 |
-
- π **One finger**: Drawing mode
|
| 222 |
-
- β **Fist**: Clear canvas
|
| 223 |
-
- π **Other**: Stop drawing
|
| 224 |
""")
|
| 225 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 226 |
# Color selection
|
| 227 |
st.sidebar.subheader("π¨ Brush Color")
|
| 228 |
color_options = {
|
|
@@ -292,32 +332,43 @@ class AirDrawingApp:
|
|
| 292 |
frame = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
| 293 |
|
| 294 |
# Process frame for hand detection
|
| 295 |
-
processed_frame,
|
| 296 |
|
| 297 |
-
# Update drawing
|
| 298 |
-
st.session_state.
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
if drawing_mode and finger_tip:
|
| 302 |
self.update_canvas(finger_tip)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 303 |
|
| 304 |
# Add status text to frame
|
| 305 |
-
cv2.putText(processed_frame,
|
|
|
|
|
|
|
| 306 |
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2)
|
| 307 |
-
cv2.putText(processed_frame, f"Color: {selected_color}", (10,
|
| 308 |
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2)
|
| 309 |
-
cv2.putText(processed_frame, f"Brush: {st.session_state.brush_size}px", (10,
|
| 310 |
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2)
|
| 311 |
|
| 312 |
# Convert back to RGB for display
|
| 313 |
display_frame = cv2.cvtColor(processed_frame, cv2.COLOR_BGR2RGB)
|
| 314 |
-
st.image(display_frame,
|
| 315 |
|
| 316 |
st.markdown('</div>', unsafe_allow_html=True)
|
| 317 |
|
| 318 |
# Status display
|
| 319 |
if camera_image:
|
| 320 |
-
st.
|
|
|
|
|
|
|
|
|
|
| 321 |
else:
|
| 322 |
st.info("π Click the camera button above to start!")
|
| 323 |
|
|
@@ -326,7 +377,7 @@ class AirDrawingApp:
|
|
| 326 |
st.markdown('<div class="canvas-container">', unsafe_allow_html=True)
|
| 327 |
|
| 328 |
# Display the canvas
|
| 329 |
-
st.image(st.session_state.canvas,
|
| 330 |
caption="Your air drawing appears here!")
|
| 331 |
|
| 332 |
st.markdown('</div>', unsafe_allow_html=True)
|
|
@@ -341,13 +392,19 @@ class AirDrawingApp:
|
|
| 341 |
|
| 342 |
with col_info2:
|
| 343 |
st.metric("Current Color", selected_color)
|
| 344 |
-
st.metric("Drawing Mode", "
|
| 345 |
|
| 346 |
# Drawing preview
|
| 347 |
if st.session_state.drawing_points:
|
| 348 |
st.subheader("π Live Preview")
|
|
|
|
| 349 |
preview_text = f"Drawing path: {len(st.session_state.drawing_points)} points"
|
| 350 |
-
st.progress(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 351 |
|
| 352 |
def save_drawing(self):
|
| 353 |
"""Save the current drawing"""
|
|
|
|
| 45 |
margin: 5px 0;
|
| 46 |
text-align: center;
|
| 47 |
}
|
| 48 |
+
.drawing-active {
|
| 49 |
+
background: #4CAF50;
|
| 50 |
+
color: white;
|
| 51 |
+
padding: 10px;
|
| 52 |
+
border-radius: 10px;
|
| 53 |
+
text-align: center;
|
| 54 |
+
font-weight: bold;
|
| 55 |
+
}
|
| 56 |
+
.drawing-inactive {
|
| 57 |
+
background: #F44336;
|
| 58 |
+
color: white;
|
| 59 |
+
padding: 10px;
|
| 60 |
+
border-radius: 10px;
|
| 61 |
+
text-align: center;
|
| 62 |
+
font-weight: bold;
|
| 63 |
+
}
|
| 64 |
.stButton button {
|
| 65 |
background: linear-gradient(45deg, #FF6B6B, #4ECDC4);
|
| 66 |
color: white;
|
|
|
|
| 104 |
if 'is_drawing' not in st.session_state:
|
| 105 |
st.session_state.is_drawing = False
|
| 106 |
|
| 107 |
+
if 'drawing_enabled' not in st.session_state:
|
| 108 |
+
st.session_state.drawing_enabled = False
|
| 109 |
+
|
| 110 |
if 'current_color' not in st.session_state:
|
| 111 |
st.session_state.current_color = (255, 0, 0) # Red
|
| 112 |
|
|
|
|
| 125 |
middle_tip = landmarks.landmark[self.mp_hands.HandLandmark.MIDDLE_FINGER_TIP]
|
| 126 |
ring_tip = landmarks.landmark[self.mp_hands.HandLandmark.RING_FINGER_TIP]
|
| 127 |
pinky_tip = landmarks.landmark[self.mp_hands.HandLandmark.PINKY_TIP]
|
|
|
|
| 128 |
|
| 129 |
# Get PIP joints (for finger extended detection)
|
| 130 |
index_pip = landmarks.landmark[self.mp_hands.HandLandmark.INDEX_FINGER_PIP]
|
|
|
|
| 143 |
|
| 144 |
# Determine drawing state
|
| 145 |
if extended_fingers == 1 and index_extended:
|
| 146 |
+
# Only index finger extended - ready to draw
|
| 147 |
+
gesture = "drawing_ready"
|
| 148 |
+
status = "π Ready to draw - Move your finger!"
|
| 149 |
elif extended_fingers == 0:
|
| 150 |
# Fist - clear mode
|
| 151 |
+
gesture = "fist"
|
| 152 |
+
status = "β Make a fist to clear canvas"
|
|
|
|
|
|
|
| 153 |
else:
|
| 154 |
+
# Other gestures - not ready
|
| 155 |
+
gesture = "other"
|
| 156 |
status = "π Show only index finger to draw"
|
|
|
|
| 157 |
|
| 158 |
# Get index finger tip position
|
| 159 |
finger_tip = (int(index_tip.x * w), int(index_tip.y * h))
|
| 160 |
|
| 161 |
+
return gesture, finger_tip, status
|
| 162 |
|
| 163 |
def update_canvas(self, current_point):
|
| 164 |
"""Update the canvas with new drawing points"""
|
|
|
|
| 187 |
st.session_state.last_point = None
|
| 188 |
st.session_state.is_drawing = False
|
| 189 |
|
| 190 |
+
def toggle_drawing(self):
|
| 191 |
+
"""Toggle drawing mode on/off"""
|
| 192 |
+
st.session_state.drawing_enabled = not st.session_state.drawing_enabled
|
| 193 |
+
if not st.session_state.drawing_enabled:
|
| 194 |
+
st.session_state.last_point = None # Reset last point when stopping
|
| 195 |
+
|
| 196 |
def process_frame(self, frame):
|
| 197 |
"""Process camera frame and detect hand gestures"""
|
| 198 |
# Convert BGR to RGB
|
|
|
|
| 202 |
results = self.hands.process(rgb_frame)
|
| 203 |
|
| 204 |
status = "π Show your hand to the camera"
|
| 205 |
+
gesture = "none"
|
| 206 |
finger_tip = None
|
| 207 |
|
| 208 |
if results.multi_hand_landmarks:
|
|
|
|
| 217 |
)
|
| 218 |
|
| 219 |
# Detect gesture and get finger tip
|
| 220 |
+
gesture, finger_tip, status = self.detect_hand_gesture(hand_landmarks, frame.shape)
|
| 221 |
|
| 222 |
# Draw finger tip circle
|
| 223 |
if finger_tip:
|
| 224 |
+
if st.session_state.drawing_enabled and gesture == "drawing_ready":
|
| 225 |
+
cv2.circle(frame, finger_tip, 10, (0, 255, 0), -1) # Green when drawing
|
| 226 |
+
cv2.putText(frame, "DRAWING", (finger_tip[0] + 15, finger_tip[1]),
|
| 227 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
|
| 228 |
+
else:
|
| 229 |
+
cv2.circle(frame, finger_tip, 10, (255, 0, 0), -1) # Blue when not drawing
|
| 230 |
+
cv2.putText(frame, "READY", (finger_tip[0] + 15, finger_tip[1]),
|
| 231 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
|
| 232 |
|
| 233 |
+
return frame, gesture, finger_tip, status
|
| 234 |
|
| 235 |
def run(self):
|
| 236 |
# Sidebar controls
|
|
|
|
| 238 |
|
| 239 |
st.sidebar.markdown("""
|
| 240 |
### How to Draw:
|
| 241 |
+
1. **Click 'Start Drawing' button** to enable drawing
|
| 242 |
+
2. **Show your hand** to the camera
|
| 243 |
+
3. **Point with index finger** to draw
|
| 244 |
+
4. **Move your finger** in the air to draw
|
| 245 |
+
5. **Click 'Stop Drawing'** when done
|
| 246 |
+
6. **Make a fist** to clear canvas
|
|
|
|
|
|
|
|
|
|
|
|
|
| 247 |
""")
|
| 248 |
|
| 249 |
+
# Drawing toggle button
|
| 250 |
+
st.sidebar.subheader("π― Drawing Control")
|
| 251 |
+
if st.session_state.drawing_enabled:
|
| 252 |
+
if st.sidebar.button("π Stop Drawing", use_container_width=True, type="primary"):
|
| 253 |
+
self.toggle_drawing()
|
| 254 |
+
st.rerun()
|
| 255 |
+
else:
|
| 256 |
+
if st.sidebar.button("π¨ Start Drawing", use_container_width=True, type="primary"):
|
| 257 |
+
self.toggle_drawing()
|
| 258 |
+
st.rerun()
|
| 259 |
+
|
| 260 |
+
# Drawing status
|
| 261 |
+
if st.session_state.drawing_enabled:
|
| 262 |
+
st.sidebar.markdown('<div class="drawing-active">π’ DRAWING ACTIVE</div>', unsafe_allow_html=True)
|
| 263 |
+
else:
|
| 264 |
+
st.sidebar.markdown('<div class="drawing-inactive">π΄ DRAWING INACTIVE</div>', unsafe_allow_html=True)
|
| 265 |
+
|
| 266 |
# Color selection
|
| 267 |
st.sidebar.subheader("π¨ Brush Color")
|
| 268 |
color_options = {
|
|
|
|
| 332 |
frame = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
| 333 |
|
| 334 |
# Process frame for hand detection
|
| 335 |
+
processed_frame, gesture, finger_tip, status = self.process_frame(frame)
|
| 336 |
|
| 337 |
+
# Update canvas if drawing is enabled and in drawing gesture
|
| 338 |
+
if (st.session_state.drawing_enabled and
|
| 339 |
+
gesture == "drawing_ready" and
|
| 340 |
+
finger_tip is not None):
|
|
|
|
| 341 |
self.update_canvas(finger_tip)
|
| 342 |
+
drawing_status = "π¨ DRAWING IN PROGRESS!"
|
| 343 |
+
elif gesture == "fist":
|
| 344 |
+
self.clear_canvas()
|
| 345 |
+
drawing_status = "β Canvas cleared!"
|
| 346 |
+
else:
|
| 347 |
+
drawing_status = status
|
| 348 |
+
st.session_state.last_point = None # Reset when not drawing
|
| 349 |
|
| 350 |
# Add status text to frame
|
| 351 |
+
cv2.putText(processed_frame, drawing_status, (10, 30),
|
| 352 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2)
|
| 353 |
+
cv2.putText(processed_frame, f"Drawing: {'ON' if st.session_state.drawing_enabled else 'OFF'}", (10, 60),
|
| 354 |
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2)
|
| 355 |
+
cv2.putText(processed_frame, f"Color: {selected_color}", (10, 90),
|
| 356 |
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2)
|
| 357 |
+
cv2.putText(processed_frame, f"Brush: {st.session_state.brush_size}px", (10, 120),
|
| 358 |
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2)
|
| 359 |
|
| 360 |
# Convert back to RGB for display
|
| 361 |
display_frame = cv2.cvtColor(processed_frame, cv2.COLOR_BGR2RGB)
|
| 362 |
+
st.image(display_frame, use_container_width=True) # FIXED: use_container_width instead of use_column_width
|
| 363 |
|
| 364 |
st.markdown('</div>', unsafe_allow_html=True)
|
| 365 |
|
| 366 |
# Status display
|
| 367 |
if camera_image:
|
| 368 |
+
if st.session_state.drawing_enabled:
|
| 369 |
+
st.markdown(f'<div class="drawing-active">{drawing_status}</div>', unsafe_allow_html=True)
|
| 370 |
+
else:
|
| 371 |
+
st.markdown(f'<div class="status-box">{drawing_status}</div>', unsafe_allow_html=True)
|
| 372 |
else:
|
| 373 |
st.info("π Click the camera button above to start!")
|
| 374 |
|
|
|
|
| 377 |
st.markdown('<div class="canvas-container">', unsafe_allow_html=True)
|
| 378 |
|
| 379 |
# Display the canvas
|
| 380 |
+
st.image(st.session_state.canvas, use_container_width=True, # FIXED: use_container_width instead of use_column_width
|
| 381 |
caption="Your air drawing appears here!")
|
| 382 |
|
| 383 |
st.markdown('</div>', unsafe_allow_html=True)
|
|
|
|
| 392 |
|
| 393 |
with col_info2:
|
| 394 |
st.metric("Current Color", selected_color)
|
| 395 |
+
st.metric("Drawing Mode", "ACTIVE" if st.session_state.drawing_enabled else "INACTIVE")
|
| 396 |
|
| 397 |
# Drawing preview
|
| 398 |
if st.session_state.drawing_points:
|
| 399 |
st.subheader("π Live Preview")
|
| 400 |
+
progress_value = min(len(st.session_state.drawing_points) / 100, 1.0)
|
| 401 |
preview_text = f"Drawing path: {len(st.session_state.drawing_points)} points"
|
| 402 |
+
st.progress(progress_value, text=preview_text)
|
| 403 |
+
|
| 404 |
+
# Show recent points
|
| 405 |
+
if len(st.session_state.drawing_points) > 1:
|
| 406 |
+
recent_points = st.session_state.drawing_points[-5:]
|
| 407 |
+
st.write(f"Recent points: {recent_points[-1]}")
|
| 408 |
|
| 409 |
def save_drawing(self):
|
| 410 |
"""Save the current drawing"""
|