예제 #1
0
def body_seg(filename="./pic/test.jpg", savefilename="./pic/fore.jpg"):
    APP_ID = '19037846'
    API_KEY = 'pi8i47A8jGBH1VZDr8ioqgfC'
    SECRET_KEY = 'qwcqweVfjf9pX2fB6MykU15u6XszXko4'
    client = AipBodyAnalysis(APP_ID, API_KEY, SECRET_KEY)
    image = get_file(filename)
    options = {}
    options["type"] = "foreground"
    res = client.bodySeg(image, options)
    foreground = base64.b64decode(res["foreground"])
    with open(savefilename, "wb") as f:
        f.write(foreground)
예제 #2
0
def AutoCutout(path):
    APP_ID = '19815134'
    API_KEY = 'i7yBb3Fx3e4ZMILo8nHsrZQT'
    SECRET_KEY = 'wuEXaPYbz1RXlAdDYYRj49tlPoNZdqfW'

    client = AipBodyAnalysis(APP_ID, API_KEY, SECRET_KEY)
    imgfile = path
    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)
    result = cv2.bitwise_and(ori_img, new_img)
    cv2.imwrite(path, result)
예제 #3
0
def Seg_img(jpg_path, crop_path, save_path):
    '''
    调用API 分割人像图片
    :param jpg_path: 原图像
    :param crop_path: 裁剪之后图像
    :param save_path: 生成 mask 存放路径
    :return:
    '''

    # 通过百度控制台申请得到的 AK 和 SK;
    APP_ID = "23633750"
    API_KEY = 'uqnHjMZfChbDHvPqWgjeZHCR'
    SECRET_KEY = 'KIKTgD5Dh0EGyqn74Zqq8wHWB39uKaS3'

    client = AipBodyAnalysis(APP_ID, API_KEY, SECRET_KEY)
    # 文件夹
    jpg_file = os.listdir(jpg_path)
    # 要保存的文件夹
    for i in jpg_file:
        open_file = os.path.join(jpg_path, i)
        save_file = os.path.join(save_path, i)
        if not os.path.exists(save_file):  #文件不存在时,进行下步操作
            img = cv2.imread(open_file)  # 获取图像尺寸
            height, width, _ = img.shape
            if crop_path:  # 若Crop_path 不为 None,则不进行裁剪
                crop_file = os.path.join(crop_path, i)
                img = img[100:-1, 300:-400]  #图片太大,对图像进行裁剪
                cv2.imwrite(crop_file, img)
                image = get_file_content(crop_file)
            else:

                image = get_file_content(open_file)

            res = client.bodySeg(image)  #调用百度API 对人像进行分割
            labelmap = base64.b64decode(res['labelmap'])
            labelimg = np.frombuffer(labelmap, np.uint8)  # 转化为np数组 0-255
            labelimg = cv2.imdecode(labelimg, 1)
            labelimg = cv2.resize(labelimg, (width, height),
                                  interpolation=cv2.INTER_NEAREST)
            img_new = np.where(labelimg == 1, 255, labelimg)  # 将 1 转化为 255
            cv2.imwrite(save_file, img_new)
            print(save_file, 'save successfully')
예제 #4
0
def cut_picture(src):

    # 在百度云中申请,每天各接口有 500 次调用限制.
    APP_ID = '16628525'
    API_KEY = '5ioBzjijPln33f7mPzyProbc'
    SECRET_KEY = 'FR4URtsr2Q77r6RvNyld4swls1Eik5Pu'

    client = AipBodyAnalysis(APP_ID, API_KEY, SECRET_KEY)

    # 读取待分割人像图片
    imgfile = src
    ori_img = cv2.imread(imgfile)
    height, width, _ = ori_img.shape

    with open(imgfile, 'rb') as fp:
        img_info = fp.read()
        # print("img_info:", img_info)

    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)
    # maskfile = imgfile.replace('.jpg', '_mask.png')
    maskfile_dir = './static/image/cut/' + \
        imgfile.split('/')[-1].split('.')[0]+'_mask.png'
    cv2.imwrite(maskfile_dir, new_img)

    # res_imgfile = imgfile.replace('.jpg', '_res.jpg')
    result = cv2.bitwise_and(ori_img, new_img)
    # 保存最终分割出的人像图片到原始图片所在目录

    result_path = './static/image/cut/' + \
        imgfile.split('/')[-1].split('.')[0]+'_res.jpg'
    cv2.imwrite(result_path, result)
