#guesture.py
import os
import cv2
import mediapipe as mp
import time
import numpy as np
import math
cap = cv2.VideoCapture("assets/guesture.mp4")
# Define the codec and create VideoWriter object
fps = 25.175
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
size = (int(frame_width), int(frame_height))
print(size)
fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
path = 'C:/Users/zchen/PycharmProjects/opencv/googleNet/record'
out = cv2.VideoWriter()
success = out.open(os.path.join(path, "guesture volume.mov"), fourcc, fps, size, True)
mpHands = mp.solutions.hands
hands = mpHands.Hands()
mpDraw = mp.solutions.drawing_utils
pTime = 0
while True:
ret, frame = cap.read()
if ret == False: break
cTime = time.time()
fps = 1/(cTime - pTime)
pTime = cTime
frame_RGB = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
results = hands.process(frame_RGB)
cv2.putText(frame, f'FPS: {int(fps)}', (100, 100), cv2.FONT_HERSHEY_COMPLEX, 3,
(255, 0, 0), 3)
h, w, c = frame.shape
vol = 0
if results.multi_hand_landmarks:
for handLms in results.multi_hand_landmarks:
"""
for id, lm in enumerate(handLms.landmark):
#print(id, lm)
h, w, c = frame.shape
cx, cy = int(lm.x * w), int(lm.y * h)
#print(id, cx, cy)
#draw circle at thumb tip
if id == 4:
cv2.circle(frame, (cx, cy), 15, (255, 0, 255), cv2.FILLED)
"""
h_marks = handLms.landmark
#print(h_marks[4], h_marks[8])
x1, y1 = int(h_marks[4].x * w), int(h_marks[4].y * h)
x2, y2 = int(h_marks[8].x * w), int(h_marks[8].y * h)
cx, cy = (x1+x2)//2, (y1+y2)//2
#print(x1, y1)
cv2.circle(frame, (x1, y1), 30, (255, 0, 255), cv2.FILLED)
cv2.circle(frame, (x2, y2), 30, (255, 0, 255), cv2.FILLED)
cv2.line(frame, (x1, y1), (x2, y2), (255, 0, 255), 3)
cv2.circle(frame, (cx, cy), 30, (255, 0, 255), cv2.FILLED)
length = math.hypot(x2 - x1, y2 - y1)
#print(length)
if length < 100:
cv2.circle(frame, (cx, cy), 30, (0, 255, 0), cv2.FILLED)
vol = np.interp(length, [100, 800], [100, 1000])
mpDraw.draw_landmarks(frame, handLms, mpHands.HAND_CONNECTIONS)
cv2.rectangle(frame, (100, 200), (1000, 300), (255, 0, 0), 3)
cv2.rectangle(frame, (100, 200), (int(vol), 300), (255, 0, 0), cv2.FILLED)
cv2.putText(frame, f'Vol: {int((vol-100)//9)}%', (600, 100), cv2.FONT_HERSHEY_COMPLEX, 3,
(255, 0, 0), 3)
cv2.imshow("frame", frame)
out.write(frame)
if cv2.waitKey(1) == ord('q'):
break
if cv2.waitKey(1) == ord('p'):
cv2.waitKey(-1) # wait until any key is pressed
cap.release()
cv2.destroyAllWindows()
reference:
hand detection
No comments:
Post a Comment