Exemplo n.º 1
0
int
main(int argc, char **argv)
{
    char *dst_path;
    size_t path_len;
    struct W2XConv *c;
    int r;
    char *src_path;

    if (argc < 2) {
        puts("usage : w2xc <in.png>");
        return 1;
    }

    src_path = argv[1];

    path_len = strlen(src_path);
    dst_path = malloc(path_len + 5);
    dst_path[0] = 'm';
    dst_path[1] = 'a';
    dst_path[2] = 'i';
    dst_path[3] = '_';
    strcpy(dst_path+4, argv[1]);

    c = w2xconv_init(1, 0, 0);
    r = w2xconv_load_models(c, "models");
    if (r < 0) {
        goto error;
    }

    r = w2xconv_convert_file(c, dst_path, src_path, 1, 2.0, 512);
    if (r < 0) {
        goto error;
    }

    w2xconv_fini(c);

    return 0;

error:
    {
        char *err = w2xconv_strerror(&c->last_error);
        puts(err);
        w2xconv_free(err);
    }

    w2xconv_fini(c);

    return 1;
}
Exemplo n.º 2
0
int main(int argc, char** argv) {
	int ret = 1;

	// definition of command line arguments
	TCLAP::CmdLine cmd("waifu2x reimplementation using OpenCV", ' ', "1.0.0");

	TCLAP::ValueArg<std::string> cmdInputFile("i", "input_file",
			"path to input image file (you should input full path)", true, "",
			"string", cmd);

	TCLAP::ValueArg<std::string> cmdOutputFile("o", "output_file",
			"path to output image file (you should input full path)", false,
			"(auto)", "string", cmd);

	std::vector<std::string> cmdModeConstraintV;
	cmdModeConstraintV.push_back("noise");
	cmdModeConstraintV.push_back("scale");
	cmdModeConstraintV.push_back("noise_scale");
	TCLAP::ValuesConstraint<std::string> cmdModeConstraint(cmdModeConstraintV);
	TCLAP::ValueArg<std::string> cmdMode("m", "mode", "image processing mode",
			false, "noise_scale", &cmdModeConstraint, cmd);

	std::vector<int> cmdNRLConstraintV;
	cmdNRLConstraintV.push_back(1);
	cmdNRLConstraintV.push_back(2);
	TCLAP::ValuesConstraint<int> cmdNRLConstraint(cmdNRLConstraintV);
	TCLAP::ValueArg<int> cmdNRLevel("", "noise_level", "noise reduction level",
			false, 1, &cmdNRLConstraint, cmd);

	TCLAP::ValueArg<double> cmdScaleRatio("", "scale_ratio",
			"custom scale ratio", false, 2.0, "double", cmd);

	TCLAP::ValueArg<std::string> cmdModelPath("", "model_dir",
			"path to custom model directory (don't append last / )", false,
			"models_rgb", "string", cmd);

	TCLAP::ValueArg<int> cmdNumberOfJobs("j", "jobs",
			"number of threads launching at the same time", false, 0, "integer",
			cmd);

	TCLAP::SwitchArg cmdForceOpenCL("", "force-OpenCL",
					"force to use OpenCL on Intel Platform",
					cmd, false);

	TCLAP::SwitchArg cmdDisableGPU("", "disable-gpu", "disable GPU", cmd, false);

	TCLAP::ValueArg<int> cmdBlockSize("", "block_size", "block size",
					  false, 0, "integer", cmd);

	// definition of command line argument : end

	// parse command line arguments
	try {
		cmd.parse(argc, argv);
	} catch (std::exception &e) {
		std::cerr << e.what() << std::endl;
		std::cerr << "Error : cmd.parse() threw exception" << std::endl;
		std::exit(-1);
	}


	std::string outputFileName = cmdOutputFile.getValue();
	if (outputFileName == "(auto)") {
		outputFileName = cmdInputFile.getValue();
		int tailDot = outputFileName.find_last_of('.');
		outputFileName.erase(tailDot, outputFileName.length());
		outputFileName = outputFileName + "(" + cmdMode.getValue() + ")";
		std::string &mode = cmdMode.getValue();
		if(mode.find("noise") != mode.npos){
			outputFileName = outputFileName + "(Level" + std::to_string(cmdNRLevel.getValue())
			+ ")";
		}
		if(mode.find("scale") != mode.npos){
			outputFileName = outputFileName + "(x" + std::to_string(cmdScaleRatio.getValue())
			+ ")";
		}
		outputFileName += ".png";
	}

	enum W2XConvGPUMode gpu = W2XCONV_GPU_AUTO;

	if (cmdDisableGPU.getValue()) {
		gpu = W2XCONV_GPU_DISABLE;
	} else if (cmdForceOpenCL.getValue()) {
		gpu = W2XCONV_GPU_FORCE_OPENCL;
	}

	W2XConv *converter = w2xconv_init(gpu,
					  cmdNumberOfJobs.getValue(), 1);

	double time_start = getsec();

	switch (converter->target_processor.type) {
	case W2XCONV_PROC_HOST:
		printf("CPU: %s\n",
		       converter->target_processor.dev_name);
		break;

	case W2XCONV_PROC_CUDA:
		printf("CUDA: %s\n",
		       converter->target_processor.dev_name);
		break;

	case W2XCONV_PROC_OPENCL:
		printf("OpenCL: %s\n",
		       converter->target_processor.dev_name);
		break;
	}

	int bs = cmdBlockSize.getValue();

	int r = w2xconv_load_models(converter, cmdModelPath.getValue().c_str());
	if (r < 0) {
		goto error;
	}


	{
		int nrLevel = 0;
		if (cmdMode.getValue() == "noise" || cmdMode.getValue() == "noise_scale") {
			nrLevel = cmdNRLevel.getValue();
		}

		double scaleRatio = 1;
		if (cmdMode.getValue() == "scale" || cmdMode.getValue() == "noise_scale") {
			scaleRatio = cmdScaleRatio.getValue();
		}

		r = w2xconv_convert_file(converter,
					 outputFileName.c_str(),
					 cmdInputFile.getValue().c_str(),
					 nrLevel,
					 scaleRatio, bs);
	}

	if (r < 0) {
		goto error;
	}

	{
		double time_end = getsec();

		double gflops_proc = (converter->flops.flop/(1000.0*1000.0*1000.0)) / converter->flops.filter_sec;
		double gflops_all = (converter->flops.flop/(1000.0*1000.0*1000.0)) / (time_end-time_start);

		std::cout << "process successfully done! (all:"
			  << (time_end - time_start)
			  << "[sec], " << gflops_all << "[GFLOPS], filter:"
			  << converter->flops.filter_sec
			  << "[sec], " << gflops_proc << "[GFLOPS])" << std::endl;
	}

	ret = 0;

error:
	if (ret != 0) {
		char *err = w2xconv_strerror(&converter->last_error);
		puts(err);
		w2xconv_free(err);
	}

	w2xconv_fini(converter);

	return ret;
}
Exemplo n.º 3
0
int
w2xconv_test(struct W2XConv *conv, int block_size)
{
	int w = 200;
	int h = 100;
	int r;

	cv::Mat src_rgb = cv::Mat::zeros(h, w, CV_8UC3);
	cv::Mat dst = cv::Mat::zeros(h, w, CV_8UC3);

	cv::line(src_rgb,
		 cv::Point(10, 10),
		 cv::Point(20, 20),
		 cv::Scalar(255,0,0), 8);

	cv::line(src_rgb,
		 cv::Point(20, 10),
		 cv::Point(10, 20),
		 cv::Scalar(0,255,0), 8);

	cv::line(src_rgb,
		 cv::Point(50, 30),
		 cv::Point(10, 30),
		 cv::Scalar(0,0,255), 1);

	cv::line(src_rgb,
		 cv::Point(50, 80),
		 cv::Point(10, 80),
		 cv::Scalar(255,255,255), 3);

	cv::Mat src_32fc3;
	cv::Mat src_yuv;

	src_rgb.convertTo(src_32fc3, CV_32F, 1.0 / 255.0);
	cv::cvtColor(src_32fc3, src_yuv, cv::COLOR_RGB2YUV);

	cv::Mat dst_rgb_x2(h*2, w*2, CV_8UC3);
	cv::Mat dst_rgb_f32_x2(h*2, w*2, CV_32FC3);
	cv::Mat dst_yuv_x2(h*2, w*2, CV_32FC3);

	cv::imwrite("test_src.png", src_rgb);

	w2xconv_convert_rgb(conv,
			    dst_rgb_x2.data, dst_rgb_x2.step[0],
			    src_rgb.data, src_rgb.step[0],
			    w, h,
			    1,
			    2.0,
			    block_size);

	cv::imwrite("test_rgb.png", dst_rgb_x2);


	w2xconv_convert_rgb_f32(conv,
				dst_rgb_f32_x2.data, dst_rgb_f32_x2.step[0],
				src_32fc3.data, src_32fc3.step[0],
				w, h,
				1,
				2.0,
				block_size);

	dst_rgb_f32_x2.convertTo(dst_rgb_x2, CV_8U, 255.0);
	cv::imwrite("test_rgb_f32.png", dst_rgb_x2);

	r = w2xconv_convert_yuv(conv,
				dst_yuv_x2.data, dst_yuv_x2.step[0],
				src_yuv.data, src_yuv.step[0],
				w, h,
				1,
				2.0,
				block_size);

	if (r < 0) {
		char *e = w2xconv_strerror(&conv->last_error);
		puts(e);
		w2xconv_free(e);
	} else {
		cv::cvtColor(dst_yuv_x2, dst_yuv_x2, cv::COLOR_YUV2RGB);
		dst_yuv_x2.convertTo(dst_rgb_x2, CV_8U, 255.0);
		cv::imwrite("test_yuv.png", dst_rgb_x2);
	}

	std::vector<cv::Mat> imageSplit;
	cv::split(src_yuv, imageSplit);
	cv::Mat split_src = imageSplit[0].clone();
	cv::Mat split_dst, dst_rgb;
	cv::Mat split_dsty(h, w, CV_32F);

	r = w2xconv_apply_filter_y(conv,
				   W2XCONV_FILTER_DENOISE1,
				   split_dsty.data, split_dsty.step[0],
				   split_src.data, split_src.step[0],
				   w, h, block_size);
	if (r < 0) {
		char *e = w2xconv_strerror(&conv->last_error);
		puts(e);
		w2xconv_free(e);
	} else {
		imageSplit[0] = split_dsty.clone();

		cv::merge(imageSplit, split_dst);

		cv::cvtColor(split_dst, split_dst, cv::COLOR_YUV2RGB);
		split_dst.convertTo(dst_rgb, CV_8U, 255.0);

		cv::imwrite("test_apply.png", dst_rgb);
	}

	return 0;
}