[英]Python loop inside another while loop
I'm trying to get the commands _press(ser, 'd', .3)
and _press(ser, 'a', .3)
to repeat until OpenCV detects a pixel and to break and continue to the other loop until the if statement is fulfilled.我试图让命令_press(ser, 'd', .3)
和_press(ser, 'a', .3)
重复直到 OpenCV 检测到一个像素并中断并继续到另一个循环直到 if声明得到满足。
This is a snippet of code as the full is around 150 lines.这是一个完整的代码片段,大约 150 行。 Let me know if you need more.如果您需要更多,请告诉我。 The code itself works;代码本身有效; it's just that since it's a random amount of time of a & d movement I can't time it manually.只是因为它是随机的 a & d 运动时间,我无法手动计时。
with serial.Serial(args.serial, 9600) as ser, _shh(ser):
while True:
while True:
_press(ser, 'd', .3) # Tells controller right or left and for how long
_press(ser, 'a', .3)
_await_pixel(ser, vid, x=290, y=862, pixel=(33, 188, 50)) # X Y coordinates & RGB
print('pixel found')
break
_await_pixel(ser, vid, x=464, y=905, pixel=(0, 0, 0))
print('dialog started')
_await_not_pixel(ser, vid, x=464, y=905, pixel=(0, 0, 0))
print('dialog ended')
t0 = time.time()
_await_pixel(ser, vid, x=309, y=896, pixel=(0, 0, 0))
t1 = time.time()
print(f'dialog delay: {t1 - t0:.3f}s')
if (t1 - t0) > 1: # If dialog delay is > than 1 to finish
print('Finished')
playsound('WEGOTEM.MP3')
break
elif (t1 - t0) < 1: # commands to reset positioning
time.sleep(11)
_press(ser, 's', )
time.sleep(.5)
_press(ser, 's', )
time.sleep(.5)
_press(ser, 's', )
time.sleep(1)
_press(ser, 'A', )
time.sleep(6)
vid.release()
cv2.destroyAllWindows()
return 0
if __name__ == '__main__':
exit(main())
You clearly can't use _await_pixel
, if that blocks until the pixel is found.如果在找到像素之前阻塞,您显然不能使用_await_pixel
。 You just need to fetch the current pixel value, and if it matches, break.您只需要获取当前像素值,如果匹配,则中断。 Or, you could just make that your while
condition.或者,您可以将其设为您的while
条件。 I don't know where your _await_pixel
came from, but perhaps you can use it to find out how to read a single pixel.我不知道您的_await_pixel
来自哪里,但也许您可以使用它来了解如何读取单个像素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.