Skip to content

Commit

Permalink
obsproject#11 1 添加字体库FreeType(C++开源库) 处理opencv中中文显示
Browse files Browse the repository at this point in the history
2 添加字体库simsun.ttc(windows字体库),在非中文系统下,也可以显示中文
3 opencv窗口显示图标 vhome 图标
  • Loading branch information
5455945 committed Dec 28, 2016
1 parent a410649 commit 829f21d
Show file tree
Hide file tree
Showing 60 changed files with 22,963 additions and 4 deletions.
7 changes: 5 additions & 2 deletions plugins/face-detect/CMakeLists.txt
@@ -1,12 +1,14 @@
project(face-detect)

set(face-detect_inc_HEADERS
inc/face-detect.h)
inc/face-detect.h
CvTextFreeType.h)

set(face-detect_SOURCES
face-detect.def
face-detect-plugin-main.c
face-detect.cpp)
face-detect.cpp
CvTextFreeType.cpp)

set(face-detect_HEADERS
${face-detect_inc_HEADERS}
Expand All @@ -31,6 +33,7 @@ include_directories(${LIBCURL_INCLUDE_DIRS})
add_definitions(${LIBCURL_DEFINITIONS})

target_link_libraries(face-detect libobs
freetype.lib
IlmImf.lib
ippicvmt.lib
libjasper.lib
Expand Down
170 changes: 170 additions & 0 deletions plugins/face-detect/CvTextFreeType.cpp
@@ -0,0 +1,170 @@
#include <wchar.h>
#include <assert.h>
#include <locale.h>
#include <ctype.h>
#include "CvTextFreeType.h"

CvTextFreeType::CvTextFreeType(const char *freeType)
{
assert(freeType != NULL);

// 打开字库文件, 创建一个字体
if (FT_Init_FreeType(&m_library)) throw;
if (FT_New_Face(m_library, freeType, 0, &m_face)) throw;

// 设置字体输出参数
restoreFont();

// 设置C语言的字符集环境
setlocale(LC_ALL, "");
}

// 释放FreeType资源
CvTextFreeType::~CvTextFreeType()
{
FT_Done_Face(m_face);
FT_Done_FreeType(m_library);
}

// 设置字体参数:
// font 字体类型, 目前不支持
// size 字体大小/空白比例/间隔比例/旋转角度
// underline 下画线
// diaphaneity 透明度
void CvTextFreeType::getFont(int *type, CvScalar *size, bool *underline, float *diaphaneity)
{
if (type) *type = m_fontType;
if (size) *size = m_fontSize;
if (underline) *underline = m_fontUnderline;
if (diaphaneity) *diaphaneity = m_fontDiaphaneity;
}

void CvTextFreeType::setFont(int *type, CvScalar *size, bool *underline, float *diaphaneity)
{
// 参数合法性检查
if (type) {
if (type >= 0) {
m_fontType = *type;
}
}
if (size) {
m_fontSize.val[0] = fabs(size->val[0]);
m_fontSize.val[1] = fabs(size->val[1]);
m_fontSize.val[2] = fabs(size->val[2]);
m_fontSize.val[3] = fabs(size->val[3]);
}
if (underline) {
m_fontUnderline = *underline;
}
if (diaphaneity) {
m_fontDiaphaneity = *diaphaneity;
}
}

// 恢复原始的字体设置
void CvTextFreeType::restoreFont()
{
m_fontType = 0; // 字体类型(不支持)
m_fontSize.val[0] = 20; // 字体大小
m_fontSize.val[1] = 0.5; // 空白字符大小比例
m_fontSize.val[2] = 0.1; // 间隔大小比例
m_fontSize.val[3] = 0; // 旋转角度(不支持)
m_fontUnderline = false; // 下画线(不支持)
m_fontDiaphaneity = 1.0; // 色彩比例(可产生透明效果)

// 设置字符大小
FT_Set_Pixel_Sizes(m_face, (int)m_fontSize.val[0], 0);
}

// 输出函数(颜色默认为黑色)
int CvTextFreeType::putText(cv::Mat &frame, const char *text, CvPoint pos)
{
return putText(frame, text, pos, CV_RGB(255, 255, 255));
}

int CvTextFreeType::putText(cv::Mat &frame, const wchar_t *text, CvPoint pos)
{
return putText(frame, text, pos, CV_RGB(255, 255, 255));
}

int CvTextFreeType::putText(cv::Mat &frame, const char *text, CvPoint pos, CvScalar color)
{
if (frame.empty() || (text == nullptr)) {
return -1;
}

int i;
for (i = 0; text[i] != '\0'; ++i) {
wchar_t wc = text[i];
// 解析双字节符号
if (!isascii(wc)) {
mbtowc(&wc, &text[i++], 2);
}
// 输出当前的字符
putWChar(frame, wc, pos, color);
}
return i;
}

int CvTextFreeType::putText(cv::Mat &frame, const wchar_t *text, CvPoint pos, CvScalar color)
{
if (frame.empty() || (text == nullptr)) {
return -1;
}

int i;
for (i = 0; text[i] != '\0'; ++i) {
// 输出当前的字符
putWChar(frame, text[i], pos, color);
}

return i;
}

// 输出当前字符, 更新m_pos位置
void CvTextFreeType::putWChar(cv::Mat &frame, wchar_t wc, CvPoint &pos, CvScalar color)
{
// 根据unicode生成字体的二值位图
IplImage* img=NULL;
img = &(IplImage)frame;

FT_UInt glyph_index = FT_Get_Char_Index(m_face, wc);
FT_Load_Glyph(m_face, glyph_index, FT_LOAD_DEFAULT);
FT_Render_Glyph(m_face->glyph, FT_RENDER_MODE_MONO);

FT_GlyphSlot slot = m_face->glyph;

// 行列数
int rows = slot->bitmap.rows;
int cols = slot->bitmap.width;

for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
int off = ((img->origin == 0) ? i : (rows - 1 - i))* slot->bitmap.pitch + j / 8;

if (slot->bitmap.buffer[off] & (0xC0 >> (j % 8))) {
int r = (img->origin == 0) ? pos.y - (rows - 1 - i) : pos.y + i;
int c = pos.x + j;

if ((r >= 0) && (r < img->height)
&& (c >= 0) && (c < img->width)) {
CvScalar scalar = cvGet2D(img, r, c);

// 进行色彩融合
float p = m_fontDiaphaneity;
for (int k = 0; k < 4; ++k) {
scalar.val[k] = scalar.val[k] * (1 - p) + color.val[k] * p;
}

cvSet2D(img, r, c, scalar);
}
}
}
}

// 修改下一个字的输出位置
double space = m_fontSize.val[0] * m_fontSize.val[1];
double sep = m_fontSize.val[0] * m_fontSize.val[2];

pos.x += (int)((cols ? cols : space) + sep);
}
73 changes: 73 additions & 0 deletions plugins/face-detect/CvTextFreeType.h
@@ -0,0 +1,73 @@
#pragma once
#include <ft2build.h>
#include FT_FREETYPE_H
#include <opencv/cv.h>
#include <opencv/highgui.h>

