2021年4月5日星期一

cv2.waitKey() works for 'q' key but does not works for any other keys

I have this code which is actually a part of one of my projects for Sign Language Recognition. So I am taking user's hand image in each frame and passing it to model and showing the prediction for the sign on the screen and I have added a feature where if user presses 's' key then prediction will get appended to string sen and similarily I will show that string sen on screen .

But after running the code, for 'q' it closes the window without any problems but when I press 's' nothing happens.

Instead of 's' , I tried the same with other keys but still nothing happens.

Here's the code :

import cv2  import numpy as np  from model import predict      def capture():      sen = ''      cap = cv2.VideoCapture(0)      while True:            ret, frame = cap.read()          frame = cv2.flip(frame, 1)          frame_height = 480          frame_width = 640          # a standard frame window of size 640*480          frame = cv2.resize(frame, (frame_width, frame_height))            # drawing a rectangle in a frame which will capture hand          cv2.rectangle(frame, (300, 100), (500, 300), (0, 300, 0), 2)            # rectangle of background of text s          cv2.rectangle(frame, (0, 0), (300, 50), (0, 0, 0), -1)          frame = cv2.putText(frame, 'press q to exit', (30, 30), cv2.FONT_HERSHEY_SIMPLEX,                              1, (255, 255, 255), 1, cv2.LINE_AA)            # region of interest i.e rectangle which we drawn earlier          roi = frame[100:300, 300:500]          roi = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)          roi = cv2.GaussianBlur(roi, (5, 5), 0)              # applying edge detection on roi          roi_edges = cv2.Canny(roi, 100, 200)          # cv2.imshow('Edges', roi_edges)              # to make roid_edges of shape (200,200,1) , specifying color channel which is required for model          img = np.expand_dims(roi_edges, axis=2)          cv2.imshow('input for model', img)            frame = cv2.putText(frame, predict(img), (300, 400), cv2.FONT_HERSHEY_SIMPLEX,                              1, (255, 255, 255), 1, cv2.LINE_AA)            if cv2.waitKey(1) & 0xFF == ord('q'):              break          elif cv2.waitKey(1) & 0xFF == ord('s'):              sen += predict(img)              print(sen)            # printing whole sentence i.e. sen          frame = cv2.putText(frame, sen , (300, 500),                              cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 1, cv2.LINE_AA)            cv2.imshow('Smile', frame)        cv2.destroyAllWindows()      cap.release()      capture()    

So what could be the problem here?

https://stackoverflow.com/questions/66962685/cv2-waitkey-works-for-q-key-but-does-not-works-for-any-other-keys April 06, 2021 at 12:24PM

没有评论:

发表评论