人脸修复祛马赛克算法CodeFormer——C++与Python模型部署

2023-10-29

一、人脸修复算法

1.算法简介

CodeFormer是一种基于AI技术深度学习的人脸复原模型,由南洋理工大学和商汤科技联合研究中心联合开发,它能够接收模糊或马赛克图像作为输入,并生成更清晰的原始图像。算法源码地址:https://github.com/sczhou/CodeFormer
这种技术在图像修复、图像增强和隐私保护等领域可能会有广泛的应用。算法是由南洋理工大学和商汤科技联合研究中心联合开发的,结合了VQGAN和Transformer。
VQGAN是一个生成模型,通常用于图像生成任务。它使用了向量量化技术,将图像编码成一系列离散的向量,然后通过解码器将这些向量转化为图像。这种方法通常能够生成高质量的图像,尤其在与Transformer等神经网络结合使用时。
Transformer是一种广泛用于自然语言处理和计算机视觉等领域的神经网络架构。它在序列数据处理中表现出色,也可以用于图像生成和处理任务。
在这里插入图片描述

在监控、安全和隐私保护领域,人脸图像通常会受到多种因素的影响,其中包括光照、像素限制、聚焦问题和人体运动等。这些因素可能导致图像模糊、变形或者包含大量的噪声。在这种情况下,尝试恢复清晰的原始人脸图像是一个极具挑战性的任务。
盲人脸复原是一个不适定问题(ill-posed problem),这意味着存在多个可能的解决方案,而且从有限的观察数据中无法唯一确定真实的原始图像。因此,在这个领域中,通常需要依赖先进的计算机视觉和图像处理技术,以及深度学习模型,来尝试改善模糊或受损图像的质量。
一些方法和技术可以用于处理盲人脸复原问题,包括但不限于:
深度学习模型: 使用卷积神经网络(CNN)和生成对抗网络(GAN)等深度学习模型,可以尝试从模糊或变形的人脸图像中恢复原始细节。
超分辨率技术: 超分辨率方法旨在从低分辨率图像中重建高分辨率图像,这也可以用于人脸图像复原。
先验知识: 利用先验知识,如人脸结构、光照模型等,可以帮助提高复原的准确性。
多模态融合: 结合不同传感器和信息源的数据,可以提高复原的鲁棒性。
然而,即使使用这些技术,由于问题的不适定性,完全恢复清晰的原始人脸图像仍然可能是一项极具挑战性的任务,特别是在极端条件下。在实际应用中,可能需要权衡图像质量和可用的信息,以达到最佳的结果。

2.算法效果

在官方公布修复的人脸效果里面,可以看到算法在各种输入的修复效果:
老照片修复
在这里插入图片描述
人脸修复
在这里插入图片描述
黑白人脸图像增强修复
在这里插入图片描述
人脸恢复
在这里插入图片描述

二、模型部署

如果想用C++进行模型推理部署,首先要把模型转换成onnx,转成onnx就可以使用onnxruntime c++库进行部署,或者使用OpenCV的DNN也可以,转成onnx后,还可以再转成ncnn模型使用ncnn进行模型部署。原模型可以从官方开源界面可以下载
模型推理这块有两种做法,一是不用判断有没有人脸,直接对全图进行超分,但这种方法好像对本来是清晰的图像会出现bug,就是生成一些无法理解的处理。

1. C++使用onnxruntime部署模型

#include "CodeFormer.h"

CodeFormer::CodeFormer(std::string model_path)
{
	//OrtStatus* status = OrtSessionOptionsAppendExecutionProvider_CUDA(sessionOptions, 0);  ///nvidia-cuda加速
	sessionOptions.SetGraphOptimizationLevel(ORT_ENABLE_BASIC);
	std::wstring widestr = std::wstring(model_path.begin(), model_path.end());   ///如果在windows系统就这么写
	ort_session = new Ort::Session(env, widestr.c_str(), sessionOptions);   ///如果在windows系统就这么写
	///ort_session = new Session(env, model_path.c_str(), sessionOptions);  ///如果在linux系统,就这么写

	size_t numInputNodes = ort_session->GetInputCount();
	size_t numOutputNodes = ort_session->GetOutputCount();
	Ort::AllocatorWithDefaultOptions allocator;
	for (int i = 0; i < numInputNodes; i++)
	{
		input_names.push_back(ort_session->GetInputName(i, allocator));
		Ort::TypeInfo input_type_info = ort_session->GetInputTypeInfo(i);
		auto input_tensor_info = input_type_info.GetTensorTypeAndShapeInfo();
		auto input_dims = input_tensor_info.GetShape();
		input_node_dims.push_back(input_dims);
	}
	for (int i = 0; i < numOutputNodes; i++)
	{
		output_names.push_back(ort_session->GetOutputName(i, allocator));
		Ort::TypeInfo output_type_info = ort_session->GetOutputTypeInfo(i);
		auto output_tensor_info = output_type_info.GetTensorTypeAndShapeInfo();
		auto output_dims = output_tensor_info.GetShape();
		output_node_dims.push_back(output_dims);
	}

	this->inpHeight = input_node_dims[0][2];
	this->inpWidth = input_node_dims[0][3];
	this->outHeight = output_node_dims[0][2];
	this->outWidth = output_node_dims[0][3];
	input2_tensor.push_back(0.5);
}

