百度AI的图像处理SDK使用

news/2024/7/21 7:33:10 标签: 人工智能, 图像处理, 计算机视觉

百度智能云平台

1. 创建想要使用的应用

2. 可以选择在线调用或者离线装载SDK

3. 参考所选应用的使用手册调整代码

使用百度API进行人像分割

官方使用说明:人像分割

import argparse
import cv2
import base64
import numpy as np
import time
import os
from aip import AipBodyAnalysis
def parse_args():
    parser=argparse.ArgumentParser(description="convert a text colmap export to nerf format transforms.json; optionally convert video to images, and optionally run colmap in the first place")

    parser.add_argument("--img_group", default="", help="output path")

    args=parser.parse_args()
    return args

if __name__=="__main__":

    startTime=time.time()
    APP_ID='***'
    API_KEY='***'
    SECRET_KEY='***'
    
    client=AipBodyAnalysis(APP_ID, API_KEY, SECRET_KEY)

    # root="/home/paper/Desktop/Contest/refine_a/"
    # folder="M2_03/"#"M1_02/"
    args=parse_args()
    
    FM_img=args.img_group

    os1=os.listdir(FM_img) # /home/paper/Desktop/Contest/test_a/M1_02/
    
    num=len(os1)
    for i in range(num):
        sub_folder=os1[i]+"/"
        
        imgfoder_path=FM_img+sub_folder+"images/"
        print(imgfoder_path)

        os2=os.listdir(imgfoder_path)
        images_num=len(os2)
        print("images num:",images_num)
        
        writefoder_path=FM_img+sub_folder+"forward/"#root+folder+os1[i]+"_mask/"
        if not os.path.exists(writefoder_path):
            os.makedirs(writefoder_path)
        
        for j in range(images_num):
            img_name=os2[j]
            #print(os2[j]) # 图像全名
            img_id=os2[j][9:11]

            imgfile=imgfoder_path+img_name # 图像的绝对路径
            ori_img=cv2.imread(imgfile)
            height,width,_=ori_img.shape
            with open(imgfile, 'rb') as fp:
                img_info=fp.read()
            
            seg_res=client.bodySeg(img_info)
            labelmap=base64.b64decode(seg_res['labelmap'])
            nparr=np.fromstring(labelmap, np.uint8)
            labelimg=cv2.imdecode(nparr,1)
            labelimg=cv2.resize(labelimg,(width,height), interpolation=cv2.INTER_NEAREST)
            new_img=np.where(labelimg==1, 255, labelimg)
            #print(new_img.shape)
            # if((new_img[:,:,0]==new_img[:,:,1]).all() and (new_img[:,:,2]==new_img[:,:,1]).all()):
            #     print("True") # True
            '''
            cv2.imshow("hello",new_img[:,:,0])
            cv2.waitKey(0)
            cv2.imshow("hello1",new_img[:,:,1])
            cv2.waitKey(0)
            cv2.imshow("hello2",new_img[:,:,2])
            cv2.waitKey(0)
            '''
            # 二值mask
            # maskfile=imgfile.replace('.jpg', '_mask.png')
            # cv2.imwrite(maskfile, new_img)
            
            res_imgfile=writefoder_path+img_name
            res_imgfile=res_imgfile.replace('.jpg', '.png')
            #result=cv2.bitwise_and(ori_img, new_img)

            # 背景无像素
            result=np.zeros((height,width,4))
            result[:,:,0:3]=ori_img
            result[:,:,3]=new_img[:,:,0]
            cv2.imwrite(res_imgfile, result)

    endTime=time.time()
    print("useTime: ", endTime-startTime)
    print('Done.')

参考链接:百度一键人像分割 API 试用

使用百度API进行图像清晰化/对比度增强

官方使用说明:图像清晰度增强

import argparse
import cv2
import base64
import requests
import numpy as np
import time
import os
from aip import AipImageProcess # 注意不同api import的函数不同
def parse_args():
    parser=argparse.ArgumentParser(description="convert a text colmap export to nerf format transforms.json; optionally convert video to images, and optionally run colmap in the first place")

    parser.add_argument("--img_group", default="", help="output path")

    args=parser.parse_args()
    return args

