0

I tried work with

if cv2.waitKey(0) & 0xFF == ord('q'):
        break 

But I kept on getting TypeError. However, when I use either one my camera does not respond to the code.

import cv2
import numpy as np

capture = cv2.VideoCapture(0)


while True:
    ret, frame = capture.read()
    cv2.imshow("frame", frame)

    if cv2.waitKey(0) & 0xFF == ord('q'):
        break

capture.release()
cv2.destroyAllWindows()

if cv2.waitKey(0) & 0xFF == ord('q'):

TypeError: unsupported operand type(s) for &: 'NoneType' and 'int'

Process finished with exit code 1

Community
  • 1
  • 1
  • 3
    `0xFF` is an integer value, `255` and apparently, `cv.waitKey(0)` returns with a value of `None`. Your problem though, is that you're using `&` instead of `and`. `&` is a binary operator, you're looking for the logical operator `and` - that will also fix the ordering, since `&` is stronger than `==`, but `==` is stronger than `and`, so you won't need parentheses. – Grismar Sep 12 '19 at 07:08
  • 1
    @Gris I think the binary operator is fine https://stackoverflow.com/a/52913689/2308683 – OneCricketeer Sep 12 '19 at 07:13
  • @FunnyBunny Could you give some more insight:Are you seing the captured image after `cv2.imshow("frame", frame)`? Does the error you posted happen immediately or only once you have pressed a button? Could you also clarify what you mean by "However, when I use either one my camera does not respond to the code" – FlyingTeller Sep 12 '19 at 07:16
  • &cricket_007 you may be right, depending on what OP expects - in that case it's just a matter of applying the binary `&` to an `int` and `None`. – Grismar Sep 12 '19 at 07:23
  • @FlyingTeller I don't see any image after `cv2.imshow("frame",frame)` . When I use only `waitKey` or `0xFF`, there is no error but my display will not give any respond. I thought it might be related but when using both together it will appear to have `TypeError`. – FunnyBunny Sep 12 '19 at 08:03

1 Answers1

0

Although the docs state the waitkey function returns the code of the pressed key, you could pull out the value and perform a none check

c = cv2.waitKey(0)
if c is not None and c & 0xFF == ord('q'):
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245