Signals
Signals
Signals
import copy
import argparse
import itertools
from collections import Counter
from collections import deque
import cv2
import numpy as np
import mediapipe as mp
def get_args():
parser = argparse.ArgumentParser()
#function defines a command-line argument parser using the argparse
module in Python.
#This function is typically used to parse command-line arguments when
running a script or program.
parser.add_argument('--use_static_image_mode', action='store_true')
parser.add_argument("--min_detection_confidence",
help='min_detection_confidence',
type=float,
default=0.7)
parser.add_argument("--min_tracking_confidence",
help='min_tracking_confidence',
type=int,
default=0.5)
args = parser.parse_args()
return args
#python script.py --width 1280 --height 720 --use_static_image_mode
def main():
# Argument parsing // if none given takes the default
args = get_args()
cap_device = args.device
cap_width = args.width
cap_height = args.height
use_static_image_mode = args.use_static_image_mode
min_detection_confidence = args.min_detection_confidence
min_tracking_confidence = args.min_tracking_confidence
use_brect = True
#an option for choosing the need of a bounding box
# Camera preparation
cap = cv2.VideoCapture(cap_device)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, cap_width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, cap_height)
keypoint_classifier = KeyPointClassifier()
point_history_classifier = PointHistoryClassifier()
'model/point_history_classifier/point_history_classifier_label.csv',
encoding='utf-8-sig') as f:
point_history_classifier_labels = csv.reader(f)
point_history_classifier_labels = [
str(row[0]) for row in point_history_classifier_labels if row!
=None
]
# FPS Measurement------------------------------------------------
cvFpsCalc = CvFpsCalc(buffer_len=10)
#buffer to smooth out FPS calculations, especially if you want to display
#an average FPS over a certain number of frames rather than the
instantaneous FPS.
while True:
fps = cvFpsCalc.get()
# Camera capture
#####################################################
ret, image = cap.read()
if not ret:
break
image = cv2.flip(image, 1) # Mirror display
debug_image = copy.deepcopy(image)
'''
In Python, you can create a deep copy of an image using the copy
module or by using NumPy's copy function. A deep copy
creates a new object that is a copy of the original object, and it
recursively copies all nested objects within it.'''
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# rgb as input to mediapipe
image.flags.writeable = False
#setting the flags to be false
#passing the image as input to the called model with initial
thresholds set
results = hands.process(image)
#Setting us to render on the img/draw stuff on it
image.flags.writeable = True
# Drawing part
debug_image = draw_bounding_rect(use_brect, debug_image,
brect)
debug_image = draw_landmarks(debug_image, landmark_list)
debug_image = draw_info_text(
debug_image,
brect,
handedness,
keypoint_classifier_labels[hand_sign_id],
point_history_classifier_labels[most_common_fg_id[0][0]],
)
else:
point_history.append([0, 0])
# Screen reflection
#############################################################
cv2.imshow('Hand Gesture Recognition', debug_image)
cap.release()
cv2.destroyAllWindows()
x, y, w, h = cv2.boundingRect(landmark_array)
#x, y, w, h = cv2.boundingRect(landmark_array): Calculates the bounding
rectangle (x, y, width, height) around the set of landmarks using the
cv2.boundingRect function.
landmark_point = []
# Keypoint
for _, landmark in enumerate(landmarks.landmark):
landmark_x = min(int(landmark.x * image_width), image_width - 1)
landmark_y = min(int(landmark.y * image_height), image_height - 1)
# landmark_z = landmark.z
landmark_point.append([landmark_x, landmark_y])
return landmark_point
def pre_process_landmark(landmark_list):
temp_landmark_list = copy.deepcopy(landmark_list)
# Normalization
max_value = max(list(map(abs, temp_landmark_list)))
def normalize_(n):
return n / max_value
return temp_landmark_list
temp_point_history = copy.deepcopy(point_history)
temp_point_history[index][0] = (temp_point_history[index][0] -
base_x) / image_width
temp_point_history[index][1] = (temp_point_history[index][1] -
base_y) / image_height
return temp_point_history
if len(landmark_point) > 0:
#To check if it actually exists
# Thumb
cv2.line(image, tuple(landmark_point[2]), tuple(landmark_point[3]),
(0, 0, 0), 6) #6 is the thickness of line in pixels
cv2.line(image, tuple(landmark_point[2]), tuple(landmark_point[3]),
(255, 255, 255), 2)
#Atempting to get white line with black borders
#Thumb line1
cv2.line(image, tuple(landmark_point[3]), tuple(landmark_point[4]),
(0, 0, 0), 6)
cv2.line(image, tuple(landmark_point[3]), tuple(landmark_point[4]),
(255, 255, 255), 2)
#Top of that thumb line 2
# Index finger
cv2.line(image, tuple(landmark_point[5]), tuple(landmark_point[6]),
(0, 0, 0), 6)
cv2.line(image, tuple(landmark_point[5]), tuple(landmark_point[6]),
(255, 255, 255), 2)
# Index line 1
cv2.line(image, tuple(landmark_point[6]), tuple(landmark_point[7]),
(0, 0, 0), 6)
cv2.line(image, tuple(landmark_point[6]), tuple(landmark_point[7]),
(255, 255, 255), 2)
# Index line 2 on top of that
cv2.line(image, tuple(landmark_point[7]), tuple(landmark_point[8]),
(0, 0, 0), 6)
cv2.line(image, tuple(landmark_point[7]), tuple(landmark_point[8]),
(255, 255, 255), 2)
# Index line 3 on top of that as well
# Middle finger
cv2.line(image, tuple(landmark_point[9]), tuple(landmark_point[10]),
(0, 0, 0), 6)
cv2.line(image, tuple(landmark_point[9]), tuple(landmark_point[10]),
(255, 255, 255), 2)
#middle line 1
cv2.line(image, tuple(landmark_point[10]), tuple(landmark_point[11]),
(0, 0, 0), 6)
cv2.line(image, tuple(landmark_point[10]), tuple(landmark_point[11]),
(255, 255, 255), 2)
#middle line 2 on top of that
cv2.line(image, tuple(landmark_point[11]), tuple(landmark_point[12]),
(0, 0, 0), 6)
cv2.line(image, tuple(landmark_point[11]), tuple(landmark_point[12]),
(255, 255, 255), 2)
#middle line 3 on top of that as well
# Ring finger
cv2.line(image, tuple(landmark_point[13]), tuple(landmark_point[14]),
(0, 0, 0), 6)
cv2.line(image, tuple(landmark_point[13]), tuple(landmark_point[14]),
(255, 255, 255), 2)
cv2.line(image, tuple(landmark_point[14]), tuple(landmark_point[15]),
(0, 0, 0), 6)
cv2.line(image, tuple(landmark_point[14]), tuple(landmark_point[15]),
(255, 255, 255), 2)
cv2.line(image, tuple(landmark_point[15]), tuple(landmark_point[16]),
(0, 0, 0), 6)
cv2.line(image, tuple(landmark_point[15]), tuple(landmark_point[16]),
(255, 255, 255), 2)
# Little finger
cv2.line(image, tuple(landmark_point[17]), tuple(landmark_point[18]),
(0, 0, 0), 6)
cv2.line(image, tuple(landmark_point[17]), tuple(landmark_point[18]),
(255, 255, 255), 2)
cv2.line(image, tuple(landmark_point[18]), tuple(landmark_point[19]),
(0, 0, 0), 6)
cv2.line(image, tuple(landmark_point[18]), tuple(landmark_point[19]),
(255, 255, 255), 2)
cv2.line(image, tuple(landmark_point[19]), tuple(landmark_point[20]),
(0, 0, 0), 6)
cv2.line(image, tuple(landmark_point[19]), tuple(landmark_point[20]),
(255, 255, 255), 2)
# Palm
cv2.line(image, tuple(landmark_point[0]), tuple(landmark_point[1]),
(0, 0, 0), 6)
cv2.line(image, tuple(landmark_point[0]), tuple(landmark_point[1]),
(255, 255, 255), 2)
cv2.line(image, tuple(landmark_point[1]), tuple(landmark_point[2]),
(0, 0, 0), 6)
cv2.line(image, tuple(landmark_point[1]), tuple(landmark_point[2]),
(255, 255, 255), 2)
cv2.line(image, tuple(landmark_point[2]), tuple(landmark_point[5]),
(0, 0, 0), 6)
cv2.line(image, tuple(landmark_point[2]), tuple(landmark_point[5]),
(255, 255, 255), 2)
cv2.line(image, tuple(landmark_point[5]), tuple(landmark_point[9]),
(0, 0, 0), 6)
cv2.line(image, tuple(landmark_point[5]), tuple(landmark_point[9]),
(255, 255, 255), 2)
cv2.line(image, tuple(landmark_point[9]), tuple(landmark_point[13]),
(0, 0, 0), 6)
cv2.line(image, tuple(landmark_point[9]), tuple(landmark_point[13]),
(255, 255, 255), 2)
cv2.line(image, tuple(landmark_point[13]), tuple(landmark_point[17]),
(0, 0, 0), 6)
cv2.line(image, tuple(landmark_point[13]), tuple(landmark_point[17]),
(255, 255, 255), 2)
cv2.line(image, tuple(landmark_point[17]), tuple(landmark_point[0]),
(0, 0, 0), 6)
cv2.line(image, tuple(landmark_point[17]), tuple(landmark_point[0]),
(255, 255, 255), 2)
# Key Points
for index, landmark in enumerate(landmark_point):
if index == 0: #Wrist
cv2.circle(image, (landmark[0], landmark[1]), 5, (255, 255, 255),
-1) #completefill
cv2.circle(image, (landmark[0], landmark[1]), 5, (0, 0, 0), 1)
if index == 1: #Thumb cmc
cv2.circle(image, (landmark[0], landmark[1]), 5, (255, 255, 255),
-1)
cv2.circle(image, (landmark[0], landmark[1]), 5, (0, 0, 0), 1)
if index == 2: # Thumb mcp
cv2.circle(image, (landmark[0], landmark[1]), 5, (255, 255, 255),
-1)
cv2.circle(image, (landmark[0], landmark[1]), 5, (0, 0, 0), 1)
if index == 3: # Thumb ip
cv2.circle(image, (landmark[0], landmark[1]), 5, (255, 255, 255),
-1)
cv2.circle(image, (landmark[0], landmark[1]), 5, (0, 0, 0), 1)
if index == 4: # Thumb tip
cv2.circle(image, (landmark[0], landmark[1]), 8, (255, 255, 255),
-1)
cv2.circle(image, (landmark[0], landmark[1]), 8, (0, 0, 0), 1)
if index == 5: # Index mcp
cv2.circle(image, (landmark[0], landmark[1]), 5, (255, 255, 255),
-1)
cv2.circle(image, (landmark[0], landmark[1]), 5, (0, 0, 0), 1)
if index == 6: # index pip
cv2.circle(image, (landmark[0], landmark[1]), 5, (255, 255, 255),
-1)
cv2.circle(image, (landmark[0], landmark[1]), 5, (0, 0, 0), 1)
if index == 7: # index dip
cv2.circle(image, (landmark[0], landmark[1]), 5, (255, 255, 255),
-1)
cv2.circle(image, (landmark[0], landmark[1]), 5, (0, 0, 0), 1)
if index == 8: # index tip
cv2.circle(image, (landmark[0], landmark[1]), 8, (255, 255, 255),
-1)
cv2.circle(image, (landmark[0], landmark[1]), 8, (0, 0, 0), 1)
if index == 9: # middle mcp
cv2.circle(image, (landmark[0], landmark[1]), 5, (255, 255, 255),
-1)
cv2.circle(image, (landmark[0], landmark[1]), 5, (0, 0, 0), 1)
if index == 10: # middle pip
cv2.circle(image, (landmark[0], landmark[1]), 5, (255, 255, 255),
-1)
cv2.circle(image, (landmark[0], landmark[1]), 5, (0, 0, 0), 1)
if index == 11: # middle dip
cv2.circle(image, (landmark[0], landmark[1]), 5, (255, 255, 255),
-1)
cv2.circle(image, (landmark[0], landmark[1]), 5, (0, 0, 0), 1)
if index == 12: # middle tip
cv2.circle(image, (landmark[0], landmark[1]), 8, (255, 255, 255),
-1)
cv2.circle(image, (landmark[0], landmark[1]), 8, (0, 0, 0), 1)
if index == 13: # ring mcp
cv2.circle(image, (landmark[0], landmark[1]), 5, (255, 255, 255),
-1)
cv2.circle(image, (landmark[0], landmark[1]), 5, (0, 0, 0), 1)
if index == 14: # ring pip
cv2.circle(image, (landmark[0], landmark[1]), 5, (255, 255, 255),
-1)
cv2.circle(image, (landmark[0], landmark[1]), 5, (0, 0, 0), 1)
if index == 15: # ring dip
cv2.circle(image, (landmark[0], landmark[1]), 5, (255, 255, 255),
-1)
cv2.circle(image, (landmark[0], landmark[1]), 5, (0, 0, 0), 1)
if index == 16: # ring tip
cv2.circle(image, (landmark[0], landmark[1]), 8, (255, 255, 255),
-1)
cv2.circle(image, (landmark[0], landmark[1]), 8, (0, 0, 0), 1)
if index == 17: # little mcp
cv2.circle(image, (landmark[0], landmark[1]), 5, (255, 255, 255),
-1)
cv2.circle(image, (landmark[0], landmark[1]), 5, (0, 0, 0), 1)
if index == 18: # little pip
cv2.circle(image, (landmark[0], landmark[1]), 5, (255, 255, 255),
-1)
cv2.circle(image, (landmark[0], landmark[1]), 5, (0, 0, 0), 1)
if index == 19: # little dip
cv2.circle(image, (landmark[0], landmark[1]), 5, (255, 255, 255),
-1)
cv2.circle(image, (landmark[0], landmark[1]), 5, (0, 0, 0), 1)
if index == 20: # little tip
cv2.circle(image, (landmark[0], landmark[1]), 8, (255, 255, 255),
-1)
cv2.circle(image, (landmark[0], landmark[1]), 8, (0, 0, 0), 1)
return image
#RGB values (e.g., white is (255, 255, 255), and black is (0, 0, 0)).
return image
info_text = handedness.classification[0].label[0:]
if hand_sign_text != "":
info_text = info_text + ':' + hand_sign_text
cv2.putText(image, info_text, (brect[0] + 5, brect[1] - 4),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 1,
cv2.LINE_AA)
if finger_gesture_text != "":
cv2.putText(image, "Finger Gesture:" + finger_gesture_text, (10, 60),
cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 0, 0), 4, cv2.LINE_AA)
cv2.putText(image, "Finger Gesture:" + finger_gesture_text, (10, 60),
cv2.FONT_HERSHEY_SIMPLEX, 1.0, (255, 255, 255), 2,
cv2.LINE_AA)
return image
return image
if __name__ == '__main__':
main()
# Volume controller:
import mediapipe as mp
import cv2
# pycaw. This library will handle the controlling of our system volume.
#from cvzone.HandTrackingModule import HandDetector
import math
import numpy as np
mpHands = mp.solutions.hands
mpDraw = mp.solutions.drawing_utils
class HandDetector:
def __init__(self, max_num_hands=2, min_detection_confidence=0.5,
min_tracking_confidence=0.5):
self.hands = mpHands.Hands(max_num_hands=max_num_hands,
min_detection_confidence=min_detection_confidence,
min_tracking_confidence=min_tracking_confidence)
if draw:
mpDraw.draw_landmarks(originalImage, hand,
mpHands.HAND_CONNECTIONS)
#image=draw_landmarks(originalImage,hand.landmark)
return landMarkList
webcamFeed = cv2.VideoCapture(0)
handDetector = HandDetector(min_detection_confidence=0.7)
while True:
status, image = webcamFeed.read()
handLandmarks = handDetector.findHandLandMarks(image=image, draw=True)
if(len(handLandmarks) != 0):
#for volume control we need 4th and 8th landmark
cv2.imshow("Volume", image)
key = cv2.waitKey(1) & 0xFF # Add the bitwise AND operation
if key == ord('q'):
break
cv2.destroyAllWindows()
webcamFeed.release()
cap=cv2.VideoCapture(0)
cap.set(3,1280)
cap.set(4,720)
folderPath="sources"
folder=os.listdir(folderPath)
print(folder)
overlayList=[]
for imPath in folder:
image=cv2.imread(f'{folderPath}\{imPath}')
print(f'{folderPath}\{imPath}')
overlayList.append(image)
previous=0
#detector=htm.imgDetector(detectionCon=0.7)
detector=FindHands()
while True:
ret,img =cap.read()
#img=htm.FindHands(img)
#h,w,c=overlayList[0].shape
#img[0:h,0:w]=overlayList[0]
current=time.time()
fps=1/(current-previous)
previous=current
cv2.putText(img,f'FPS:{int(fps)}',(430,70),cv2.FONT_HERSHEY_SIMPLEX,1,
(255,0,0),2)
cv2.imshow("Image",img)
cv2.waitKey(1)