refactor face detection code
|
@ -1,62 +1,89 @@
|
|||
import cv2
|
||||
import sys
|
||||
import os
|
||||
|
||||
import cv2
|
||||
import numpy as np
|
||||
|
||||
import sys,os,numpy
|
||||
from glob import glob
|
||||
from skimage import io
|
||||
|
||||
#read test photo
|
||||
pwd = sys.path[0]
|
||||
img = cv2.imread(pwd + "/Facial_test_images/6.jpg")
|
||||
|
||||
grey = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
||||
def face_detector_haarcascade(image):
|
||||
|
||||
resize_fx = 1
|
||||
resize_fy = 1
|
||||
grey = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
||||
|
||||
grey = cv2.resize(grey, dsize=None, fx=resize_fx, fy=resize_fy, interpolation = cv2.INTER_AREA)
|
||||
resize_fx = 1
|
||||
resize_fy = 1
|
||||
grey = cv2.resize(grey, dsize=None, fx=resize_fx, fy=resize_fy, interpolation = cv2.INTER_AREA)
|
||||
|
||||
pwd = sys.path[0]
|
||||
classfier = cv2.CascadeClassifier(pwd + "/Facial_models/haarcascade_frontalface_alt2.xml")
|
||||
|
||||
faceRects = classfier.detectMultiScale(grey, scaleFactor=1.2, minNeighbors=1, minSize=(16, 16))
|
||||
|
||||
if len(faceRects) > 0:
|
||||
for faceRect in faceRects:
|
||||
x, y, w, h = faceRect
|
||||
x = int(x/resize_fx)
|
||||
y = int(y/resize_fy)
|
||||
w = int(w/resize_fx)
|
||||
h = int(h/resize_fy)
|
||||
cv2.rectangle(image, (x - 10, y - 10), (x + w + 10, y + h + 10), (0, 255, 0), 5)
|
||||
|
||||
return image
|
||||
|
||||
|
||||
classfier = cv2.CascadeClassifier(pwd + "/Facial_models/haarcascade_frontalface_alt2.xml")
|
||||
def face_detector_ssd(image):
|
||||
|
||||
faceRects = classfier.detectMultiScale(grey, scaleFactor=1.2, minNeighbors=1, minSize=(16, 16))
|
||||
pwd = sys.path[0]
|
||||
net = cv2.dnn.readNetFromCaffe(pwd+"/Facial_models/deploy.prototxt", pwd+"/Facial_models/res10_300x300_ssd_iter_140000_fp16.caffemodel")
|
||||
|
||||
color = (0, 255, 0)
|
||||
if len(faceRects) > 0:
|
||||
for faceRect in faceRects:
|
||||
x, y, w, h = faceRect
|
||||
x = int(x/resize_fx)
|
||||
y = int(y/resize_fy)
|
||||
w = int(w/resize_fx)
|
||||
h = int(h/resize_fy)
|
||||
cv2.rectangle(img, (x - 10, y - 10), (x + w + 10, y + h + 10), color, 5)
|
||||
resize = (800, 800)
|
||||
confidence_thres = 0.65
|
||||
|
||||
blob = cv2.dnn.blobFromImage(cv2.resize(image, dsize=resize), 1.0, resize, (104.0, 177.0, 123.0))
|
||||
# blob = cv2.dnn.blobFromImage(cv2.resize(image, (300, 300)), 1.0, (300, 300), (104.0, 177.0, 123.0))
|
||||
|
||||
cv2.imwrite(pwd + "/Facial_test_images/output-a.jpg",img)
|
||||
cv2.imshow("face_image_a",img)
|
||||
net.setInput(blob)
|
||||
|
||||
detections = net.forward()
|
||||
|
||||
h,w,c=image.shape
|
||||
|
||||
for i in range(0, detections.shape[2]):
|
||||
confidence = detections[0, 0, i, 2]
|
||||
if confidence > confidence_thres:
|
||||
box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
|
||||
(startX, startY, endX, endY) = box.astype("int")
|
||||
text = "{:.2f}%".format(confidence * 100)
|
||||
y = startY - 10 if startY - 10 > 10 else startY + 10
|
||||
cv2.rectangle(image, (startX, startY), (endX, endY),(0, 255,0), 5)
|
||||
cv2.putText(image, text, (startX, y), cv2.FONT_HERSHEY_SIMPLEX, 1.00, (0, 255, 0), 3)
|
||||
|
||||
return image
|
||||
|
||||
|
||||
|
||||
image = cv2.imread(pwd + "/Facial_test_images/6.jpg")
|
||||
if __name__=="__main__":
|
||||
|
||||
net = cv2.dnn.readNetFromCaffe(pwd+"/Facial_models/deploy.prototxt", pwd+"/Facial_models/res10_300x300_ssd_iter_140000_fp16.caffemodel")
|
||||
image_name = "8.jpg"
|
||||
split_name = image_name.split(".")
|
||||
|
||||
blob = cv2.dnn.blobFromImage(cv2.resize(image, (300, 300)), 1.0, (300, 300), (104.0, 177.0, 123.0))
|
||||
image_read_path = sys.path[0]+"/Facial_test_images/"+image_name
|
||||
image_save_path = sys.path[0]+"/Facial_test_images/output/"+split_name[0]+"_result."+split_name[1]
|
||||
|
||||
net.setInput(blob)
|
||||
image = cv2.imread(image_read_path)
|
||||
|
||||
detections = net.forward()
|
||||
image = face_detector_ssd(image)
|
||||
#image = face_detector_haarcascade(image)
|
||||
|
||||
print(image_save_path)
|
||||
|
||||
cv2.imwrite(image_save_path, image)
|
||||
cv2.imshow("result", image)
|
||||
cv2.waitKey()
|
||||
cv2.destroyAllWindows()
|
||||
|
||||
|
||||
|
||||
h,w,c=image.shape
|
||||
for i in range(0, detections.shape[2]):
|
||||
confidence = detections[0, 0, i, 2]
|
||||
if confidence > 0.65:
|
||||
box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
|
||||
(startX, startY, endX, endY) = box.astype("int")
|
||||
text = "{:.2f}%".format(confidence * 100)
|
||||
y = startY - 10 if startY - 10 > 10 else startY + 10
|
||||
cv2.rectangle(image, (startX, startY), (endX, endY),(0, 255,0), 5)
|
||||
cv2.putText(image, text, (startX, y), cv2.FONT_HERSHEY_SIMPLEX, 1.00, (0, 255, 0), 3)
|
||||
|
||||
cv2.imwrite(pwd + "/Facial_test_images/output-b.jpg", image)
|
||||
cv2.imshow("face_image_b",image)
|
||||
cv2.waitKey(0)
|
||||
|
|
Before Width: | Height: | Size: 81 KiB After Width: | Height: | Size: 80 KiB |
BIN
Facial_test_images/8.jpg
Normal file
After Width: | Height: | Size: 106 KiB |
BIN
Facial_test_images/9.jpg
Normal file
After Width: | Height: | Size: 90 KiB |
Before Width: | Height: | Size: 236 KiB |
Before Width: | Height: | Size: 240 KiB |
Before Width: | Height: | Size: 267 KiB |
Before Width: | Height: | Size: 145 KiB |
Before Width: | Height: | Size: 292 KiB |
Before Width: | Height: | Size: 329 KiB |
Before Width: | Height: | Size: 240 KiB |
BIN
Facial_test_images/output/3_result.jpg
Normal file
After Width: | Height: | Size: 159 KiB |
BIN
Facial_test_images/output/3s_result.jpg
Normal file
After Width: | Height: | Size: 162 KiB |
BIN
Facial_test_images/output/8_result.jpg
Normal file
After Width: | Height: | Size: 196 KiB |
BIN
Facial_test_images/output/8s_result.jpg
Normal file
After Width: | Height: | Size: 203 KiB |
BIN
Facial_test_images/output/9_result.jpg
Normal file
After Width: | Height: | Size: 170 KiB |
BIN
Facial_test_images/output/9s_result.jpg
Normal file
After Width: | Height: | Size: 178 KiB |
Before Width: | Height: | Size: 270 KiB |
Before Width: | Height: | Size: 129 KiB |
Before Width: | Height: | Size: 179 KiB |
Before Width: | Height: | Size: 273 KiB |
Before Width: | Height: | Size: 331 KiB |
Before Width: | Height: | Size: 235 KiB |