├── 983794168.jpg ├── fash mesh.py ├── face detection.py ├── Volume Control.py ├── paintWindow.py ├── part 1.py └── pose_estimation.py /983794168.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jagathratchakan/Open-Cv/HEAD/983794168.jpg -------------------------------------------------------------------------------- /fash mesh.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import mediapipe as mp 3 | 4 | 5 | class Fashmesh(): 6 | 7 | def __init__(self): 8 | self.mpDraw = mp.solutions.drawing_utils 9 | self.mpFashmesh = mp.solutions.face_mesh 10 | self.fashmesh = self.mpFashmesh.FaceMesh() 11 | 12 | def findface(self, frame, draw=True): 13 | img = cv.cvtColor(frame, cv.COLOR_BGR2RGB) 14 | self.results = self.fashmesh.process(img) 15 | 16 | if self.results.multi_face_landmarks: 17 | for facelms in self.results.multi_face_landmarks: 18 | self.mpDraw.draw_landmarks(frame, facelms , self.mpFashmesh.FACEMESH_CONNECTION) 19 | 20 | return frame 21 | 22 | 23 | 24 | def main(): 25 | video = cv.VideoCapture(0) 26 | faceMesh = Fashmesh() 27 | while True: 28 | _, frame = video.read() 29 | frame = faceMesh.findface(frame) 30 | cv.imshow("Test", frame) 31 | k = cv.waitKey(1) 32 | if k == ord("q"): 33 | break 34 | 35 | video.release() 36 | cv.destroyAllWindows() 37 | 38 | if __name__ == "__main__": 39 | main() -------------------------------------------------------------------------------- /face detection.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import mediapipe as mp 3 | 4 | 5 | class position(): 6 | def __init__(self): 7 | self.mpface = mp.solutions.face_detection 8 | self.face = self.mpface.FaceDetection(0.75) 9 | self.myDraw = mp.solutions.drawing_utils 10 | 11 | def findface(self,frame,draw=True): 12 | imgRBG = cv.cvtColor(frame, cv.COLOR_BGR2RGB) 13 | self.results = self.face.process(imgRBG) 14 | 15 | if self.results.detections: 16 | for id , detection in enumerate(self.results.detections): 17 | box = detection.location_data.relative_bounding_box 18 | h,w,c = frame.shape 19 | 20 | bbox = int(box.xmin*w) , int(box.ymin*h), int(box.width*w),int(box.height*h) 21 | 22 | cv.rectangle(frame,bbox,(255,0,255),2) 23 | return frame 24 | 25 | 26 | 27 | 28 | def main(): 29 | cap = cv.VideoCapture(0) 30 | position_obj = position() 31 | while True: 32 | _, frame = cap.read() 33 | frame = position_obj.findface(frame) 34 | cv.imshow("Test", frame) 35 | k = cv.waitKey(1) 36 | if k == ord("q"): 37 | break 38 | 39 | cap.release() 40 | cv.destroyAllWindows() 41 | 42 | 43 | if __name__ == "__main__": 44 | main() -------------------------------------------------------------------------------- /Volume Control.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import os 3 | import mediapipe as mp 4 | import HandTracking as ht 5 | import math 6 | 7 | 8 | 9 | video = cv.VideoCapture(0) 10 | detector = ht.handDetect() 11 | 12 | tipIds = [4,8,12,16,20] 13 | 14 | while True: 15 | _,frame=video.read() 16 | 17 | frame = detector.findhand(frame) 18 | lmlist = detector.findpostion(frame,draw=False) 19 | 20 | if len(lmlist) != 0: 21 | fingures = [] 22 | 23 | if lmlist[tipIds[0]][1] > lmlist[tipIds[0] - 1][1]: 24 | fingures.append(1) 25 | else: 26 | fingures.append(0) 27 | 28 | for id in range(1,5): 29 | if lmlist[tipIds[id]][2] < lmlist[tipIds[id]-2][2]: 30 | fingures.append(1) 31 | else: 32 | fingures.append(0) 33 | 34 | if fingures[0] == 1: 35 | print("Thump Fingure") 36 | elif fingures[1] == 1: 37 | print("Index Fingure") 38 | elif fingures[2] == 1: 39 | print("Middle Fingure") 40 | elif fingures[3] == 1: 41 | print("Ring Finger") 42 | elif fingures[4] == 1: 43 | print("Pink Fingure") 44 | else: 45 | print("Unknown") 46 | 47 | cv.imshow("Test", frame) 48 | k = cv.waitKey(1) 49 | if k == ord("q"): 50 | break 51 | 52 | video.release() 53 | cv.destroyAllWindows() -------------------------------------------------------------------------------- /paintWindow.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import os 3 | import mediapipe as mp 4 | import HandTracking as ht 5 | import math 6 | import numpy as np 7 | 8 | 9 | FolderPath = "img" 10 | myList = os.listdir(FolderPath) 11 | overlayout = [] 12 | 13 | 14 | for impath in myList: 15 | image = cv.imread(f'{FolderPath}/{impath}') 16 | overlayout.append(image) 17 | 18 | header = overlayout[3] 19 | 20 | video = cv.VideoCapture(0) 21 | detector = ht.handDetect() 22 | 23 | xp , yp = 0,0 24 | imgCanvas = np.zeros((720,1200, 3),np.uint8) 25 | 26 | 27 | tipIds = [4,8,12,16,20] 28 | def make_720(): 29 | video.set(3,1080) 30 | video.set(4,720) 31 | 32 | 33 | while True: 34 | _,frame=video.read() 35 | #frame[170:1080, 0:400] = header 36 | frame = detector.findhand(frame) 37 | lmlist = detector.findpostion(frame,draw=False) 38 | 39 | if len(lmlist) != 0: 40 | x1,y1 = lmlist[8][1:] 41 | x2, y2 = lmlist[12][1:] 42 | fingures = detector.fingureup() 43 | #print(fingures) 44 | #Erase mode 45 | if fingures[1] and fingures[2] == False: 46 | cv.circle(frame, (x1, y1), 10, (0, 50, 210), cv.FILLED) 47 | if xp == 0 and yp == 0: 48 | xp, yp = x1, y1 49 | cv.line(frame, (xp, yp), (x1, y1), (130, 50, 120), 15) 50 | cv.line(imgCanvas, (xp, yp), (x1, y1), (130, 50, 120), 10) 51 | xp, yp = x1, y1 52 | elif fingures[1] and fingures[2]: 53 | cv.line(frame, (xp, yp), (x1, y1), (0, 0, 0), 15) 54 | cv.line(imgCanvas, (xp, yp), (x1, y1), (0, 0, 0), 40) 55 | 56 | else: 57 | pass 58 | 59 | cv.imshow("Test", frame) 60 | cv.imshow("cam",imgCanvas) 61 | k = cv.waitKey(1) 62 | if k == ord("q"): 63 | break 64 | 65 | video.release() 66 | cv.destroyAllWindows() -------------------------------------------------------------------------------- /part 1.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import numpy as np 3 | import os 4 | 5 | #img = cv.imread("C:/Users/Sanjay/Downloads/t.jpg") 6 | #cv.imshow("Test",img) 7 | 8 | haar_cascade = cv.CascadeClassifier('haar_face.xml') 9 | # Contour Detection 10 | def gray(): 11 | gray_1 = cv.cvtColor(img,cv.COLOR_BGR2GRAY) 12 | cv.imshow("test1",gray_1) 13 | 14 | def canny(): 15 | can = cv.Canny(img,150,200) 16 | cv.imshow("test2",can) 17 | ################################################################################################################### 18 | # DETECT THE HUMAN FACES BY USING CV2 AND HAAR CASACADE DATASET FOR FACIAL MARKS 19 | def face(): 20 | haar_cascade = cv.CascadeClassifier('haar_face.xml') 21 | 22 | face_rect = haar_cascade.detectMultiScale(img,scaleFactor=1.1,minNeighbors=6) 23 | for (x,y,w,h) in face_rect: 24 | cv.rectangle(img,(x,y),(x+w,y+h), (0,255,0) , thickness=2) 25 | 26 | cv.imshow("Detected",img) 27 | ################################################################################################################## 28 | 29 | def identify(): 30 | people = ["Barack Obama","Donald Trump"] 31 | Dir = r"E:\img" 32 | global features 33 | features = [] 34 | labels = [] 35 | 36 | for person in people: 37 | path = os.path.join(Dir,person) 38 | label = people.index(person) 39 | 40 | for img in os.listdir(path): 41 | img_path = os.path.join(path,img) 42 | 43 | img_arr = cv.imread(img_path) 44 | gray = cv.cvtColorTwoPlane(img_arr,cv.COLOR_BGR2GRAY) 45 | 46 | face_rect = haar_cascade.detectMultiScale(img, scaleFactor=1.1, minNeighbors=3) 47 | for (x, y, w, h) in face_rect: 48 | faces_rol = gray[y:y+h,x:x+w] 49 | features.append(faces_rol) 50 | labels.append(label) 51 | 52 | 53 | identify() 54 | print(f'Length of the features = {len(features)}') 55 | cv.waitKey(0) 56 | -------------------------------------------------------------------------------- /pose_estimation.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import mediapipe as mp 3 | 4 | """cap = cv.VideoCapture(0) 5 | 6 | mpPose = mp.solutions.pose 7 | pose = mpPose.Pose() 8 | myDraw = mp.solutions.drawing_utils 9 | while True: 10 | _,frame=cap.read() 11 | imgRBG = cv.cvtColor(frame,cv.COLOR_BGR2RGB) 12 | results = pose.process(imgRBG) 13 | 14 | if results.pose_landmarks: 15 | myDraw.draw_landmarks(frame,results.pose_landmarks,mpPose.POSE_CONNECTIONS) 16 | 17 | cv.imshow("Test", frame) 18 | k = cv.waitKey(1) 19 | if k == ord("q"): 20 | break 21 | 22 | cap.release() 23 | cv.destroyAllWindows()""" 24 | class position(): 25 | def __init__(self): 26 | self.mpPose = mp.solutions.pose 27 | self.pose = self.mpPose.Pose() 28 | self.myDraw = mp.solutions.drawing_utils 29 | 30 | def findBody(self,frame,draw=True): 31 | imgRBG = cv.cvtColor(frame, cv.COLOR_BGR2RGB) 32 | self.results = self.pose.process(imgRBG) 33 | 34 | if self.results.pose_landmarks: 35 | self.myDraw.draw_landmarks(frame, self.results.pose_landmarks, self.mpPose.POSE_CONNECTIONS) 36 | return frame 37 | 38 | def findpostion(self,frame,handNo=0,draw= True): 39 | 40 | lmlist = [] 41 | 42 | if self.results.pose_landmarks: 43 | for id , ln in enumerate(self.results.pose_landmarks.landmark): 44 | h,w,c = frame.shape 45 | cx,cy =int(ln.x*w), int(ln.y*h) 46 | lmlist.append([id,cx,cy]) 47 | if draw: 48 | cv.circle(frame,(cx,cy),5,(255,0,255),cv.FILLED) 49 | return lmlist 50 | 51 | def main(): 52 | cap = cv.VideoCapture(0) 53 | position_obj = position() 54 | 55 | while True: 56 | _, frame = cap.read() 57 | frame = position_obj.findBody(frame) 58 | lmlist = position_obj.findpostion(frame,draw=False) 59 | if len(lmlist) !=0: 60 | print(lmlist[14]) 61 | cv.circle(frame,(lmlist[14][1],lmlist[14][2]), 15, (0, 0, 255), cv.FILLED) 62 | cv.imshow("Test", frame) 63 | k = cv.waitKey(1) 64 | if k == ord("q"): 65 | break 66 | 67 | cap.release() 68 | cv.destroyAllWindows() 69 | 70 | 71 | if __name__ == "__main__": 72 | main() 73 | --------------------------------------------------------------------------------