Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,7 @@ from PIL import Image, ImageDraw
|
|
| 6 |
import time
|
| 7 |
import io
|
| 8 |
|
| 9 |
-
class
|
| 10 |
def __init__(self):
|
| 11 |
self.setup_page()
|
| 12 |
self.setup_mediapipe()
|
|
@@ -43,76 +43,95 @@ class WorkingAirDrawingApp:
|
|
| 43 |
border-radius: 10px;
|
| 44 |
text-align: center;
|
| 45 |
margin: 10px 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
}
|
| 47 |
</style>
|
| 48 |
""", unsafe_allow_html=True)
|
| 49 |
|
| 50 |
-
st.markdown('<h1 class="main-title">โจ Air Drawing Canvas</h1>', unsafe_allow_html=True)
|
| 51 |
-
st.markdown("###
|
| 52 |
|
| 53 |
def setup_mediapipe(self):
|
| 54 |
self.mp_hands = mp.solutions.hands
|
| 55 |
self.mp_drawing = mp.solutions.drawing_utils
|
|
|
|
|
|
|
|
|
|
| 56 |
self.hands = self.mp_hands.Hands(
|
| 57 |
-
static_image_mode=False,
|
| 58 |
max_num_hands=1,
|
| 59 |
-
|
| 60 |
-
|
|
|
|
| 61 |
)
|
| 62 |
|
| 63 |
def initialize_canvas(self):
|
| 64 |
"""Initialize canvas and session state"""
|
| 65 |
if 'canvas' not in st.session_state:
|
| 66 |
-
st.session_state.canvas = Image.new('RGB', (
|
| 67 |
st.session_state.draw = ImageDraw.Draw(st.session_state.canvas)
|
| 68 |
|
| 69 |
if 'last_point' not in st.session_state:
|
| 70 |
st.session_state.last_point = None
|
| 71 |
|
| 72 |
if 'current_color' not in st.session_state:
|
| 73 |
-
st.session_state.current_color = (255, 0, 0)
|
| 74 |
|
| 75 |
if 'brush_size' not in st.session_state:
|
| 76 |
-
st.session_state.brush_size =
|
| 77 |
|
| 78 |
if 'is_drawing' not in st.session_state:
|
| 79 |
st.session_state.is_drawing = False
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
-
def
|
| 82 |
-
"""
|
| 83 |
-
h, w = frame_shape
|
| 84 |
-
|
| 85 |
-
# Get index finger tip landmark
|
| 86 |
-
index_tip = landmarks.landmark[self.mp_hands.HandLandmark.INDEX_FINGER_TIP]
|
| 87 |
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
-
def
|
| 95 |
-
"""Check if
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
| 116 |
|
| 117 |
def update_canvas(self, finger_tip, frame_shape):
|
| 118 |
"""Update canvas with finger movement"""
|
|
@@ -120,106 +139,151 @@ class WorkingAirDrawingApp:
|
|
| 120 |
return
|
| 121 |
|
| 122 |
# Scale coordinates from camera to canvas
|
| 123 |
-
canvas_x = int(finger_tip[0] *
|
| 124 |
-
canvas_y = int(finger_tip[1] *
|
| 125 |
current_point = (canvas_x, canvas_y)
|
| 126 |
|
| 127 |
-
# Draw
|
| 128 |
if st.session_state.last_point and st.session_state.is_drawing:
|
| 129 |
st.session_state.draw.line(
|
| 130 |
[st.session_state.last_point, current_point],
|
| 131 |
fill=st.session_state.current_color,
|
| 132 |
-
width=st.session_state.brush_size
|
|
|
|
| 133 |
)
|
| 134 |
|
| 135 |
st.session_state.last_point = current_point
|
| 136 |
|
| 137 |
def clear_canvas(self):
|
| 138 |
"""Clear the canvas"""
|
| 139 |
-
st.session_state.canvas = Image.new('RGB', (
|
| 140 |
st.session_state.draw = ImageDraw.Draw(st.session_state.canvas)
|
| 141 |
st.session_state.last_point = None
|
| 142 |
st.session_state.is_drawing = False
|
|
|
|
| 143 |
|
| 144 |
def process_frame(self, frame):
|
| 145 |
-
"""Process frame for hand detection
|
| 146 |
# Convert BGR to RGB for MediaPipe
|
| 147 |
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
results = self.hands.process(rgb_frame)
|
| 149 |
|
| 150 |
finger_tip = None
|
| 151 |
status = "๐ Show your hand to camera"
|
| 152 |
-
|
|
|
|
| 153 |
|
| 154 |
if results.multi_hand_landmarks:
|
| 155 |
for hand_landmarks in results.multi_hand_landmarks:
|
| 156 |
-
# Draw hand landmarks
|
| 157 |
self.mp_drawing.draw_landmarks(
|
| 158 |
frame,
|
| 159 |
hand_landmarks,
|
| 160 |
-
self.mp_hands.HAND_CONNECTIONS
|
|
|
|
|
|
|
| 161 |
)
|
| 162 |
|
| 163 |
-
# Check if
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
|
| 169 |
-
# Draw circle on finger tip
|
| 170 |
-
cv2.circle(frame, finger_tip, 10, (0, 255, 0), -1)
|
| 171 |
else:
|
| 172 |
-
drawing_mode = False
|
| 173 |
status = "๐ Point with index finger to draw"
|
| 174 |
-
|
| 175 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
|
| 177 |
def run(self):
|
| 178 |
# Sidebar
|
| 179 |
st.sidebar.title("๐ฎ Controls")
|
| 180 |
|
| 181 |
st.sidebar.markdown("""
|
| 182 |
-
### How to
|
| 183 |
-
1. **
|
| 184 |
-
2. **
|
| 185 |
-
3. **
|
| 186 |
-
4. **
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
""")
|
| 188 |
|
| 189 |
# Color selection
|
| 190 |
st.sidebar.subheader("๐จ Colors")
|
| 191 |
colors = {
|
| 192 |
-
"Red": (255, 0, 0),
|
| 193 |
-
"Blue": (0, 0, 255),
|
| 194 |
-
"Green": (0, 255, 0),
|
| 195 |
-
"Purple": (128, 0, 128),
|
| 196 |
-
"Black": (0, 0, 0)
|
|
|
|
| 197 |
}
|
| 198 |
|
| 199 |
selected_color = st.sidebar.selectbox("Choose color:", list(colors.keys()))
|
| 200 |
st.session_state.current_color = colors[selected_color]
|
| 201 |
|
| 202 |
# Brush size
|
| 203 |
-
st.sidebar.subheader("๐๏ธ Brush")
|
| 204 |
-
st.session_state.brush_size = st.sidebar.slider("Size:",
|
| 205 |
|
| 206 |
# Control buttons
|
| 207 |
st.sidebar.subheader("โก Actions")
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
|
| 215 |
-
# Main content
|
| 216 |
col1, col2 = st.columns(2)
|
| 217 |
|
| 218 |
with col1:
|
| 219 |
st.subheader("๐น Camera View")
|
|
|
|
| 220 |
st.markdown('<div class="container">', unsafe_allow_html=True)
|
| 221 |
|
| 222 |
-
camera_image = st.camera_input("Start drawing...", key="
|
| 223 |
|
| 224 |
if camera_image:
|
| 225 |
# Convert to OpenCV
|
|
@@ -227,69 +291,90 @@ class WorkingAirDrawingApp:
|
|
| 227 |
frame = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
| 228 |
|
| 229 |
# Process frame
|
| 230 |
-
processed_frame,
|
| 231 |
|
| 232 |
-
# Update
|
| 233 |
-
st.session_state.is_drawing =
|
|
|
|
| 234 |
|
| 235 |
-
# Update canvas if
|
| 236 |
-
if
|
| 237 |
-
self.update_canvas(finger_tip,
|
| 238 |
else:
|
| 239 |
-
st.session_state.last_point = None
|
| 240 |
|
| 241 |
-
# Add status
|
| 242 |
-
cv2.putText(processed_frame, status, (
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 2)
|
| 244 |
-
cv2.putText(processed_frame, f"Brush: {st.session_state.brush_size}px", (10, 60),
|
| 245 |
-
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2)
|
| 246 |
|
| 247 |
# Display frame
|
| 248 |
display_frame = cv2.cvtColor(processed_frame, cv2.COLOR_BGR2RGB)
|
| 249 |
st.image(display_frame, use_container_width=True)
|
| 250 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 251 |
st.markdown('</div>', unsafe_allow_html=True)
|
| 252 |
|
| 253 |
-
# Status
|
| 254 |
if camera_image:
|
| 255 |
st.markdown(f'<div class="status-box">{status}</div>', unsafe_allow_html=True)
|
| 256 |
-
else:
|
| 257 |
-
st.info("๐ Click the camera to start!")
|
| 258 |
|
| 259 |
with col2:
|
| 260 |
st.subheader("๐จ Drawing Canvas")
|
|
|
|
| 261 |
st.markdown('<div class="container">', unsafe_allow_html=True)
|
| 262 |
|
| 263 |
# Display canvas
|
| 264 |
st.image(st.session_state.canvas, use_container_width=True,
|
| 265 |
-
caption="Your drawing appears here!")
|
| 266 |
|
| 267 |
st.markdown('</div>', unsafe_allow_html=True)
|
| 268 |
|
| 269 |
-
#
|
| 270 |
-
st.
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 274 |
def save_drawing(self):
|
| 275 |
-
"""Save the drawing"""
|
| 276 |
-
timestamp =
|
| 277 |
-
filename = f"
|
| 278 |
|
| 279 |
-
# Convert to bytes
|
| 280 |
img_buffer = io.BytesIO()
|
| 281 |
st.session_state.canvas.save(img_buffer, format='PNG')
|
| 282 |
|
|
|
|
| 283 |
st.sidebar.download_button(
|
| 284 |
-
label="๐ฅ Download Drawing",
|
| 285 |
data=img_buffer.getvalue(),
|
| 286 |
file_name=filename,
|
| 287 |
mime="image/png",
|
| 288 |
use_container_width=True
|
| 289 |
)
|
|
|
|
|
|
|
| 290 |
|
| 291 |
def main():
|
| 292 |
-
app =
|
| 293 |
app.run()
|
| 294 |
|
| 295 |
if __name__ == "__main__":
|
|
|
|
| 6 |
import time
|
| 7 |
import io
|
| 8 |
|
| 9 |
+
class FixedAirDrawingApp:
|
| 10 |
def __init__(self):
|
| 11 |
self.setup_page()
|
| 12 |
self.setup_mediapipe()
|
|
|
|
| 43 |
border-radius: 10px;
|
| 44 |
text-align: center;
|
| 45 |
margin: 10px 0;
|
| 46 |
+
font-weight: bold;
|
| 47 |
+
}
|
| 48 |
+
.debug-info {
|
| 49 |
+
background: #FF6B6B;
|
| 50 |
+
color: white;
|
| 51 |
+
padding: 10px;
|
| 52 |
+
border-radius: 5px;
|
| 53 |
+
font-family: monospace;
|
| 54 |
}
|
| 55 |
</style>
|
| 56 |
""", unsafe_allow_html=True)
|
| 57 |
|
| 58 |
+
st.markdown('<h1 class="main-title">โจ Air Drawing Canvas - FIXED</h1>', unsafe_allow_html=True)
|
| 59 |
+
st.markdown("### Point with index finger - Green dot WILL appear! ๐ฏ")
|
| 60 |
|
| 61 |
def setup_mediapipe(self):
|
| 62 |
self.mp_hands = mp.solutions.hands
|
| 63 |
self.mp_drawing = mp.solutions.drawing_utils
|
| 64 |
+
self.mp_drawing_styles = mp.solutions.drawing_styles
|
| 65 |
+
|
| 66 |
+
# Enhanced hand detection with better parameters
|
| 67 |
self.hands = self.mp_hands.Hands(
|
| 68 |
+
static_image_mode=False, # Changed to False for video
|
| 69 |
max_num_hands=1,
|
| 70 |
+
model_complexity=1, # Added for better accuracy
|
| 71 |
+
min_detection_confidence=0.5, # Lowered threshold
|
| 72 |
+
min_tracking_confidence=0.5 # Lowered threshold
|
| 73 |
)
|
| 74 |
|
| 75 |
def initialize_canvas(self):
|
| 76 |
"""Initialize canvas and session state"""
|
| 77 |
if 'canvas' not in st.session_state:
|
| 78 |
+
st.session_state.canvas = Image.new('RGB', (800, 600), 'white')
|
| 79 |
st.session_state.draw = ImageDraw.Draw(st.session_state.canvas)
|
| 80 |
|
| 81 |
if 'last_point' not in st.session_state:
|
| 82 |
st.session_state.last_point = None
|
| 83 |
|
| 84 |
if 'current_color' not in st.session_state:
|
| 85 |
+
st.session_state.current_color = (255, 0, 0) # Red
|
| 86 |
|
| 87 |
if 'brush_size' not in st.session_state:
|
| 88 |
+
st.session_state.brush_size = 10
|
| 89 |
|
| 90 |
if 'is_drawing' not in st.session_state:
|
| 91 |
st.session_state.is_drawing = False
|
| 92 |
+
|
| 93 |
+
if 'debug_info' not in st.session_state:
|
| 94 |
+
st.session_state.debug_info = "Waiting for camera..."
|
| 95 |
|
| 96 |
+
def get_finger_tip(self, landmarks, frame_shape):
|
| 97 |
+
"""Get index finger tip position with better detection"""
|
| 98 |
+
h, w, c = frame_shape
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
+
try:
|
| 101 |
+
# Get index finger tip (landmark 8)
|
| 102 |
+
index_tip = landmarks.landmark[8]
|
| 103 |
+
x = int(index_tip.x * w)
|
| 104 |
+
y = int(index_tip.y * h)
|
| 105 |
+
|
| 106 |
+
return (x, y)
|
| 107 |
+
except:
|
| 108 |
+
return None
|
| 109 |
|
| 110 |
+
def is_pointing_gesture(self, landmarks):
|
| 111 |
+
"""Check if hand is making a pointing gesture"""
|
| 112 |
+
try:
|
| 113 |
+
# Get relevant landmarks
|
| 114 |
+
index_tip = landmarks.landmark[8]
|
| 115 |
+
middle_tip = landmarks.landmark[12]
|
| 116 |
+
ring_tip = landmarks.landmark[16]
|
| 117 |
+
pinky_tip = landmarks.landmark[20]
|
| 118 |
+
|
| 119 |
+
# Get PIP joints (second knuckles)
|
| 120 |
+
index_pip = landmarks.landmark[6]
|
| 121 |
+
middle_pip = landmarks.landmark[10]
|
| 122 |
+
ring_pip = landmarks.landmark[14]
|
| 123 |
+
pinky_pip = landmarks.landmark[18]
|
| 124 |
+
|
| 125 |
+
# Check if index finger is extended and others are folded
|
| 126 |
+
index_extended = index_tip.y < index_pip.y # Tip above PIP
|
| 127 |
+
middle_folded = middle_tip.y > middle_pip.y # Tip below PIP
|
| 128 |
+
ring_folded = ring_tip.y > ring_pip.y
|
| 129 |
+
pinky_folded = pinky_tip.y > pinky_pip.y
|
| 130 |
+
|
| 131 |
+
return index_extended and middle_folded and ring_folded and pinky_folded
|
| 132 |
+
|
| 133 |
+
except Exception as e:
|
| 134 |
+
return False
|
| 135 |
|
| 136 |
def update_canvas(self, finger_tip, frame_shape):
|
| 137 |
"""Update canvas with finger movement"""
|
|
|
|
| 139 |
return
|
| 140 |
|
| 141 |
# Scale coordinates from camera to canvas
|
| 142 |
+
canvas_x = int(finger_tip[0] * 800 / frame_shape[1])
|
| 143 |
+
canvas_y = int(finger_tip[1] * 600 / frame_shape[0])
|
| 144 |
current_point = (canvas_x, canvas_y)
|
| 145 |
|
| 146 |
+
# Draw line from last point to current point
|
| 147 |
if st.session_state.last_point and st.session_state.is_drawing:
|
| 148 |
st.session_state.draw.line(
|
| 149 |
[st.session_state.last_point, current_point],
|
| 150 |
fill=st.session_state.current_color,
|
| 151 |
+
width=st.session_state.brush_size,
|
| 152 |
+
joint="curve"
|
| 153 |
)
|
| 154 |
|
| 155 |
st.session_state.last_point = current_point
|
| 156 |
|
| 157 |
def clear_canvas(self):
|
| 158 |
"""Clear the canvas"""
|
| 159 |
+
st.session_state.canvas = Image.new('RGB', (800, 600), 'white')
|
| 160 |
st.session_state.draw = ImageDraw.Draw(st.session_state.canvas)
|
| 161 |
st.session_state.last_point = None
|
| 162 |
st.session_state.is_drawing = False
|
| 163 |
+
st.session_state.debug_info = "Canvas cleared!"
|
| 164 |
|
| 165 |
def process_frame(self, frame):
|
| 166 |
+
"""Process frame for hand detection with enhanced visualization"""
|
| 167 |
# Convert BGR to RGB for MediaPipe
|
| 168 |
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 169 |
+
|
| 170 |
+
# Flip frame horizontally for mirror effect
|
| 171 |
+
rgb_frame = cv2.flip(rgb_frame, 1)
|
| 172 |
+
frame = cv2.flip(frame, 1)
|
| 173 |
+
|
| 174 |
results = self.hands.process(rgb_frame)
|
| 175 |
|
| 176 |
finger_tip = None
|
| 177 |
status = "๐ Show your hand to camera"
|
| 178 |
+
is_pointing = False
|
| 179 |
+
debug_info = "No hand detected"
|
| 180 |
|
| 181 |
if results.multi_hand_landmarks:
|
| 182 |
for hand_landmarks in results.multi_hand_landmarks:
|
| 183 |
+
# Draw ALL hand landmarks (for debugging)
|
| 184 |
self.mp_drawing.draw_landmarks(
|
| 185 |
frame,
|
| 186 |
hand_landmarks,
|
| 187 |
+
self.mp_hands.HAND_CONNECTIONS,
|
| 188 |
+
self.mp_drawing_styles.get_default_hand_landmarks_style(),
|
| 189 |
+
self.mp_drawing_styles.get_default_hand_connections_style()
|
| 190 |
)
|
| 191 |
|
| 192 |
+
# Check if pointing gesture
|
| 193 |
+
is_pointing = self.is_pointing_gesture(hand_landmarks)
|
| 194 |
+
finger_tip = self.get_finger_tip(hand_landmarks, frame.shape)
|
| 195 |
+
|
| 196 |
+
if is_pointing and finger_tip:
|
| 197 |
+
status = "๐จ DRAWING MODE - Move your finger!"
|
| 198 |
+
debug_info = f"Pointing! Finger at: {finger_tip}"
|
| 199 |
+
|
| 200 |
+
# Draw LARGE green dot on finger tip
|
| 201 |
+
cv2.circle(frame, finger_tip, 15, (0, 255, 0), -1) # Green fill
|
| 202 |
+
cv2.circle(frame, finger_tip, 8, (255, 255, 255), -1) # White inner
|
| 203 |
+
cv2.circle(frame, finger_tip, 20, (0, 255, 0), 2) # Green outline
|
| 204 |
|
|
|
|
|
|
|
| 205 |
else:
|
|
|
|
| 206 |
status = "๐ Point with index finger to draw"
|
| 207 |
+
debug_info = "Hand detected but not pointing"
|
| 208 |
+
|
| 209 |
+
else:
|
| 210 |
+
debug_info = "No hand landmarks detected - try moving closer"
|
| 211 |
+
|
| 212 |
+
return frame, is_pointing, finger_tip, status, debug_info
|
| 213 |
|
| 214 |
def run(self):
|
| 215 |
# Sidebar
|
| 216 |
st.sidebar.title("๐ฎ Controls")
|
| 217 |
|
| 218 |
st.sidebar.markdown("""
|
| 219 |
+
### ๐ฏ How to Make it Work:
|
| 220 |
+
1. **Good lighting** - Face a light source
|
| 221 |
+
2. **Show entire hand** - Palm facing camera
|
| 222 |
+
3. **Point index finger** - Keep other fingers folded
|
| 223 |
+
4. **Move slowly** - Smooth movements work better
|
| 224 |
+
5. **Stay close** - 1-2 feet from camera
|
| 225 |
+
|
| 226 |
+
### โจ Gesture:
|
| 227 |
+
- **Index finger extended**
|
| 228 |
+
- **Other fingers folded**
|
| 229 |
+
- **Palm facing camera**
|
| 230 |
""")
|
| 231 |
|
| 232 |
# Color selection
|
| 233 |
st.sidebar.subheader("๐จ Colors")
|
| 234 |
colors = {
|
| 235 |
+
"Red ๐ด": (255, 0, 0),
|
| 236 |
+
"Blue ๐ต": (0, 0, 255),
|
| 237 |
+
"Green ๐ข": (0, 255, 0),
|
| 238 |
+
"Purple ๐ฃ": (128, 0, 128),
|
| 239 |
+
"Black โซ": (0, 0, 0),
|
| 240 |
+
"Orange ๐ ": (255, 165, 0)
|
| 241 |
}
|
| 242 |
|
| 243 |
selected_color = st.sidebar.selectbox("Choose color:", list(colors.keys()))
|
| 244 |
st.session_state.current_color = colors[selected_color]
|
| 245 |
|
| 246 |
# Brush size
|
| 247 |
+
st.sidebar.subheader("๐๏ธ Brush Settings")
|
| 248 |
+
st.session_state.brush_size = st.sidebar.slider("Size:", 5, 25, 12)
|
| 249 |
|
| 250 |
# Control buttons
|
| 251 |
st.sidebar.subheader("โก Actions")
|
| 252 |
+
col1, col2 = st.sidebar.columns(2)
|
| 253 |
+
|
| 254 |
+
with col1:
|
| 255 |
+
if st.button("๐๏ธ Clear Canvas", use_container_width=True):
|
| 256 |
+
self.clear_canvas()
|
| 257 |
+
st.rerun()
|
| 258 |
+
|
| 259 |
+
with col_2:
|
| 260 |
+
if st.button("๐ง Test Camera", use_container_width=True):
|
| 261 |
+
st.session_state.debug_info = "Camera test requested..."
|
| 262 |
+
st.rerun()
|
| 263 |
+
|
| 264 |
+
# Debug info in sidebar
|
| 265 |
+
st.sidebar.subheader("๐ง Debug Info")
|
| 266 |
+
st.sidebar.code(st.session_state.debug_info)
|
| 267 |
+
|
| 268 |
+
# Quick tips
|
| 269 |
+
st.sidebar.subheader("๐ก Quick Tips")
|
| 270 |
+
st.sidebar.info("""
|
| 271 |
+
- Ensure good lighting
|
| 272 |
+
- Show entire hand clearly
|
| 273 |
+
- Point only index finger
|
| 274 |
+
- Keep hand steady
|
| 275 |
+
- Move slowly for smooth lines
|
| 276 |
+
""")
|
| 277 |
|
| 278 |
+
# Main content
|
| 279 |
col1, col2 = st.columns(2)
|
| 280 |
|
| 281 |
with col1:
|
| 282 |
st.subheader("๐น Camera View")
|
| 283 |
+
st.markdown("**Look for the GREEN DOT on your finger tip!**")
|
| 284 |
st.markdown('<div class="container">', unsafe_allow_html=True)
|
| 285 |
|
| 286 |
+
camera_image = st.camera_input("Start drawing...", key="main_camera")
|
| 287 |
|
| 288 |
if camera_image:
|
| 289 |
# Convert to OpenCV
|
|
|
|
| 291 |
frame = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
| 292 |
|
| 293 |
# Process frame
|
| 294 |
+
processed_frame, is_pointing, finger_tip, status, debug_info = self.process_frame(frame)
|
| 295 |
|
| 296 |
+
# Update session state
|
| 297 |
+
st.session_state.is_drawing = is_pointing
|
| 298 |
+
st.session_state.debug_info = debug_info
|
| 299 |
|
| 300 |
+
# Update canvas if pointing
|
| 301 |
+
if is_pointing and finger_tip:
|
| 302 |
+
self.update_canvas(finger_tip, processed_frame.shape)
|
| 303 |
else:
|
| 304 |
+
st.session_state.last_point = None # Reset when not pointing
|
| 305 |
|
| 306 |
+
# Add status overlay
|
| 307 |
+
cv2.putText(processed_frame, status, (20, 40),
|
| 308 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 255, 255), 2)
|
| 309 |
+
cv2.putText(processed_frame, f"Brush: {st.session_state.brush_size}px", (20, 80),
|
| 310 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 2)
|
| 311 |
+
cv2.putText(processed_frame, f"Color: {selected_color}", (20, 120),
|
| 312 |
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 2)
|
|
|
|
|
|
|
| 313 |
|
| 314 |
# Display frame
|
| 315 |
display_frame = cv2.cvtColor(processed_frame, cv2.COLOR_BGR2RGB)
|
| 316 |
st.image(display_frame, use_container_width=True)
|
| 317 |
|
| 318 |
+
else:
|
| 319 |
+
st.info("๐ **Click the camera button above to start!**")
|
| 320 |
+
st.session_state.debug_info = "Waiting for camera input..."
|
| 321 |
+
|
| 322 |
st.markdown('</div>', unsafe_allow_html=True)
|
| 323 |
|
| 324 |
+
# Status display
|
| 325 |
if camera_image:
|
| 326 |
st.markdown(f'<div class="status-box">{status}</div>', unsafe_allow_html=True)
|
|
|
|
|
|
|
| 327 |
|
| 328 |
with col2:
|
| 329 |
st.subheader("๐จ Drawing Canvas")
|
| 330 |
+
st.markdown("**Your drawing will appear here!**")
|
| 331 |
st.markdown('<div class="container">', unsafe_allow_html=True)
|
| 332 |
|
| 333 |
# Display canvas
|
| 334 |
st.image(st.session_state.canvas, use_container_width=True,
|
| 335 |
+
caption="โจ Your air drawing appears here!")
|
| 336 |
|
| 337 |
st.markdown('</div>', unsafe_allow_html=True)
|
| 338 |
|
| 339 |
+
# Drawing info
|
| 340 |
+
st.subheader("๐ Drawing Info")
|
| 341 |
+
col_info1, col_info2 = st.columns(2)
|
| 342 |
+
|
| 343 |
+
with col_info1:
|
| 344 |
+
st.metric("Drawing Mode", "ACTIVE ๐ข" if st.session_state.is_drawing else "INACTIVE ๐ด")
|
| 345 |
+
st.metric("Brush Size", f"{st.session_state.brush_size}px")
|
| 346 |
+
|
| 347 |
+
with col_info2:
|
| 348 |
+
st.metric("Current Color", selected_color)
|
| 349 |
+
st.metric("Canvas Size", "800ร600")
|
| 350 |
+
|
| 351 |
+
# Save functionality
|
| 352 |
+
st.subheader("๐พ Save Drawing")
|
| 353 |
+
if st.button("๐ซ Download Masterpiece", use_container_width=True):
|
| 354 |
+
self.save_drawing()
|
| 355 |
+
|
| 356 |
def save_drawing(self):
|
| 357 |
+
"""Save the drawing as downloadable file"""
|
| 358 |
+
timestamp = time.strftime("%Y%m%d_%H%M%S")
|
| 359 |
+
filename = f"air_drawing_{timestamp}.png"
|
| 360 |
|
| 361 |
+
# Convert canvas to bytes
|
| 362 |
img_buffer = io.BytesIO()
|
| 363 |
st.session_state.canvas.save(img_buffer, format='PNG')
|
| 364 |
|
| 365 |
+
# Create download button
|
| 366 |
st.sidebar.download_button(
|
| 367 |
+
label="๐ฅ Download Your Drawing",
|
| 368 |
data=img_buffer.getvalue(),
|
| 369 |
file_name=filename,
|
| 370 |
mime="image/png",
|
| 371 |
use_container_width=True
|
| 372 |
)
|
| 373 |
+
|
| 374 |
+
st.sidebar.success(f"โ
Ready to download: {filename}")
|
| 375 |
|
| 376 |
def main():
|
| 377 |
+
app = FixedAirDrawingApp()
|
| 378 |
app.run()
|
| 379 |
|
| 380 |
if __name__ == "__main__":
|