if __name__ == "__main__":

    startTime=time.time()
    # 创建相应的应用可获取
    APP_ID='***'
    API_KEY='***'
    SECRET_KEY='***'
    
    client=AipImageProcess(APP_ID, API_KEY, SECRET_KEY)  # 这一部分根据相应SDK使用说明进行修改

    args=parse_args()
    
    FM_img=args.img_group

    os1=os.listdir(FM_img) # /home/paper/Desktop/Contest/test_a/M1_02/
    
    num = len(os1)
    for i in range(1):#(num):
        sub_folder=os1[i]+"/"
        
        imgfoder_path=FM_img+sub_folder #+"images/"
        print(imgfoder_path)

        os2=os.listdir(imgfoder_path)
        images_num=len(os2)
        print("images num:",images_num)
        
        writefoder_path="/home/paper/Desktop/H/"+sub_folder#+sub_folder+"forward/"#root+folder+os1[i]+"_mask/"
        if not os.path.exists(writefoder_path):
            os.makedirs(writefoder_path)
        
        for j in range(images_num):
            img_name=os2[j]
            print(os2[j]) # 图像全名
            img_id=os2[j][9:11]

            imgfile=imgfoder_path+img_name # 图像的绝对路径
            ori_img=cv2.imread(imgfile)
            height,width,_=ori_img.shape
            with open(imgfile, 'rb') as fp:
                img_info=fp.read()
            
            #seg_res=client.imageDefinitionEnhance(img_info) # #这一部分根据相应SDK使用说明进行修改
            seg_res=client.contrastEnhance(img_info)

            labelmap=base64.b64decode(seg_res['image']) # 解码
            
            nparr=np.fromstring(labelmap, np.uint8)
            image=cv2.imdecode(nparr,1)
            print(image.shape)

            res_imgfile=writefoder_path+img_name
            cv2.imwrite(res_imgfile,image)
           
    endTime=time.time()
    print("useTime: ", endTime-startTime)
    print('Done.')

学习链接:基于百度AI+Python编程的简单应用:关于人像照片动漫化的分析实现(可进行批量化处理)


http://www.niftyadmin.cn/n/735367.html

相关文章

jquery自带的进度条功能如何使用?

弹出进度条:先做弹出的功能modal,再做进度条显示。在弹出的界面上增加进度条功能 1 $.ajax({2 xhr: function()3 {4 var xhr new window.XMLHttpRequest();5 6 //Upload progress7 xhr.upload.addEventListener("progress&quo…

Java 踩坑笔记: YYYY-MM-dd 的误用

Java 踩坑笔记: YYYY-MM-dd 的误用 文章目录Java 踩坑笔记: YYYY-MM-dd 的误用简介参考完整示例代码正文测试代码问题来源结语简介 可能对于熟悉 java 的开发者来说已经是老生常谈了,本篇将要来说说在 SimpleDateFormat 中使用 YYYY-MM-dd 可能会带来的问题。 参考…

leetcode-137-Single Number II-第一种解法

题目描述: Given an array of integers, every element appears three times except for one, which appears exactly once. Find that single one. Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra mem…

根据内参调整图像大小

根据内参进行图像缩放 公式推导参考:图像缩放后相机内参如何变化的 注意:直接调用OpenCV对图像进行resize只能改变图像shape,从相机层级出发 应该是根据内参来进行转换的(其中可能涉及 焦距 和 分辨率的调整)。 设原…

spring + mybatis整合(java配置方式)

一、导入spring 和 mybatis 的依赖。 1、spring核心容器的五个依赖、spring单元测试依赖&#xff08;此依赖和spring核心版本最好一致。&#xff09; <properties><java.version>1.8</java.version><spring.version>5.0.4.RELEASE</spring.version&…

设计模式: Proxy 代理模式

设计模式: Proxy 代理模式 文章目录设计模式: Proxy 代理模式简介参考完整示例代码正文场景模式结构代码示例Subject 操作接口Real Subject 操作实体Proxy Subject 操作代理测试代码结语简介 目的创建型结构型行为型类Factory Method 工厂方法Adapter 适配器Interpreter 解释器…

BA-NeRF ICCV 2021

蓝色 紫色 红色 BARF: Bundle-Adjusting Neural Radiance Fields Author From: Abstract NeRF的缺点之一是需要 准确的相机位姿 来学习场景表示。本文提出了BA-NeRF&#xff0c;可以使用不完美&#xff08;甚至未知&#xff09;的相机位姿来训练NeRF&#xff08;联合解决3D场…

Android端实现多人音视频聊天应用(一)

作者&#xff1a;声网用户&#xff0c;资深Android工程师吴东洋。 本系列文章分享了基于Agora SDK 2.1实现多人视频通话的实践经验。 转载已经过原作者许可。原文地址 自从2016年&#xff0c;鼓吹“互联网寒冬”的论调甚嚣尘上&#xff0c;2017年亦有愈演愈烈之势。但连麦直播、…