예제 #5
0
def bodyseg(filename):

    #    APP_ID = 'd90755ad2f2047dbabb12ad0adaa0b03'
    #    API_KEY = 'b7b0f4d01f7f4aef9b5453f6558c23b1'
    #    SECRET_KEY = '6ad666162ef24213b5bde7bdd904fcbe'

    client = AipBodyAnalysis(APP_ID, API_KEY, SECRET_KEY)

    # """ 读取图片 """
    image = get_file_content(filename)

    res = client.bodySeg(image)
    print(res)
    labelmap = base64.b64decode(res['labelmap'])
    # time.sleep(2)
    nparr_labelmap = np.fromstring(labelmap, np.uint8)
    labelmapimg = cv2.imdecode(nparr_labelmap, 1)
    print(labelmapimg.shape)
    im_new_labelmapimg = np.where(labelmapimg == 1, 255, labelmapimg)
    # print(im_new_labelmapimg.shape)
    # img=cv2.cvtColor(im_new_labelmapimg, cv2.COLOR_BGR2GRAY)
    # return img.astype('uint8')
    # cv2.imwrite('outline.png',im_new_labelmapimg)
    return im_new_labelmapimg
예제 #6
0
# 在百度云中申请,每天各接口有 500 次调用限制.
startTime = time.time()
APP_ID = '19037846'
API_KEY = 'pi8i47A8jGBH1VZDr8ioqgfC'
SECRET_KEY = 'qwcqweVfjf9pX2fB6MykU15u6XszXko4'

client = AipBodyAnalysis(APP_ID, API_KEY, SECRET_KEY)

imgfile = 'test.jpg'
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)
maskfile = imgfile.replace('.jpg', '_mask.png')
cv2.imwrite(maskfile, new_img)

res_imgfile = imgfile.replace('.jpg', '_res.jpg')
result = cv2.bitwise_and(ori_img, new_img)
cv2.imwrite(res_imgfile, result)
endTime = time.time()
print("useTime: ", endTime - startTime)
print('Done.')
예제 #7
0
APP_ID = '22952720'
API_KEY = 'inMk4SrpYUxPmSUGmqGw89Ow'
SECRET_KEY = '7ecoXWQBLwwEjwp83Od0jV5EZM166oG7'

client = AipBodyAnalysis(APP_ID, API_KEY, SECRET_KEY)
""" 读取图片 """


def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()


image = get_file_content('post_img.jpg')
""" 调用人像分割 """
client.bodySeg(image)
""" 如果有可选参数 """
options = {}
options["type"] = "labelmap"


def base64_to_rgb(base64_str):
    if isinstance(base64_str, bytes):
        base64_str = base64_str.decode("utf-8")

    img_data = base64.b64decode(base64_str)
    return skimage.io.imread(img_data, plugin='imageio')


""" 带参数调用人像分割 """
resp = client.bodySeg(image, options)
예제 #8
0
APP_ID = '14724666'
API_KEY = '9RgncDsMemqT2vFNlPGWsVRM'
SECRET_KEY = 'RqQh1PEKh3REH0cNgBwKCBZGRzeaitsP'

client = AipBodyAnalysis(APP_ID, API_KEY, SECRET_KEY)
""" 读取图片 """


def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()


image = get_file_content('example.png')
""" 调用人像分割 """
dict = client.bodySeg(image)
labelmap = dict['labelmap']
print(labelmap)
ret = base64.b64decode(labelmap)
print(ret)

with open('labelmap.png', 'wb') as fp:
    fp.write(ret)

