Reputation: 61
the code below as I said on topic detects each faces in an image by using haarcascade- Opencv/Python.
Code detects all faces,
But I need to detect SAME faces in an image and then to draw bounding boxes with different colors
Iam beginer I googled to find how I can do this but I was inadequate.
I know that I need dataset, but I dont know how to train it and how to implement to the code below..
Does anyone had experinece about this before?
Maybe someone can give me an example according to code below and then I will try to follow his steps.
code that detects faces:
import cv2
import matplotlib.pyplot as plt
test_image = cv2.imread("C:\Users\erdal.alimovski\Desktop\faces.jpg")
test_image_gray = cv2.cvtColor(test_image, cv2.COLOR_BGR2GRAY)
plt.imshow(test_image_gray, cmap='gray')
def convertToRGB(image):
return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
haar_cascade_face = cv2.CascadeClassifier('C:\Users\haarcascade_frontalface_default.xml')
faces_rects = haar_cascade_face.detectMultiScale(test_image_gray, scaleFactor = 1.2, minNeighbors = 5);
print('Faces found: ', len(faces_rects))
for (x,y,w,h) in faces_rects:
cv2.rectangle(test_image, (x, y), (x+w, y+h), (0, 255, 0), 2)
cv2.imshow("yeni",test_image)
cv2.waitKey(10000)
Upvotes: 1
Views: 1740
Reputation: 106
At line cv2.rectangle(test_image, (x, y), (x+w, y+h), (0, 255, 0), 2)
the parameter (0,255,0) means that you draw everytime a green rectangle (because 255 is the green component).
You could either
1- pass a random-generated color on this parameter
2- init a list of colors that you want to use and loop through it in for (x,y,w,h) in faces_rects:
like
colorList=[(0,255,0),(255,0,0),(0,0,255)] # etc ...
i=0
for (x,y,w,h) in faces_rects:
cv2.rectangle(test_image, (x, y), (x+w, y+h), colorList[i], 2)
i=i+1
Upvotes: 0