def Generate(self):
   n = len(self.labels)
   i = 0
   clone_labels = self.labels.copy()
   while True:
     if i==0:
       random.shuffle(clone_labels)
     # 数据平均
     label = clone_labels[i]
     # print('image_path:', label['image_path'])
     # 读取图片
     image_path = label['image_path']
     img = image_helper.fileToOpencvImage(image_path)
     # print('imgType:', type(img))
     # width, height = ImageHelper.opencvGetImageSize(img)
     # print('imgSize:', width, height)
     # 获取随机变换图片及标签
     points = label['points']
     img, points = self._get_random_data(
         img, target_points=points)
     # 缩放图片
     img, points, _ = image_helper.opencvProportionalResize(
         img, self.input_shape, points=points, bg_color=None, bg_mode=None)
     # 最后输出图片
     img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
     # 调整参数范围
     img =img.astype(np.float32)
     img = img / 255
     
     points = points/self.input_shape
     points = points[...,::-1]
     i = (i+1) % n
     if (points<0).any() or (points>1).any():
       continue
     yield img, points
Ejemplo n.º 2
0
    def get_random_data(self, label):
        '''
    随机数据扩展

    Args:
      label: {
        'image_path': 图片路径,
        'classes': [obj_num,],
        'boxes': [obj_num,[x1,y1,x2,y2]]
        }

    Returns:
      random_img: [h,w,3]
      random_boxes: [obj_num,[y1,x1,y2,x2]]
      classes: [obj_num,]
    '''
        random_img = ImageHelper.fileToOpencvImage(label['image_path'])
        random_boxes = label['boxes']
        random_boxes = np.array(random_boxes)
        # 转成点集
        random_boxes = random_boxes.reshape((-1, 2))
        # 图片大小
        # width, height = ImageHelpler.opencvGetImageSize(random_img)
        # 画矩形
        # cv2.rectangle(image, (20, 20), (380, 380), tuple(np.random.randint(0, 30, (3), dtype=np.int32)), thickness=8)
        # 增加模糊
        ksize = random.randint(0, 4)
        if ksize > 0:
            random_img = ImageHelper.opencvBlur(random_img, (ksize, ksize))
        # 变换图像,旋转会导致框不准
        # random_offset_x = random.random()*width-width/2
        # random_offset_y = random.random()*height-height/2
        random_offset_x = random.random() * 90 - 45
        random_offset_y = random.random() * 90 - 45
        # random_angle_x = random.random()*60-30
        # random_angle_y = random.random()*60-30
        # random_angle_z = random.random()*40-20
        random_scale_x = random.random() * 1.5 + 0.5
        random_scale_y = random.random() * 1.5 + 0.5
        random_scale_z = 1
        # random_offset_x = 0
        # random_offset_y = 0
        random_angle_x = 0
        random_angle_y = 0
        random_angle_z = 0
        # random_scale = 1
        random_img, org, dst, random_boxes = ImageHelper.opencvPerspective(
            random_img,
            offset=(random_offset_x, random_offset_y, 0),
            angle=(random_angle_x, random_angle_y, random_angle_z),
            scale=(random_scale_x, random_scale_y, random_scale_z),
            points=random_boxes,
            bg_color=None,
            bg_mode=None)
        # 增加线条
        # random_img = image_helper.opencvRandomLines(random_img, 8)
        # 增加噪声
        random_img = ImageHelper.opencvNoise(random_img)
        # 颜色抖动
        # random_img = ImageHelper.opencvRandomColor(random_img)
        # 调整图片大小
        random_img, random_boxes, padding = ImageHelper.opencvProportionalResize(
            random_img,
            self.image_size,
            points=random_boxes,
            bg_color=None,
            bg_mode=None)
        # 最后输出图片
        random_img = cv2.cvtColor(random_img, cv2.COLOR_BGR2RGB)
        # 调整参数范围
        random_img = random_img.astype(np.float32)
        random_img = random_img / 255
        # 转换boxes成框列表
        random_boxes = random_boxes.reshape((-1, 4))
        # # 将4角坐标转换成矩形坐标
        # random_boxes = np.reshape(random_boxes, (-1, 4, 2))
        # boxes_min = np.min(random_boxes, axis=1)
        # boxes_max = np.max(random_boxes, axis=1)
        # random_boxes = np.concatenate([boxes_min, boxes_max], axis=-1)
        # 截取框超出图片部分
        random_boxes[:, 0][random_boxes[:, 0] < 0] = 0
        random_boxes[:, 1][random_boxes[:, 1] < 0] = 0
        random_boxes[:, 2][
            random_boxes[:, 2] > self.image_size[0]] = self.image_size[0]
        random_boxes[:, 3][
            random_boxes[:, 3] > self.image_size[1]] = self.image_size[1]
        # 去掉无效框
        mask = np.logical_and(random_boxes[:, 2] - random_boxes[:, 0] >= 2,
                              random_boxes[:, 3] - random_boxes[:, 1] >= 2)
        random_boxes = random_boxes[mask][:, [1, 0, 3, 2]]
        classes = np.array(label['classes'], dtype=np.int32)
        classes = classes[mask]

        # cv2.imwrite(path, image)
        return random_img, random_boxes, classes
