OpenCV waitKey in python doesn't work on mac












0















I'm writing code to face recognition in python and i'm using open cv on mac (PyCharm).
I don't understand why:



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


doesn't work on mac, but on windows this code work.
In particular q doesn't trigger the if.



I try to change with:
k = cv2.waitKey(0)
if k == 27:
break



Below write my code



def run(self):
video_capture = cv2.VideoCapture(0)
try:
while self.active:
# Grab a single frame of video
ret, frame = video_capture.read()

# Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
rgb_frame = frame[:, :, ::-1]

# Find all the faces and face enqcodings in the frame of video
face_locations = face_recognition.face_locations(rgb_frame)
face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)

# Loop through each face in this frame of video
for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
# See if the face is a match for the known face(s)
matches = face_recognition.compare_faces(self.known_face_encodings, face_encoding)

name = "Unknown"

# If a match was found in known_face_encodings, just use the first one.
if True in matches:
first_match_index = matches.index(True)
name = self.known_face_names[first_match_index]

# Draw a box around the face
cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)

# Draw a label with a name below the face
cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
if (name != "Unknown"):
self.reco.emit(name)
self.deactivate()
video_capture.release()
cv2.destroyAllWindows()
else:
self.unreco.emit()
self.deactivate()
video_capture.release()
cv2.destroyAllWindows()
# Display the resulting image
# cv2.imshow('Video', frame)
# Hit 'q' on the keyboard to quit!
if cv2.waitKey(1) & 0xFF == ord('q'):
break
except:
print("error while recognize")









share|improve this question

























  • could you be a little more specific? what does doesn't work mean? do you get an error? does q not trigger the if? did you try clicking the opencv window and then hitting q?

    – Nullman
    Jan 20 at 15:13











  • if i press q, q not trigger the if

    – Alessandro Minervini
    Jan 20 at 15:24











  • OK, so did you inspect the return value from cv2.waitKey to see what it actually returns when you press q? Is there a window present? (I see the only imshow commented out -- perhaps it needs a window to be able to capture keyboard events?)

    – Dan Mašek
    Jan 20 at 17:22











  • Returns -1 before i type from the keyboard, (for the window there's video_capture.read() i think)

    – Alessandro Minervini
    Jan 20 at 17:31











  • Isn't PyCharm some dev environment? Maybe it grabs keyboard input. Try running outside of PyCharm, maybe.

    – Mark Setchell
    Jan 20 at 17:48
















0















I'm writing code to face recognition in python and i'm using open cv on mac (PyCharm).
I don't understand why:



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


doesn't work on mac, but on windows this code work.
In particular q doesn't trigger the if.



I try to change with:
k = cv2.waitKey(0)
if k == 27:
break



Below write my code



def run(self):
video_capture = cv2.VideoCapture(0)
try:
while self.active:
# Grab a single frame of video
ret, frame = video_capture.read()

# Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
rgb_frame = frame[:, :, ::-1]

# Find all the faces and face enqcodings in the frame of video
face_locations = face_recognition.face_locations(rgb_frame)
face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)

# Loop through each face in this frame of video
for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
# See if the face is a match for the known face(s)
matches = face_recognition.compare_faces(self.known_face_encodings, face_encoding)

name = "Unknown"

# If a match was found in known_face_encodings, just use the first one.
if True in matches:
first_match_index = matches.index(True)
name = self.known_face_names[first_match_index]

# Draw a box around the face
cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)

# Draw a label with a name below the face
cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
if (name != "Unknown"):
self.reco.emit(name)
self.deactivate()
video_capture.release()
cv2.destroyAllWindows()
else:
self.unreco.emit()
self.deactivate()
video_capture.release()
cv2.destroyAllWindows()
# Display the resulting image
# cv2.imshow('Video', frame)
# Hit 'q' on the keyboard to quit!
if cv2.waitKey(1) & 0xFF == ord('q'):
break
except:
print("error while recognize")









