python how to use the camera (opencv)

1 how opencv  retrieval camera

import cv2

# Using opencv library function to call cameras

import time

 

cap=cv2.VideoCapture(0)

# cv2.VideoCapture (0) on behalf of the transfer of webcam, wherein 0 representative of computer camera, a representative of an external camera (usb camera )

cap.set(3,900)

cap.set(4,900)

Cap.set # () camera set up parameters: 3: Width    4: High

while(cap.isOpened()):

# cap.isOpened () returns a Boolean value, to see if the camera is initializing successfully

ret_flag, Vshow = cap.read ()

# cap.read () returns two values, the first value is a Boolean value, if the video correctly, it returns true,   the second image represents a three-dimensional matrix of pixels

    cv2.imshow('Capture', Vshow)

k=cv2.waitKey(1)

# Top priority, this must have, get this letter, but has been unable to pass , that is waiting for user feedback within a certain period of time, if the user does not press the button, wait for the cycle to continue.

 

Waitkey (0): wait indefinitely button

Waitkey (1): wait 1 millisecond, did not continue to refresh

Waitkey (100): wait for the user 100 milliseconds, did not continue to refresh

 

And to achieve this press q functions, it must be in opencv to achieve the window, rather than in the terminal in

    if k==ord('s'):

        print('222222')

        print(cap.get(3))

        print(cap.get(4))

    elif k == words ( 'q'):

        print ( ' complete ' )

        break

    print ( ' camera capture success ' )

    # pass

# time.sleep(1)

cap.release()

cv2.destoryAllWindows ()

 

 

 

 

 

Code:

import cv2

import time

 

cap=cv2.VideoCapture(0)

cap.set(3,900)

cap.set(4,900)

while(cap.isOpened()):

    ret_flag, Vshow = cap.read ()

    cv2.imshow('Capture', Vshow)

    k=cv2.waitKey(1)

    if k==ord('s'):

        print('222222')

        print(cap.get(3))

        print(cap.get(4))

    elif k == words ( 'q'):

        print ( ' complete ' )

        break

    #Print ( ' camera capture success ' )

    # pass

# time.sleep(1)

cap.release()

cv2.destoryAllWindows ()

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

OpenCV-Python (1) used for face detection OpenCV in Python

OpenCV is now the most popular computer vision library, and we are today is to learn how to install and use OpenCV, and how to access our camera. Then take a look at writing a face detection process is how simple, simple to just a few lines of code.

Before we begin, I assume that you already have some knowledge of Python. Of course, if you think you qualify, here are some of the recommended learning Python's e-book , you can learn at first Python, so you can make a better understanding of the next steps. In addition, there is also recommended an e-book to learn OpenCV.

Well, do not waste time and get started.

To setup opencv in python environment you will need these things ready (match the versions to follow along with this tutorial),
first of all we need to prepare well in these environments (remember with a good version):

  • Python 2.x
  • OpenCV 3 (2 also, empathy
  • Numpy library (this can be downloaded at a later pip)

First, download the Python, we can go to the official website of the corresponding down version, if Windows version msi format may be, if it could be a Mac format pkg installation package, if it could be a Linux source packages.

And mounting the Python command line can be used to open the pip Python command to install the package, such as:

 

Since the OpenCV library using Numpy, so the first command pip install numpy installed Numpy library. After installation, try to import, no error is ok:

 

Then enter OpenCV official website under the corresponding version and install, try to import:

 

Face detection attempt

Preparation: We need to prepare pretrained classifier, in github inside opencv download the source code, search for like

All is ready except for the opportunity. We write code to detect the human face, come out of the OpenCV Hello world.

Here we are ready to use pre-trained XML file (the file download methods: find opencv on github, and then find, and then locate the file, download it using the right) , these XML files are more difficult to train, but we do not need worried, so a lot of people have been provided OpenCV face detection related to the pre-trained classifier for us.

You want to use to write this classification, we need to classifier XML file haarcascade_frontalface_default.xml from opencv folder / sources / data / haarcascades / down copied to our project directory, the directory is what we will write the program. If you do not opencv folder / sources / data / haarcascades / directory, you can try to find some opencv folder / report this content share / OpenCV / haarcascades / . Just find the following documents to:

 

Then if we want to load the classifier, then, like so:

cv2.CascadeClassifier detector = ( 'haarcascade_frontalface_default.xml)

Then let's first take a test camera it,

cap = cv2.VideoCapture(0)

right, img = cap.read ()

cv2.imshow ( "Window", img)

cv2.waitKey(0)

 

# Release webcam

cap.release()

The above code is to call your computer's 0 camera and displayed. Of course, if you have more than one camera, then you can also try other id, you can modify the parameters VideoCapture function.

Wherein cap.read () is obtained from the camera image, this function returns two variables, the first success expressed as a Boolean value, and a second image.

Then the program by showing imshow () picture, its first argument passed to the name of the window, while the second is to show the picture, the incoming code above is our selfie.

The waitKey is used to stop at the picture show interface lets you see, the parameters may be 10, 100, etc., in milliseconds, where 0 is the fill has been parked. Note, if the residence time is not long, it may not be visible in the photograph imshow.

Run this code, you will see a screen shot of the camera, in general, is your own.

 

Next, we will first picture is converted to grayscale images,

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

Then began the journey of face detection:

faces = detector.detectMultiScale(gray, 1.3, 5)

This code will wait until the above series of list, list each has x, y, height, width four variables. Wherein the list indicating the detection of a human face, i.e., the list size is the number of the face, and the position of each face in the image is (x, y, height, width ).

In order to allow us to more intuitively see test results, we will face these people out of the box:

for (x, y, w, h) in faces:

    cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)

Further

We have now detected by the camera to face, but we really need is not a static picture, what we need is a real-time dynamic video stream that can be detected. So we add a loop, and then continue testing, ultimately displayed in a new window.

cv2.CascadeClassifier detector = ( 'haarcascade_frontalface_default.xml)

cap = cv2.VideoCapture(0)

 

while True:

    right, img = cap.read ()

    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    faces = detector.detectMultiScale(gray, 1.3, 5)

    for (x, y, w, h) in faces:

        cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)

 

    cv2.imshow('frame', img)

    if cv2.waitKey(1) & 0xFF == ord('q'):

        break

 

cap.release()

cv2.destroyAllWindows()

Note that, with the end of the waitKey ord achieved by q and exit function, is detected in every millisecond there is no keyboard q pressed, and if pressed to exit the loop. Then they release resources.

to sum up

In this article, we learned how to use Python, OpenCV, that is code written by a program of face detection. We review these knowledge or learning points:

  • Using OpenCV classifier
  • Read photos from the camera
  • In other box in the picture
  • Display pictures in the new window
  • Real-time face detection

PS you find the XML file where there is a cat face recognition called it! ! !

First so be it

If the wrong place, please point out that more attention fried fish

 

Guess you like

Origin www.cnblogs.com/lllcccddd/p/11263983.html