void CodeFormer::preprocess(cv::Mat &srcimg)
{
	cv::Mat dstimg;
	cv::cvtColor(srcimg, dstimg, cv::COLOR_BGR2RGB);
	resize(dstimg, dstimg, cv::Size(this->inpWidth, this->inpHeight), cv::INTER_LINEAR);
	this->input_image_.resize(this->inpWidth * this->inpHeight * dstimg.channels());
	int k = 0;
	for (int c = 0; c < 3; c++)
	{
		for (int i = 0; i < this->inpHeight; i++)
		{
			for (int j = 0; j < this->inpWidth; j++)
			{
				float pix = dstimg.ptr<uchar>(i)[j * 3 + c];
				this->input_image_[k] = (pix / 255.0 - 0.5) / 0.5;
				k++;
			}
		}
	}
}

cv::Mat CodeFormer::detect(cv::Mat &srcimg)
{
	int im_h = srcimg.rows;
	int im_w = srcimg.cols;
	this->preprocess(srcimg);
	std::array<int64_t, 4> input_shape_{ 1, 3, this->inpHeight, this->inpWidth };
	std::vector<int64_t> input2_shape_ = { 1 };

	auto allocator_info = Ort::MemoryInfo::CreateCpu(OrtDeviceAllocator, OrtMemTypeCPU);
	std::vector<Ort::Value> ort_inputs;
	ort_inputs.push_back(Ort::Value::CreateTensor<float>(allocator_info, input_image_.data(), input_image_.size(), input_shape_.data(), input_shape_.size()));
	ort_inputs.push_back(Ort::Value::CreateTensor<double>(allocator_info, input2_tensor.data(), input2_tensor.size(), input2_shape_.data(), input2_shape_.size()));
	std::vector<Ort::Value> ort_outputs = ort_session->Run(Ort::RunOptions{ nullptr }, input_names.data(), ort_inputs.data(), ort_inputs.size(), output_names.data(), output_names.size());

	post_process
	float* pred = ort_outputs[0].GetTensorMutableData<float>();
	//cv::Mat mask(outHeight, outWidth, CV_32FC3, pred); /经过试验,直接这样赋值,是不行的
	const unsigned int channel_step = outHeight * outWidth;
	std::vector<cv::Mat> channel_mats;
	cv::Mat rmat(outHeight, outWidth, CV_32FC1, pred); // R
	cv::Mat gmat(outHeight, outWidth, CV_32FC1, pred + channel_step); // G
	cv::Mat bmat(outHeight, outWidth, CV_32FC1, pred + 2 * channel_step); // B
	channel_mats.push_back(rmat);
	channel_mats.push_back(gmat);
	channel_mats.push_back(bmat);
	cv::Mat mask;
	merge(channel_mats, mask); // CV_32FC3 allocated

	///不用for循环遍历cv::Mat里的每个像素值,实现numpy.clip函数
	mask.setTo(this->min_max[0], mask < this->min_max[0]);
	mask.setTo(this->min_max[1], mask > this->min_max[1]);   也可以用threshold函数,阈值类型THRESH_TOZERO_INV

	mask = (mask - this->min_max[0]) / (this->min_max[1] - this->min_max[0]);
	mask *= 255.0;
	mask.convertTo(mask, CV_8UC3);
	//cvtColor(mask, mask, cv::COLOR_BGR2RGB);
	return mask;
}