def predict(request):
    '''识别'''
    request_data = json.loads(request.body)
    # print('request_data:', request_data)
    read = request_data['read']
    img_data = request_data['img_data'].split(',')[1]
    img_data = ImageHelper.base64ToBytes(img_data)
    img_old = ImageHelper.bytesToOpencvImage(img_data)
    # 图片大小
    image_size = np.int32([416, 416])
    # 缩放图片
    img, _, padding = ImageHelper.opencvProportionalResize(img_old,
                                                           image_size,
                                                           bg_color=(0, 0, 0))

    # print('imgType:', type(img))
    width, height = ImageHelper.opencvGetImageSize(img_old)
    image_size_old = np.int32([width, height])
    print('imgSize:', width, height)
    # 最后输出图片
    predict_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    # 调整参数范围
    predict_img = predict_img.astype(np.float32)
    predict_img = predict_img / 255
    # 增加一个维度
    predict_img = np.expand_dims(predict_img, 0)
    y_boxes, y_classes_id, y_scores, y_classes, y_confidence = model.Predict(
        predict_img, confidence_thresh=0.5, scores_thresh=0.2, iou_thresh=0.5)
    # 结果转物体框列表
    y_boxes = y_boxes.numpy()
    y_classes_id = y_classes_id.numpy()
    y_scores = y_scores.numpy()
    y_classes = y_classes.numpy()
    y_confidence = y_confidence.numpy()
    y_boxes[:, [0, 2]] = (y_boxes[:, [0, 2]] * image_size[0] - padding[2]) / (
        image_size[0] - padding[2] - padding[3]) * image_size_old[0]
    y_boxes[:, [1, 3]] = (y_boxes[:, [1, 3]] * image_size[1] - padding[0]) / (
        image_size[1] - padding[0] - padding[1]) * image_size_old[1]
    # 截取框超出图片部分
    y_boxes[:, 0][y_boxes[:, 0] < 0] = 0
    y_boxes[:, 1][y_boxes[:, 1] < 0] = 0
    y_boxes[:, 2][y_boxes[:, 2] > image_size_old[0]] = image_size_old[0]
    y_boxes[:, 3][y_boxes[:, 3] > image_size_old[1]] = image_size_old[1]
    # 去掉无效框
    y_mask = np.logical_and(y_boxes[:, 2] - y_boxes[:, 0] > 2,
                            y_boxes[:, 3] - y_boxes[:, 1] > 2)
    y_boxes = y_boxes[y_mask]
    y_classes_id = y_classes_id[y_mask]
    y_scores = y_scores[y_mask]
    y_classes = y_classes[y_mask]
    y_confidence = y_confidence[y_mask]
    y_boxes = y_boxes.astype(np.int32)
    print('y_boxes:', y_boxes.shape)
    print('y_classes_id:', y_classes_id.shape)
    print('y_scores:', y_scores.shape)
    print('y_classes:', y_classes.shape)
    print('y_confidence:', y_confidence.shape)
    # 画框
    result_img = img_old.copy()
    for i in range(y_boxes.shape[0]):
        print('y_boxes:', y_boxes[i, :])
        print('y_classes_id:', y_classes_id[i])
        print('y_scores:', y_scores[i])
        print('y_classes:', y_classes[i])
        print('y_confidence:', y_confidence[i])
        cv2.rectangle(result_img,
                      tuple(y_boxes[i, 0:2]),
                      tuple(y_boxes[i, 2:4]), (0, 0, 255),
                      thickness=1)
        cv2.putText(result_img, classes_name[y_classes_id[i]],
                    tuple(y_boxes[i, 0:2]), cv2.FONT_HERSHEY_COMPLEX, 0.6,
                    (0, 100, 0), 1)
        cv2.putText(result_img, str(y_scores[i]),
                    tuple(y_boxes[i, 0:2] + (0, 20)), cv2.FONT_HERSHEY_COMPLEX,
                    0.6, (0, 0, 100), 1)
    jsonObj = {
        'boxes':
        y_boxes.tolist(),
        'classes':
        y_classes.tolist(),
        'random_img':
        ImageHelper.bytesTobase64(ImageHelper.opencvImageToBytes(img)),
        'result_img':
        ImageHelper.bytesTobase64(ImageHelper.opencvImageToBytes(result_img)),
    }
    # print('jsonObj:',jsonObj)
    return HttpResponse(json.dumps(jsonObj), content_type="application/json")
