OpenCV face detection methods hangs - python

I have used OpenCVs detectMultiScale and res10_300x300_ssd_iter_140000.caffemodel.forward() methods to detect faces in the images. Exact code is shown.
Both these methods worked well providing good results until a day ago. Today, both of these processes hangs at detectMultiScale and net.forward commands respectively. Additionally when the DNN based model runs, the system memory slowly starts building up until the system hangs.
There has been no modification in any of the python libraries or system configuration in the last day. I have tried to reinstall openCV and python so far which has not been benificial.
#code for For cascade based detection:
faceCascade = cv2.CascadeClassifier('./haarcascade_frontalface_default.xml')
faces = faceCascade.detectMultiScale(frame)
#Python code for For DNN based detection:
modelFile = "res10_300x300_ssd_iter_140000.caffemodel"
configFile = "deploy.prototxt"
net = cv2.dnn.readNetFromCaffe(configFile, modelFile)
net.setInput(blob)
detections = net.forward()
I am unable to understand the reason behind the memory leak that is happening and possible solution to overcome this.

Related

Occlusion handling in object tracking

I am implementing motion based object tracking program which is using background substraction, Kalman Filter and Hungarian algorithm. Everything is working fine except the occlusions. When two object are close enough to each other the background substraction recognizes it as one of these two objects. After they split the program recognizes these two objects correctly. I am looking for solution/algorithm which will detect occlusion like shown in point c) in the example below.
I will appreciate any references or code examples reffering to occlusion detecion problem when using background substraction.
Object detection using a machine learning algorithm should reliably distinguish between these objects, even with significant occlusion. You haven't shared anything about your environment so I don't know what kind of constraints you have, but using an ML approach, here is how I would tackle your problem.
import cv2
from sort import *
tracker = Sort() # Create instance of tracker (see link below for repo)
cap = cv2.VideoCapture(0)
while(True):
ret, frame = cap.read()
# Not sure what your environment is,
#but get your objects bounding boxes like this
detected_objects = detector.detect(frame) #pseudo code
# Get tracking IDs for objects and bounding boxes
detected_objects_with_ids = tracker.update(detected_objects)
...
The above example uses this Kalman Filter and Hungarian algorithm, which can track multiple objects in real-time.
Again, not sure about your environment, but you could find pre-built object detection algorithms on the Tensorflow site.

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.

Finetuning a tensorflow object detection pretrained model

I'm working on a real-time object detector with tensorflow and opencv.
I've used different SSD and Faster-RCNN based frozen inference graphs and they almost never fail.
The video stream comes from a IR camera fixed to a wall that has a background that almost never changes. There are some misdetections at particular hours of the day (e.g. when the light changes in the afternoon) that occur in the background area or on small objects too close to the camera.
So to fix this little mistakes i wanted to finetune the model with images from the same background.
Being the background always the same, how do i approach the retraining of the model having 1000 misdetections pics that are all almost the same?
In case of variations in the background lighting, it might be possible to use Background Subtraction
https://docs.opencv.org/3.4.1/d1/dc5/tutorial_background_subtraction.html
,while dynmically updating it as shown here:
https://www.pyimagesearch.com/2015/06/01/home-surveillance-and-motion-detection-with-the-raspberry-pi-python-and-opencv/
Thank you.

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.

boost face detection/recognition process in python (opencv + tensorflow +CNN)

I am working on a face detection and recognition app in python using tensorflow and opencv. The overall flow is as follow:
while True:
#1) read one frame using opencv: ret, frame = video_capture.read()
#2) Detect faces in the current frame using tensorflow (using mxnet_mtcnn_face_detection)
#3) for each detected face in the current frame, run facenet algorithm (tensorflow) and compare with my Database, to find the name of the detected face/person
#4) displlay a box around each face with the name of the person using opencv
Now, my issue is that the overhead (runtime) of face detection and recognition is very high and thus sometime the output video is more like a slow motion! I tried to use tracking methods (e.g., MIL, KCF), but in this case, I cannot detect new faces coming into the frame! Any approach to increase the speedup? At least to get rid of "face recognition function" for those faces that already recognized in previous frames!

Categories