void CodeFormer::detect_video(const std::string& video_path,const std::string& output_path, unsigned int writer_fps)
{
	cv::VideoCapture video_capture(video_path);

	if (!video_capture.isOpened())
	{
		std::cout << "Can not open video: " << video_path << "\n";
		return;
	}

	cv::Size S = cv::Size((int)video_capture.get(cv::CAP_PROP_FRAME_WIDTH),
		(int)video_capture.get(cv::CAP_PROP_FRAME_HEIGHT));

	cv::VideoWriter output_video(output_path, cv::VideoWriter::fourcc('m', 'p', '4', 'v'),
		30.0, S);
	
	if (!output_video.isOpened())
	{
		std::cout << "Can not open writer: " << output_path << "\n";
		return;
	}

	cv::Mat cv_mat;
	while (video_capture.read(cv_mat))
	{
		cv::Mat cv_dst = detect(cv_mat);

		output_video << cv_dst;
	}
	video_capture.release();
	output_video.release();
}

先试试官方给的样本的效果:
在这里插入图片描述
薄马赛克的超分效果:
在这里插入图片描述
在这里插入图片描述
厚马赛克的超分效果不是很好,就是有点贴脸的感觉:
在这里插入图片描述
如果是已经是清晰的图像,超分之后不是很理想,基本上是不能用的,onnx这个效果只能优化人脸:

在这里插入图片描述
在这里插入图片描述

2.onnx模型python推理

import os
import cv2
import argparse
import glob
import torch
import torch.onnx
from torchvision.transforms.functional import normalize
from basicsr.utils import imwrite, img2tensor, tensor2img
from basicsr.utils.download_util import load_file_from_url
from basicsr.utils.misc import gpu_is_available, get_device
from facelib.utils.face_restoration_helper import FaceRestoreHelper
from facelib.utils.misc import is_gray
import onnxruntime as ort

from basicsr.utils.registry import ARCH_REGISTRY

pretrain_model_url = {
    'restoration': 'https://github.com/sczhou/CodeFormer/releases/download/v0.1.0/codeformer.pth',
}

