Exemplo n.º 1
0
def get_pic_detail():
    """ 你的 APPID AK SK """
    APP_ID = '10327698'
    API_KEY = 'xe7uEVwG7aPfIwrYdCpbBBke'
    SECRET_KEY = 'rEdBGcYjOPmq8ZZG2GVF4XXjkvva3UGC'

    aipImageClassify = AipImageClassify(APP_ID, API_KEY, SECRET_KEY)

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

    image = get_file_content('00.jpg')
    options = {}
    print(aipImageClassify.objectDetect(image, options))
Exemplo n.º 2
0
class ImageColor:
    def __init__(self, url=None, file_path=None):
        self.file_path = file_path
        self.client = AipImageClassify(APP_ID, API_KEY, SECRET_KEY)
        self.param = None
        self.url = url
        self.img = None
        self.response = None

    # 读取图片
    def read_image(self):
        if self.file_path:
            self.img = cv2.imread(self.file_path)
        elif self.url:
            self.response = requests.get(self.url)
            image_numpy = np.asarray(bytearray(self.response.content), dtype="uint8")
            img = cv2.imdecode(image_numpy, cv2.IMREAD_COLOR)
            self.img = img

    # 主体检测
    def subject_detection(self):
        if self.file_path:
            with open(self.file_path, 'rb') as fp:
                # 调用图像主体检测
                options = dict()
                options["with_face"] = 0
                response = self.client.objectDetect(fp.read(), options)
                self.param = response['result']
        elif self.url:
            # 调用图像主体检测
            options = dict()
            options["with_face"] = 0
            response = self.client.objectDetect(self.response.content, options)
            if 'result' in response:
                self.param = response['result']

    # 主体参数裁剪图片
    def tailoring(self):
        if self.param:
            self.img = self.img[self.param['top']:self.param['top'] + self.param['height'],
                            self.param['left']:self.param['left'] + self.param['width']]
        # cv2.imshow('ai', initial_image)  # 显示图片
        # cv2.waitKey(0)

    # 颜色识别
    def color_recognition(self, expected_size=40, in_clusters=7, out_clusters=3):
        # a = time.time()
        self.read_image()
        # b = time.time()
        # print('读取图片',b-a)
        # self.subject_detection()
        # c = time.time()
        # print('主体检测', c - b)
        # self.tailoring()
        # d = time.time()
        # print('裁剪', d - c)
        height, width = self.img.shape[:2]

        factor = math.sqrt(width * height / (expected_size * expected_size))

        # 下采样 缩小图片
        image = cv2.resize(self.img,
                           (int(width / factor), int(height / factor)),
                           interpolation=cv2.INTER_LINEAR)
        LAB_image = cv2.cvtColor(image, cv2.COLOR_BGR2Lab)
        frame_width = int(expected_size / 10 + 2)
        in_samples = []

        border_samples = []

        limit_Y = LAB_image.shape[0] - frame_width
        limit_X = LAB_image.shape[1] - frame_width

        for y in range(LAB_image.shape[0] - 1):
            for x in range(LAB_image.shape[1] - 1):
                pt = LAB_image[y][x]
                if x < frame_width or y < frame_width or y >= limit_Y or x >= limit_X:
                    border_samples.append(pt)
                else:
                    in_samples.append(pt)

        in_samples = np.array(in_samples, dtype=float)
        border_samples = np.array(border_samples, dtype=float)

        em_in = cv2.ml.EM_create()
        em_in.setClustersNumber(in_clusters)
        in_etval, in_likelihoods, in_labels, in_probs = em_in.trainEM(in_samples)

        in_means = em_in.getMeans()
        in_covs = em_in.getCovs()

        em_border = cv2.ml.EM_create()
        em_border.setClustersNumber(out_clusters)
        border_etval, border_likelihoods, border_labels, border_probs = em_border.trainEM(border_samples)

        border_means = em_border.getMeans()

        unique_border, counts_border = np.unique(border_labels, return_counts=True)
        count_border_labels = dict(zip(unique_border, counts_border))

        unique, counts = np.unique(in_labels, return_counts=True)

        count_in_labels = dict(zip(unique, counts))
        for i in range(in_clusters):
            if i not in count_in_labels:
                count_in_labels[i] = 0
        in_len = len(in_covs)

        valid = [True] * in_len

        # colors vs background
        for i in range(in_len):
            if not valid[i]:
                continue

            prop_in = float(count_in_labels[i]) / len(in_labels)

            # 如果比例太小,只能是按钮或标签
            if prop_in < 0.05:
                valid[i] = False
                continue

            # 删除相似的颜色
            for key in count_border_labels:
                prop_border = float(count_border_labels[key]) / len(border_labels)

                # 如果颜色更多地出现在中间,它就属于主色调,而不是背景。
                if prop_in > prop_border:
                    continue

                cie_dst = CIE2000_distance(in_means[i], border_means[key])

                if cie_dst < 5:
                    valid[i] = False

        # colors vs colors
        for i in range(in_len):
            if not valid[i]:
                continue

            for j in range(i + 1, in_len):
                if not valid[j]:
                    continue

                # 删除阴影和类似颜色
                cie_dst = CIE2000_distance(in_means[i], in_means[j])
                is_shadow = LAB_shadow(in_means[i], in_means[j])

                if is_shadow or cie_dst < 30:
                    if count_in_labels[j] > count_in_labels[i]:
                        valid[i] = False

                        count_in_labels[j] += count_in_labels[i]
                        break
                    else:
                        valid[j] = False
                        count_in_labels[i] += count_in_labels[j]

        num_valid = sum(True == x for x in valid)

        colors = []
        proportions = []
        total_color = 0

        # 如果布料与背景颜色相同,则采用更常见的颜色。
        if num_valid == 0:
            pos = max(count_in_labels, key=count_in_labels.get)
            colors = [in_means[pos]]
            proportions = [count_in_labels[pos]]
            total_color = count_in_labels[pos]

        for i in range(in_len):

            if not valid[i]:
                continue

            color = in_means[i]
            quantity = count_in_labels[i]
            colors.append(color)
            proportions.append(quantity)
            total_color += quantity

        color_list = []
        for i, color_LAB in enumerate(colors):
            color_LAB = np.array([[[color_LAB[0], color_LAB[1], color_LAB[2]]]])
            color_LAB = color_LAB.astype(np.uint8)
            color = cv2.cvtColor(color_LAB, cv2.COLOR_Lab2BGR)[0][0]
            color = color.tolist()

            color[0], color[2] = color[2], color[0]
            color = ','.join('%s' % id for id in color)
            color_weight = round(proportions[i] / sum(proportions), 2)
            color_list.append((color_weight, color, rgb2hex(color)))
        color_list = sorted(color_list, key=lambda d: d[0], reverse=True)
        e = time.time()
        # print('识别',e-d)
        return color_list
