Unable to detect faces of cartoon images - python

I am trying to implement a simple face detection of cartoons code using opencv and python. Though the code I have used works for faces of humans I am not able to detect cartoon faces using this. Is there any way I can make it detect cartoon faces too.
import cv2
import matplotlib.pyplot as plt
imagePath = 'frame179.jpg'
cascPath = '/Users/tonystark/opencv/data/haarcascades/haarcascade_frontalface_default.xml'
faceCascade = cv2.CascadeClassifier(cascPath)
image = cv2.imread(imagePath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=40,
minSize=(24, 24),
flags=cv2.CASCADE_SCALE_IMAGE
)
The images are human for which I am able to get the location of the face, whereas for cartoon, I am not able to get the location of faces.
Thanks a lot in advance !

Haar cascades are used to detect only one particular thing and in your case, it has been trained to detect only human faces. You will have to create another haar cascade to detect cartoon faces.
You can refer to this video for creating one -https://www.youtube.com/watch?v=jG3bu0tjFbk

Actually I agree with all above comments. There is no ready sollution for catroon objects and I've tried a lot of models and nothing works (ORB, Haar Cascade, SuperPixel and etc.) and no AutoML solution (from Google or Amazon) also does not work. There is only one way to do the face recognition in cartoons objects. This solution is firstly to make your own labeled data set of images and then try to train some models to detect objects. So I've chosen this way, and results are great, 91% accuracy.

So as cartoon images are very different to that of humans, we need to train a model from scratch for them separately.
I implemented a YOLO model, and trained it, and the results were really good !
labelImg can be used to prepare the dataset.
I referred this blog : Darknet YOLO using OpenCV to create my own YOLO detector in which the github repository Darknet YOLO is cloned and modified.
EDIT :
My project on github

Related

How to detect landmarks of a face including forehead?

This tutorial teaches how to detect landmarks of a face.
Here, there are no landmarks for the forehead. What can I do so that the landmarks also detect the forehead?
Forehead is not "re;iable" part of face, it's often being covered by hairs.. By the same reason ears are not included too in that 68-points dlib model. So, you can either create your own model or use, for example, Mediapipe Mesh.

Facial detection fails on iPhone photos only using dlib

I've been using the dlib library to detect faces, testing on both Python and DotNet wrappers. Both produce the same bizarre issue: poor (low image quality, poor lighting, etc.) images pulled from the internet are almost always detected while photos shot from my iPhone with high quality/lighting are almost never detected.
I am completely certain that the faces shot on my iPhone are of higher quality than those pulled from the internet, yet regardless of how many times I change the lighting and camera angle of my iPhone shots the results are always the same. An occasional iPhone face is detected while the large majority of perfectly acceptable face shots go undetected. I'm at a complete loss, any ideas are much appreciated.
A sample of my relatively straightforward code is below.
from face_recognition import load_image_file, face_locations
faces = face_locations(load_image_file("foo.jpg"))
face_detected = len(faces) >= 1
I figured out a solution and figured I'd share it in case anyone has this specific problem in the future. Converting all images to grayscale fixed literally every undetected face in my dataset. I wrote this in C# instead of Python, but I will share anyway. It can be done using the Emgu.CV library and the following code:
Image<Bgr, byte> emguImage = new Image<Bgr, byte>(path_to_image);
Image<Gray, byte> grayscaleEmguImage = emguImage.Convert<Gray, byte>().Clone();
Bitmap grayscaleBitmapImage = grayscaleEmguImage.ToBitmap();
It's fast and it works.
Could you try deepface as well?
#!pip install deepface
from deepface import DeepFace
img = DeepFace.detectFace("foo.jpg")
It wraps several face detectors actually. Its default detector is mtcnn but you can still set the detector you want.
detectors = ['mtcnn', 'opencv', 'ssd', 'dlib']
DeepFace.detectFace("foo.jpg", detector_backend = detectors[0])
detectFace function return exception if a face could not be detected.

Raspberry pi live video Object detection

I am new to using python and object detection and need some help for a uni project!
1- I am wandering if it is possible to use the same haar cascade file i used to train the pi to detect a specific object in a still image (using the picamera) for detecting the same image in a live video feed as my uni project requires the robot to search and find the specific object and retrieve it or do is it a completely separate process (i.e. setting up classes etc)? if so, where could I i find the most helpful information on doing this as I haven't been too successful when looking online. (pyimagesearch has a blog post on this but goes about using classes and i'm not sure how to go about even creating a class just for a specific object or if you even need one..)
2- Currently, my pi can kind of detect the object (a specific cube) in the still images but isnt very accurate or consistent, it often detects around the edges of the object as well as incorrectly detecting other things in the background or shadows that are part of the image, as the object as well. It tends to find more than one of the cube (lots of small rectangles mostly around - in close proximity) and in the object so it says its detecting 2+ cubes in an image (sometimes 15-20 or more) rather than just the one cube. I am wandering how I could reduce this error and increase the accuracy and consistency of the pi so it detects just the one, or at least doesn't wrongfully detect background shadows or other things in the image? I understand that lighting affects the result but I am wandering if its due to the quality of the original image i took with the picamera of the cube I used to train the haar cascade (it was quite a dark photo due to insufficient lighting), or maybe the size of the image itself (cropped it down to the edges so it is just the cube and resized it to 50x50), or maybe that I didnt train more than one image of the object against negatives when training the cascade file..? do the images you supply for training the pi have to taken with the picamera or could you take clearer pictures say with your phone and use them to train the cascade for detection via the picamera?I tried upgrading the resolution in the code but that made the data analysis take too long and didnt make much difference. Apologies for the long post as I am new to all this and wandering if theres any way to improve the results or is the only way for higher accuracy to retrain the cascade which I'd rather not do as it took two days to complete due to working with a pi zero W board!
Much Appreciated!
My specs: A raspberry pi Zero W board with a 16gb SD Card on Mac, running openCV 3.2 and Python 2.7.
Code for object detection in an image taken using the pi camera:
import io
import picamera
import cv2
import numpy as np
stream = io.BytesIO()
with picamera.PiCamera() as camera: camera.resolution = (320, 240) camera.capture(stream, format='jpeg')
buff = numpy.fromstring(stream.getvalue(), dtype=numpy.uint8)
image = cv2.imdecode(buff, 1)
cube_cascade = cv2.CascadeClassifier('/home/pi/data/cube1-cascade-10stages.xml')
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
Cube = cube_cascade.detectMultiScale(gray, 1.1, 5)
print "Found "+str(len(Cube))+" cube(s)"
for (x,y,w,h) in Cube: cv2.rectangle(image, (x,y), (x+w,y+h), (255,255,0), 2)
cv2.imwrite('result.jpg',image)
Thanks in advance.

Static Image Background Subtraction

I am trying to do background subtraction on static images of faces (extract the face), but the only OpenCV tutorials I can find are for video.
I understand that background is easier to identify from a video, but are there any approaches for photos?
Haar Cascade is widely used in that purpose. It is good to detect human face, but hardly used to classify who it is.
Here is nice reference : https://docs.opencv.org/3.4.1/d7/d8b/tutorial_py_face_detection.html

Face detection for two faces-Cascade opencv python

I am working on with the Face detection using cascade Classifier in opencv python. Its working fine. But i want to develop my code to detect only one face and also the largest face only to detect.
Sort the detected faces by size and keep the biggest one only?

Categories