if __name__ == '__main__':
    # device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    device = get_device()
    parser = argparse.ArgumentParser()

    parser.add_argument('-i', '--input_path', type=str, default='./inputs/whole_imgs', 
            help='Input image, video or folder. Default: inputs/whole_imgs')
    parser.add_argument('-o', '--output_path', type=str, default=None, 
            help='Output folder. Default: results/<input_name>_<w>')
    parser.add_argument('-w', '--fidelity_weight', type=float, default=0.5, 
            help='Balance the quality and fidelity. Default: 0.5')
    parser.add_argument('-s', '--upscale', type=int, default=2, 
            help='The final upsampling scale of the image. Default: 2')
    parser.add_argument('--has_aligned', action='store_true', help='Input are cropped and aligned faces. Default: False')
    parser.add_argument('--only_center_face', action='store_true', help='Only restore the center face. Default: False')
    parser.add_argument('--draw_box', action='store_true', help='Draw the bounding box for the detected faces. Default: False')
    # large det_model: 'YOLOv5l', 'retinaface_resnet50'
    # small det_model: 'YOLOv5n', 'retinaface_mobile0.25'
    parser.add_argument('--detection_model', type=str, default='retinaface_resnet50', 
            help='Face detector. Optional: retinaface_resnet50, retinaface_mobile0.25, YOLOv5l, YOLOv5n, dlib. \
                Default: retinaface_resnet50')
    parser.add_argument('--bg_upsampler', type=str, default='None', help='Background upsampler. Optional: realesrgan')
    parser.add_argument('--face_upsample', action='store_true', help='Face upsampler after enhancement. Default: False')
    parser.add_argument('--bg_tile', type=int, default=400, help='Tile size for background sampler. Default: 400')
    parser.add_argument('--suffix', type=str, default=None, help='Suffix of the restored faces. Default: None')
    parser.add_argument('--save_video_fps', type=float, default=None, help='Frame rate for saving video. Default: None')

    args = parser.parse_args()

    # ------------------------ input & output ------------------------
    w = args.fidelity_weight
    input_video = False
    if args.input_path.endswith(('jpg', 'jpeg', 'png', 'JPG', 'JPEG', 'PNG')): # input single img path
        input_img_list = [args.input_path]
        result_root = f'results/test_img_{w}'
    # elif args.input_path.endswith(('mp4', 'mov', 'avi', 'MP4', 'MOV', 'AVI')): # input video path
    #     from basicsr.utils.video_util import VideoReader, VideoWriter
    #     input_img_list = []
    #     vidreader = VideoReader(args.input_path)
    #     image = vidreader.get_frame()
    #     while image is not None:
    #         input_img_list.append(image)
    #         image = vidreader.get_frame()
    #     audio = vidreader.get_audio()
    #     fps = vidreader.get_fps() if args.save_video_fps is None else args.save_video_fps   
    #     video_name = os.path.basename(args.input_path)[:-4]
    #     result_root = f'results/{video_name}_{w}'
    #     input_video = True
    #     vidreader.close()
    # else: # input img folder
    #     if args.input_path.endswith('/'):  # solve when path ends with /
    #         args.input_path = args.input_path[:-1]
    #     # scan all the jpg and png images
    #     input_img_list = sorted(glob.glob(os.path.join(args.input_path, '*.[jpJP][pnPN]*[gG]')))
    #     result_root = f'results/{os.path.basename(args.input_path)}_{w}'
    else:
        raise ValueError("wtf???")

    if not args.output_path is None: # set output path
        result_root = args.output_path

    test_img_num = len(input_img_list)
    if test_img_num == 0:
        raise FileNotFoundError('No input image/video is found...\n' 
            '\tNote that --input_path for video should end with .mp4|.mov|.avi')

    # # ------------------ set up background upsampler ------------------
    # if args.bg_upsampler == 'realesrgan':
    #     bg_upsampler = set_realesrgan()
    # else:
    #     bg_upsampler = None

    # # ------------------ set up face upsampler ------------------
    # if args.face_upsample:
    #     if bg_upsampler is not None:
    #         face_upsampler = bg_upsampler
    #     else:
    #         face_upsampler = set_realesrgan()
    # else:
    #     face_upsampler = None

    # ------------------ set up CodeFormer restorer -------------------
    net = ARCH_REGISTRY.get('CodeFormer')(dim_embd=512, codebook_size=1024, n_head=8, n_layers=9, 
                                            connect_list=['32', '64', '128', '256']).to(device)
    
    # ckpt_path = 'weights/CodeFormer/codeformer.pth'
    ckpt_path = load_file_from_url(url=pretrain_model_url['restoration'], 
                                    model_dir='weights/CodeFormer', progress=True, file_name=None)
    checkpoint = torch.load(ckpt_path)['params_ema']
    net.load_state_dict(checkpoint)
    net.eval()

    # # ------------------ set up FaceRestoreHelper -------------------
    # # large det_model: 'YOLOv5l', 'retinaface_resnet50'
    # # small det_model: 'YOLOv5n', 'retinaface_mobile0.25'
    # if not args.has_aligned: 
    #     print(f'Face detection model: {args.detection_model}')
    # # if bg_upsampler is not None: 
    # #     print(f'Background upsampling: True, Face upsampling: {args.face_upsample}')
    # # else:
    # #     print(f'Background upsampling: False, Face upsampling: {args.face_upsample}')
    # else:
    #     raise ValueError("wtf???")

    face_helper = FaceRestoreHelper(
        args.upscale,
        face_size=512,
        crop_ratio=(1, 1),
        # det_model = args.detection_model,
        # save_ext='png',
        # use_parse=True,
        # device=device
    )

    # -------------------- start to processing ---------------------
    for i, img_path in enumerate(input_img_list):
        # # clean all the intermediate results to process the next image
        # face_helper.clean_all()
        
        if isinstance(img_path, str):
            img_name = os.path.basename(img_path)
            basename, ext = os.path.splitext(img_name)
            print(f'[{i+1}/{test_img_num}] Processing: {img_name}')
            img = cv2.imread(img_path, cv2.IMREAD_COLOR)
        # else: # for video processing
        #     basename = str(i).zfill(6)
        #     img_name = f'{video_name}_{basename}' if input_video else basename
        #     print(f'[{i+1}/{test_img_num}] Processing: {img_name}')
        #     img = img_path

        if args.has_aligned: 
            # the input faces are already cropped and aligned
            img = cv2.resize(img, (512, 512), interpolation=cv2.INTER_LINEAR)
            # face_helper.is_gray = is_gray(img, threshold=10)
            # if face_helper.is_gray:
            #     print('Grayscale input: True')
            face_helper.cropped_faces = [img]
        # else:
        #     face_helper.read_image(img)
        #     # get face landmarks for each face
        #     num_det_faces = face_helper.get_face_landmarks_5(
        #         only_center_face=args.only_center_face, resize=640, eye_dist_threshold=5)
        #     print(f'\tdetect {num_det_faces} faces')
        #     # align and warp each face
        #     face_helper.align_warp_face()
        else:
            raise ValueError("wtf???")

        # face restoration for each cropped face
        for idx, cropped_face in enumerate(face_helper.cropped_faces):
            # prepare data
            cropped_face_t = img2tensor(cropped_face / 255., bgr2rgb=True, float32=True)
            normalize(cropped_face_t, (0.5, 0.5, 0.5), (0.5, 0.5, 0.5), inplace=True)
            cropped_face_t = cropped_face_t.unsqueeze(0).to(device)

            try:
                with torch.no_grad():
                    # output = net(cropped_face_t, w=w, adain=True)[0]
                    # output = net(cropped_face_t)[0]
                    output = net(cropped_face_t, w)[0]
                    restored_face = tensor2img(output, rgb2bgr=True, min_max=(-1, 1))
                del output
                # torch.cuda.empty_cache()
            except Exception as error:
                print(f'\tFailed inference for CodeFormer: {error}')
                restored_face = tensor2img(cropped_face_t, rgb2bgr=True, min_max=(-1, 1))
            
            # now, export the "net" codeformer to onnx
            print("Exporting CodeFormer to ONNX...")
            torch.onnx.export(net,
                # (cropped_face_t,),
                (cropped_face_t,w),
                "codeformer.onnx", 
                # verbose=True,
                export_params=True,
                opset_version=11,
                do_constant_folding=True,
                input_names = ['x','w'],
                output_names = ['y'],
            )

            # now, try to load the onnx model and run it
            print("Loading CodeFormer ONNX...")
            ort_session = ort.InferenceSession("codeformer.onnx", providers=['CPUExecutionProvider'])
            print("Running CodeFormer ONNX...")
            ort_inputs = {
                ort_session.get_inputs()[0].name: cropped_face_t.cpu().numpy(),
                ort_session.get_inputs()[1].name: torch.tensor(w).double().cpu().numpy(),
            }
            ort_outs = ort_session.run(None, ort_inputs)
            restored_face_onnx = tensor2img(torch.from_numpy(ort_outs[0]), rgb2bgr=True, min_max=(-1, 1))
            restored_face_onnx = restored_face_onnx.astype('uint8')

            restored_face = restored_face.astype('uint8')

            print("Comparing CodeFormer outputs...")
            # see how similar the outputs are: flatten and then compute all the differences
            diff = (restored_face_onnx.astype('float32') - restored_face.astype('float32')).flatten()
            # calculate min, max, mean, and std
            min_diff = diff.min()
            max_diff = diff.max()
            mean_diff = diff.mean()
            std_diff = diff.std()
            print(f"Min diff: {min_diff}, Max diff: {max_diff}, Mean diff: {mean_diff}, Std diff: {std_diff}")

            # face_helper.add_restored_face(restored_face, cropped_face)
            face_helper.add_restored_face(restored_face_onnx, cropped_face)

        # # paste_back
        # if not args.has_aligned:
        #     # upsample the background
        #     if bg_upsampler is not None:
        #         # Now only support RealESRGAN for upsampling background
        #         bg_img = bg_upsampler.enhance(img, outscale=args.upscale)[0]
        #     else:
        #         bg_img = None
        #     face_helper.get_inverse_affine(None)
        #     # paste each restored face to the input image
        #     if args.face_upsample and face_upsampler is not None: 
        #         restored_img = face_helper.paste_faces_to_input_image(upsample_img=bg_img, draw_box=args.draw_box, face_upsampler=face_upsampler)
        #     else:
        #         restored_img = face_helper.paste_faces_to_input_image(upsample_img=bg_img, draw_box=args.draw_box)

        # save faces
        for idx, (cropped_face, restored_face) in enumerate(zip(face_helper.cropped_faces, face_helper.restored_faces)):
            # save cropped face
            if not args.has_aligned: 
                save_crop_path = os.path.join(result_root, 'cropped_faces', f'{basename}_{idx:02d}.png')
                imwrite(cropped_face, save_crop_path)
            # save restored face
            if args.has_aligned:
                save_face_name = f'{basename}.png'
            else:
                save_face_name = f'{basename}_{idx:02d}.png'
            if args.suffix is not None:
                save_face_name = f'{save_face_name[:-4]}_{args.suffix}.png'
            save_restore_path = os.path.join(result_root, 'restored_faces', save_face_name)
            imwrite(restored_face, save_restore_path)

        # # save restored img
        # if not args.has_aligned and restored_img is not None:
        #     if args.suffix is not None:
        #         basename = f'{basename}_{args.suffix}'
        #     save_restore_path = os.path.join(result_root, 'final_results', f'{basename}.png')
        #     imwrite(restored_img, save_restore_path)

    # # save enhanced video
    # if input_video:
    #     print('Video Saving...')
    #     # load images
    #     video_frames = []
    #     img_list = sorted(glob.glob(os.path.join(result_root, 'final_results', '*.[jp][pn]g')))
    #     for img_path in img_list:
    #         img = cv2.imread(img_path)
    #         video_frames.append(img)
    #     # write images to video
    #     height, width = video_frames[0].shape[:2]
    #     if args.suffix is not None:
    #         video_name = f'{video_name}_{args.suffix}.png'
    #     save_restore_path = os.path.join(result_root, f'{video_name}.mp4')
    #     vidwriter = VideoWriter(save_restore_path, height, width, fps, audio)
         
    #     for f in video_frames:
    #         vidwriter.write_frame(f)
    #     vidwriter.close()

    print(f'\nAll results are saved in {result_root}')
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