mask = cv.imread('labelmap.png')
mask = cv.resize(mask, (640, 360), cv.INTER_NEAREST)
print(mask.shape)
print(np.max(mask))
print(np.min(mask))
mask = mask * 255
예제 #9
0
class BodyClient(object):
    # APP_ID = '44eab39f0ed44844b94f487a6e88fdbc'  # 'd90755ad2f2047dbabb12ad0adaa0b03'
    # API_KEY = '55e735d6888b46908915f3533a6b7442'  # '22f1025b88f54494bcf5d980697b4b83 '
    # SECRET_KEY = '41213cbdaffa483d9ed9a59a24157d4b'  # '4a4b41139c204905be1db659d751355f'
    APP_ID = '18176250'  # 'd90755ad2f2047dbabb12ad0adaa0b03'
    API_KEY = 'jgfFvfLmKWBTuuIFdFgF4YXI'  # '22f1025b88f54494bcf5d980697b4b83 '
    SECRET_KEY = 'NCnPnPOcSRexlxoz9fI75fUmEGl3H15f'  # '4a4b41139c204905be1db659d751355f'

    def __init__(self):
        self.client = AipBodyAnalysis(BodyClient.APP_ID, BodyClient.API_KEY, BodyClient.SECRET_KEY)

    def _body_seg(self,filename):
        image = get_file_content(filename)
        res = self.client.bodySeg(image)
        labelmap = base64.b64decode(res['labelmap'])
        nparr_labelmap = np.fromstring(labelmap, np.uint8)
        labelmapimg = cv2.imdecode(nparr_labelmap, 1)
        im_new_labelmapimg = np.where(labelmapimg == 1, 255, labelmapimg)
        return im_new_labelmapimg

    def _canny_points(self,img,rect=None):
        if rect:
            x,y,w,h = rect
        else:
            x,y=0,0
            h,w = img.shape[:2]
        body = img[y:y + h, x:x + w]
        edges = cv2.Canny(body, 10, 100)
        edgesmat = np.mat(edges)
        points = [(j + x, i + y) for i in range(h) for j in range(w) if edgesmat[i, j] == 255]
        return points

    def body_seg_points(self,filename):
        img_seg=self._body_seg(filename)
        return self._canny_points(img_seg)

    def body_seg(self,filename):
        img_seg = self._body_seg(filename)
        h, w = img_seg.shape[:2]
        return self._canny_points(img_seg),w,h

    '''
    {'person_num': 2, 'person_info': [{'body_parts': {'left_hip': {'y': 549.78125, 'x': 423.34375, 'score': 0.8641700744628906}, 'top_head': {'y': 295.46875, 'x': 394.0, 'score': 0.8867737650871277}, 'right_mouth_corner': {'y': 344.375, 'x': 384.21875, 'score': 0.8865712285041809}, 'neck': {'y': 363.9375, 'x': 394.0, 'score': 0.8912984728813171}, 'left_shoulder': {'y': 383.5, 'x': 442.90625, 'score': 0.8800243139266968}, 'left_knee': {'y': 657.375, 'x': 433.125, 'score': 0.8804177045822144}, 'left_ankle': {'y': 755.1875, 'x': 423.34375, 'score': 0.8549085855484009}, 'left_mouth_corner': {'y': 344.375, 'x': 403.78125, 'score': 0.8695278763771057}, 'right_elbow': {'y': 442.1875, 'x': 305.96875, 'score': 0.9053295850753784}, 'right_ear': {'y': 324.8125, 'x': 374.4375, 'score': 0.8913755416870117}, 'nose': {'y': 324.8125, 'x': 394.0, 'score': 0.8767616748809814}, 'left_eye': {'y': 315.03125, 'x': 403.78125, 'score': 0.8842508792877197}, 'right_eye': {'y': 315.03125, 'x': 384.21875, 'score': 0.872444748878479}, 'right_hip': {'y': 549.78125, 'x': 374.4375, 'score': 0.8706536293029785}, 'left_wrist': {'y': 491.09375, 'x': 462.46875, 'score': 0.8681846857070923}, 'left_ear': {'y': 324.8125, 'x': 413.5625, 'score': 0.8833358883857727}, 'left_elbow': {'y': 432.40625, 'x': 491.8125, 'score': 0.8757244944572449}, 'right_shoulder': {'y': 383.5, 'x': 345.09375, 'score': 0.8604100942611694}, 'right_ankle': {'y': 755.1875, 'x': 364.65625, 'score': 0.883700966835022}, 'right_knee': {'y': 657.375, 'x': 364.65625, 'score': 0.8726198673248291}, 'right_wrist': {'y': 491.09375, 'x': 335.3125, 'score': 0.8524751663208008}}, 'location': {'height': 522.3967895507812, 'width': 213.4878540039062, 'top': 279.5125427246094, 'score': 0.9985131025314331, 'left': 288.0614013671875}}, {'body_parts': {'left_hip': {'y': 539.0, 'x': 413.25, 'score': 0.2676204741001129}, 'top_head': {'y': 741.5, 'x': 524.625, 'score': 0.0297189150005579}, 'right_mouth_corner': {'y': 478.25, 'x': 463.875, 'score': 0.009633682668209076}, 'neck': {'y': 852.875, 'x': 332.25, 'score': 0.01634016819298267}, 'left_shoulder': {'y': 377.0, 'x': 423.375, 'score': 0.0272684283554554}, 'left_knee': {'y': 650.375, 'x': 423.375, 'score': 0.3098172545433044}, 'left_ankle': {'y': 751.625, 'x': 433.5, 'score': 0.4415453672409058}, 'left_mouth_corner': {'y': 478.25, 'x': 463.875, 'score': 0.01229123119264841}, 'right_elbow': {'y': 427.625, 'x': 494.25, 'score': 0.08809270709753036}, 'right_ear': {'y': 680.75, 'x': 750, 'score': 0.02279716171324253}, 'nose': {'y': 488.375, 'x': 453.75, 'score': 0.02511453814804554}, 'left_eye': {'y': 488.375, 'x': 443.625, 'score': 0.02269705384969711}, 'right_eye': {'y': 751.625, 'x': 750, 'score': 0.02191649936139584}, 'right_hip': {'y': 539.0, 'x': 372.75, 'score': 0.1868444383144379}, 'left_wrist': {'y': 488.375, 'x': 474.0, 'score': 0.3365231156349182}, 'left_ear': {'y': 893.375, 'x': 403.125, 'score': 0.007937739603221416}, 'left_elbow': {'y': 437.75, 'x': 484.125, 'score': 0.1944440901279449}, 'right_shoulder': {'y': 377.0, 'x': 433.5, 'score': 0.02875573188066483}, 'right_ankle': {'y': 751.625, 'x': 423.375, 'score': 0.1604309529066086}, 'right_knee': {'y': 670.625, 'x': 372.75, 'score': 0.1398747861385345}, 'right_wrist': {'y': 488.375, 'x': 474.0, 'score': 0.07319473475217819}}, 'location': {'height': 539.47509765625, 'width': 126.1507263183594, 'top': 458.58251953125, 'score': 0.00636356882750988, 'left': 622.8492431640625}}], 'log_id': 1953527121404955486}
    '''
    def _body_part(self,filename):
        image = get_file_content(filename)
        para = self.client.bodyAnalysis(image)
        if DEBUG:
            print(para)
        person_num=para.get('person_num',0)
        if person_num < 1:
            raise NoBodyException("文件%s 没有检测到人像,详细信息:%s"%(filename,para))
        person=para['person_info'][0]
        # score = person['location']['score']
        # if score < 0.5:
        #     raise NoBodyException()
        loc = person['location']
        x_left = int(loc['left'])
        y_top = int(loc['top'])
        w = int(loc['width'])
        h = int(loc['height'])
        return person['body_parts'],(x_left,y_top,w,h)

    #top_head, left_ankle, right_ankle
    def body_points(self,filename):
        parts,rect = self._body_part(filename)
        points = {k: (v['x'], v['y']) for k, v in parts.items()}
        return points,rect

    def process_body(self,filename):
        img = cv2.imread(filename)
        height, width = img.shape[:2]
        body_points, rect = self.body_points(filename)

        x_left, y_top, w, h = rect
        new_rect=expand_rect(x_left,y_top,w,h,width,height)

        img_seg = self._body_seg(filename)
        # cv2.imshow("seg",img_seg)
        outline_points = self._canny_points(img_seg, new_rect)
        # print(outline_points)
        return body_points, outline_points, rect
