【发布时间】:2022-01-17 11:38:16
【问题描述】:
我在 python 3.10.1 上运行该模块时遇到了一些问题。这是我的代码:
import mediapipe as mp
import cv2
import time
class handDetector:
def __init__(self, mode=False, maxHands=2, complexity = 1, detectionCon=0.5, trackCon=0.5):
self.mode = mode
self.maxHands = maxHands
self.complexity = complexity
self.detectionCon = detectionCon
self.trackCon = trackCon
self.mpDraw = mp.solutions.drawing_utils
def FindHands(self, img, draw = True):
imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
results = self.hands.process(imgRGB)
landmarks = results.multi_hand_landmarks
#print(results.multi_hand_landmarks)
if landmarks:
for handLms in landmarks:
if draw:
self.mpDraw.draw_landmarks(img,
handLms,
self.mpHands.HAND_CONNECTIONS)
return img
#for id, lm in enumerate(handLms.landmark):
#print(id,lm)
# height, width, c = img.shape
# cx, cy = int(lm.x*width), int(lm.y*height)
# print(id, ", x=",cx, ", y=",cy)
# if id%10 == 0:
# cv2.circle(img, (cx,cy), 8, (255,0,255), cv2.FILLED)
def main():
pTime = 0
cTime = 0
cap = cv2.VideoCapture(1)
detector = handDetector()
while True:
success, img = cap.read()
img = detector.FindHands(img)
cTime = time.time()
fps = 1/(cTime-pTime)
pTime = cTime
cv2.putText(img, str(int(fps)), (10,70),
cv2.FONT_HERSHEY_PLAIN, 3,(255,0,255), 3)
cv2.imshow("Image",img)
cv2.waitKey(1)
if __name__ == '__main__':
main()
它返回给我以下回溯:
Traceback (most recent call last):
File "C:\Users\Eduardo.PC\Documents\UNAM 2020-24\COMPU\HandTracker\HandTrackModule.py", line 65, in <module>
main()
File "C:\Users\Eduardo.PC\Documents\UNAM 2020-24\COMPU\HandTracker\HandTrackModule.py", line 55, in main
img = detector.FindHands(img)
File "C:\Users\Eduardo.PC\Documents\UNAM 2020-24\COMPU\HandTracker\HandTrackModule.py", line 21, in FindHands
imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
cv2.error: OpenCV(4.5.4) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
我正在关注计算机视觉的 youtube 在线课程linked here。在大约 30 分钟时,添加了上面介绍的模块。我几乎复制了它,但它不起作用。发生了什么事?
感谢和抱歉发了这么长的帖子
【问题讨论】:
-
请发布回溯,使其可读。 edit你的帖子来改变它。
-
完成。谢谢
-
if not success: break
标签: python computer-vision mediapipe