我收到此错误:
File "eye_motion_tracking.py", line 17
_, contours, _= cv2.findContours(skin_ycrcb, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
^
IndentationError: unindent does not match any outer indentation level
代码如下:
import cv2
import numpy as np
cap = cv2.VideoCapture("eye_recording.flv")
while True:
ret, frame = cap.read()
if ret is False:
break
roi = frame[269: 795, 537: 1416]
rows, cols, _ = roi.shape
gray_roi = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
gray_roi = cv2.GaussianBlur(gray_roi, (7, 7), 0)
_, threshold = cv2.threshold(gray_roi, 3, 255, cv2.THRESH_BINARY_INV)
_, contours, _= cv2.findContours(skin_ycrcb, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = sorted(contours, key=lambda x: cv2.contourArea(x), reverse=True)
for cnt in contours:
(x, y, w, h) = cv2.boundingRect(cnt)
#cv2.drawContours(roi, [cnt], -1, (0, 0, 255), 3)
cv2.rectangle(roi, (x, y), (x + w, y + h), (255, 0, 0), 2)
cv2.line(roi, (x + int(w/2), 0), (x + int(w/2), rows), (0, 255, 0), 2)
cv2.line(roi, (0, y + int(h/2)), (cols, y + int(h/2)), (0, 255, 0), 2)
break
cv2.imshow("Threshold", threshold)
cv2.imshow("gray roi", gray_roi)
cv2.imshow("Roi", roi)
key = cv2.waitKey(30)
if key == 27:
break
cv2.destroyAllWindows()
答案 0 :(得分:2)
如果您查看错误消息,它会告诉您问题出在哪里:
File "eye_motion_tracking.py", line 17
_, contours, _= cv2.findContours(skin_ycrcb, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
^
IndentationError: unindent does not match any outer indentation level
如果您随后查看该行,并假定您发布的代码与编辑器上的实际代码相同,则很显然该行没有正确缩进。
_, threshold = cv2.threshold(gray_roi, 3, 255, cv2.THRESH_BINARY_INV)
_, contours, _= cv2.findContours(skin_ycrcb, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = sorted(contours, key=lambda x: cv2.contourArea(x), reverse=True)
在Python中,indentation is important,并确保您的代码不是mixing tabs with spaces。
只需正确,一致地缩进即可。
_, threshold = cv2.threshold(gray_roi, 3, 255, cv2.THRESH_BINARY_INV)
_, contours, _= cv2.findContours(skin_ycrcb, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = sorted(contours, key=lambda x: cv2.contourArea(x), reverse=True)