예제 #10
0
class BaiduApiUtil:

	def __init__(self, configPath, configSection):
		keyInfo = fu.readConfig(configPath, configSection)
		self.appid = keyInfo['appid']
		self.api_key = keyInfo['api_key']
		self.secret_key = keyInfo['secret_key']
		self.client = AipBodyAnalysis(self.appid, self.api_key, self.secret_key)
		self.filename = None
		self.picture = None
		self.picture_size = None
		self.picture_format = None

	def upload(self, filename):
		self.picture = fu.readPicture(filename)
		self.filename = filename.split('.')[0]
		img = Image.open(filename)
		self.picture_size = img.size
		self.picture_format = img.format

	def getAccessToken(self, configPath, configSection):
		keyInfo = fu.readConfig(configPath, configSection)
		host = keyInfo['addr'] % (keyInfo['grant_type'], keyInfo['client_id'], keyInfo['client_secret'])
		response = requests.post(host)
		if response.status_code != 200:
			print('Error Happened When Acquiring Access Token')
			return -1
		content = demjson.decode(response.text)
		if 'error' in content.keys():
			print('Invalid API Key or Secret Key')
			return -1
		return content['refresh_token']

	def getBodyAnalysis(self):
		response = self.client.bodyAnalysis(self.picture)
		if 'error_code' in response.keys():
			print(response['error_msg'])
			exit(-1)
		return response


	def getBodySeg(self):
		result = self.client.bodySeg(self.picture, {'type':'labelmap'})
		# foreground = base64.b64decode(result['foreground'])
		labelmap = base64.b64decode(result['labelmap'])
		# scoremap = base64.b64decode(result['scoremap'])

		# nparr_foreground = np.fromstring(foreground, np.uint8)
		# foregroundimg = cv2.imdecode(nparr_foreground, 1)
		# foregroundimg = cv2.resize(foregroundimg, self.picture_size, interpolation=cv2.INTER_NEAREST)
		# im_new_foreground = np.where(foregroundimg == 1, 10, foregroundimg)
		# cv2.imwrite(self.filename + '-foreground.png', im_new_foreground)

		nparr_labelmap = np.fromstring(labelmap, np.uint8)
		labelmapimg = cv2.imdecode(nparr_labelmap, 1)
		labelmapimg = cv2.resize(labelmapimg, self.picture_size, interpolation=cv2.INTER_NEAREST)
		im_new_labelmapimg = np.where(labelmapimg == 1, 255, labelmapimg)
		cv2.imwrite(self.filename + '-labelmap.png', im_new_labelmapimg)

		# nparr_scoremap = np.fromstring(scoremap, np.uint8)
		# scoremapimg = cv2.imdecode(nparr_scoremap, 1)
		# scoremapimg = cv2.resize(scoremapimg, self.picture_size, interpolation=cv2.INTER_NEAREST)
		# im_new_scoremapimg = np.where(scoremapimg == 1, 255, scoremapimg)
		# cv2.imwrite(self.filename + '-scoremap.png', im_new_scoremapimg)
예제 #11
0
def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()


image = get_file_content('./111.jpg')

# """ 调用手势识别 """
# results = client.gesture(image)
# print(results)

# """ 调用图像分割 """
# options = {}
# options["type"] = 'foreground'
# client.bodySeg(image)
res = client.bodySeg(image)

foreground = base64.b64decode(res['foreground'])
labelmap = base64.b64decode(res['labelmap'])
scoremap = base64.b64decode(res['scoremap'])

nparr_foreground = np.fromstring(foreground, np.uint8)
foregroundimg = cv2.imdecode(nparr_foreground, 1)
foregroundimg = cv2.resize(foregroundimg, (2131, 1438),
                           interpolation=cv2.INTER_NEAREST)
im_new_foreground = np.where(foregroundimg == 1, 10, foregroundimg)
cv2.imwrite('foreground.png', im_new_foreground)

nparr_labelmap = np.fromstring(labelmap, np.uint8)
labelmapimg = cv2.imdecode(nparr_labelmap, 1)
labelmapimg = cv2.resize(labelmapimg, (2131, 1438),