class CvTextFreeType
{
public:
// 装载字库文件
CvTextFreeType(const char *freeType);
virtual ~CvTextFreeType();

//获取字体。目前有些参数尚不支持。
//param font 字体类型, 目前不支持
//param size 字体大小/空白比例/间隔比例/旋转角度
//param underline 下画线
//param diaphaneity 透明度
void getFont(int *type, CvScalar *size = nullptr, bool *underline = nullptr, float *diaphaneity = nullptr);

// 设置字体。目前有些参数尚不支持。
//param font 字体类型, 目前不支持
//param size 字体大小/空白比例/间隔比例/旋转角度
//param underline 下画线
//param diaphaneity 透明度
void setFont(int *type, CvScalar *size = nullptr, bool *underline = nullptr, float *diaphaneity = nullptr);

// 恢复原始的字体设置。
void restoreFont();

// 输出汉字(颜色默认为黑色)。遇到不能输出的字符将停止。
//param img 输出的影象
//param text 文本内容
//param pos 文本位置
//return 返回成功输出的字符长度,失败返回-1。
int putText(cv::Mat &frame, const char *text, CvPoint pos);

// 输出汉字(颜色默认为黑色)。遇到不能输出的字符将停止。
//param img 输出的影象
//param text 文本内容
//param pos 文本位置
//return 返回成功输出的字符长度,失败返回-1。
int putText(cv::Mat &frame, const wchar_t *text, CvPoint pos);

// 输出汉字。遇到不能输出的字符将停止。
//param img 输出的影象
//param text 文本内容
//param pos 文本位置
//param color 文本颜色
//return 返回成功输出的字符长度,失败返回-1。
int putText(cv::Mat &frame, const char *text, CvPoint pos, CvScalar color);

// 输出汉字。遇到不能输出的字符将停止。
//param img 输出的影象
//param text 文本内容
//param pos 文本位置
//param color 文本颜色
//return 返回成功输出的字符长度,失败返回-1。
int putText(cv::Mat &frame, const wchar_t *text, CvPoint pos, CvScalar color);

private:
// 输出当前字符, 更新m_pos位置
void putWChar(cv::Mat &frame, wchar_t wc, CvPoint &pos, CvScalar color);

private:
FT_Library m_library; // 字库
FT_Face m_face; // 字体
// 默认的字体输出参数
int m_fontType;
CvScalar m_fontSize;
bool m_fontUnderline;
float m_fontDiaphaneity;
};
Binary file added plugins/face-detect/data/font/simsun.ttc
Binary file not shown.
29 changes: 27 additions & 2 deletions plugins/face-detect/face-detect.cpp
Expand Up @@ -2,13 +2,14 @@
#include <opencv2/opencv.hpp>
#include <string>
#include "windows.h"
#include "CvTextFreeType.h"

