mirror of
https://git.roussel.pro/telecom-paris/pact.git
synced 2026-02-09 10:30:17 +01:00
restructuration du backend
préparation de l'intégration des modules suivants
This commit is contained in:
84
code/backend_reconnaissance/hand_detector.py
Normal file
84
code/backend_reconnaissance/hand_detector.py
Normal file
@@ -0,0 +1,84 @@
|
||||
import cv2
|
||||
import mediapipe as mp
|
||||
import numpy as np
|
||||
|
||||
class HandDetector():
|
||||
def __init__(self):
|
||||
self.mp_drawing = mp.solutions.drawing_utils
|
||||
self.mp_drawing_styles = mp.solutions.drawing_styles
|
||||
self.mp_hands = mp.solutions.hands
|
||||
self.cap = cv2.VideoCapture(0)
|
||||
self.hands = self.mp_hands.Hands(
|
||||
model_complexity=0,
|
||||
min_detection_confidence=0.5,
|
||||
min_tracking_confidence=0.5)
|
||||
#Paramètres
|
||||
self.BUFFER_LENGTH = 30
|
||||
self.DETECTION_THRESHOLD = 3/4
|
||||
|
||||
self.resultBuffer = []
|
||||
|
||||
|
||||
|
||||
def reconnaissancePouce(self,handLandmarks):
|
||||
etatDuPouce=["neutre","thumbs_down","thumbs_up"]
|
||||
i=0
|
||||
j=0
|
||||
for cpt in range (0,4):
|
||||
V1=[handLandmarks[(4*cpt)+6][0]-handLandmarks[(4*cpt)+5][0],handLandmarks[(4*cpt)+6][1]-handLandmarks[(4*cpt)+5][1]]
|
||||
V2=[handLandmarks[(4*cpt)+8][0]-handLandmarks[(4*cpt)+6][0],handLandmarks[(4*cpt)+8][1]-handLandmarks[(4*cpt)+6][1]]
|
||||
j=np.dot(V1,V2)
|
||||
if (j>0):
|
||||
return etatDuPouce[0]
|
||||
V1=[handLandmarks[4][0]-handLandmarks[1][0],handLandmarks[4][1]-handLandmarks[1][1]]
|
||||
V2=[handLandmarks[2][0]-handLandmarks[1][0],handLandmarks[2][1]-handLandmarks[1][1]]
|
||||
if((np.dot(V1,V2))>0 and (handLandmarks[4][1]>handLandmarks[2][1])):
|
||||
i=1
|
||||
elif(np.dot(V1,V2)>0 and handLandmarks[4][1]<handLandmarks[2][1]):
|
||||
i=2
|
||||
return etatDuPouce[i]
|
||||
|
||||
|
||||
def detect(self):
|
||||
if self.cap.isOpened():
|
||||
success, image = self.cap.read()
|
||||
if not success:
|
||||
print("Ignoring empty camera frame.")
|
||||
# If loading a video, use 'break' instead of 'continue'.
|
||||
return False
|
||||
|
||||
# To improve performance, optionally mark the image as not writeable to
|
||||
# pass by reference.
|
||||
image.flags.writeable = False
|
||||
results = self.hands.process(image)
|
||||
# print(results)
|
||||
handLandmarks = []
|
||||
if results.multi_hand_landmarks:
|
||||
for hand_landmarks in results.multi_hand_landmarks:
|
||||
# Fill list with x and y positions of each landmark
|
||||
for landmarks in hand_landmarks.landmark:
|
||||
handLandmarks.append([landmarks.x, landmarks.y])
|
||||
|
||||
thumbState = self.reconnaissancePouce(handLandmarks)
|
||||
|
||||
self.resultBuffer.append(thumbState)
|
||||
if(len(self.resultBuffer) > self.BUFFER_LENGTH):
|
||||
self.resultBuffer.pop(0)
|
||||
|
||||
thumbsUpCount = sum(map(lambda x : x == "thumbs_up", self.resultBuffer))
|
||||
thumbsDownCount = sum(map(lambda x : x == "thumbs_down", self.resultBuffer))
|
||||
|
||||
print(thumbsUpCount,thumbsDownCount)
|
||||
|
||||
if(thumbsUpCount > self.DETECTION_THRESHOLD * self.BUFFER_LENGTH):
|
||||
result = "thumbs_up"
|
||||
elif(thumbsDownCount > self.DETECTION_THRESHOLD * self.BUFFER_LENGTH):
|
||||
result = "thumbs_down"
|
||||
else:
|
||||
result = False
|
||||
|
||||
if(thumbState != "neutre"):
|
||||
return thumbState, handLandmarks[9], np.linalg.norm(np.array(handLandmarks[9]) - np.array(handLandmarks[0])), result
|
||||
return False
|
||||
|
||||
|
||||
Reference in New Issue
Block a user