share|improve this question

























  • could you be a little more specific? what does doesn't work mean? do you get an error? does q not trigger the if? did you try clicking the opencv window and then hitting q?

    – Nullman
    Jan 20 at 15:13











  • if i press q, q not trigger the if

    – Alessandro Minervini
    Jan 20 at 15:24











  • OK, so did you inspect the return value from cv2.waitKey to see what it actually returns when you press q? Is there a window present? (I see the only imshow commented out -- perhaps it needs a window to be able to capture keyboard events?)

    – Dan Mašek
    Jan 20 at 17:22











  • Returns -1 before i type from the keyboard, (for the window there's video_capture.read() i think)

    – Alessandro Minervini
    Jan 20 at 17:31











  • Isn't PyCharm some dev environment? Maybe it grabs keyboard input. Try running outside of PyCharm, maybe.

    – Mark Setchell
    Jan 20 at 17:48














0












0








0








I'm writing code to face recognition in python and i'm using open cv on mac (PyCharm).
I don't understand why:



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


doesn't work on mac, but on windows this code work.
In particular q doesn't trigger the if.



I try to change with:
k = cv2.waitKey(0)
if k == 27:
break



Below write my code



def run(self):
video_capture = cv2.VideoCapture(0)
try:
while self.active:
# Grab a single frame of video
ret, frame = video_capture.read()

# Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
rgb_frame = frame[:, :, ::-1]

# Find all the faces and face enqcodings in the frame of video
face_locations = face_recognition.face_locations(rgb_frame)
face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)

# Loop through each face in this frame of video
for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
# See if the face is a match for the known face(s)
matches = face_recognition.compare_faces(self.known_face_encodings, face_encoding)

name = "Unknown"

# If a match was found in known_face_encodings, just use the first one.
if True in matches:
first_match_index = matches.index(True)
name = self.known_face_names[first_match_index]

# Draw a box around the face
cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)

# Draw a label with a name below the face
cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
if (name != "Unknown"):
self.reco.emit(name)
self.deactivate()
video_capture.release()
cv2.destroyAllWindows()
else:
self.unreco.emit()
self.deactivate()
video_capture.release()
cv2.destroyAllWindows()
# Display the resulting image
# cv2.imshow('Video', frame)
# Hit 'q' on the keyboard to quit!
if cv2.waitKey(1) & 0xFF == ord('q'):
break
except:
print("error while recognize")









share|improve this question
















I'm writing code to face recognition in python and i'm using open cv on mac (PyCharm).
I don't understand why:



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


doesn't work on mac, but on windows this code work.
In particular q doesn't trigger the if.



I try to change with:
k = cv2.waitKey(0)
if k == 27:
break



Below write my code



def run(self):
video_capture = cv2.VideoCapture(0)
try:
while self.active:
# Grab a single frame of video
ret, frame = video_capture.read()

# Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
rgb_frame = frame[:, :, ::-1]

# Find all the faces and face enqcodings in the frame of video
face_locations = face_recognition.face_locations(rgb_frame)
face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)

# Loop through each face in this frame of video
for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
# See if the face is a match for the known face(s)
matches = face_recognition.compare_faces(self.known_face_encodings, face_encoding)

name = "Unknown"

# If a match was found in known_face_encodings, just use the first one.
if True in matches:
first_match_index = matches.index(True)
name = self.known_face_names[first_match_index]

# Draw a box around the face
cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)

# Draw a label with a name below the face
cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
if (name != "Unknown"):
self.reco.emit(name)
self.deactivate()
video_capture.release()
cv2.destroyAllWindows()
else:
self.unreco.emit()
self.deactivate()
video_capture.release()
cv2.destroyAllWindows()
# Display the resulting image
# cv2.imshow('Video', frame)
# Hit 'q' on the keyboard to quit!
if cv2.waitKey(1) & 0xFF == ord('q'):
break
except:
print("error while recognize")