人脸修复祛马赛克算法CodeFormer——C++与Python模型部署 的相关文章

  • Python 多处理:全局对象未正确复制到子级

    前几天我回答了一个关于SO的问题 https stackoverflow com q 67047533 1925388关于并行读取 tar 文件 这是问题的要点 import bz2 import tarfile from multipro
  • 如何使用 C# 调用 REST API?

    这是我到目前为止的代码 public class Class1 private const string URL https sub domain com objects json api key 123 private const str
  • WCF 客户端返回空数组 - XML 响应似乎正常

    我正在尝试为我们的 Intranet 上托管的 Web 服务创建一个简单的 WCF 客户端 C 使用 Fiddler 和 SoapUI 我可以看到请求和响应似乎正常 但是当我运行代码时返回一个空数组 我会尝试只粘贴相关的行 但会是很多东西
  • “gi.repository.Gtk”对象没有属性“gdk”

    我正在尝试使用 GTK 创建多线程 需要 Gtk gdk 但我收到有关没有 gdk 属性的错误 我正在使用带有 Raspbian 的 Raspberry Pi 这就是我导入 GTK 库的方式 try import pygtk pygtk r
  • C++ 为非虚方法指定初始化

    我有 a h 如下所示 class A public void doSomething 0 然后我有 b h 如下所示 include a h class b public A public void doSomething 我只是想通过尝
  • 模板“内联”函数的静态局部变量[重复]

    这个问题在这里已经有答案了 static的局部变量inline如果我的理解是正确的 C 中的函数保证像单个全局变量一样存在 如果inline函数是一个模板 编译器可以在哪里生成该函数的多个版本 下面这篇文章应该很好地回答你的问题 http
  • C++ fill() 与 uninitialized_fill()

    您好 我是初学者 我想知道容器的 fill 和 uninitialized fill 之间的区别 我在谷歌上进行了快速搜索 但没有得到很好的答案 有人可以帮助我吗 fill 将值 使用赋值运算符 分配给已构造的对象 uninitialize
  • Python,质数检查器[重复]

    这个问题在这里已经有答案了 你好 我正在创建一个函数来检查一个数字是否是素数 但它告诉我 9 是一个素数 def eprimo num if num lt 2 return False if num 2 return True else f
  • 为什么 C++ 元组如此奇怪?

    我通常创建自定义structs将不同类型的值分组在一起时 这通常很好 而且我个人发现命名成员访问更容易阅读 但我想创建一个更通用的 API 在其他语言中广泛使用元组后 我想返回类型的值std tuple但发现它们在 C 中使用比在其他语言中
  • 添加条件计数器:基于其他列的值的计数器列

    我有一张这样的桌子 id id2 val a red apple a red orange b blue fish c violet beef a yellow banana a black pork 我想根据 id 和 id2 的值创建一
  • 将纬度/经度转换为 X/Y,以便在美国地图图像上进行阿尔伯斯投影

    我正在尝试使用 C 或 Javascript 将纬度 经度转换为 X Y 坐标 以将带有 CSS 的 div 左 上 定位到美国地图的背景图像上 美国的标准地图投影是阿尔伯斯投影 如下所示 但 StackOverflow 仅提供参考基本墨卡
  • Roslyn,通过 hostObject 传递值

    我正在尝试通过 hostObject 发送一个类 但显然它不想工作 using Roslyn Compilers using Roslyn Compilers CSharp using Roslyn Scripting using Rosl
  • 如何将此 Boost ASIO 示例应用到我的应用程序中

    我已经阅读了很多 ASIO 示例 但我仍然对如何在我的应用程序中使用它们感到困惑 基本上 我的服务器端需要接受超过100个连接 客户端 这部分是通过使用线程池 通常每个CPU核心2 4个线程 来完成的 为简单起见 我们假设只有一个连接 为了
  • 我们可以使用 C# 录制发送到扬声器的声音吗

    我有一个软件 SoundTap Streaming Audio Recorder 它记录发送到扬声器的任何音频 无论流是来自网络还是来自某些文件或麦克风 我可以在桌面应用程序中制作这样的应用程序 以便我可以录制发送到扬声器的流 无论来源如何
  • 具有两个表的谓词构建器

    A Party可以有一个或多个Contact对象 我想选择全部Parties谁的街道名称包含特定关键字 如果我只想搜索Party我可以使用下面的代码 但我如何扩展它来搜索Contact public IQueryable
  • 恐怖分子已弃用

    正在接听另一个问题 https stackoverflow com q 11830514 1468366 我偶然发现了man page http linux die net man 3 herror一个名为的函数herror 看起来很像pe
  • Django 表单中的只读字段

    如何在 Django 表单中将字段设置为只读 我知道如何禁用某个字段 但这不是我想要的 任何帮助 将不胜感激 您可以使用可选的attrs定义时的参数Field 以机智 somefield forms CharField widget for
  • 在 Visual Studio C++ 资源编辑器中导入 png 文件

    我希望能够在 Visual Studio 资源编辑器中导入 png 文件 以便能够在不同的其他项目中使用嵌入的资源 有解决办法吗 我知道它适用于位图 但我对 png 感兴趣 因为即使在较低格式 16x16 或 32x32 上也可以使用 透明
  • Django Python - LDAP 身份验证

    我目前正在研究 Django Python 我的目标是从 Ldap 目录对用户进行身份验证 我确实有 python 代码来访问 ldap 目录并检索信息 Code import ldap try l ldap open ldap forum
  • 我应该为每个 Web 请求使用静态缓存的 ResourceManager 还是一个新实例?有关系吗?

    创建新的 NET 对性能 或其他 有何影响 如果有 ResourceManager根据每个请求new ResourceManger myResourceType FullName myResourceType Assembly 与在 Des

