I am trying to make a ar mouse (camera vould detect your hand and than your finger vould be the mouse). But when checking which fingers are up I get an error. When making this program I followed this tutorial: https://www.youtube.com/watch?v=8gPONnGIPgw&t=332s. When doing that I made one thing different which is that I didn’t make a program file called HandTrackingModule.py as he did and than import that but I just imported from cvzone.HandTrackingModule import HandDetector which shoud work the same.
This is the error:
fingers = detector.fingersUp()
in fingersUp return fingers
UnboundLocalError: local variable ‘fingers’ referenced before assignment
this is the code:
JavaScript
x
68
68
1
import cv2
2
import numpy as np
3
import time
4
import autopy
5
from cvzone.HandTrackingModule import HandDetector
6
7
wCam, hCam = 640, 480
8
frameR = 100 # Frame Reduction
9
smoothening = 7
10
11
pTime = 0
12
plocX, plocY = 0, 0
13
clocX, clocY = 0, 0
14
15
cap = cv2.VideoCapture(0)
16
cap.set(3, wCam)
17
cap.set(4, hCam)
18
detector = HandDetector(detectionCon=0.7,maxHands=1)
19
wScr, hScr = autopy.screen.size()
20
21
while True:
22
# 1. Find hand Landmarks
23
success, img = cap.read()
24
img = detector.findHands(img)
25
lmList, bbox = detector.findPosition(img)
26
27
# 2. Get the tip of the index and middle fingers
28
if len(lmList) != 0:
29
x1, y1 = lmList[8][:1]
30
x2, y2 = lmList[12][:1]
31
32
# 3. Check which fingers are up
33
fingers = detector.fingersUp() #------------------this is where the error happens
34
print(fingers)
35
cv2.rectangle(img, (frameR, frameR), (wCam - frameR, hCam - frameR),
36
(255, 0, 255), 2)
37
38
# 4. Only Index Finger : Moving Mode
39
if fingers[1] == 1 and fingers[2] == 0:
40
# 5. Convert Coordinates
41
x3 = np.interp(x1, (frameR, wCam - frameR), (0, wScr))
42
y3 = np.interp(y1, (frameR, hCam - frameR), (0, hScr))
43
44
# 6. Smoothen Values
45
clocX = plocX + (x3 - plocX) / smoothening
46
clocY = plocY + (y3 - plocY) / smoothening
47
48
# 7. Move Mouse
49
autopy.mouse.move(wScr - clocX, clocY)
50
cv2.circle(img, (x1, y1), 15, (255, 0, 255), cv2.FILLED)
51
plocX, plocY = clocX, clocY
52
53
# 8. Both Index and middle fingers are up : Clicking Mode
54
if fingers[1] == 1 and fingers[2] == 1:
55
56
# 9. Find distance between fingers
57
length, img, lineInfo = detector.findDistance(8, 12, img)
58
59
# 10. Click mouse if distance short
60
if length < 40:
61
cv2.circle(img, (lineInfo[4], lineInfo[5]),
62
15, (0, 255, 0), cv2.FILLED)
63
autopy.mouse.click()
64
65
# 12. Display
66
cv2.imshow("Image", img)
67
cv2.waitKey(1)
68
Advertisement
Answer
Your code indentation is wrong.
JavaScript
1
38
38
1
# 2. Get the tip of the index and middle fingers
2
if len(lmList) != 0:
3
x1, y1 = lmList[8][:1]
4
x2, y2 = lmList[12][:1]
5
6
# 3. Check which fingers are up
7
fingers = detector.fingersUp() #------------------this is where the error happens
8
print(fingers)
9
cv2.rectangle(img, (frameR, frameR), (wCam - frameR, hCam - frameR),
10
(255, 0, 255), 2)
11
12
# 4. Only Index Finger : Moving Mode
13
if fingers[1] == 1 and fingers[2] == 0:
14
# 5. Convert Coordinates
15
x3 = np.interp(x1, (frameR, wCam - frameR), (0, wScr))
16
y3 = np.interp(y1, (frameR, hCam - frameR), (0, hScr))
17
18
# 6. Smoothen Values
19
clocX = plocX + (x3 - plocX) / smoothening
20
clocY = plocY + (y3 - plocY) / smoothening
21
22
# 7. Move Mouse
23
autopy.mouse.move(wScr - clocX, clocY)
24
cv2.circle(img, (x1, y1), 15, (255, 0, 255), cv2.FILLED)
25
plocX, plocY = clocX, clocY
26
27
# 8. Both Index and middle fingers are up : Clicking Mode
28
if fingers[1] == 1 and fingers[2] == 1:
29
30
# 9. Find distance between fingers
31
length, img, lineInfo = detector.findDistance(8, 12, img)
32
33
# 10. Click mouse if distance short
34
if length < 40:
35
cv2.circle(img, (lineInfo[4], lineInfo[5]),
36
15, (0, 255, 0), cv2.FILLED)
37
autopy.mouse.click()
38