0
I'm using 
    OS : Ubuntu
    Python : 3.8*
    Opencv version: 4.6.0

What I'm trying to do is capture the emotion & save the feed..For that I've used opencv 4.0

Code I've

while True:
    ret, frame = cap.read()
    frame = cv2.resize(frame, (720, 480))

    if not ret:
        break

    grayFrame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    rects = detector(grayFrame, 0)
    for rect in rects:
        shape = predictor(grayFrame, rect)
        points = shapePoints(shape)
        (x, y, w, h) = rectPoints(rect)
        grayFace = grayFrame[y:y + h, x:x + w]
        try:
            grayFace = cv2.resize(grayFace, (emotionTargetSize))
        except:
            continue

        grayFace = grayFace.astype('float32')
        grayFace = grayFace / 255.0
        grayFace = (grayFace - 0.5) * 2.0
        grayFace = np.expand_dims(grayFace, 0)
        grayFace = np.expand_dims(grayFace, -1)
        emotion_prediction = emotionClassifier.predict(grayFace)
        emotion_probability = np.max(emotion_prediction)
        if (emotion_probability > 0.36):
            emotion_label_arg = np.argmax(emotion_prediction)
            color = emotions[emotion_label_arg]['color']
            cv2.rectangle(frame, (x, y), (x + w, y + h), color, 2)
            cv2.line(frame, (x, y + h), (x + 20, y + h + 20),
                     color,
                     thickness=2)
            cv2.rectangle(frame, (x + 20, y + h + 20), (x + 110, y + h + 40),
                          color, -1)
            cv2.putText(frame, emotions[emotion_label_arg]['emotion'],
                        (x + 25, y + h + 36), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
                        (255, 255, 255), 1, cv2.LINE_AA)
        else:
            color = (255, 255, 255)
            cv2.rectangle(frame, (x, y), (x + w, y + h), color, 2)

    
        
        out.write(frame)
    #Wait for user input - q, then you will stop the loop
    k = cv2.waitKey(1)
    if k == 27:
        break




cap.release()
if args["isVideoWriter"] == True:
    videoWrite.release()
cv2.destroyAllWindows()

Here Issue with wait key ...when I hit on esc program is not exiting...I've tried all the possible methods...the only method that working is keyboard interruption i.e, ctrl+c

Method1: Not working :(
c = cv2.waitKey(0) % 256

if c == ord('a'):
    break

Method 2: Not working :(
if cv2.waitKey(0) & 0xFF == ord('q'):
    break

Did i missing anything?...Thanks in advance

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
  • 1
    You should use `cv2.waitKey(1)`. Pass `1` within `cv2.waitKey` – Jeru Luke Jul 04 '22 at 19:35
  • Hi @JeruLuke Greetings for the day...I've already tried same snippet pls,check my main code –  Jul 04 '22 at 19:37
  • 1
    Is the indentation correct? `break` must be within the `if` condition. – Jeru Luke Jul 04 '22 at 19:39
  • Hi @JeruLuke yeah typos...edited –  Jul 04 '22 at 19:41
  • The condition in code should work when you press lowercase `q` during execution. Tested it. – Jeru Luke Jul 04 '22 at 19:45
  • Have u tested in Ubuntu? ...I'm completely clueless ...I don't know why It's not working for me...I've tried every possible methods –  Jul 04 '22 at 19:47
  • Wait! The indentation is **still** wrong. The condition should be in line with the first line inside `while` condition `ret, frame = cap.read()` – Jeru Luke Jul 04 '22 at 19:47
  • Hi!..i've edited & check dthe indentation & tried matching with while loop...Still no luck :( –  Jul 04 '22 at 19:51
  • Just pause and let's take a step back. Open a fresh console/script and try executing the code to display your webcam output in gray: https://docs.opencv.org/4.x/dd/d43/tutorial_py_video_display.html . If it works follow the same indentation style for your program. – Jeru Luke Jul 04 '22 at 19:56
  • 2
    When there is no opencv window,waitKey does nothing. Please use an imshow or cv2.namedWindow once before the intended waitKey call. In addition,if you want to block until an input is presed,usw waitKey(0). – Micka Jul 04 '22 at 20:01
  • Hi @Micka...I don't to want to display window...Can't we do that without hiding opencv window? –  Jul 04 '22 at 20:04
  • Hi @Luke I've tried the code it is working for that code...But When I tried pasting same logic....Same output...It's not exiting –  Jul 04 '22 at 20:07
  • @Bharth As Micka has stated, the note within `help(cv2.waitKey)` confirms it **The function only works if there is at least one HighGUI window created and the window is active**. You must use `cv2.imshow` for at least one window for it to work. – Jeru Luke Jul 04 '22 at 20:09
  • Just use `cv2.imshow('Frame', frame)` before `waitKey`. It should work. – Jeru Luke Jul 04 '22 at 20:10
  • Yeah I understood @Luke...but i was trying to do this as a application...I just don't want to show user the opencv window...So I just hided it...Is there is any alternate way like you have any ideas in mind like how we can exit the loop without showing cv2.imshow()? –  Jul 04 '22 at 20:15
  • 2
    Have a look at https://stackoverflow.com/q/24072790/2393191 – Micka Jul 04 '22 at 20:28
  • 1
    if you want to press a key in the **terminal**, `waitKey` is the wrong function to use. in fact, OpenCV does not concern itself at all with terminal I/O. that would be a plain python question. – Christoph Rackwitz Jul 04 '22 at 20:48
  • @Bharth The `keyboard` module will help – Jeru Luke Jul 05 '22 at 21:42

0 Answers0