解决c++ - In Dlib how do I save image with overlay?
推荐:how to save a c++ object in java object and use it
http://blog.csdn.net/luoshengyang/article/details/6817933 save: static void android_os_MessageQueue_setNativeMessageQueue(JNIEnv* env, jobject message
2018阿里云全部产品优惠券(新购或升级都可以使用,强烈推荐)
领取地址:https://promotion.aliyun.com/ntms/yunparter/invite.html
I'm trying to modify Dlib's face detection example to save an image with detections to a file since I'm using a server without GUI. So far I have only figured how to save the image but not the overlay. How do I save both to the same file?
//win.add_overlay(dets, rgb_pixel(255,0,0));
save_png(img, "detected.png");
c++ dlib
|
this question asked Apr 21 '15 at 4:13 Tim Clemans 127 9 How to use draw_rectangle in c++, Please help – Mavie Apr 18 '16 at 4:44 I have asked about it but It seems not got attention ..can u please tell me how did u saved image with overlay...You can also answer my question. link to question– Mavie Apr 18 '16 at 4:47
|
2 Answers
2
解决方法
You can call draw_rectangle on the image before saving it.
|
this answer answered Apr 21 '15 at 8:38 Davis King 2,750 14 15 what is the syntax to usedraw_rectangle
. – Mavie Apr 16 '16 at 10:31
|
Try this: dlib::draw_rectangle()
Example: dlib::draw_rectangle(rect_image, rect, dlib::rgb_pixel(255, 0, 0), 1);
|
this answer answered Oct 12 '16 at 8:43 Dmitry 31 5
how to save/crop detected faces in dlib python
up vote7down votefavorite
1
i want to save the detected face in dlib by cropping the rectangle do anyone have any idea how can i crop it. i am using dlib first time and having so many problems. i also want to run the fisherface algorithm on the detected faces but it is giving me type error when i pass the detected rectangle to pridictor. i seriously need help in this issue.
import cv2, sys, numpy, os
import dlib
from skimage import io
import json
import uuid
import random
from datetime import datetime
from random import randint
#predictor_path = sys.argv[1]
fn_haar = 'haarcascade_frontalface_default.xml'
fn_dir = 'att_faces'
size = 4
detector = dlib.get_frontal_face_detector()
#predictor = dlib.shape_predictor(predictor_path)
options=dlib.get_frontal_face_detector()
options.num_threads = 4
options.be_verbose = True
win = dlib.image_window()
# Part 1: Create fisherRecognizer
print('Training...')
# Create a list of images and a list of corresponding names
(images, lables, names, id) = ([], [], {}, 0)
for (subdirs, dirs, files) in os.walk(fn_dir):
for subdir in dirs:
names[id] = subdir
subjectpath = os.path.join(fn_dir, subdir)
for filename in os.listdir(subjectpath):
path = subjectpath + '/' + filename
lable = id
images.append(cv2.imread(path, 0))
lables.append(int(lable))
id += 1
(im_width, im_height) = (112, 92)
# Create a Numpy array from the two lists above
(images, lables) = [numpy.array(lis) for lis in [images, lables]]
# OpenCV trains a model from the images
model = cv2.createFisherFaceRecognizer(0,500)
model.train(images, lables)
haar_cascade = cv2.CascadeClassifier(fn_haar)
webcam = cv2.VideoCapture(0)
webcam.set(5,30)
while True:
(rval, frame) = webcam.read()
frame=cv2.flip(frame,1,0)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
mini = cv2.resize(gray, (gray.shape[1] / size, gray.shape[0] / size))
dets = detector(gray, 1)
print "length", len(dets)
print("Number of faces detected: {}".format(len(dets)))
for i, d in enumerate(dets):
print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(
i, d.left(), d.top(), d.right(), d.bottom()))
cv2.rectangle(gray, (d.left(), d.top()), (d.right(), d.bottom()), (0, 255, 0), 3)
'''
#Try to recognize the face
prediction = model.predict(dets)
print "Recognition Prediction" ,prediction'''
win.clear_overlay()
win.set_image(gray)
win.add_overlay(dets)
if (len(sys.argv[1:]) > 0):
img = io.imread(sys.argv[1])
dets, scores, idx = detector.run(img, 1, -1)
for i, d in enumerate(dets):
print("Detection {}, score: {}, face_type:{}".format(
d, scores[i], idx[i]))
python opencv face-detection face-recognition dlib
asked Oct 12 '16 at 21:39
116111
add a comment
4 Answers
up vote5down voteaccepted
Should be like this:
crop_img = img_full[d.top():d.bottom(),d.left():d.right()]
answered Oct 13 '16 at 15:35
8,46422333
add a comment
up vote3down vote
Please use minimal-working sample code to get answers faster.
After you have detected face - you have a rect. So you can crop image and save with opencv functions:
img = cv2.imread("test.jpg")
dets = detector.run(img, 1)
for i, d in enumerate(dets):
print("Detection {}, score: {}, face_type:{}".format(
d, scores[i], idx[i]))
crop = img[d.top():d.bottom(), d.left():d.right()]
cv2.imwrite("cropped.jpg", crop)
6,21722243
answered Oct 13 '16 at 11:21
1,918821
add a comment
up vote2down vote
Answer by Andrey was good but it misses edge cases where original rectangle is partially outside the image window. (Yes that happens with dlib.)
crop_img = img_full[max(0, d.top()): min(d.bottom(), image_height),
max(0, d.left()): min(d.right(), image_width)]
2,18892533
answered May 31 '17 at 1:47
39719
add a comment
up vote0down vote
# Select one of the haarcascade files:
# haarcascade_frontalface_alt.xml
# haarcascade_frontalface_alt2.xml
# haarcascade_frontalface_alt_tree.xml
# haarcascade_frontalface_default.xml
# haarcascade_profileface.xml
I remember haarcascade_frontalface_alt.xml is the best one?
In Dlib how do I save image with overlay?
up vote8down votefavorite
1
I'm trying to modify Dlib's face detection example to save an image with detections to a file since I'm using a server without GUI. So far I have only figured how to save the image but not the overlay. How do I save both to the same file?
//win.add_overlay(dets, rgb_pixel(255,0,0));
save_png(img, "detected.png");
asked Apr 21 '15 at 4:13
177111
-
How to use draw_rectangle in c++, Please help – Mavie Apr 18 '16 at 4:44
-
I have asked about it but It seems not got attention ..can u please tell me how did u saved image with overlay...You can also answer my question.link to question – Mavie Apr 18 '16 at 4:47
add a comment
2 Answers
up vote6down voteaccepted
You can call draw_rectangle on the image before saving it.
answered Apr 21 '15 at 8:38
3,79511820
-
what is the syntax to use
draw_rectangle
. – Mavie Apr 16 '16 at 10:31
add a comment
up vote2down vote
Try this: dlib::draw_rectangle()
Example:
dlib::draw_rectangle(rect_image, rect, dlib::rgb_pixel(255, 0, 0), 1);