Related
I wish to write a program for face recognition. I want my program to automatically take a specific number of images(say,5). Here is what I am currently doing:
import cv2
cam=cv2.VideoCapture(0)
a=0
count=0
for a in range(5):
ret,img=cam.read()
cv2.imshow("Test",img)
if not ret:
break
k=cv2.waitKey(1)
print("Image"+str(count)+"saved")
file='C:/Users/User/Desktop/FACEREC/known_faces/img_test/ad2'+str(count)+'.jpg'
cv2.imwrite(file,img)
a+=1
cam.release()
cv2.destroyAllWindows()
cam.release()
cv2.destroyAllWindows()
It shows "Image 0 saved" five times but in the folder(ad2), there is only a single image. Need some guidance.
Thank you
Lets start interpreting the code line-by-line:
Variable initialization
import cv2
cam=cv2.VideoCapture(0)
a=0
count=0
You load the opencv library and initialize the cam variable for webcam display. Then both a and count are intialized to 0. Now count variable is understandable, most probably count count the frames. What does a do?
Display and save frames
First-part
for a in range(0, 5):
ret,img=cam.read()
cv2.imshow("Test",img)
if not ret:
break
variable a declared again so we don't need to intialize at the beginning. Then current web-frame is being fetched. The frame is given as an input to imshow without even the current frame returned or not. The correct logic should be:
for a in range(5):
ret,img=cam.read()
if ret:
cv2.imshow("Test",img)
Second-part
k=cv2.waitKey(1)
print("Image"+str(count)+"saved")
file='C:/Users/User/Desktop/FACEREC/known_faces/img_test/ad2'+str(count)+'.jpg'
cv2.imwrite(file,img)
a+=1
Now, the k variable is initialized as a key for quiting the program when a key is pressed. Say, when q is pressed program terminates.
k=cv2.waitKey(1) & 0xFF
if k == ord('q'):
break
But if you break during the process, then you can't save the images. Therefore break should be at the end of the line.
For the rest of the code, you can use a as a counter. If you specify the start and end values. For each value a is increased by one. Then you can use it to discriminate the frames.
Code:
# Load the library
import cv2
# Initialize webcam
cam = cv2.VideoCapture(0)
# Save the frames
for a in range(0, 5):
# Get current frame
ret, img=cam.read()
# If frame is returned
if ret:
# Set the file-name
file='C:/Users/User/Desktop/FACEREC/known_faces/img_test/ad2' + str(a) + '.jpg'
# Write the frame
cv2.imwrite(file, img)
# Inform the user
print("Image" + str(a) + "saved")
# Display the frame
cv2.imshow("Test", img)
# If q is pressed quit
k=cv2.waitKey(1) & 0xFF
if k == ord('q'):
break
# Release the object
cam.release()
# Close the window
cv2.destroyAllWindows()
I'm a noobie at programming and this question may sound easy and dumb but I really can not do it! The goal here is to change the color between green, blue and red everytime I click. I'm sorry if that's obvious, but I'm for hours stuck at this. That's the code I have, that prints everytime the same color. Now, I want to add something to change colors everytime I click.
import cv2
import numpy as np
def draw_circle(event,x,y,flags,param):
if event == cv2.EVENT_LBUTTONDOWN:
cv2.circle(img,(x,y),100,color=(0,255,0),thickness=10)
cv2.namedWindow(winname='my_drawing')
cv2.setMouseCallback('my_drawing', draw_circle)
img = np.zeros((512,512,3),np.int8)
while True:
cv2.imshow('my_drawing', img)
if cv2.waitKey(20) &0xFF == 27:
break
cv2.destroyAllWindows
I've tried a lot of stuff that I don't even know where to start, but I've tried creating a variable inside the function that each time it enters on the function, it sums, and depending on the value(using if) it goes to a different color, but the variable doesn't seem to change if it enters the loop again, then I've tried returning the variable as well. No success. Adding a Paramater. No Success as well. I believe it's such a simple thing that my head cannot think at this point.
Here's one way... make a list of the colours you want to cycle through. Have a global variable that you use as the index into the list to get the next colour. Each time you use it, add 1 to it and reduce it modulo the length of the list of colours. Colour the circle with value from the colour list as indexed by the index.
#!/usr/bin/env python3
import cv2
import numpy as np
index = 0
colours = [[255,0,0], [0,255,0], [0,0,255]]
def draw_circle(event,x,y,flags,param):
global index
if event == cv2.EVENT_LBUTTONDOWN:
cv2.circle(img,(x,y),100,color=colours[index],thickness=10)
index = (index+1) % len(colours)
cv2.namedWindow(winname='my_drawing')
cv2.setMouseCallback('my_drawing', draw_circle)
img = np.zeros((512,512,3),np.int8)
while True:
cv2.imshow('my_drawing', img)
if cv2.waitKey(20) &0xFF == 27:
break
cv2.destroyAllWindows
Here is the code for the same, have a look at it. In this, below code I am creating a Motion Detector and with this I will be recording the timings of when the various objects appeared and disappeared for which I am using dataframe.
The issue with this is that the program executes but when the output is displayed in the form of a Window, it freezes when I try to terminate.
import cv2, pandas
from datetime import datetime
first_frame = None
status_list = [None,None]
times = []
df = pandas.DataFrame(columns=["Start", "End"]) #Dataframe to store the time values during which object detection and movement appears.
video = cv2.VideoCapture(0)#VideoCapture object is used to record video using web cam
while True:
#check is a bool data type, returns true if VideoCapture object is read
check,frame = video.read()
status = 0
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY) # For converting the frame color to gray scale
gray = cv2.GaussianBlur(gray,(21,21),0) # For converting the gray scale frame to GaussianBlur
if first_frame is None:
first_frame = gray # used to store the first image/frame of the video
continue
delta_frame = cv2.absdiff(first_frame,gray)#calculates the difference between first and other frames
thresh_delta = cv2.threshold(delta_frame,30,255,cv2.THRESH_BINARY)[1]
thresh_delta = cv2.dilate(thresh_delta,None,iterations=0) #Provides threshold value, so if the difference is <30 it will turn to black otherwise if >30 pixels will turn to white
(_,cnts,_) = cv2.findContours(thresh_delta.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE) #Define the contour area,i.e. adding borders
#Removing noises and shadows, any part which is greater than 1000 pixels will be converted to white
for contour in cnts:
if cv2.contourArea(contour) < 1000:
continue
status = 1 #change in status when the object is detected
#Creating a rectangular box around the object in frame
(x,y,w,h) = cv2.boundingRect(contour)
cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),3)
#list of status for every frame
status_list.append(status)
status_list=status_list[-2:]
#Record datetime in a list when change occurs
if status_list[-1]==1 and status_list[-2]==0:
times.append(datetime.now())
if status_list[-1]==0 and status_list[-2]==1:
times.append(datetime.now())
cv2.imshow('frame',frame)
#cv2.imshow('Capturing',gray)
#cv2.imshow('delta',delta_frame)
#cv2.imshow('thresh',thresh_delta)
key = cv2.waitKey(1) #changing the frame after 1 millisecond
#Used for terminating the loop once 'q' is pressed
if key == ord('q'):
break
print(status_list)
print(times)
for i in range(0,len(times),2):
df = df.append({"Start":times[i],"End":times[i+1]},ignore_index=True)
df.to_csv("Times.csv")
video.release()
cv2.destroyAllWindows #will be closing all the windows
Try this:
if cv2.waitKey(1) & 0xFF == ord('q'):
break
For a brief explanation about what "& 0xFF" is, see: What's 0xFF for in cv2.waitKey(1)?
It is basically a bit mask that takes the portion of 'waitKey' value(32 bit) that can be compared to ASCII (8 bit).
You can try this.
k = cv2.waitKey(1) & 0xFF
if k == 27 :
break
where k can be any ASCII code on the keyboard ASCII code help
In this case 27 is 'Esc' button.
However, the problem you're having where it is freezing, have you tried continuously pressing any button besides q? I had a similar problem when I was testing out OpenCV's tutorial code here
but i figured it out by playing around and then realizing i needed to hold down any button besides the 'exit' button.
I had faced the same issue bcz of this piece of code from geeksforgeeks
Please chck the last line of the code:
cv2.destroyAllWindows #will be closing all the windows
Add parenthesis, it shud b:
cv2.destroyAllWindows()
Please try this, rest looks fine
Well you can also pass a particular frame that you want to close like:
cv2.destroyAllWindows("frame")
I have a the following python code (and I'm using opencv3). What this code needs accomplish is to take a live stream of a generic camera, and then analyze a static picture (or frame). The problem that I have have, is that after exiting the second nested while loop, i can figure out how to go back to the initial loop (#1st loop).
The code should :
Take live stream
press 'q' to capture frame and go to the next loop
on this second loop:
if 's' -after the images is analyzed- it should go back to the 1st loop. if 'esc' the program should be terminated.
I did tried the recursion method (def () ) but I run into another problem in which i couldn't figure out how to terminate the program.
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
#Set LED brightness for camera
cap.set(cv2.CAP_PROP_BRIGHTNESS, 120)
#1st loop
while (True):
#capture camera stream
ret, frame = cap.read()
cv2.imshow('live image', frame)
if cv2.waitKey(0) == ord('q'):
cap.release()
cv2.destroyAllWindows()
pass
print ('image alteration block')
#2nd loop
while (True):
img = frame
cv2.imshow('image',img)
k = cv2.waitKey(0)
if k == 27: # wait for ESC key to exit and terminate progra,
cv2.destroyAllWindows()
break
elif k == ord('s'): # wait for 's' key to save the image and go back to the live stream
cv2.imwrite('messigray.png',img)
print ('go back to the beginning')
cv2.destroyAllWindows()
continue #NOT SURE WHAT TO DO HERE
You're not allowing the program to escape the second while loop. Use a different while condition in the second loop. Instead of while(True) use a boolean:
#2nd loop
second=True
while (second):
img = frame
cv2.imshow('image',img)
k = cv2.waitKey(0)
if k == 27: # wait for ESC key to exit and terminate progra,
cv2.destroyAllWindows()
break
elif k == ord('s'):
cv2.imwrite('messigray.png',img)
print ('go back to the beginning')
cv2.destroyAllWindows()
#modify the loop condition
second = False
continue
That should get you out of the second loop, relying on breaks is not always the best way to handle while loops.
I'm working on a program (python ,opencv) in which I use the spacebar to go to the next frame, and Esc to exit the program. These are the only two keys i've got working. I tried to find out about more keys , tried various codes for them but didnt work. especially arrow keys.
I found this about waitkey, but it doesn't work.
So my question is, How do I catch other keys besides esc and spacebar to trigger certain functions in my python-opencv program?
You can use ord() function in Python for that.
For example, if you want to trigger 'a' key press, do as follows :
if cv2.waitKey(33) == ord('a'):
print "pressed a"
See a sample code here: Drawing Histogram
UPDATE :
To find the key value for any key is to print the key value using a simple script as follows :
import cv2
img = cv2.imread('sof.jpg') # load a dummy image
while(1):
cv2.imshow('img',img)
k = cv2.waitKey(33)
if k==27: # Esc key to stop
break
elif k==-1: # normally -1 returned,so don't print it
continue
else:
print k # else print its value
With this code, I got following values :
Upkey : 2490368
DownKey : 2621440
LeftKey : 2424832
RightKey: 2555904
Space : 32
Delete : 3014656
...... # Continue yourself :)
The keycodes returned by waitKey seem platform dependent.
However, it may be very educative, to see what the keys return
(and by the way, on my platform, Esc does not return 27...)
The integers thay Abid's answer lists are mosty useless to the human mind
(unless you're a prodigy savant...). However, if you examine them in hex,
or take a look at the Least Significant Byte, you may notice patterns...
My script for examining the return values from waitKey is below:
#!/usr/bin/env python
import cv2
import sys
cv2.imshow(sys.argv[1], cv2.imread(sys.argv[1]))
res = cv2.waitKey(0)
print('You pressed %d (0x%x), LSB: %d (%s)' % (res, res, res % 256,
repr(chr(res%256)) if res%256 < 128 else '?'))
You can use it as a minimal, command-line image viewer.
Some results, which I got:
q letter:
You pressed 1048689 (0x100071), LSB: 113 ('q')
Escape key (traditionally, ASCII 27):
You pressed 1048603 (0x10001b), LSB: 27 ('\x1b')
Space:
You pressed 1048608 (0x100020), LSB: 32 (' ')
This list could go on, however you see the way to go, when you get 'strange' results.
BTW, if you want to put it in a loop, you can just waitKey(0) (wait forever), instead of ignoring the -1 return value.
EDIT: There's more to these high bits than meets the eye - please see Andrew C's answer (hint: it has to do with keyboard modifiers like all the "Locks" e.g. NumLock).
My recent experience shows however, that there is a platform dependence - e.g. OpenCV 4.1.0 from Anaconda on Python 3.6 on Windows doesn't produce these bits, and for some (important) keys is returns 0 from waitKey() (arrows, Home, End, PageDn, PageUp, even Del and Ins). At least Backspace returns 8 (but... why not Del?).
So, for a cross platform UI you're probably restricted to W, A, S, D, letters, digits, Esc, Space and Backspace ;)
The answers which have already been posted suggest that some of the unusual values obtained by waitKey are due to platform differences. Below, I propose that (at least on some platforms) the apparently odd behaviour of waitKey is due to keyboard modifiers. This post looks similar to Tomasz's answer because I initially wrote this as an edit, which was rejected.
The keycodes returned by waitKey change depending on which modifiers are enabled. NumLock, CapsLock, and the Shift, Ctrl, and Alt keys all modify the keycode returned by waitKey by enabling certain bits above the two Least Significant Bytes. The smallest of these flags is Shift at 0x10000.
A modified version of the script Tomasz posted is given below:
#!/usr/bin/env python
import cv2
import sys
cv2.imshow(sys.argv[1], cv2.imread(sys.argv[1]))
res = cv2.waitKey(0)
print 'You pressed %d (0x%x), 2LSB: %d (%s)' % (res, res, res % 2**16,
repr(chr(res%256)) if res%256 < 128 else '?')
Which give the following results:
q letter with NumLock:
You pressed 1048689 (0x100071), 2LSB: 113 ('q')
Escape key with CapsLock but not NumLock:
You pressed 131099 (0x2001b), 2LSB: 27 ('\x1b')
Space with Shift and NumLock:
You pressed 1114144 (0x110020), 2LSB: 32 (' ')
Right Arrow Key with Control, NumLock off:
You pressed 327507 (0x4ff53), 2LSB: 65363 ('S')
I hope that helps to explain the unusual behaviour of waitKey and how to get the actual key pressed regardless of the state of NumLock and CapLock. From here it's relatively simple to do something like:
ctrlPressed = 0 != res & (1 << 18)
...as the "control key" flag is (counting the least significant bit as bit 0) bit 18. Shift is at bit 16, the state of CapsLock at bit 17, Alt is at bit 19, and NumLock is at bit 20. As Tomasz was kind enough to point out, just pressing Shift on its own also returns a value, with distinct values for LShift and RShift (still with all these modifiers just described). I encourage you to double-check all of these modifiers and values on your own platform before relying on them. :)
As to me, the below code does't work, when it runs,the image will step to the next quickly without your press:
import cv2
img = cv2.imread('sof.jpg') # load a dummy image
while(1):
cv2.imshow('img',img)
k = cv2.waitKey(33)
if k==27: # Esc key to stop
break
elif k==-1: # normally -1 returned,so don't print it
continue
else:
print k # else print its value
But this works:
def test_wait_key():
lst_img_path = [
'/home/xy/yy_face_head/face_det_test/111.png',
'/home/xy/yy_face_head/face_det_test/222.png'
#.....more path ...
]
for f_path in lst_img_path:
img = cv2.imread(f_path)
cv2.imshow('tmp', img)
c = cv2.waitKey(0) % 256
if c == ord('a'):
print "pressed a"
else:
print 'you press %s' % chr(c)
Output as below:
Interesting nobody has mentioned cv2.waitKeyEx() as described in this answer of another Stack Overflow thread. OpenCV's documentation on cv2.waitKeyEx() reads as follows:
Similar to waitKey, but returns full key code.
Note
Key code is implementation specific and depends on used backend:
QT/GTK/Win32/etc
So, some attention may be required for cross-platform implementations. However, for me this was by far the easiest and most straight forward solution to get arrow keys etc. working on Windows.
For C++:
In case of using keyboard characters/numbers, an easier solution would be:
int key = cvWaitKey();
switch(key)
{
case ((int)('a')):
// do something if button 'a' is pressed
break;
case ((int)('h')):
// do something if button 'h' is pressed
break;
}
With Ubuntu and C++ I had problems with the Character/Integer cast. I needed to use cv::waitKey()%256 to obtain the correct ASCII value.
The answer that works on Ubuntu18, python3, opencv 3.2.0 is similar to the one above. But with the change in line cv2.waitKey(0). that means the program waits until a button is pressed.
With this code I found the key value for the arrow buttons: arrow up (82), down (84), arrow left(81) and Enter(10) and etc..
import cv2
img = cv2.imread('sof.jpg') # load a dummy image
while(1):
cv2.imshow('img',img)
k = cv2.waitKey(0)
if k==27: # Esc key to stop
break
elif k==-1: # normally -1 returned,so don't print it
continue
else:
print k # else print its value
If you want to pause the program to take screenshots of the progress
(shown in let's say cv2.imshow)
cv2.waitKey(0) would continue after pressing "Scr" button (or its combination), but you can try this
cv2.waitKey(0)
input('')
cv2.waitkey(0) to give the program enough time to process everything you want to see in the imshow and input('')
to make it wait for you to press Enter in the console window
this works on python 3
I too found this perplexing.
I'm running Ubuntu 18 and found the following:
If the cv.imshow window has focus, you'll get one set of values in the terminal - like the ASCII values discussed above.
If the Terminal has focus, you'll see different values. IE- you'll see "a" when you press the a key (instead of ASCII value 97) and "^]" instead of "27" when you press Escape.
I didn't see the 6 digit numbers mentioned above in either case and I used similar code. It seems the value for waitKey is the polling period in mS. The dots illustrate this.
Run this snippet and press keys while focus is on the test image, then click on the terminal window and press the same keys.
import cv2
img = cv2.imread('test.jpg')
cv2.imshow('Your test image', img)
while(1):
k = cv2.waitKey(300)
if k == 27:
break
elif k==-1:
print "."
continue
else:
print k
This prints the key combination directly to the image:
The first window shows 'z' pressed, the second shows 'ctrl' + 'z' pressed. When a key combination is used, a question mark appear.
Don't mess up with the question mark code, which is 63.
import numpy as np
import cv2
im = np.zeros((100, 300), np.uint8)
cv2.imshow('Keypressed', im)
while True:
key = cv2.waitKey(0)
im_c = im.copy()
cv2.putText(
im_c,
f'{chr(key)} -> {key}',
(10, 60),
cv2.FONT_HERSHEY_SIMPLEX,
1,
(255,255,255),
2)
cv2.imshow('Keypressed', im_c)
if key == 27: break # 'ESC'
flag = True
while flag:
cv2.imshow("result", image_to_show)
key = cv2.waitKey(0)
if key == ord('b'): # pressed a key
<Do something>
elif key == ord('b'): # pressed b key
<Do something>
elif key == 27: # pressed Esc key
flag = False