-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpaint.py
77 lines (45 loc) · 1.81 KB
/
paint.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import cv2
import mediapipe as mp
import time
from collections import deque
mp_drawing = mp.solutions.drawing_utils
mp_hands = mp.solutions.hands
pointx = []
pointy = []
cap = cv2.VideoCapture(0)
rpoints = [deque(maxlen=512)]
def paint(frame,rpoints):
points = [rpoints]
for i in range(len(points)):
for j in range(len(points[i])):
for k in range(1, len(points[i][j])):
cv2.line(frame, points[i][j][k - 1], points[i][j][k], (100, 0, 255), 2)
with mp_hands.Hands(
min_detection_confidence=0.5,
min_tracking_confidence=0.5) as hands:
while cap.isOpened():
success, frame = cap.read()
imgResult = frame.copy()
start = time.time()
frame = cv2.cvtColor(cv2.flip(frame, 1), cv2.COLOR_BGR2RGB)
frame.flags.writeable = False
results = hands.process(frame)
frame.flags.writeable = True
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
if results.multi_hand_landmarks:
for hand_landmarks in results.multi_hand_landmarks:
mp_drawing.draw_landmarks(
frame, hand_landmarks, mp_hands.HAND_CONNECTIONS)
x = int(hand_landmarks.landmark[8].x*640)
y= int(hand_landmarks.landmark[8].y*480)
rpoints[0].append((x, y))
cv2.circle(frame, (x, y), 3, (250, 250, 0), 6)
paint(frame,rpoints)
end = time.time()
totalTime = end - start
fps = 1 / totalTime
cv2.putText(frame, f'FPS: {int(fps)}', (20,70), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0,255,0), 2)
cv2.imshow('Hand Painting', frame)
if cv2.waitKey(5) & 0xFF == 27:
break
cap.release()