-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpalmist.py
More file actions
162 lines (140 loc) · 6.91 KB
/
Copy pathpalmist.py
File metadata and controls
162 lines (140 loc) · 6.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import logging
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(name)s - %(message)s",handlers=[
logging.FileHandler("/home/pi/estevan/log/esteban.log"),
logging.StreamHandler() # Optional: log to console
])
import time
import json
import redis
from fastapi import FastAPI
from threading import Thread
import mediapipe as mp
import cv2
import numpy as np
import asyncio
import uvicorn
# Configure logging with class name reference
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(name)s - %(message)s")
class Palmist:
def __init__(self, settings):
self.logger = logging.getLogger(self.__class__.__name__)
try:
self.settings = settings["Palmist"]
self.queue_settings = settings["queue"]
self.logger.info("Palmist initialized successfully.")
self.last_hand_position = (None, None) # To store the last (x, y) position
self.notfoundnotified=True
except Exception as e:
self.logger.error("Error initializing Palmist: %s", e)
def process_init(self):
try:
# Initialize settings and Redis connection
self.redis_conn = redis.Redis(
host=self.queue_settings["host"],
port=self.queue_settings["port"],
db=self.queue_settings["db"]
)
self.current_gesture = None
self.app = FastAPI()
# Setup Mediapipe GestureRecognizer
self.setup_mediapipe()
self.setup_routes()
self.logger.info("Palmist process initialized successfully.")
except Exception as e:
self.logger.error("Error initializing Palmist: %s", e)
def setup_mediapipe(self):
BaseOptions = mp.tasks.BaseOptions
GestureRecognizer = mp.tasks.vision.GestureRecognizer
GestureRecognizerOptions = mp.tasks.vision.GestureRecognizerOptions
RunningMode = mp.tasks.vision.RunningMode
# Initialize GestureRecognizer with callback for result processing
self.options = GestureRecognizerOptions(
base_options=BaseOptions(model_asset_path='gesture_recognizer.task'),
running_mode=RunningMode.LIVE_STREAM,
result_callback=self.gesture_callback
)
self.gesture_recognizer = GestureRecognizer.create_from_options(self.options)
def gesture_callback(self, result, img, timestamp_ms):
# Handle gesture recognition result
if result.gestures:
top_gesture = result.gestures[0][0]
gesture_name = top_gesture.category_name
confidence = top_gesture.score
# Check if the detected gesture is different from the current one
if confidence > 0.50 and gesture_name != self.current_gesture:
self.current_gesture = gesture_name
# Push the new gesture into the gesture_queue
self.redis_conn.rpush("gesture_queue", gesture_name)
self.logger.info("New gesture detected and pushed to gesture_queue: %s", gesture_name)
elif gesture_name!= self.current_gesture:
self.current_gesture = "No gesture"
self.redis_conn.rpush("gesture_queue", self.current_gesture)
else:
self.current_gesture = "No gesture"
self.redis_conn.rpush("gesture_queue", self.current_gesture)
# Handle hand position
if result.hand_landmarks:
self.notfoundnotified=False
hand_landmarks = result.hand_landmarks[0]
wrist_landmark = hand_landmarks[0]
hand_x = wrist_landmark.x
hand_y = wrist_landmark.y
# Calculate the difference from the last known position
last_x, last_y = self.last_hand_position
if last_x is not None and last_y is not None:
delta_x = abs(hand_x - last_x)
delta_y = abs(hand_y - last_y)
# Check if the change is greater than the defined offset
if delta_x > self.settings["position_offest"] or delta_y > self.settings["position_offest"]:
# Push hand position to the hand_position_queue
self.redis_conn.rpush("hand_position_queue", f"Hand position: ({hand_x}, {hand_y})")
self.logger.info("New hand position detected and pushed to hand_position_queue: (%f, %f)", hand_x, hand_y)
# Update the last known hand position
self.last_hand_position = (hand_x, hand_y)
else:
# If there was no last position, initialize it
self.last_hand_position = (hand_x, hand_y)
else:
if not self.notfoundnotified:
self.redis_conn.rpush("hand_position_queue", f"Hand position: (0, 0)")
self.logger.info("Hand not found")
self.notfoundnotified=True
def setup_routes(self):
@self.app.get("/current_gesture")
async def current_gesture():
# Return the current detected gesture
return {"gesture": self.current_gesture or "No gesture detected"}
self.logger.info("Routes setup completed successfully.")
def process_frame(self, frame):
try:
# Convert frame data into the format required by Mediapipe
image = mp.Image(image_format=mp.ImageFormat.SRGB, data=cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
self.gesture_recognizer.recognize_async(image, int(time.time() * 1e6))
except Exception as e:
self.logger.error("Error processing frame for gesture recognition: %s", e)
def read_stream_and_detect(self):
while True:
try:
# Get the latest frame from the Redis streaming queue
frame_data = self.redis_conn.get("streaming_queue")
if frame_data:
frame = np.frombuffer(frame_data, dtype=np.uint8)
frame = cv2.imdecode(frame, cv2.IMREAD_COLOR)
self.process_frame(frame)
else:
self.logger.warning("No frame available in Redis streaming queue.")
time.sleep(1 / self.settings["fps_streaming"])
except Exception as e:
self.logger.error("Error in read_stream_and_detect loop: %s", e)
break
def run(self):
try:
# Start frame reading and gesture recognition in a separate thread
self.process_init()
self.logger.info("Starting Palmist frame processing.")
Thread(target=self.read_stream_and_detect).start()
# Run FastAPI server
self.logger.info("Starting Palmist FastAPI server.")
uvicorn.run(self.app, host="0.0.0.0", port=self.settings["fastapi_port"])
except Exception as e:
self.logger.error("Error running Palmist: %s", e)