I have a simple face detection implementation as following
import cv2
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
filename = "path/to/image"
img = cv2.imread(filename)
cv2.imshow("Original image", img)
face_region = face_cascade.detectMultiScale(img, 1.1, 4)
for (x, y, w, h) in face_region:
cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)
cv2.imshow("Output", img)
cv2.waitKey(0)
after running the code, I got the following result
As you can see that, the implementation detects two faces! How can I get rid of this kind of false detection?
first delete the textual data like in this link Delete OCR word from Image (OpenCV,Python)
after that try to use you face detection code...then it will improve your accuracy
Related
I am trying to use face detection but I do not want the video feed window to open up when I use videocapture, this is the code I'm working on:
import cv2
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
cap = cv2.VideoCapture(0)
while True:
_, img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)
cv2.imshow('img', img)
k = cv2.waitKey(30) & 0xff
if k==27:
break
cap.release()
I just want to somehow disable the video feed window that opens up automatically and instead get an output on the command line every time it detects a face.
I am using the code from here: https://github.com/adarsh1021/facedetection.
Thank you.
Reomve cv2.imshow('img', img) and replace it with print(faces)
cv2.imshow() is responsible for opening of image window.
I have not bordered table like on picture .
I tried to use example from this post
I have got this result
But I need something like this
How I could tune Open CV to get needed result?
You can easily achieved by using image_to_data method of pytesseract.
You also need to know:
Pre-processing methods
Page-segmentation-modes
Steps:
Load the image in BGR mode and convert it to the gray-scale
Get region-of-interest areas
From each area, get the coordinates and draw rectangle.
Result:
Code:
# Load the library
import cv2
import pytesseract
# Load the image
img = cv2.imread("1Tksb.jpg")
# Convert to gry-scale
gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# OCR detection
d = pytesseract.image_to_data(gry, config="--psm 6", output_type=pytesseract.Output.DICT)
# Get ROI part from the detection
n_boxes = len(d['level'])
# For each detected part
for i in range(n_boxes):
# Get the localized region
(x, y, w, h) = (d['left'][i], d['top'][i], d['width'][i], d['height'][i])
# Draw rectangle to the detected region
cv2.rectangle(img, (x, y), (x+w, y+h), (0, 0, 255), 1)
# Display
cv2.imshow("img", img)
cv2.waitKey(0)
If you want to read you can use image_to_string method.
I use open cv and haar cascade features to detect faces from an image. And after I load the cascade.xml library it will test the face read and draw a green rectangle in the face. My question is how to get confidence value from this library, like percentage or accusation value?
import cv2 as cv
img = cv.imread('Bryan/2.PNG')
cv.imshow('Bryan', img)
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
cv.imshow('Gray', gray)
haar_cascade = cv.CascadeClassifier('cascade.xml')
faces_rect = haar_cascade.detectMultiScale(
gray, scaleFactor=1.1, minNeighbors=6)
print(f'Number of faces of found = {len(faces_rect)}')
for (x, y, w, h) in faces_rect:
cv.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), thickness=2)
cv.imshow('Detected faces', img)
cv.waitKey(0)
I'm trying to write a Python 3.7 script to detect faces and features using the OpenCV Haar classifier files which stores images as n-dimensional Numpy arrays. Right now I'm only working with two features:
- The entire face
- The eyes
Both of these are obtained using two different classifiers.
The Code detects the presence of both these features in the image and then marks them with a rectangle using the cv2.rectangle() function inside a for loop for each feature i.e, one for detected faces and one for detected eyes.
I want the script to mark the eye-rectangles ONLY if those points were found in the face array as well as the eye array. numpy.intersect1d() only finds intersection within one-dimensional arrays.
I've even tried
for x,y,w,h in eyes and x,y,w,h in faces:
and all it does is return this error message:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Any help would be greatly appreciated.
This is being attempted on Windows 10 64-bit, the code is written in Pycharm 2019, OpenCV is imported as CV2.
# Creating the cascade classifier objects.
face_cascade =
cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
eye_cascade = cv2.CascadeClassifier("haarcascade_eye.xml")
# Entering and displaying the original image.
img = cv2.imread("filename.jpg", 1)
cv2.imshow("Original Images",img)
cv2.waitKey(0)
cv2.destroyAllWindows()
# Convert the image to Black and White and store in a variable.
gry_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gry_img, scaleFactor = 1.05, minNeighbors = 8)
eyes = eye_cascade.detectMultiScale(gry_img, scaleFactor = 1.30, minNeighbors = 12)
# Now, we mark the detected features with a rectangle on the original image.
for x,y,w,h in faces:
img = cv2.rectangle(img, (x,y), (x+w, y+h), (255, 21, 21), 3) # Blue.
for x,y,w,h in eyes:
img = cv2.rectangle(img, (x,y), (x+w, y+h), (255, 255, 24), 3) # Cyan.
cv2.imshow("Detected Faces and Eyes",img)
cv2.waitKey(0)
cv2.destroyAllWindows()
I need the eye-features to be marked only if they were ALSO found in the face-features array.
A better approach to solve this problem is already suggested in the OpenCV docs. It suggests that instead of passing the whole image for detecting the eyes, you should first detect the face, then crop the image, and use this cropped image for eyes detection. For cropping the face image you can use numpy slicing as:
for x,y,w,h in faces:
face_img = gry_img[y:y+h, x:x+w]
# Now detect the eyes in this `face_img` only.
eyes = eye_cascade.detectMultiScale(face_img, scaleFactor = 1.30, minNeighbors = 12)
Oh boy, do I feel stupid now. Well here goes:
As ZdaR said, the OpenCV documentation does indeed give a perfect example on how to achieve the result I required, the gist of which is:
for x,y,w,h in faces:
face_img = gry_img[y:y+h, x:x+w]
# Now detect the eyes in this `face_img` only.
eyes = eye_cascade.detectMultiScale(face_img, scaleFactor = 1.30, minNeighbors = 12)
What I needed to do other than this, apparently was to crop the original image to the face coordinates as well for the eye-features to be drawn upon. I'm still not sure if this should work, but for now it seems that adding that extra line does work.
Code that finally worked:
faces = face_cascade.detectMultiScale(gry_img, scaleFactor = 1.05, minNeighbors = 10)
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y),(x + w, y + h), (255, 0, 0), 3)
gry_eye = gry_img[y:y+h, x:x+w]
eye_img = img[y:y + h, x:x + w]
eyes = eye_cascade.detectMultiScale(gry_eye, scaleFactor = 1.05, minNeighbors = 5)
for (x, y, w, h) in eyes:
cv2.rectangle(eye_img, (x, y), (x + w, y + h), (255, 255, 24), 3)
Well, this has been a learning experience.
Thanks to ZdaR for the help!
I have started to learn OpenCV lib in Python. I want to write a code that will detect faces and eyes on the photo but also count number of detections.
I have wrote a picture that has 6 people on it.
You can download the image form here:
https://www.sendspace.com/file/cznbqa
I wrote a code that detect faces and eyes, but I do not know how to count number of detections.
Also, my code detect 4/8 faces and 1/12 eyes on the photo so...I probably have some issues in the code, If you can help me with that also, I would be very thankful.
# Standard imports
import cv2
import numpy as np
# Read image
my_image = cv2.imread("ljudi.jpg", 1)
# data for detecting faces and eyes
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
img = cv2.imread("ljudi.jpg", 1)
gray = cv2.cvtColor(img, 0)
faces = face_cascade.detectMultiScale(gray, 1.2, 4)
for (x,y,w,h) in faces:
#detect face with bule rectangle
#rec. start point (x,y), rec. end point (x+w, y+h), blue color(255,0,0), line width 2
face_rec = cv2.rectangle(img, (x,y), (x+w, y+h), (255,0,0), 1)
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
eyes = eye_cascade.detectMultiScale(roi_gray, 1.1, 2)
for (ex,ey,ew,eh) in eyes:
eye_rec = cv2.rectangle(roi_color, (ex,ey), (ex+ew, ey+eh), (0,255,0), )
cv2.imshow('img', img)
cv2.waitKey(0)
From the OpenCV documentation:
https://www.docs.opencv.org/2.4/modules/objdetect/doc/cascade_classification.html#cascadeclassifier-detectmultiscale
Detects objects of different sizes in the input image. The detected objects are returned as a list of rectangles.
where a rectangle for the Python bindings simply seems to be of type list [x,y,w,h] (https://www.docs.opencv.org/2.4/modules/core/doc/basic_structures.html#rect).
To the number of faces / rectangles returned inside the list can easily be retrieved by getting the length of the list (i.e. number of items inside), which is done with the Python builtin function len(): https://docs.python.org/3/library/functions.html#len.