cv2.waitKey()
# function store 'key' that you press during output windows open,
# also wait for certain amount of time
#if yoy press 'q' then cv2.waitKey(1) replace with ASCII value which is 113
if cv2.waitKey(1000) & 0xFF == ord('q'): # wait for 1000 millisecond
break
# now your code will look like this
if 113 & 0xFF == ord('q'):
break