随机推荐

  • 华为OD机试 - 计算礼品发放的最小分组数目(Java)

    题目描述 又到了一年的末尾 项目组让小明负责新年晚会的小礼品发放工作 为使得参加晚会的同事所获得的小礼品价值相对平衡 需要把小礼品根据价格进行分组 但每组最多只能包括两件小礼品 并且每个分组的价格总和不能超过一个价格上限 为了保证发放小礼品
  • 小试牛刀1:使用Python-OpenCV 实现一副图片的高斯模糊处理

    实验步骤 1 环境 安装好python3 x opencv3 3 window下 2 高斯模糊 概念 高斯模糊本质上是低通滤波器 输出图像的每个像素点是原图像上对应像素点与周围像素点的加权和 使用cv2做高斯模糊 只要一行代码调用Gauss
  • 微信小程序页面栈_小程序页面栈详解

    在做小程序项目的时候不难发现 使用navigateTo进行页面跳转后 点击左上角或使用navigateBack返回 总是会按照之前的页面进入倒序来展示页面 那么问题来了 它们的跳转规则是什么样的呢 结合到实际业务中如何灵活运用呢 什么是页面
  • No SOURCES given to target: xxx

    文章目录 一 问题描述 二 解决办法 一 问题描述 CMake Error at CMakeLists txt add executable Cannot find source file src rs capture cpp Tried
  • 关于搭建简易广域网私人通信程序(python)一步到位!

    原料 python3 腾讯云服务器 用到的库 socket sys threading time pyinstaller 除pyinstaller外均不需单独安装 首先 默认已经买好云服务器 且安装好了python3 此处使用腾讯云服务器
  • csv文件的读与写

    csv文件的读取与写入 1 csv文件的读取 import pandas as pd csv path dataA1 csv df pd read csv csv path 可以通过csv文件的表头读取出某一列 表头见下图 print it
  • 理解inode

    inode是一个重要概念 是理解Unix Linux文件系统和硬盘储存的基础 我觉得 理解inode 不仅有助于提高系统操作水平 还有助于体会Unix设计哲学 即如何把底层的复杂性抽象成一个简单概念 从而大大简化用户接口 下面就是我的ino
  • 华为OD机试真题-基站维护最短距离 【2023.Q1】

    参考代码 小王是一名基站维护工程师 负责某区域的基站维护 某地方有n个基站 1
  • Android中的签名和签名文件的生成过程

    一 概述 二 还是从Key Store和keytool说起吧 三 数字签名和数字证书 四 apk的签名过程 五 签名中 MANIFEST MF CERT SF和 CERT RSA是怎么生成的 1 MANIFEST MF 2 CERT SF
  • QT日志系统创建,读取,修复

    一 内容简介 构建QT日志系统 遇到的问题以及结局办法 读取日志文件 用于检查程序运行情况 并且修复异常数据 二 软件环境 2 1 QT 5 14 1 新版QT6 4 6 5在线安装经常失败 而5 9版本又无法编译64位程序 所以就采用5
  • 巧用千寻位置GNSS软件

    千寻位置GNSS软件中的CAD功能 用于已有 CAD的图形的导入和编辑 并且可以对 CAD图形已有线条进行线放样 在日常测绘工作中十分常见 下面向各位介绍CAD功能的使用技巧 点击 测量 gt CAD 进入 CAD功能如图 5 3 1所示
  • 显卡无法为此计算机,如何解决win10系统电脑中无法安装intel显卡驱动的问题

    如何解决win10系统电脑中无法安装intel显卡驱动的问题 腾讯视频 爱奇艺 优酷 外卖 充值4折起 在使用win10系统电脑的时候 如果我们想要安装intel显卡驱动 却提示无法安装的话 应如何处理呢 接下来就由小编来告诉大家 一 手动
  • CentOS7(Linux)安装总结

    主要参考以下文章 CentOS7 Linux 详细安装教程 图文详解 缄默 給 傷的博客 CSDN博客 linuxcentos7安装步骤https blog csdn net weixin 43849575 article details
  • Mutex内核同步机制详解

    一 Mutex锁简介 在linux内核中 互斥量 mutex 即mutual exclusion 是一种保证串行化的睡眠锁机制 和spinlock的语义类似 都是允许一个执行线索进入临界区 不同的是当无法获得锁的时候 spinlock原地自
  • 使用VNC远程桌面Ubuntu【内网穿透实现公网远程】

    文章目录 前言 1 ubuntu安装VNC 2 设置vnc开机启动 3 windows 安装VNC viewer连接工具 4 内网穿透 4 1 安装cpolar 支持使用一键脚本命令安装 4 2 创建隧道映射 4 3 测试公网远程访问 5
  • 【深度学习】 Python 和 NumPy 系列教程(十五):Matplotlib详解:2、3d绘图类型(1):线框图(Wireframe Plot)

    目录 一 前言 二 实验环境 三 Matplotlib详解 1 2d绘图类型 2 3d绘图类型 0 设置中文字体 1 线框图 Wireframe Plot 一 前言 Python是一种高级编程语言 由Guido van Rossum于199
  • Qt事件过滤器

    有时候 对象需要查看 甚至要拦截发送到另外对象的事件 例如 对话框可能想要拦截按键事件 不让别的组件接收到 或者要修改回车键的默认处理 通过前面的章节 我们已经知道 Qt 创建了 QEvent 事件对象之后 会调用 QObject 的 ev
  • dll 导出函数名的那些事

    经常使用VC6的Dependency查看DLL导出函数的名字 会发现有DLL导出函数的名字有时大不相同 导致不同的原因大多是和编译DLL时候指定DLL导出函数的界定符有关系 VC 支持两种语言 即C C 这也是造成DLL导出函数差异的根源
  • 2019年,iOS开发的你准备何时跳槽?

    序言 我相信很多人都在说 iOS行业不好了 iOS现在行情越来越难了 失业的人比找工作的人还要多 失业即相当于转行 跳槽即相当于降低自己的身价 那么做iOS开发的你 你是否在时刻准备着跳槽或者转行了 我们先看一下现在iOS行业 iOS程序员
  • 人脸修复祛马赛克算法CodeFormer——C++与Python模型部署

    一 人脸修复算法 1 算法简介 CodeFormer是一种基于AI技术深度学习的人脸复原模型 由南洋理工大学和商汤科技联合研究中心联合开发 它能够接收模糊或马赛克图像作为输入 并生成更清晰的原始图像 算法源码地址 https github