using namespace cv;
using namespace std;

bool g_bStop = false;
HWND g_hParentHwnd = nullptr;
char g_window_title[256] = {0};
char g_window_title[256] = { 0 };
int PreWndProcCallback(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, int* was_processed);

int __stdcall FaceDetect(const char* filename, const char* window_title, void* parent_hwnd)
Expand Down Expand Up @@ -119,6 +120,18 @@ int __stdcall FaceDetect(const char* filename, const char* window_title, void* p
}
}
}

CvTextFreeType text("../../data/obs-plugins/face-detect/font/simsun.ttc");
float p = 1.0f; // 透明度
CvScalar size(48, 0.5, 0.1, 0); // 字体大小/空白比例/间隔比例/旋转角度
text.setFont(NULL, &size, NULL, &p);
if (nFaces * nEyes <= 1) {
text.putText(image, "未检测到完整脸部,请将脸部正对摄像头", cvPoint(20, 30), CV_RGB(255, 0, 0));
}
else {
text.putText(image, "检测到完整脸部,正在向服务端认证,请稍等...", cvPoint(20, 30), CV_RGB(0, 255, 0));
}

Size image_size = image.size();
int cx = GetSystemMetrics(SM_CXFULLSCREEN);
int cy = GetSystemMetrics(SM_CYFULLSCREEN);
Expand All @@ -132,6 +145,19 @@ int __stdcall FaceDetect(const char* filename, const char* window_title, void* p
::SetWindowLongA(hParent, GWL_STYLE, ::GetWindowLongA(hParent, GWL_STYLE) & ~WS_MAXIMIZEBOX & ~WS_MINIMIZEBOX);
// 设置刷脸窗口置顶 HWND_TOP HWND_TOPMOST SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE
::SetWindowPos(hParent, HWND_TOP, x, y, image_size.width, image_size.height, SWP_SHOWWINDOW | SWP_NOSIZE);

// 设置窗口的大小图标
char szFileName[MAX_PATH];
memset(szFileName, 0, sizeof(szFileName));
HINSTANCE hInstance = ::GetModuleHandle(NULL);
::GetModuleFileNameA(hInstance, szFileName, MAX_PATH);
HICON hIcon = ExtractIconA(hInstance, szFileName, 0);
if (hIcon) {
// 大图标:按下alt+tab键切换窗口时对应的图标
// 小图标:就是窗口左上角对应的那个图标
::SendMessage(hParent, WM_SETICON, ICON_BIG, (LPARAM)hIcon);
::SendMessage(hParent, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
}
}
waitKey(1);
}
Expand Down Expand Up @@ -210,7 +236,6 @@ int __stdcall ImageFaceDetect(const char* filename)
mouth_cascade.detectMultiScale(ROI, mouth, 1.10, 3, 0 | CASCADE_SCALE_IMAGE, Size(20, 20));
nMouths = (nMouths >= (int)mouth.size()) ? nMouths : mouth.size();
}

waitKey(1);
}
//nRet = nFaces * nEyes * nNoses * nMouths;
Expand Down

0 comments on commit 829f21d

Please sign in to comment.