简体   繁体   English

我正在尝试在 python 中进行基本的颜色转换,但是我似乎无法克服以下错误

[英]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:可能的解决方案:

  • Explicitly break out of the loop.明确地跳出循环。 Somewhere, at some point inside the body of a while True loop, you need to have a break command.在某个地方,在while True循环体内的某个点,您需要有一个break命令。
  • Use 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时,循环将终止。
  • Use a for loop instead: 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.

相关问题 我正在尝试为caeser密码创建频率分析,但是我似乎无法获得输出 - I'm trying to create a frequency analysis for a caeser cipher,However I can't seem to get an output 我正在尝试使用 OpenCV 的项目,但无法克服此错误 - I'm trying a project with OpenCV and can't get past this error 我正在尝试在 python 中编写递归程序,但我似乎无法掌握逻辑 - I am trying to write a recursive programme in python but I can't seem grasp the logic 我正在尝试从 python 中的 csv 中获取单词计数器和单词出现次数。 但是我收到一个错误 - I am trying to get a word counter of and word occurrence of words from a csv in python. However I receive an error 如何克服下面的SettingWithCopyWarning? - How can I get past the SettingWithCopyWarning below? 我正在使用tkinter在python中的页面之间导航,但是我无法在某些页面上使用该功能 - I am navigating between pages in python using tkinter.however i can't get the functionality to work on some pages 我试图编写游戏代码并发生冲突,但是我总是遇到相同的错误 - I am trying to code a game, and make a collision, however i always get the same error 我正在尝试在python上做基本的数字猜谜游戏 - I am trying to do the basic number guessing game on python 我无法通过任何 python package 上的“收集 '无论 package 名称'” - I cannot get past “collecting 'whatever package name' ” on ANY python package I am trying to install 我正在尝试编写一个“机器人”服务员程序,但我似乎找不到我出错的地方 - I am trying to program a "robot" waiter program but I can't seem to find where I went wrong
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM