[英]I am trying to do a basic colour conversion in python however I can't seem to get past the below error
while True:
ret, frame = cap.read()
image, face = face_detector(frame)
def face_detector(img):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
I think your problem is that you're stuck in an infinite loop.我认为您的问题是您陷入了无限循环。 while True
will go on forever until you break out of it, or an exception is raised from something in the loop body. while True
将 go 永远打开,直到你打破它,或者从循环体中的某些东西引发异常。 Why?为什么? Because True
always evaluates to True
.因为True
总是计算为True
。 Go figure. Go 图。
Possible solutions:可能的解决方案:
while True
loop, you need to have a break
command.在某个地方,在while True
循环体内的某个点,您需要有一个break
命令。while...:
where ...
is some expression, eg while foo < bar:
.使用while...:
where ...
是一些表达式,例如while foo < bar:
。 That way, the loop will terminate when the expression evaluates False
.这样,当表达式计算为False
时,循环将终止。for foo in foos:
where foos
is an iterable and foo
is the variable name assigned to each item in that iterable in turn.改用 for 循环: for foo in foos:
其中foos
是一个可迭代对象,而foo
是依次分配给该可迭代对象中每个项目的变量名。 This way the loop naturally terminates when you run out of foos.这样,当您用完 foos 时,循环自然会终止。Which solution is best depends on what you're trying to do and what your data or state looks like.哪种解决方案最好取决于您要做什么以及您的数据或 state 的样子。 Without more information, I can't tell you which to use.没有更多信息,我无法告诉您使用哪个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.