python opencv cv2






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 20 at 15:27







Alessandro Minervini

















asked Jan 20 at 15:12









Alessandro MinerviniAlessandro Minervini

63




63













  • could you be a little more specific? what does doesn't work mean? do you get an error? does q not trigger the if? did you try clicking the opencv window and then hitting q?

    – Nullman
    Jan 20 at 15:13











  • if i press q, q not trigger the if

    – Alessandro Minervini
    Jan 20 at 15:24











  • OK, so did you inspect the return value from cv2.waitKey to see what it actually returns when you press q? Is there a window present? (I see the only imshow commented out -- perhaps it needs a window to be able to capture keyboard events?)

    – Dan Mašek
    Jan 20 at 17:22











  • Returns -1 before i type from the keyboard, (for the window there's video_capture.read() i think)

    – Alessandro Minervini
    Jan 20 at 17:31











  • Isn't PyCharm some dev environment? Maybe it grabs keyboard input. Try running outside of PyCharm, maybe.

    – Mark Setchell
    Jan 20 at 17:48



















  • could you be a little more specific? what does doesn't work mean? do you get an error? does q not trigger the if? did you try clicking the opencv window and then hitting q?

    – Nullman
    Jan 20 at 15:13











  • if i press q, q not trigger the if

    – Alessandro Minervini
    Jan 20 at 15:24











  • OK, so did you inspect the return value from cv2.waitKey to see what it actually returns when you press q? Is there a window present? (I see the only imshow commented out -- perhaps it needs a window to be able to capture keyboard events?)

    – Dan Mašek
    Jan 20 at 17:22











  • Returns -1 before i type from the keyboard, (for the window there's video_capture.read() i think)

    – Alessandro Minervini
    Jan 20 at 17:31











  • Isn't PyCharm some dev environment? Maybe it grabs keyboard input. Try running outside of PyCharm, maybe.

    – Mark Setchell
    Jan 20 at 17:48

















could you be a little more specific? what does doesn't work mean? do you get an error? does q not trigger the if? did you try clicking the opencv window and then hitting q?

– Nullman
Jan 20 at 15:13





could you be a little more specific? what does doesn't work mean? do you get an error? does q not trigger the if? did you try clicking the opencv window and then hitting q?

– Nullman
Jan 20 at 15:13













if i press q, q not trigger the if

– Alessandro Minervini
Jan 20 at 15:24





if i press q, q not trigger the if

– Alessandro Minervini
Jan 20 at 15:24













OK, so did you inspect the return value from cv2.waitKey to see what it actually returns when you press q? Is there a window present? (I see the only imshow commented out -- perhaps it needs a window to be able to capture keyboard events?)

– Dan Mašek
Jan 20 at 17:22





OK, so did you inspect the return value from cv2.waitKey to see what it actually returns when you press q? Is there a window present? (I see the only imshow commented out -- perhaps it needs a window to be able to capture keyboard events?)

– Dan Mašek
Jan 20 at 17:22













Returns -1 before i type from the keyboard, (for the window there's video_capture.read() i think)

– Alessandro Minervini
Jan 20 at 17:31





Returns -1 before i type from the keyboard, (for the window there's video_capture.read() i think)

– Alessandro Minervini
Jan 20 at 17:31













Isn't PyCharm some dev environment? Maybe it grabs keyboard input. Try running outside of PyCharm, maybe.

– Mark Setchell
Jan 20 at 17:48





Isn't PyCharm some dev environment? Maybe it grabs keyboard input. Try running outside of PyCharm, maybe.

– Mark Setchell
Jan 20 at 17:48












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54277825%2fopencv-waitkey-in-python-doesnt-work-on-mac%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54277825%2fopencv-waitkey-in-python-doesnt-work-on-mac%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Liquibase includeAll doesn't find base path

How to use setInterval in EJS file?

Petrus Granier-Deferre