Toasty animation to play in top of OpenCV VideoCapture
Project description
python_opencv_toasty_animation
Requirements
opencv-contrib-python 4.10.0.82 playsound 1.3.0
Installation
Install with pip
pip install opencv-toasty-animation
Usage
Add these lines before the main loop to instantiate animation
animation = Animation()
Then before the cv2.imshow('Video', frame)
execution add these
animation.update_animation_state()
frame = animation.apply_animation(frame)
The animation now is ready to be played, now you need to choice an event to fire the start. Anywhere in the main loop add this line
animation.start()
Optionally you can pass the prevent_repetition=True
argument if the animation play too many times in the loop to control the repetition. By default it doesn't play until 300 seconds passed. You can change it passing repetition_delay=300
in the arguments during the instantiation of the animation.
How to try it
Create a python file and copy this code, then run it and hit "start" button
import cv2
from opencv_toasty_animation.animation import Animation
# Función para manejar los eventos del mouse
def mouse_callback(event, x, y, flags, param):
global mouseX, mouseY, start_animation, animation_phase
if event == cv2.EVENT_LBUTTONDOWN:
mouseX, mouseY = x, y
# Coordenadas del botón
button_rect = param
# Verificar si el clic ocurrió dentro del botón
if button_rect[0] <= mouseX <= button_rect[2] and button_rect[1] <= mouseY <= button_rect[3]:
animation.start()
# Captura de video desde la webcam
cap = cv2.VideoCapture(0)
# Verificar si la cámara se abrió correctamente
if not cap.isOpened():
print("No se puede abrir la cámara")
exit()
# Variables para almacenar la posición del clic
mouseX, mouseY = 0, 0
# Coordenadas del botón (rectángulo)
button_rect = (50, 50, 150, 100) # (x1, y1, x2, y2)
# Configurar la ventana de OpenCV para capturar eventos del mouse
cv2.namedWindow('Webcam')
cv2.setMouseCallback('Webcam', mouse_callback, button_rect)
animation = Animation()
try:
while True:
ret, frame = cap.read()
if not ret:
print("No se puede recibir frames. Terminando...")
break
# Dibujar el botón (rectángulo) en el frame
cv2.rectangle(frame, (button_rect[0], button_rect[1]),
(button_rect[2], button_rect[3]), (0, 0, 255), cv2.FILLED)
cv2.putText(frame, "Start", (button_rect[0] + 10, button_rect[1] + 30),
cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
# Actualizar el estado de la animación y aplicar la animación al frame
animation.update_animation_state()
frame = animation.apply_animation(frame)
# Mostrar el frame con la animación
cv2.imshow('Webcam', frame)
# Finalizar la animación y esperar a que se presione 'q' para salir
if cv2.waitKey(1) & 0xFF == ord('q'):
break
finally:
cap.release()
cv2.destroyAllWindows()
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file opencv_toasty_animation-0.0.1.tar.gz
.
File metadata
- Download URL: opencv_toasty_animation-0.0.1.tar.gz
- Upload date:
- Size: 47.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 81778dc8304a14d48d7bd6dd025849321ac67f5143a2e9da809441f3c6b36e6c |
|
MD5 | fd21f95f894426daa6406887d1be81e3 |
|
BLAKE2b-256 | c927d87e3f2092ce8bf8ef44b6082b3236ead267e1e3783efd5810d53ff2ee8a |
File details
Details for the file opencv_toasty_animation-0.0.1-py2.py3-none-any.whl
.
File metadata
- Download URL: opencv_toasty_animation-0.0.1-py2.py3-none-any.whl
- Upload date:
- Size: 44.8 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7f42186adcd1df1f9af6affbb7be0e4094aa99dc95fde4089924b8afa9ebdf41 |
|
MD5 | b29606eabb456879751b9270caa88998 |
|
BLAKE2b-256 | dba6b17636fad86dafea9737cd57cac2730ca58f4bb906c36191f548875cd7a5 |