73853

Use waitKey in order pause and play video

Question:

I have a VideoCapture in OpenCV, I can successfully display a given video. What I want to do now is to pause and play by pressing a key (optional which one as long as it works). I have been reading about waitKey but there is something about this whole thing I don't get (ASCII) and how to bind keys. What I understand it is used to let highgui process but can also be used for other purposes?

If it is hard/impossible to pause a video and start it again I would be happy with just a delay when key is pressed.

Help is much appreciated!

Answer1:

I have same problem as you before. I solved it in Python instead of C++, but I think the logic behind is same.

import cv2 cap = cv2.VideoCapture('my.avi') while True: ret, frame = cap.read() key = cv2.waitKey(1) & 0xff if not ret: break if key == ord('p'): while True: key2 = cv2.waitKey(1) or 0xff cv2.imshow('frame', frame) if key2 == ord('p'): break cv2.imshow('frame',frame) if key == 27: break cap.release() cv2.destroyAllWindows()

Answer2:

You dont need anything like binding keys. I have written a sample code which will play/pause the video whenever you press <strong>"p"</strong>.

#include <iostream> #include <fstream> #include <string> #include "opencv2/opencv_modules.hpp" #include "opencv2/highgui/highgui.hpp" using namespace std; using namespace cv; int main(int argc, char **argv) { bool playVideo = true; VideoCapture cap(argv[1]); if(!cap.isOpened()) { cout<<"Unable to open video "<<argv[1]<<"\n"; return 0; } Mat frame; while(1) { if(playVideo) cap >> frame; if(frame.empty()) { cout<<"Empty Frame\n"; return 0; } imshow("Video",frame); char key = waitKey(5); if(key == 'p') playVideo = !playVideo; } return 0; }

Answer3:

If you want to pause and play with p use

if(cv::waitKey(1) == 'p') while(cv::waitKey(1) != 'p');

Recommend

  • How to handle Edm:DateTime form OData interface in SAPUI5 correct?
  • How can I pass a structure's array in the right way to my function?
  • Understanding foreign keys
  • Firebase email verification invalid certificate issue
  • How can I change default tenant in Microsoft Graph Explorer
  • How to integrate Struts2 with Thymeleaf?
  • Details on WSDL bindings
  • How to deploy a COM
  • App crash when selecting more than 10 images
  • Macbook React Native install
  • Trigger to delete rows from related tables before deleting rows from actual table
  • Access VBA lookup folder and create if does not exist DLOOKUP
  • Reading large text file very slow
  • Counting the Words in a String [closed]
  • NextJS auth with an external server
  • typescript multidimensional array with different types
  • Existing data serialized as hash produces error when upgrading to Rails 5
  • JSF validateLength question
  • Coin change recursive approach
  • How to make infinite threaded comments
  • How to use mixed C++ & .Net dll in node.js? (Error: abort() has been called)
  • Whatsapp Image sharing not working
  • jqGrid Filter Toolbar and Date and Select filters
  • Eric5: The OK button of 'new project' dialog is disable
  • How to convert days into months using datetime in Python3?
  • Tkinter tkMessageBox disables Tkinter key bindings
  • Protractor Page objects - TypeError: Object # has no method 'methodName'
  • How can I filter an array of dictionaries in 'updateSearchResultsForSearchController' to s
  • Implementation of timeout in LDAP
  • Facebook Error (#200) The user hasn't authorized the application to perform this action (PHP)
  • How to change the host IP sent in emails to new GitLab users to a publicly visible IP, not the local
  • How to load dynamic images in custom ListView
  • Request Access Token in Postman for Azure Function App protected by Azure AD B2C
  • What is the difference between dynamically creating a script tag and statically embed a script tag?
  • C++ STL stack pop operation giving segmentation fault
  • Drag and drop unicode TText in DelphiXe4
  • matrix multiplication apache pig
  • Comma decimal separator is ignored by ASP.NET MVC model binder
  • How to mutate multiple variables without repeating codes?
  • ARKit code issue {unknown error -1=ffffffffffffffff error: Task failed with exit 1}