create a person detector in python

To create a person detector in Python, you can use the OpenCV library along with a pre-trained classifier. Here is a step-by-step guide:

  1. Install OpenCV library by running the following command in your command line interface:
    main.py
    pip install opencv-python
    
    26 chars
    2 lines
  2. Download a pre-trained classifier for detecting people. You can use the Haar Cascade classifier for this purpose. Download the XML file from this link.
  3. Import the OpenCV library and read the image using the imread() function:
    main.py
    import cv2
    image = cv2.imread('path_to_image')
    
    47 chars
    3 lines
  4. Convert the image to grayscale using the cvtColor() function:
    main.py
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    
    47 chars
    2 lines
  5. Load the pre-trained classifier using the CascadeClassifier() function:
    main.py
    classifier = cv2.CascadeClassifier('path_to_haar_cascade')
    
    59 chars
    2 lines
  6. Detect the persons in the image using the detectMultiScale() function:
    main.py
    persons = classifier.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
    
    95 chars
    2 lines
  7. Draw a rectangle around each detected person using the rectangle() function:
    main.py
    for (x, y, w, h) in persons:
        cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
    
    94 chars
    3 lines
  8. Display the image using the imshow() function:
    main.py
    cv2.imshow("Image", image)
    cv2.waitKey(0)
    
    42 chars
    3 lines

This will create a person detector using Python and OpenCV library.

gistlibby LogSnag