Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,365 +1,292 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
-
|
| 5 |
-
import
|
| 6 |
-
import
|
| 7 |
-
import os
|
| 8 |
-
from pathlib import Path
|
| 9 |
|
| 10 |
-
class
|
| 11 |
def __init__(self):
|
| 12 |
self.setup_page()
|
| 13 |
-
self.
|
|
|
|
| 14 |
|
| 15 |
def setup_page(self):
|
| 16 |
st.set_page_config(
|
| 17 |
-
page_title="
|
| 18 |
-
page_icon="
|
| 19 |
-
layout="wide"
|
| 20 |
-
initial_sidebar_state="expanded"
|
| 21 |
)
|
| 22 |
|
| 23 |
-
# Custom CSS for better styling
|
| 24 |
st.markdown("""
|
| 25 |
<style>
|
| 26 |
-
.main-
|
| 27 |
-
font-size:
|
| 28 |
-
|
|
|
|
|
|
|
| 29 |
text-align: center;
|
| 30 |
-
margin-bottom:
|
| 31 |
font-weight: bold;
|
| 32 |
-
text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
|
| 33 |
}
|
| 34 |
-
.
|
| 35 |
border: 3px solid #4ECDC4;
|
| 36 |
border-radius: 15px;
|
| 37 |
-
padding:
|
| 38 |
-
background:
|
| 39 |
-
|
| 40 |
}
|
| 41 |
-
.
|
| 42 |
-
background: linear-gradient(
|
| 43 |
color: white;
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
font-weight: bold;
|
| 48 |
-
transition: all 0.3s ease;
|
| 49 |
-
}
|
| 50 |
-
.stButton button:hover {
|
| 51 |
-
transform: scale(1.05);
|
| 52 |
-
box-shadow: 0 5px 15px rgba(0,0,0,0.3);
|
| 53 |
}
|
| 54 |
</style>
|
| 55 |
""", unsafe_allow_html=True)
|
| 56 |
|
| 57 |
-
st.markdown('<h1 class="main-
|
| 58 |
-
st.markdown("###
|
| 59 |
|
| 60 |
-
def
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
"face_position": (300, 150),
|
| 70 |
-
"face_size": (200, 200)
|
| 71 |
-
},
|
| 72 |
-
"surprised": {
|
| 73 |
-
"name": "Surprised Pikachu",
|
| 74 |
-
"text": "When you realize\nhow awesome this app is",
|
| 75 |
-
"text_position": (50, 450),
|
| 76 |
-
"text_color": "black",
|
| 77 |
-
"font_size": 35,
|
| 78 |
-
"face_position": (250, 100),
|
| 79 |
-
"face_size": (250, 250)
|
| 80 |
-
},
|
| 81 |
-
"happy": {
|
| 82 |
-
"name": "Success Kid",
|
| 83 |
-
"text": "Made a viral meme\nWent famous on social media",
|
| 84 |
-
"text_position": (50, 50),
|
| 85 |
-
"text_color": "white",
|
| 86 |
-
"font_size": 30,
|
| 87 |
-
"face_position": (150, 200),
|
| 88 |
-
"face_size": (180, 180)
|
| 89 |
-
},
|
| 90 |
-
"sad": {
|
| 91 |
-
"name": "Sad Cat",
|
| 92 |
-
"text": "When you have to wait\nfor the next meme",
|
| 93 |
-
"text_position": (50, 400),
|
| 94 |
-
"text_color": "white",
|
| 95 |
-
"font_size": 35,
|
| 96 |
-
"face_position": (280, 150),
|
| 97 |
-
"face_size": (200, 200)
|
| 98 |
-
},
|
| 99 |
-
"neutral": {
|
| 100 |
-
"name": "Drake Reaction",
|
| 101 |
-
"text_top": "Other meme apps",
|
| 102 |
-
"text_bottom": "This Meme Yourself app",
|
| 103 |
-
"text_position_top": (200, 100),
|
| 104 |
-
"text_position_bottom": (200, 300),
|
| 105 |
-
"text_color": "white",
|
| 106 |
-
"font_size": 30,
|
| 107 |
-
"face_position": (50, 150),
|
| 108 |
-
"face_size": (150, 150)
|
| 109 |
-
}
|
| 110 |
-
}
|
| 111 |
-
return templates
|
| 112 |
-
|
| 113 |
-
def create_meme_background(self, template_key):
|
| 114 |
-
"""Create colored background for memes"""
|
| 115 |
-
background_colors = {
|
| 116 |
-
"angry": (41, 128, 185), # Blue
|
| 117 |
-
"surprised": (241, 196, 15), # Yellow
|
| 118 |
-
"happy": (39, 174, 96), # Green
|
| 119 |
-
"sad": (142, 68, 173), # Purple
|
| 120 |
-
"neutral": (230, 126, 34) # Orange
|
| 121 |
-
}
|
| 122 |
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
try:
|
| 130 |
-
# Convert to grayscale
|
| 131 |
-
gray = cv2.cvtColor(face_image, cv2.COLOR_BGR2GRAY)
|
| 132 |
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_eye.xml')
|
| 136 |
-
smile_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_smile.xml')
|
| 137 |
|
| 138 |
-
|
|
|
|
| 139 |
|
| 140 |
-
|
| 141 |
-
|
| 142 |
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
# Detect eyes and smile
|
| 147 |
-
eyes = eye_cascade.detectMultiScale(roi_gray, 1.1, 5)
|
| 148 |
-
smiles = smile_cascade.detectMultiScale(roi_gray, 1.8, 20)
|
| 149 |
-
|
| 150 |
-
# Simple expression logic
|
| 151 |
-
if len(smiles) > 0:
|
| 152 |
-
return "happy"
|
| 153 |
-
elif len(eyes) == 0:
|
| 154 |
-
return "surprised"
|
| 155 |
-
elif len(eyes) >= 2 and len(smiles) == 0:
|
| 156 |
-
return "neutral"
|
| 157 |
-
else:
|
| 158 |
-
return "neutral"
|
| 159 |
-
|
| 160 |
-
except Exception as e:
|
| 161 |
-
st.warning(f"Expression detection: {e}")
|
| 162 |
|
| 163 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
|
| 165 |
-
def
|
| 166 |
-
"""
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
|
|
|
| 174 |
|
| 175 |
-
if
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
x = max(0, x - 20)
|
| 179 |
-
y = max(0, y - 20)
|
| 180 |
-
w = min(w + 40, opencv_image.shape[1] - x)
|
| 181 |
-
h = min(h + 40, opencv_image.shape[0] - y)
|
| 182 |
|
| 183 |
-
|
| 184 |
-
return Image.fromarray(cv2.cvtColor(face, cv2.COLOR_BGR2RGB))
|
| 185 |
-
|
| 186 |
-
except Exception as e:
|
| 187 |
-
st.error(f"Face extraction error: {e}")
|
| 188 |
-
|
| 189 |
-
return None
|
| 190 |
|
| 191 |
-
def
|
| 192 |
-
"""
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
try:
|
| 196 |
-
font = ImageFont.truetype("arial.ttf", template_config["font_size"])
|
| 197 |
-
except:
|
| 198 |
-
font = ImageFont.load_default()
|
| 199 |
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
fill=template_config["text_color"],
|
| 209 |
-
font=font)
|
| 210 |
-
else:
|
| 211 |
-
# Single text block
|
| 212 |
-
lines = template_config["text"].split('\n')
|
| 213 |
-
y_offset = template_config["text_position"][1]
|
| 214 |
-
for line in lines:
|
| 215 |
-
draw.text((template_config["text_position"][0], y_offset),
|
| 216 |
-
line,
|
| 217 |
-
fill=template_config["text_color"],
|
| 218 |
-
font=font)
|
| 219 |
-
y_offset += template_config["font_size"] + 5
|
| 220 |
-
|
| 221 |
-
except Exception as e:
|
| 222 |
-
st.error(f"Text rendering error: {e}")
|
| 223 |
|
| 224 |
-
def
|
| 225 |
-
"""
|
| 226 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 227 |
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 231 |
|
| 232 |
-
#
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 236 |
|
| 237 |
-
|
| 238 |
-
|
|
|
|
|
|
|
|
|
|
| 239 |
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
st.sidebar.markdown("""
|
| 246 |
-
1. **Take a photo** with your camera
|
| 247 |
-
2. **AI detects your expression** automatically
|
| 248 |
-
3. **Choose your favorite meme template**
|
| 249 |
-
4. **Download and share** on social media!
|
| 250 |
|
| 251 |
-
#
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
- 😊 Happy → "Success Kid"
|
| 255 |
-
- 😢 Sad → "Sad Cat"
|
| 256 |
-
- 😐 Neutral → "Drake Reaction"
|
| 257 |
-
""")
|
| 258 |
|
| 259 |
-
|
| 260 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
|
| 262 |
with col1:
|
| 263 |
-
st.subheader("
|
| 264 |
-
|
|
|
|
|
|
|
| 265 |
|
| 266 |
if camera_image:
|
| 267 |
-
# Convert to
|
| 268 |
image = Image.open(camera_image)
|
|
|
|
|
|
|
| 269 |
|
| 270 |
-
#
|
| 271 |
-
|
| 272 |
-
face_img = self.extract_face(image)
|
| 273 |
-
|
| 274 |
-
if face_img:
|
| 275 |
-
# Convert to OpenCV for expression detection
|
| 276 |
-
opencv_face = cv2.cvtColor(np.array(face_img), cv2.COLOR_RGB2BGR)
|
| 277 |
-
expression = self.detect_face_expression(opencv_face)
|
| 278 |
-
|
| 279 |
-
st.success(f"Detected Expression: {expression.upper()} 😄")
|
| 280 |
-
|
| 281 |
-
# Show extracted face
|
| 282 |
-
st.image(face_img, caption="Your extracted face", use_column_width=True)
|
| 283 |
-
|
| 284 |
-
# Store in session state
|
| 285 |
-
st.session_state.face_image = face_img
|
| 286 |
-
st.session_state.detected_expression = expression
|
| 287 |
-
else:
|
| 288 |
-
st.error("😞 No face detected! Please try again with better lighting.")
|
| 289 |
-
st.session_state.face_image = None
|
| 290 |
-
|
| 291 |
-
with col2:
|
| 292 |
-
st.subheader("🎭 Step 2: Create Your Meme")
|
| 293 |
-
|
| 294 |
-
if 'face_image' in st.session_state and st.session_state.face_image is not None:
|
| 295 |
-
# Expression selection
|
| 296 |
-
detected_exp = st.session_state.detected_expression
|
| 297 |
-
expressions = list(self.meme_templates.keys())
|
| 298 |
-
selected_expression = st.selectbox(
|
| 299 |
-
"Choose meme template:",
|
| 300 |
-
options=expressions,
|
| 301 |
-
index=expressions.index(detected_exp) if detected_exp in expressions else 0,
|
| 302 |
-
format_func=lambda x: f"{x.title()} - {self.meme_templates[x]['name']}"
|
| 303 |
-
)
|
| 304 |
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 311 |
)
|
| 312 |
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
if 'meme_result' in st.session_state:
|
| 317 |
-
st.subheader("✨ Your Meme Masterpiece!")
|
| 318 |
-
st.image(st.session_state.meme_result, use_column_width=True)
|
| 319 |
-
|
| 320 |
-
# Download button
|
| 321 |
-
buf = tempfile.NamedTemporaryFile(delete=False, suffix='.png')
|
| 322 |
-
st.session_state.meme_result.save(buf, format='PNG')
|
| 323 |
-
|
| 324 |
-
with open(buf.name, "rb") as file:
|
| 325 |
-
btn = st.download_button(
|
| 326 |
-
label="📥 Download Meme",
|
| 327 |
-
data=file,
|
| 328 |
-
file_name="my_viral_meme.png",
|
| 329 |
-
mime="image/png"
|
| 330 |
)
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 348 |
|
| 349 |
-
#
|
| 350 |
-
|
| 351 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 352 |
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 359 |
|
| 360 |
def main():
|
| 361 |
-
|
| 362 |
-
app = MemeYourselfApp()
|
| 363 |
app.run()
|
| 364 |
|
| 365 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
+
import mediapipe as mp
|
| 5 |
+
from PIL import Image, ImageDraw
|
| 6 |
+
import time
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
class AirDrawingCanvas:
|
| 9 |
def __init__(self):
|
| 10 |
self.setup_page()
|
| 11 |
+
self.setup_mediapipe()
|
| 12 |
+
self.initialize_canvas()
|
| 13 |
|
| 14 |
def setup_page(self):
|
| 15 |
st.set_page_config(
|
| 16 |
+
page_title="Magic Air Drawing 🎨",
|
| 17 |
+
page_icon="✨",
|
| 18 |
+
layout="wide"
|
|
|
|
| 19 |
)
|
| 20 |
|
|
|
|
| 21 |
st.markdown("""
|
| 22 |
<style>
|
| 23 |
+
.main-title {
|
| 24 |
+
font-size: 3.5rem;
|
| 25 |
+
background: linear-gradient(45deg, #FF6B6B, #4ECDC4, #45B7D1, #96CEB4);
|
| 26 |
+
-webkit-background-clip: text;
|
| 27 |
+
-webkit-text-fill-color: transparent;
|
| 28 |
text-align: center;
|
| 29 |
+
margin-bottom: 1rem;
|
| 30 |
font-weight: bold;
|
|
|
|
| 31 |
}
|
| 32 |
+
.canvas-container {
|
| 33 |
border: 3px solid #4ECDC4;
|
| 34 |
border-radius: 15px;
|
| 35 |
+
padding: 10px;
|
| 36 |
+
background: white;
|
| 37 |
+
box-shadow: 0 8px 25px rgba(0,0,0,0.1);
|
| 38 |
}
|
| 39 |
+
.control-panel {
|
| 40 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 41 |
color: white;
|
| 42 |
+
padding: 20px;
|
| 43 |
+
border-radius: 15px;
|
| 44 |
+
margin: 10px 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
}
|
| 46 |
</style>
|
| 47 |
""", unsafe_allow_html=True)
|
| 48 |
|
| 49 |
+
st.markdown('<h1 class="main-title">🎨 Magic Air Drawing Canvas</h1>', unsafe_allow_html=True)
|
| 50 |
+
st.markdown("### Draw in the air with your finger and watch it appear magically on the screen! ✨")
|
| 51 |
|
| 52 |
+
def setup_mediapipe(self):
|
| 53 |
+
self.mp_hands = mp.solutions.hands
|
| 54 |
+
self.mp_drawing = mp.solutions.drawing_utils
|
| 55 |
+
self.hands = self.mp_hands.Hands(
|
| 56 |
+
static_image_mode=False,
|
| 57 |
+
max_num_hands=1,
|
| 58 |
+
min_detection_confidence=0.7,
|
| 59 |
+
min_tracking_confidence=0.5
|
| 60 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
+
def initialize_canvas(self):
|
| 63 |
+
"""Initialize the drawing canvas"""
|
| 64 |
+
if 'canvas' not in st.session_state:
|
| 65 |
+
# Create a white canvas
|
| 66 |
+
st.session_state.canvas = Image.new('RGB', (600, 400), 'white')
|
| 67 |
+
st.session_state.draw = ImageDraw.Draw(st.session_state.canvas)
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
+
if 'drawing_points' not in st.session_state:
|
| 70 |
+
st.session_state.drawing_points = []
|
|
|
|
|
|
|
| 71 |
|
| 72 |
+
if 'is_drawing' not in st.session_state:
|
| 73 |
+
st.session_state.is_drawing = False
|
| 74 |
|
| 75 |
+
if 'current_color' not in st.session_state:
|
| 76 |
+
st.session_state.current_color = (255, 0, 0) # Red
|
| 77 |
|
| 78 |
+
if 'brush_size' not in st.session_state:
|
| 79 |
+
st.session_state.brush_size = 5
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
+
def detect_drawing_gesture(self, landmarks, img_width, img_height):
|
| 82 |
+
"""Detect if user is making a drawing gesture (index finger pointing)"""
|
| 83 |
+
# Get key landmarks
|
| 84 |
+
index_tip = landmarks[8]
|
| 85 |
+
middle_tip = landmarks[12]
|
| 86 |
+
ring_tip = landmarks[16]
|
| 87 |
+
pinky_tip = landmarks[20]
|
| 88 |
+
thumb_tip = landmarks[4]
|
| 89 |
+
|
| 90 |
+
# Convert to pixel coordinates
|
| 91 |
+
index_tip_px = (int(index_tip.x * img_width), int(index_tip.y * img_height))
|
| 92 |
+
|
| 93 |
+
# Check if only index finger is extended (pointing gesture)
|
| 94 |
+
fingers_extended = self.count_extended_fingers(landmarks, img_width, img_height)
|
| 95 |
+
|
| 96 |
+
return fingers_extended == 1, index_tip_px
|
| 97 |
|
| 98 |
+
def count_extended_fingers(self, landmarks, img_width, img_height):
|
| 99 |
+
"""Count how many fingers are extended"""
|
| 100 |
+
finger_tips = [8, 12, 16, 20] # Index, Middle, Ring, Pinky
|
| 101 |
+
finger_pips = [6, 10, 14, 18] # PIP joints
|
| 102 |
+
|
| 103 |
+
extended_count = 0
|
| 104 |
+
|
| 105 |
+
for tip_idx, pip_idx in zip(finger_tips, finger_pips):
|
| 106 |
+
tip_y = landmarks[tip_idx].y * img_height
|
| 107 |
+
pip_y = landmarks[pip_idx].y * img_height
|
| 108 |
|
| 109 |
+
# Finger is extended if tip is above PIP joint
|
| 110 |
+
if tip_y < pip_y:
|
| 111 |
+
extended_count += 1
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
+
return extended_count
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
+
def update_canvas(self, current_point):
|
| 116 |
+
"""Update the canvas with new drawing points"""
|
| 117 |
+
if len(st.session_state.drawing_points) > 0:
|
| 118 |
+
last_point = st.session_state.drawing_points[-1]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
+
# Draw line between last point and current point
|
| 121 |
+
st.session_state.draw.line(
|
| 122 |
+
[last_point, current_point],
|
| 123 |
+
fill=st.session_state.current_color,
|
| 124 |
+
width=st.session_state.brush_size
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
+
st.session_state.drawing_points.append(current_point)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
+
def clear_canvas(self):
|
| 130 |
+
"""Clear the entire canvas"""
|
| 131 |
+
st.session_state.canvas = Image.new('RGB', (600, 400), 'white')
|
| 132 |
+
st.session_state.draw = ImageDraw.Draw(st.session_state.canvas)
|
| 133 |
+
st.session_state.drawing_points = []
|
| 134 |
+
st.session_state.is_drawing = False
|
| 135 |
+
|
| 136 |
+
def run(self):
|
| 137 |
+
# Sidebar for controls
|
| 138 |
+
st.sidebar.title("🎮 Drawing Controls")
|
| 139 |
|
| 140 |
+
st.sidebar.markdown("""
|
| 141 |
+
### How to Draw:
|
| 142 |
+
1. **Show your hand** to the camera
|
| 143 |
+
2. **Point with index finger** to start drawing
|
| 144 |
+
3. **Move your finger** in the air to draw
|
| 145 |
+
4. **Make a fist** to stop drawing
|
| 146 |
+
5. **Use controls below** to customize
|
| 147 |
+
""")
|
| 148 |
|
| 149 |
+
# Color selection
|
| 150 |
+
st.sidebar.subheader("🎨 Brush Color")
|
| 151 |
+
color_options = {
|
| 152 |
+
"Red": (255, 0, 0),
|
| 153 |
+
"Blue": (0, 0, 255),
|
| 154 |
+
"Green": (0, 255, 0),
|
| 155 |
+
"Purple": (128, 0, 128),
|
| 156 |
+
"Orange": (255, 165, 0),
|
| 157 |
+
"Black": (0, 0, 0)
|
| 158 |
+
}
|
| 159 |
|
| 160 |
+
selected_color = st.sidebar.selectbox(
|
| 161 |
+
"Choose color:",
|
| 162 |
+
list(color_options.keys())
|
| 163 |
+
)
|
| 164 |
+
st.session_state.current_color = color_options[selected_color]
|
| 165 |
|
| 166 |
+
# Brush size
|
| 167 |
+
st.sidebar.subheader("🖌️ Brush Size")
|
| 168 |
+
st.session_state.brush_size = st.sidebar.slider(
|
| 169 |
+
"Size:", 1, 20, st.session_state.brush_size
|
| 170 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
|
| 172 |
+
# Drawing modes
|
| 173 |
+
st.sidebar.subheader("⚡ Quick Actions")
|
| 174 |
+
col1, col2 = st.sidebar.columns(2)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
|
| 176 |
+
with col1:
|
| 177 |
+
if st.button("🗑️ Clear Canvas", use_container_width=True):
|
| 178 |
+
self.clear_canvas()
|
| 179 |
+
st.rerun()
|
| 180 |
+
|
| 181 |
+
with col2:
|
| 182 |
+
if st.button("💾 Save Drawing", use_container_width=True):
|
| 183 |
+
self.save_drawing()
|
| 184 |
+
|
| 185 |
+
# Main content area
|
| 186 |
+
col1, col2 = st.columns([2, 1])
|
| 187 |
|
| 188 |
with col1:
|
| 189 |
+
st.subheader("📹 Live Camera View")
|
| 190 |
+
|
| 191 |
+
# Camera input
|
| 192 |
+
camera_image = st.camera_input("Point your finger to start drawing!", key="drawing_camera")
|
| 193 |
|
| 194 |
if camera_image:
|
| 195 |
+
# Convert to OpenCV format
|
| 196 |
image = Image.open(camera_image)
|
| 197 |
+
frame = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
| 198 |
+
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 199 |
|
| 200 |
+
# Process with MediaPipe
|
| 201 |
+
results = self.hands.process(frame_rgb)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
|
| 203 |
+
current_gesture = "Waiting for hand..."
|
| 204 |
+
current_point = None
|
| 205 |
+
|
| 206 |
+
if results.multi_hand_landmarks:
|
| 207 |
+
for hand_landmarks in results.multi_hand_landmarks:
|
| 208 |
+
# Draw hand landmarks on frame
|
| 209 |
+
self.mp_drawing.draw_landmarks(
|
| 210 |
+
frame, hand_landmarks, self.mp_hands.HAND_CONNECTIONS,
|
| 211 |
+
self.mp_drawing.DrawingSpec(color=(0, 255, 0), thickness=2, circle_radius=2),
|
| 212 |
+
self.mp_drawing.DrawingSpec(color=(255, 0, 0), thickness=2)
|
| 213 |
)
|
| 214 |
|
| 215 |
+
# Detect drawing gesture
|
| 216 |
+
is_drawing_gesture, finger_tip = self.detect_drawing_gesture(
|
| 217 |
+
hand_landmarks.landmark, frame.shape[1], frame.shape[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
)
|
| 219 |
+
|
| 220 |
+
if is_drawing_gesture:
|
| 221 |
+
current_gesture = "✍️ Drawing Mode"
|
| 222 |
+
current_point = finger_tip
|
| 223 |
+
|
| 224 |
+
# Scale point to canvas coordinates
|
| 225 |
+
canvas_point = (
|
| 226 |
+
int(finger_tip[0] * 600 / frame.shape[1]),
|
| 227 |
+
int(finger_tip[1] * 400 / frame.shape[0])
|
| 228 |
+
)
|
| 229 |
+
|
| 230 |
+
if st.session_state.is_drawing:
|
| 231 |
+
self.update_canvas(canvas_point)
|
| 232 |
+
else:
|
| 233 |
+
st.session_state.is_drawing = True
|
| 234 |
+
st.session_state.drawing_points = [canvas_point]
|
| 235 |
+
else:
|
| 236 |
+
current_gesture = "✋ Stop Drawing"
|
| 237 |
+
st.session_state.is_drawing = False
|
| 238 |
+
|
| 239 |
+
else:
|
| 240 |
+
current_gesture = "👋 Show your hand to camera"
|
| 241 |
+
st.session_state.is_drawing = False
|
| 242 |
|
| 243 |
+
# Add gesture info to frame
|
| 244 |
+
cv2.putText(frame, f"Status: {current_gesture}", (10, 30),
|
| 245 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 2)
|
| 246 |
+
cv2.putText(frame, f"Brush: {selected_color}", (10, 60),
|
| 247 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 2)
|
| 248 |
+
cv2.putText(frame, f"Size: {st.session_state.brush_size}px", (10, 90),
|
| 249 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 2)
|
| 250 |
|
| 251 |
+
# Convert back to RGB for display
|
| 252 |
+
result_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 253 |
+
st.image(result_rgb, caption="Live Camera Feed", use_column_width=True)
|
| 254 |
+
|
| 255 |
+
with col2:
|
| 256 |
+
st.subheader("🎨 Your Drawing Canvas")
|
| 257 |
+
|
| 258 |
+
# Display the canvas
|
| 259 |
+
st.markdown('<div class="canvas-container">', unsafe_allow_html=True)
|
| 260 |
+
st.image(st.session_state.canvas, use_column_width=True, caption="Your Air Drawing")
|
| 261 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 262 |
+
|
| 263 |
+
# Drawing statistics
|
| 264 |
+
st.subheader("📊 Drawing Info")
|
| 265 |
+
st.write(f"**Points drawn:** {len(st.session_state.drawing_points)}")
|
| 266 |
+
st.write(f"**Current color:** {selected_color}")
|
| 267 |
+
st.write(f"**Brush size:** {st.session_state.brush_size}px")
|
| 268 |
+
st.write(f"**Drawing mode:** {'Active' if st.session_state.is_drawing else 'Inactive'}")
|
| 269 |
+
|
| 270 |
+
# Quick color buttons
|
| 271 |
+
st.subheader("🎯 Quick Colors")
|
| 272 |
+
color_cols = st.columns(3)
|
| 273 |
+
quick_colors = [("🔴", (255, 0, 0)), ("🔵", (0, 0, 255)), ("🟢", (0, 255, 0))]
|
| 274 |
+
|
| 275 |
+
for i, (emoji, color) in enumerate(quick_colors):
|
| 276 |
+
with color_cols[i]:
|
| 277 |
+
if st.button(emoji, use_container_width=True):
|
| 278 |
+
st.session_state.current_color = color
|
| 279 |
+
st.rerun()
|
| 280 |
+
|
| 281 |
+
def save_drawing(self):
|
| 282 |
+
"""Save the current drawing"""
|
| 283 |
+
timestamp = time.strftime("%Y%m%d-%H%M%S")
|
| 284 |
+
filename = f"air_drawing_{timestamp}.png"
|
| 285 |
+
st.session_state.canvas.save(filename)
|
| 286 |
+
st.sidebar.success(f"Drawing saved as {filename}")
|
| 287 |
|
| 288 |
def main():
|
| 289 |
+
app = AirDrawingCanvas()
|
|
|
|
| 290 |
app.run()
|
| 291 |
|
| 292 |
if __name__ == "__main__":
|