Exemplo n.º 3
0
def BaiDu_image_recognize(file_path, recognize_type):
    """
    图像识别
    :param file_path: 文件路径
    :param recognize_type: 识别类型,共12种:
                1. 通用图像识别
                2. 菜品识别
                3. 车辆识别
                4. logo商标识别
                5. 动物识别
                6. 植物识别
                7. 图像主体检测
                8. 地标识别
                9. 食材识别
                10. 红酒识别
                11. 货币识别
    :return:
    """
    # TODO 隐私信息
    """ 你的 APP_ID API_KEY SECRET_KEY """
    app_id = '23899102'  # '你的 App ID'
    api_key = 'GKHIEq6gVOfp2AeRxfUopSDM'  # '你的 Api Key'
    secret_key = 'dLrlMlGemQ1oan2OS8GogLDD0dt1HuVI'  # '你的 Secret Key'

    # 获取百度云操作类对象
    client = AipImageClassify(app_id, api_key, secret_key)
    image = get_file_content(file_path)

    # """ 调用通用物体识别 """
    # result = client.dishDetect(image)
    # print(result)
    """ 如果有可选参数 """
    options = {"baike_num": 5}
    """ 带参数调用通用物体识别 """
    if recognize_type == 1:  # 通用图像识别
        response = client.advancedGeneral(image, options)
    elif recognize_type == 2:  # 菜品识别
        response = client.dishDetect(image, options)
    elif recognize_type == 3:  # 车辆识别
        response = client.carDetect(image, options)
    elif recognize_type == 4:  # logo商标识别
        response = client.logoSearch(image)
    elif recognize_type == 5:  # 动物识别
        response = client.animalDetect(image, options)
    elif recognize_type == 6:  # 植物识别
        response = client.plantDetect(image, options)
    elif recognize_type == 7:  # 图像主体检测
        response = client.objectDetect(image)
    elif recognize_type == 8:  # 地标识别
        response = client.landmark(image)
    # 花卉识别已经移除
    # elif recognize_type == 9:   # 花卉识别
    #     response = client.flower(image)
    elif recognize_type == 9:  # 食材识别
        response = client.ingredient(image, options)
    elif recognize_type == 10:  # 红酒识别
        response = client.redwine(image)
    elif recognize_type == 11:  # 货币识别
        response = client.currency(image)
    else:
        response = None
    response = response['result'][0]
    return response
Exemplo n.º 4
0
from aip import AipImageClassify

""" 你的 APPID AK SK """
APP_ID = '15180965'
API_KEY = 'Gjw3RzhDcMSS8RESUEiVNWkH'
SECRET_KEY = 'pZVtDe5Z74cA2ropdsI3s3rGMrFmXH9N'

client = AipImageClassify(APP_ID, API_KEY, SECRET_KEY)

""" 读取图片 """


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


image = get_file_content('images/1.jpg')

""" 调用图像主体检测 """
client.objectDetect(image)

""" 如果有可选参数 """
options = {}
options["with_face"] = 0

""" 带参数调用图像主体检测 """
response = client.objectDetect(image, options)
a = 1