16
loading...
This website collects cookies to deliver better user experience
import mediapipe as mp
import cv2
import numpy as np
import matplotlib.pyplot as plt
from collections import Counter
def show(img, fsize=(10, 10)):
figure = plt.figure(figsize=(10, 10))
plt.imshow(img)
plt.show()
show(np.zeros((10, 10)))
def euclidean(pt1, pt2):
d = np.sqrt((pt1[0]-pt2[0])**2+(pt1[1]-pt2[1])**2)
return d
euclidean((4, 3), (0, 0))
5.0
cam = cv2.VideoCapture(0)
fsize = (600, 820)
show_every = 15
check_cnt = 0
operators = ["+", "-", "/", "*", "C", "="]
ops = len(operators)
cols = np.linspace(0, fsize[1]-1, ops+1).astype(np.int64)
cols = [(cols[i], cols[i+1]) for i in range(len(cols)-1)]
opd = np.zeros((100, fsize[1], 3), dtype="uint8")
l1,t1,r1,b1=(50, 200, 350, 500)
l2,t2,r2,b2=(500, 200, 800, 500)
for i, col in enumerate(cols):
opd[:, 0:2] =[200, 200, 200]
opd[:, col[0]-2:col[0]+2] = [200, 200, 200]
opd[:, col[1]-2:col[1]+2] = [200, 200, 200]
cv2.putText(opd, operators[i], (int((col[0]+col[1])/2), 60), cv2.FONT_HERSHEY_COMPLEX, 1, (200, 100, 200), 3)
mp_drawing = mp.solutions.drawing_utils
mp_hands = mp.solutions.hands
eqtn = ""
values
for storing all the predicted values. Define a variable pred
for storing predicted value, define a list itips
for storing index tip positions. Define a variable op
for storing current operator. Define a variable output
for storing output.
values = []
pred=0
itips = []
op=None
output = None
with mp_hands.Hands(static_image_mode = True,
max_num_hands=2,
min_detection_confidence=0.2) as hands:
while cam.isOpened():
ret, frame = cam.read()
if not ret:
continue
key = cv2.waitKey(1)&0xFF
buffer = 0
frame = cv2.flip(frame, 1)
frame = cv2.resize(frame, fsize[::-1])
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
res = hands.process(rgb)
fv=10
cv2.rectangle(frame, (l1, t1), (r1, b1), (0, 0, 255), 1)
cv2.rectangle(frame, (l2, t2), (r2, b2), (0, 0, 255), 1)
h, w = frame.shape[:-1]
index_tip=[-1,-1]
fcounts = {}
move = False
if res.multi_hand_landmarks:
for i, hand_landmarks in enumerate(res.multi_hand_landmarks):
bv = 0
mp_drawing.draw_landmarks(frame, hand_landmarks, mp_hands.HAND_CONNECTIONS)
index_tip = mp_drawing._normalized_to_pixel_coordinates(
hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP].x,
hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP].y,
w, h)
index_pip = mp_drawing._normalized_to_pixel_coordinates(
hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_PIP].x,
hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_PIP].y,
w, h)
middle_tip = mp_drawing._normalized_to_pixel_coordinates(
hand_landmarks.landmark[mp_hands.HandLandmark.MIDDLE_FINGER_TIP].x,
hand_landmarks.landmark[mp_hands.HandLandmark.MIDDLE_FINGER_TIP].y,
w, h)
middle_pip = mp_drawing._normalized_to_pixel_coordinates(
hand_landmarks.landmark[mp_hands.HandLandmark.MIDDLE_FINGER_PIP].x,
hand_landmarks.landmark[mp_hands.HandLandmark.MIDDLE_FINGER_PIP].y,
w, h)
ring_tip = mp_drawing._normalized_to_pixel_coordinates(
hand_landmarks.landmark[mp_hands.HandLandmark.RING_FINGER_TIP].x,
hand_landmarks.landmark[mp_hands.HandLandmark.RING_FINGER_TIP].y,
w, h)
ring_pip = mp_drawing._normalized_to_pixel_coordinates(
hand_landmarks.landmark[mp_hands.HandLandmark.RING_FINGER_PIP].x,
hand_landmarks.landmark[mp_hands.HandLandmark.RING_FINGER_PIP].y,
w, h)
pinky_tip = mp_drawing._normalized_to_pixel_coordinates(
hand_landmarks.landmark[mp_hands.HandLandmark.PINKY_TIP].x,
hand_landmarks.landmark[mp_hands.HandLandmark.PINKY_TIP].y,
w, h)
pinky_pip = mp_drawing._normalized_to_pixel_coordinates(
hand_landmarks.landmark[mp_hands.HandLandmark.PINKY_PIP].x,
hand_landmarks.landmark[mp_hands.HandLandmark.PINKY_PIP].y,
w, h)
thumb_tip = mp_drawing._normalized_to_pixel_coordinates(
hand_landmarks.landmark[mp_hands.HandLandmark.THUMB_TIP].x,
hand_landmarks.landmark[mp_hands.HandLandmark.THUMB_TIP].y,
w, h)
thumb_ip = mp_drawing._normalized_to_pixel_coordinates(
hand_landmarks.landmark[mp_hands.HandLandmark.THUMB_IP].x,
hand_landmarks.landmark[mp_hands.HandLandmark.THUMB_IP].y,
w, h)
wrist = mp_drawing._normalized_to_pixel_coordinates(
hand_landmarks.landmark[mp_hands.HandLandmark.WRIST].x,
hand_landmarks.landmark[mp_hands.HandLandmark.WRIST].y,
w, h)
operators
list and panel. We will take the operator with the found index. If the difference between index tip and index pip's row is smaller than fv
i.e finger value then we will increase the buffer. In the normal hand mode, i.e. not while folding fingers, the value will be always -ve.
if index_pip is not None and index_tip is not None:
if index_tip[1]<100:
itp = [v for v in cols if index_tip[0]>v[0] and index_tip[0]<v[1]]
if len(itp)>0:
itp = cols.index(itp[0])
op = operators[itp]
if index_tip[1]-index_pip[1]<fv:
bv+=1
if middle_pip is not None and middle_tip is not None:
if middle_tip[1]-middle_pip[1]<fv:
bv+=1
if ring_pip is not None and ring_tip is not None:
if ring_tip[1]-ring_pip[1]<fv:
bv+=1
if pinky_pip is not None and pinky_tip is not None:
if pinky_tip[1]-pinky_pip[1]<fv:
bv+=1
if thumb_tip is not None and index_pip is not None:
if euclidean(index_pip, thumb_tip)>60:
bv+=1
fcounts[i] = bv
if wrist is not None:
if b1>wrist[1] and (t1+b1)/2<wrist[1]:
#pass
elif b2>wrist[1] and (t2+b2)/2<wrist[1]:
#pass
else:
fcounts[i] = 0
if index_pip is not None and thumb_tip is not None:
#print(euclidean(index_pip, thumb_tip))
if euclidean(index_pip, thumb_tip)<60:
move = True
buffer = sum(list(fcounts.values()))
if move:
buffer = 0
else:
values.append(buffer)
if show_every<check_cnt:
check_cnt=0
if len(values)>1 and buffer!=0:
d = dict(Counter(values))
pred = sorted(d.items(), key=lambda x: x[1], reverse=True)[0][0]
if output is not None:
output=None
eqtn=""
eqtn+=str(pred)
if pred != 0:
cv2.putText(frame, str(pred), (50, 200), cv2.FONT_HERSHEY_COMPLEX, 3, (200, 100, 200), 3)
C
i.e clear then we will perform the equation if equation is present and operator is =
. We will also check for repeatation of operators and we will not repeat them. If last operator is not =
then we will add that operator to our equation. But if the output is not none and then which means, the current operator will be doing opeartion with last output and we will make it happen by starting new equation by giving output. Please look at the comments too.
if op is not None and op != "C": # check if operator is not clear and not none
if len(eqtn)>0: # if the equation is not only then the purpose of operator is taken
# if last operator is not equal to current operator this is necessary
# if finger lies multiple times on same operator position
# but it might fail when equation becomes something like : 4*/4
if eqtn[-1]!=op:
if eqtn[-1] in operators: # if last item of eqtn is operator then we should replace it with current
eqtn = eqtn[:-1] # but first remove that operator
if op=="=": # if operator is `=` then we will calculate the equation but use try except block
try: # not all equation can be solvable so use try except
output=eval(eqtn) # try to evaluate equation and get result in string format
eqtn=str(output)
print(f"Output: {output}") # show the output
except:
print(f"Error: {eqtn}") # if not solvable, show error
else:
if output is not None: # if output is not none, i.e. we have the output of prev. eqtn
eqtn=str(output) # take that output as new item in equation
output=None
eqtn+=op # and append that operand on the back of equation
op=None
C
i.e. clear then we will make output None, clear the equation and values.
if op == "C":
output=None
eqtn=""
values=[]
op=None
buffer=0
pred=0
values=[]
if output is not None:
cv2.putText(frame, str(output), (80, 200), cv2.FONT_HERSHEY_COMPLEX, 3, (200, 100, 100), 3)
cv2.putText(frame, str(eqtn), (200, 200), cv2.FONT_HERSHEY_COMPLEX, 3, (200, 100, 200), 3)
frame[0:100] = opd
cv2.circle(frame, index_tip, 10, (0, 0, 255), -1)
cv2.imshow("Frame", frame)
if key==27:
break
check_cnt+=1
cam.release()
cv2.destroyAllWindows()
cam = cv2.VideoCapture(0)
fsize = (600, 820)
show_every = 15
check_cnt = 0
operators = ["+", "-", "/", "*", "C", "="]
ops = len(operators)
opd = np.zeros((100, fsize[1], 3), dtype="uint8")
cols = np.linspace(0, fsize[1]-1, ops+1).astype(np.int64)
cols = [(cols[i], cols[i+1]) for i in range(len(cols)-1)]
opd = np.zeros((100, fsize[1], 3), dtype="uint8")
l1,t1,r1,b1=(50, 200, 350, 500)
l2,t2,r2,b2=(500, 200, 800, 500)
for i, col in enumerate(cols):
opd[:, 0:2] =[200, 200, 200]
opd[:, col[0]-2:col[0]+2] = [200, 200, 200]
opd[:, col[1]-2:col[1]+2] = [200, 200, 200]
cv2.putText(opd, operators[i], (int((col[0]+col[1])/2), 60), cv2.FONT_HERSHEY_COMPLEX, 1, (200, 100, 200), 3)
mp_drawing = mp.solutions.drawing_utils
mp_hands = mp.solutions.hands
eqtn = ""
values = []
pred=0
itips = []
op=None
output = None
with mp_hands.Hands(static_image_mode = True,
max_num_hands=2,
min_detection_confidence=0.2) as hands:
while cam.isOpened():
ret, frame = cam.read()
key = cv2.waitKey(1)&0xFF
if not ret:
continue
buffer = 0
frame = cv2.flip(frame, 1)
frame = cv2.resize(frame, fsize[::-1])
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
res = hands.process(rgb)
fv=10
cv2.rectangle(frame, (l1, t1), (r1, b1), (0, 0, 255), 1)
cv2.rectangle(frame, (l2, t2), (r2, b2), (0, 0, 255), 1)
h, w = frame.shape[:-1]
index_tip=[-1,-1]
fcounts = {}
move = False
if res.multi_hand_landmarks:
for i, hand_landmarks in enumerate(res.multi_hand_landmarks):
bv = 0
mp_drawing.draw_landmarks(frame, hand_landmarks, mp_hands.HAND_CONNECTIONS)
index_tip = mp_drawing._normalized_to_pixel_coordinates(
hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP].x,
hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP].y,
w, h)
index_pip = mp_drawing._normalized_to_pixel_coordinates(
hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_PIP].x,
hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_PIP].y,
w, h)
middle_tip = mp_drawing._normalized_to_pixel_coordinates(
hand_landmarks.landmark[mp_hands.HandLandmark.MIDDLE_FINGER_TIP].x,
hand_landmarks.landmark[mp_hands.HandLandmark.MIDDLE_FINGER_TIP].y,
w, h)
middle_pip = mp_drawing._normalized_to_pixel_coordinates(
hand_landmarks.landmark[mp_hands.HandLandmark.MIDDLE_FINGER_PIP].x,
hand_landmarks.landmark[mp_hands.HandLandmark.MIDDLE_FINGER_PIP].y,
w, h)
ring_tip = mp_drawing._normalized_to_pixel_coordinates(
hand_landmarks.landmark[mp_hands.HandLandmark.RING_FINGER_TIP].x,
hand_landmarks.landmark[mp_hands.HandLandmark.RING_FINGER_TIP].y,
w, h)
ring_pip = mp_drawing._normalized_to_pixel_coordinates(
hand_landmarks.landmark[mp_hands.HandLandmark.RING_FINGER_PIP].x,
hand_landmarks.landmark[mp_hands.HandLandmark.RING_FINGER_PIP].y,
w, h)
pinky_tip = mp_drawing._normalized_to_pixel_coordinates(
hand_landmarks.landmark[mp_hands.HandLandmark.PINKY_TIP].x,
hand_landmarks.landmark[mp_hands.HandLandmark.PINKY_TIP].y,
w, h)
pinky_pip = mp_drawing._normalized_to_pixel_coordinates(
hand_landmarks.landmark[mp_hands.HandLandmark.PINKY_PIP].x,
hand_landmarks.landmark[mp_hands.HandLandmark.PINKY_PIP].y,
w, h)
thumb_tip = mp_drawing._normalized_to_pixel_coordinates(
hand_landmarks.landmark[mp_hands.HandLandmark.THUMB_TIP].x,
hand_landmarks.landmark[mp_hands.HandLandmark.THUMB_TIP].y,
w, h)
thumb_ip = mp_drawing._normalized_to_pixel_coordinates(
hand_landmarks.landmark[mp_hands.HandLandmark.THUMB_IP].x,
hand_landmarks.landmark[mp_hands.HandLandmark.THUMB_IP].y,
w, h)
wrist = mp_drawing._normalized_to_pixel_coordinates(
hand_landmarks.landmark[mp_hands.HandLandmark.WRIST].x,
hand_landmarks.landmark[mp_hands.HandLandmark.WRIST].y,
w, h)
if index_pip is not None and index_tip is not None:
if index_tip[1]<100:
itp = [v for v in cols if index_tip[0]>v[0] and index_tip[0]<v[1]]
if len(itp)>0:
itp = cols.index(itp[0])
op = operators[itp]
if index_tip[1]-index_pip[1]<fv:
bv+=1
if middle_pip is not None and middle_tip is not None:
if middle_tip[1]-middle_pip[1]<fv:
bv+=1
if ring_pip is not None and ring_tip is not None:
if ring_tip[1]-ring_pip[1]<fv:
bv+=1
if pinky_pip is not None and pinky_tip is not None:
if pinky_tip[1]-pinky_pip[1]<fv:
bv+=1
if thumb_tip is not None and index_pip is not None:
if euclidean(index_pip, thumb_tip)>60:
bv+=1
fcounts[i] = bv
if wrist is not None:
if b1>wrist[1] and (t1+b1)/2<wrist[1]:
pass
elif b2>wrist[1] and (t2+b2)/2<wrist[1]:
pass
else:
fcounts[i] = 0
if index_pip is not None and thumb_tip is not None:
if euclidean(index_pip, thumb_tip)<60:
move = True
buffer = sum(list(fcounts.values()))
if move:
buffer = 0
else:
values.append(buffer)
if show_every<check_cnt:
check_cnt=0
if len(values)>1 and buffer!=0:
d = dict(Counter(values))
pred = sorted(d.items(), key=lambda x: x[1], reverse=True)[0][0]
if output is not None:
output=None
eqtn=""
#pred = sorted(values)[-1]
eqtn+=str(pred)
if pred != 0:
cv2.putText(frame, str(pred), (50, 200), cv2.FONT_HERSHEY_COMPLEX, 3, (200, 100, 200), 3)
if op is not None and op != "C":
if len(eqtn)>0:
if eqtn[-1]!=op:
if eqtn[-1] in operators:
eqtn = eqtn[:-1]
if op=="=":
try:
output=eval(eqtn)
print(f"Output: {output}")
eqtn=str(output)
except:
print(f"Error in equation: {eqtn}")
else:
if output is not None:
eqtn=str(output)
output=None
eqtn+=op
op=None
if op == "C":
output=None
eqtn=""
values=[]
op=None
buffer=0
pred=0
values=[]
if output is not None:
cv2.putText(frame, str(output), (80, 200), cv2.FONT_HERSHEY_COMPLEX, 3, (200, 100, 100), 3)
cv2.putText(frame, str(eqtn), (200, 200), cv2.FONT_HERSHEY_COMPLEX, 3, (200, 100, 200), 3)
frame[0:100] = opd
cv2.circle(frame, index_tip, 10, (0, 0, 255), -1)
cv2.imshow("Frame", frame)
if key==27:
break
check_cnt+=1
cam.release()
cv2.destroyAllWindows()
Output: 555555555550
Output: 555555555550