Skip to content

Commit

Permalink
Add video test (#2)
Browse files Browse the repository at this point in the history
* video test

* add minicaffe support

* Update README.md
  • Loading branch information
luoyetx authored Apr 1, 2017
1 parent bf7abe1 commit f7faf2a
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 1 deletion.
3 changes: 3 additions & 0 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ include(mini-caffe/mini-caffe.cmake)

add_executable(jfda main.cpp jfda.hpp jfda.cpp)
target_link_libraries(jfda caffe ${OpenCV_LIBS})

add_executable(video video.cpp jfda.hpp jfda.cpp)
target_link_libraries(video caffe ${OpenCV_LIBS})
4 changes: 4 additions & 0 deletions cpp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CPP code for JFDA
=================

View video demo on YouTube [here](https://www.youtube.com/watch?v=Ee9UT9Zf98s).
5 changes: 5 additions & 0 deletions cpp/convert.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

ffmpeg -i ./build/test.mp4 -f mp3 -vn ./build/test.mp3

ffmpeg -i ./build/test.mp3 -i ./build/result.avi -f mp4 -y ./build/result.mp4
53 changes: 53 additions & 0 deletions cpp/video.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include <iostream>
#include "./jfda.hpp"
#include "caffe/caffe.hpp"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace cv;
using namespace std;
using namespace jfda;

int main(int argc, char* argv[]) {
VideoCapture cap("./test.mp4");
if (!cap.isOpened()) {
cout << "Can\'t open test.mp4" << endl;
return 0;
}
double rate = cap.get(CV_CAP_PROP_FPS);
double width = cap.get(CV_CAP_PROP_FRAME_WIDTH);
double height = cap.get(CV_CAP_PROP_FRAME_HEIGHT);
int fourcc = CV_FOURCC('X', 'V', 'I', 'D');
VideoWriter writer("./result.avi", fourcc, rate, Size(width, height), true);
string proto_dir = "../../proto/";
string model_dir = "../../model/";
JfdaDetector detector(proto_dir + "p.prototxt", model_dir + "p.caffemodel",
proto_dir + "r.prototxt", model_dir + "r.caffemodel",
proto_dir + "o.prototxt", model_dir + "o.caffemodel",
proto_dir + "l.prototxt", model_dir + "l.caffemodel", 0);
detector.SetMinSize(24);
Mat frame;
caffe::Profiler* profiler = caffe::Profiler::Get();
while (cap.read(frame)) {
int64_t t1 = profiler->Now();
vector<FaceInfo> faces = detector.Detect(frame);
int64_t t2 = profiler->Now();
double fps = 1000000 / (t2 - t1);
for (int i = 0; i < faces.size(); i++) {
FaceInfo& face = faces[i];
cv::rectangle(frame, face.bbox, Scalar(0, 0, 255), 2);
for (int j = 0; j < 5; j++) {
cv::circle(frame, face.landmark5[j], 2, Scalar(0, 255, 0), -1);
}
}
char buff[30];
sprintf(buff, "%.2f FPS", fps);
cv::putText(frame, buff, Point2f(0, 10), FONT_HERSHEY_PLAIN, 1, Scalar(0, 255, 0));
writer.write(frame);
cv::imshow("frame", frame);
if (waitKey(1) >= 0) {
break;
}
}
return 0;
}
8 changes: 7 additions & 1 deletion demo.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python2.7
# pylint: disable=bad-indentation, no-member, invalid-name, line-too-long

import os
import argparse
Expand All @@ -12,7 +13,11 @@ def main(args):
'proto/r.prototxt', 'model/r.caffemodel',
'proto/o.prototxt', 'model/o.caffemodel',
'proto/l.prototxt', 'model/l.caffemodel',]
detector = JfdaDetector(net)
if args.minicaffe:
from jfda.mdetector import MiniCaffeDetector
detector = MiniCaffeDetector(net)
else:
detector = JfdaDetector(net)
if args.pnet_single:
detector.set_pnet_single_forward(True)
param = {
Expand Down Expand Up @@ -63,6 +68,7 @@ def gen(img, bboxes, outname):
parser = argparse.ArgumentParser()
parser.add_argument('--gpu', type=int, default=-1, help='gpu id to use, -1 for cpu')
parser.add_argument('--pnet-single', action='store_true', help='pnet use single forward')
parser.add_argument('--minicaffe', action='store_true', help='use minicaffe')
args = parser.parse_args()

if args.gpu >= 0:
Expand Down
1 change: 1 addition & 0 deletions fddb.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python2.7
# pylint: disable=bad-indentation, no-member, invalid-name, line-too-long

import argparse
import cv2
Expand Down
7 changes: 7 additions & 0 deletions jfda/detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ def detect(self, img, ths, min_size, factor, debug=False):
data[i] = cv2.resize(face, (24, 24)).transpose((2, 0, 1))
data = (data - 128) / 128
prob, bbox_pred, landmark_pred = self._forward(self.rnet, data, ['prob', 'bbox_pred', 'landmark_pred'])
prob = prob.reshape(n, 2)
bbox_pred = bbox_pred.reshape(n, 4)
landmark_pred = landmark_pred.reshape(n, 10)
keep = prob[:, 1] > ths[1]
bboxes = bboxes[keep]
bboxes[:, 4] = prob[keep, 1]
Expand Down Expand Up @@ -122,6 +125,9 @@ def detect(self, img, ths, min_size, factor, debug=False):
data[i] = cv2.resize(face, (48, 48)).transpose((2, 0, 1))
data = (data - 128) / 128
prob, bbox_pred, landmark_pred = self._forward(self.onet, data, ['prob', 'bbox_pred', 'landmark_pred'])
prob = prob.reshape(n, 2)
bbox_pred = bbox_pred.reshape(n, 4)
landmark_pred = landmark_pred.reshape(n, 10)
keep = prob[:, 1] > ths[2]
bboxes = bboxes[keep]
bboxes[:, 4] = prob[keep, 1]
Expand Down Expand Up @@ -158,6 +164,7 @@ def detect(self, img, ths, min_size, factor, debug=False):
data[i, (3*j):(3*j+3)] = patch
data = (data - 128) / 128
offset = self._forward(self.lnet, data, ['landmark_offset'])[0]
offset = offset.reshape(n, 10)
offset *= l.reshape((-1, 1))
bboxes[:, 9:] += offset
timer.toc()
Expand Down
19 changes: 19 additions & 0 deletions jfda/mdetector.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# pylint: disable=bad-indentation, no-member, invalid-name, line-too-long
from .detector import JfdaDetector
import minicaffe as caffe


class MiniCaffeDetector(JfdaDetector):

def __init__(self, nets):
assert len(nets) in [2, 4, 6, 8], 'wrong number of nets'
self.pnet, self.rnet, self.onet, self.lnet = None, None, None, None
if len(nets) >= 2:
self.pnet = caffe.Net(nets[0], nets[1])
if len(nets) >= 4:
self.rnet = caffe.Net(nets[2], nets[3])
if len(nets) >= 6:
self.onet = caffe.Net(nets[4], nets[5])
if len(nets) >= 8:
self.lnet = caffe.Net(nets[6], nets[7])
self.pnet_single_forward = False

0 comments on commit f7faf2a

Please sign in to comment.