Know how a computer captures a video image in the form of a pixel value matrix
On this occasion, we will try to do Face Recognition using python from the basics of knowing how computers capture images.
First, we import the necessary modules (make sure you have previously installed opencv-python). I write the code using Notepad ++.
# Import module
import cv2, time
then make a command to capture the object from our computer camera then close it
video = cv2.VideoCapture(0)
video.release()
thus, the whole command looks like this,
# Import module
import cv2, timevideo = cv2.VideoCapture(0)
video.release()
then save the code with a name, for example, “capture1.py” in a folder, for example, C:Usersasuscapture1
then open a command prompt and run the following command,
C:Usersasuscapture1>python capture1.py
if there is no error or error message from the command, then we can continue.
Next, we create an object frame to capture the image from the camera
import cv2, time
camera = 0
video = cv2.VideoCapture(camera, cv2.CAP_DSHOW)
check, frame = video.read()
print(check)
print(frame)
video.release()
save the file with the same name before which is capture1.py,
then open a command prompt and run the following command,
C:Usersasuscapture1>python capture1.py
you will see the following result
Then you can see the screenshot of the pixel values in the matrix.
Then we add a command to capture the image frame once and add a command to exit the image capture
import cv2, time
camera = 0
video = cv2.VideoCapture(camera,cv2.CAP_DSHOW)
check, frame = video.read()
print(check)
print(frame)
cv2.imshow("capture1", frame)
cv2.waitKey(0)
video.release()
save it again with the same name, then open a command prompt and run the following command (make sure the camera on your computer is in good condition and good lighting. If you are in dark lighting then the image capture will not appear, the alternative is to light your face with a flashlight)
C:Usersasuscapture1>python capture1.py
then an image will appear as follows
You can see the captured camera and the pixel value matrix, then we convert the image to gray,
import cv2, timecamera = 0
video = cv2.VideoCapture(camera, cv2.CAP_DSHOW)check, frame = video.read()print(check)
print(frame)gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)cv2.imshow("capture1", gray)
cv2.waitKey(0)video.release()
then an image will appear as follows
then for a continuous screenshot we can add the following command
# Import module
import cv2, time# capture command with camera
camera = 0
video = cv2.VideoCapture(camera, cv2.CAP_DSHOW)
a=0
while True :
a = a+1
check, frame = video.read()print(check)
print(frame)gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)cv2.imshow("capture1",gray)
#cv2.waitKey(0)key = cv2.waitKey(1)
if key == ord('q') :
breakprint(a)
# Command closes camera
video.release()cv2.destroyAllWindows()
make sure the code in your notepad ++ is like this because sometimes if it doesn’t fit it can get an error
then an image will appear as follows
will produce a continuous catch like a live streaming video.
yeah it worked!
We already know how computers capture video images in the form of a pixel value matrix. May be useful.
Next we will continue in part 2!
Reference
[1] https://medium.com/@986110101/pengenalan-wajah-1-6dc7d788fd07