自动化人像检测标注 | 还是百度云

标了几千张人像框实在是懒得标了,用百度云吧

主要是用百度云的人像检测API,拿到AK和SK,pip install baidu-aip

import cv2
import base64
import numpy as np
import urllib3,base64
from PIL import Image
import json
from urllib.parse import urlencode
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from aip import AipBodyAnalysis
import time
import sys
import os
import shutil
# producting 

def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()
    
def auto_annotation(pictruePath):
    APP_ID = '20***898'
    API_KEY = 'pdvIEAOdgG***RzpzGDd5wIB'
    SECRET_KEY = 'iuMrxtClDxPE****XWLE5y8r1pOcMR5Q'
#     pictruePath ='/home/*/2/*****cdaf0-6fc4-445c-8e2a-55511d90d365.jpg'
    name = pictruePath.split('.')[0].split('/')[-1]
    path = pictruePath
    every_line_xml_path = pictruePath.split('.')[0]+".xml"
    pre_fix = pictruePath.split('/')[0]
    client = AipBodyAnalysis(APP_ID, API_KEY, SECRET_KEY)
    while(True):
        try:
            image = get_file_content(pictruePath)
            request =client.bodySeg(image)
            break
        except Exception:
            time.sleep(3)
            print("try again ...")
    img_b64decode = base64.b64decode(request['labelmap']) 
    img_array = np.fromstring(img_b64decode,np.uint8)
    img=cv2.imdecode(img_array,cv2.COLOR_BGR2RGB)*255
    img = cv2.bitwise_not(img)
    # image1 = Image.fromarray(img.astype('uint8'))
    # display(image1)
    image_pic = cv2.imread("pictruePath")
    pic = cv2.merge([img,img,img])
    # processing 
    image= pic
    img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    ret, thresh = cv2.threshold(img, 230, 255, cv2.THRESH_BINARY_INV)
    contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    # for c in contours:
    #     x, y, w, h = cv2.boundingRect(c)
    #     rect = cv2.minAreaRect(c)
    #     box = cv2.boxPoints(rect)
    #     box = np.int0(box)
    # cv2.rectangle(image, (0, 0), (640, 480), (0, 255, 0), 2)
    # cv2.drawContours(image, contours, -1, (255, 0, 0), 2)
    # image1 = Image.fromarray(image.astype('uint8'))
    # display(image1)
    # filter
    list_cont = []
    x = []
    y = []
    for cont in contours:
        for con in cont:
            list_cont.append(con)
    for i in range(len(list_cont)):
        x.append(list_cont[i][0,0])
        y.append(list_cont[i][0,1])
    # General
    min_x = min(x)
    min_y = min(y)
    max_x = max(x)
    if(min_y <= 5):
        min_y = 4
    max_y = max(y)
    if(max_y >= 475 and (max_x <= 635 and min_x >= 5)):
        max_y = 480
    else:
        max_y = 476
    if(max_x >= 635):
        max_x = 636
    if(min_x <= 5):
        min_x = 4
    # cv2.rectangle(image, (min_x,min_y), (max_x,max_y), (255, 0, 255), 2)
    # image1 = Image.fromarray(image.astype('uint8'))
    # display(image1)
    res_str = ""
    part1 = "<annotation><folder>zhuofan</folder><filename>"
    part2 = "</filename><path>"
    part3 = "</path><source><database>Unknown</database></source><size><width>"
    part4 = "</width><height>"
    part5 = "</height><depth>"
    part6 = "</depth></size><segmented>0</segmented>"
    t1 = "<object><name>"
    t2 = "</name><pose>Unspecified</pose><truncated>1</truncated><difficult>0</difficult><bndbox><xmin>"
    t3 = "</xmin><ymin>"
    t4 = "</ymin><xmax>"
    t5 = "</xmax><ymax>"
    t6 = "</ymax></bndbox></object>"
    tmp =  t1+"person"+t2+str(min_x)+t3+str(min_y)+t4+str(max_x)+t5+str(max_y)+t6
    # name = "*****ecdaf0-6fc4-445c-8e2a-55511d90d365"
    # path = "/home/*/2/******f0-6fc4-445c-8e2a-55511d90d365.jpg"
    res_str = part1+name+part2+path+part3+str(640)+part4+str(480)+part5+str(3)+part6+tmp
    res_str = res_str + "</annotation>"
    # every_line_xml_path = "/home/*/2/******cdaf0-6fc4-445c-8e2a-55511d90d365.xml"
    file_xml = open(every_line_xml_path,'w')
    file_xml.write(res_str)
    file_xml.close()
    res_str = ""
    part_data = []
    shutil.move(every_line_xml_path, "/home/*/****/****/"+name+".xml")
    shutil.move(do, "/home/*/****/****/"+name+".jpg")

book_path = "/home/*/*****/****/****"
list_p = os.listdir(book_path)
num =1
lens = len(list_p)
for pa in list_p:
    do = book_path+"/"+pa
    print("["+str(num)+"/"+str(lens)+"] "+do)
    auto_annotation(do)
    num = num + 1

因为是自用 一些调试代码并没有被删除,调试时在jupyter notebook上做的

做了2499张自动标注,人工复核了一下。

需要修正的错误率大概9.9%左右,比手工标注不知道高到哪里去了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值