个人面部数据集制作,opencv + python + C++

目录

C++

python


使用opencv,利用笔记本摄像头自作自己的面部数据集,目标为1S一张,代码如下:

C++

因为文件的标签是动态变动的,如1.png,2.png,所以利用如下核心代码,进行存储:

int intName[100];
	for (int i = 0; i < 100; i++)
	{
		intName[i] = i;
	}
for (int i = 0; i < 10; i++)
	{
		stringstream ss;
		string stringName;
		ss << intName[i];
		ss >> stringName;
		cout << stringName+".png" << endl;
		waitKey(2000);    延时
	}

完整代码:

#include <opencv2\opencv.hpp>
#include <iostream>

using namespace std;
using namespace cv;

int main(int argc, char** atgv) {
	
	cv::VideoCapture cap;
	cap.open(0);
	cv::Mat frame;

	int intName[10000];
	for (int i = 0; i < 10000; i++)
	{
		intName[i] = i;
	}
	
	int i = 0;
	while (true)
	{
		stringstream ss;
		string stringName;
		ss << intName[i];
		ss >> stringName;

		cap >> frame;
		resize(frame, frame, cv::Size(0, 0), 0.5, 0.5, 1);
		cv::imshow("sunshine", frame);
		imwrite("F:\\face\\"+ stringName + ".png",frame);
		waitKey(1000);
		i++;
		int c = cv::waitKey(5);
		if (c == 27 || c == 'q' || c == 'Q'	|| i == 10000)
			break;
	}
	destroyAllWindows();
	return 0;

}

这里运用了尺寸缩小,如果是原大小摄像头像素的话(640 X 480),大约一百张图,占内存40多M,如果将像素缩小一半的话,内存大小只有13M。

python

import cv2
import time

cap = cv2.VideoCapture()
cap.open(0)
i = 9060
while(i):
    ret, frame = cap.read()
    im_save = cv2.resize(frame,(320,240))
    # show a frame
    cv2.imshow("capture", frame)

    time2 = time.sleep(0.5)
    cv2.imwrite(f'F:\\face\\{i}.png',im_save)
    print(i)
    i += 1

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
    elif i == 10000:
        break

cap.release()
cv2.destroyAllWindows()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值