Ejemplo n.º 4
0
def test():
    '''训练'''
    # 加载数据
    image_wh = (416, 416)
    # image_wh = (608, 608)
    anchors = LoadAnchors(anchorsFile)
    classes_name, classes_num = LoadClasses(classesFile)
    # 构建模型
    model = YoloV3Model(classes_num=classes_num,
                        anchors=anchors,
                        image_wh=image_wh)

    # 编译模型
    print('编译模型')
    model.compile(optimizer=tf.keras.optimizers.Adam(lr=1e-4))

    # 加载模型
    _ = model(tf.ones((1, image_wh[1], image_wh[0], 3)), training=False)
    if os.path.exists(modelPath):
        last_model_path = tf.train.latest_checkpoint(modelPath)
        model.load_weights(last_model_path).expect_partial()
        print('加载模型:{}'.format(last_model_path))
    # model.summary()

    img_old = ImageHelper.fileToOpencvImage(imageFile)
    # 缩放图片
    img, _, padding = ImageHelper.opencvProportionalResize(img_old,
                                                           image_wh,
                                                           bg_color=(0, 0, 0))

    # print('imgType:', type(img))
    width, height = ImageHelper.opencvGetImageSize(img_old)
    image_size_old = np.int32([width, height])
    print('imgSize:', width, height)
    # 最后输出图片
    predict_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    # 调整参数范围
    predict_img = predict_img.astype(np.float32)
    predict_img = predict_img / 255
    # 增加一个维度
    predict_img = np.expand_dims(predict_img, 0)
    y_boxes, y_classes_id, y_scores, y_classes, y_confidence = model.Predict(
        predict_img, confidence_thresh=0.5, scores_thresh=0.2, iou_thresh=0.5)
    # 结果转物体框列表
    y_boxes = y_boxes.numpy()
    y_classes_id = y_classes_id.numpy()
    y_scores = y_scores.numpy()
    y_classes = y_classes.numpy()
    y_confidence = y_confidence.numpy()
    y_boxes[:, [0, 2]] = (y_boxes[:, [0, 2]] * image_wh[0] - padding[2]) / (
        image_wh[0] - padding[2] - padding[3]) * image_size_old[0]
    y_boxes[:, [1, 3]] = (y_boxes[:, [1, 3]] * image_wh[1] - padding[0]) / (
        image_wh[1] - padding[0] - padding[1]) * image_size_old[1]
    # 截取框超出图片部分
    y_boxes[:, 0][y_boxes[:, 0] < 0] = 0
    y_boxes[:, 1][y_boxes[:, 1] < 0] = 0
    y_boxes[:, 2][y_boxes[:, 2] > image_size_old[0]] = image_size_old[0]
    y_boxes[:, 3][y_boxes[:, 3] > image_size_old[1]] = image_size_old[1]
    # 去掉无效框
    y_mask = np.logical_and(y_boxes[:, 2] - y_boxes[:, 0] > 2,
                            y_boxes[:, 3] - y_boxes[:, 1] > 2)
    y_boxes = y_boxes[y_mask]
    y_classes_id = y_classes_id[y_mask]
    y_scores = y_scores[y_mask]
    y_classes = y_classes[y_mask]
    y_confidence = y_confidence[y_mask]
    y_boxes = y_boxes.astype(np.int32)
    # print('y_boxes:', y_boxes.shape)
    # print('y_classes_id:', y_classes_id.shape)
    # print('y_scores:', y_scores.shape)
    # print('y_classes:', y_classes.shape)
    # print('y_confidence:', y_confidence.shape)
    colors = []
    for cr in range(0, 256, 32):
        for cg in range(0, 256, 32):
            for cb in range(0, 256, 32):
                colors.append([cb, cg, cr])
    colors = np.array(colors)
    np.random.seed(11)
    np.random.shuffle(colors)
    np.random.seed(None)
    result_img = img_old.copy()
    for i in range(y_boxes.shape[0]):
        # print('y_boxes:', y_boxes[i,:])
        # print('y_classes_id:', y_classes_id[i])
        # print('y_classes_name:', classes_name[y_classes_id[i]])
        # print('y_scores:', y_scores[i])
        # print('y_classes:', y_classes[i])
        # print('y_confidence:', y_confidence[i])
        cv2.rectangle(result_img,
                      tuple(y_boxes[i, 0:2]),
                      tuple(y_boxes[i, 2:4]),
                      colors[y_classes_id[i] % len(colors)].tolist(),
                      thickness=1)
        cv2.putText(result_img, classes_name[y_classes_id[i]],
                    tuple(y_boxes[i, 0:2]), cv2.FONT_HERSHEY_COMPLEX, 0.6,
                    (0, 100, 0), 1)
        cv2.putText(result_img, str(y_scores[i]),
                    tuple(y_boxes[i, 0:2] + (0, 20)), cv2.FONT_HERSHEY_COMPLEX,
                    0.6, (0, 0, 100), 1)
    ImageHelper.showOpencvImage(result_img)
Ejemplo n.º 5
0
    print('加载模型:{}'.format(last_model_path))
# model.summary()

file_list = ReadFileList(dataDir,
                         pattern=r'.*\.jpg',
                         select_sub_path=True,
                         is_full_path=False)

with open(outFile, 'w', encoding='utf-8') as f:
    for image_path in file_list:
        # 图片路径
        full_path = os.path.join(dataDir, image_path)
        img_old = ImageHelper.fileToOpencvImage(full_path)
        # 缩放图片
        img, _, padding = ImageHelper.opencvProportionalResize(img_old,
                                                               image_wh,
                                                               bg_color=(0, 0,
                                                                         0))

        # print('imgType:', type(img))
        width, height = ImageHelper.opencvGetImageSize(img_old)
        image_size_old = np.int32([width, height])
        # 最后输出图片
        predict_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        # 调整参数范围
        predict_img = predict_img.astype(np.float32)
        predict_img = predict_img / 255
        # 增加一个维度
        predict_img = np.expand_dims(predict_img, 0)
        y_boxes, y_classes_id, y_scores, y_classes, y_confidence = model.Predict(
            predict_img,
            confidence_thresh=0.5,