コード例 #1
0
def batch_analysis_c6(meta_file, cfg_file, wgt_file, thresh, nms, img_path,
                      xml_path):
    image_list = listdir(img_path, '.jpg')
    image_num = len(image_list)
    meta = dn.load_meta(meta_file)
    net = dn.load_net(cfg_file, wgt_file, 0)
    # meta_fr = dn.load_meta(meta_file_fr)
    # net_fr = dn.load_net(cfg_file_fr,wgt_file_fr,0)
    move_count = 0
    for j, image_path in enumerate(image_list):
        print(str(j) + '/' + str(image_num) + "  " + image_path)
        image_name = getFileName(image_path)
        img_save_path = os.path.join(img_path, image_name + '.jpg')
        xml_save_path = os.path.join(xml_path, image_name + '.xml')
        # if os.path.exists(xml_save_path):
        #     continue
        # print(img_save_path)
        det = dn.detect_ext(net, meta, bytes(image_path, 'utf-8'), thresh)
        # det_fr = dn.detect_ext(net_fr, meta_fr, bytes(image_path,'utf-8'),thresh)
        img = cv2.imread(image_path)
        if img is None:
            print('Can not open image')
            continue
        h, w, c = img.shape
        writeXml(xml_save_path, w, h, image_name, det)
    dn.free_net(net)
コード例 #2
0
def batch_analysis(weights_list_file, cfg_file, meta_file, image_list_file,
                   thresh, iou_thresh, result_dir):
    image_list = LoadFileList(image_list_file)
    image_num = len(image_list)
    weights_list = LoadFileList(weights_list_file)
    meta = dn.load_meta(meta_file)
    # print(meta.classes)
    object_type = [
        meta.names[i].decode('utf-8').strip() for i in range(meta.classes)
    ]
    result = []
    for weights in weights_list:
        weights_name = getFileName(weights)
        result_path = os.path.join(result_dir, weights_name)
        if not os.path.exists(result_path):
            os.mkdir(result_path)
        net = dn.load_net(cfg_file, bytes(weights, 'utf-8'), 0)

        # detect result and save to text
        for j, image_path in enumerate(image_list):
            print('detect: ' + str(j + 1) + '/' + str(len(image_list)))
            label_path = imagePath2labelPath(image_path)
            image_name = getFileName(image_path)
            det_save_path = os.path.join(result_path, image_name + '.txt')
            # print(img_save_path)
            det = dn.detect_ext(net, meta, bytes(image_path, 'utf-8'), thresh)
            # save detection result to text
            saveDetRes(det[0], det_save_path, object_type)
            time.sleep(0.001)
        # dn.free_net(net)

        # campare label and detection result
        for i, objtype in enumerate(object_type):
            # if objtype != 'fr':
            #     continue
            total_label = 0
            total_detect = 0
            total_corr = 0
            total_iou = 0
            cmp_result = []
            # print(objtype)
            for j, image_path in enumerate(image_list):
                label_path = imagePath2labelPath(image_path)
                image_name = getFileName(image_path)
                img_save_path = os.path.join(result_path, image_name + '.jpg')
                det_save_path = os.path.join(result_path, image_name + '.txt')
                # print(img_save_path)
                label = []
                if os.path.exists(label_path):
                    label = LoadLabel(label_path, object_type)

                # save detection result to text
                det = readDetRes(det_save_path)
                for d in det:
                    if d[0] > len(object_type) - 1:
                        d[0] = ' '
                        continue
                    d[0] = object_type[d[0]]
                # print(label)
                # print(det)
                # print('')
                if i > 0:
                    image_path = img_save_path
                # print(j,image_path)
                img = cv2.imread(image_path)
                if img is None:
                    print(
                        "load image error&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&"
                    )
                # print(img.shape)
                cmp_res = cdl.cmp_data(objtype, det, label, thresh, iou_thresh,
                                       img)

                cmp_res.update_tracking({'image_name': image_name})
                total_corr += cmp_res['correct']
                total_iou += cmp_res['avg_iou'] * cmp_res['label_num']

                cmp_result.append(cmp_res)
                print("%s: %d/%d  label: %d   detect: %d   correct: %d   recall: %f   avg_iou: %f   accuracy: %f   precision: %f\n" % \
                    (str(objtype),j+1,image_num,cmp_res['label_num'],cmp_res['detect_num'],\
                        cmp_res['correct'],cmp_res['recall'],cmp_res['avg_iou'],\
                            cmp_res['accuracy'],cmp_res['precision']))
                total_label += cmp_res['label_num']
                total_detect += cmp_res['detect_num']
                cv2.imwrite(img_save_path, img)
                time.sleep(0.001)

            #数据集分析结果
            avg_recall = 0
            if total_label > 0:
                avg_recall = total_corr / float(total_label)
            avg_iou = 0
            if total_iou > 0:
                avg_iou = total_iou / total_label
            avg_acc = 0
            if total_label + total_detect - total_corr > 0:
                avg_acc = float(total_corr) / (total_label + total_detect -
                                               total_corr)
            avg_precision = 0
            if total_detect > 0:
                avg_precision = float(total_corr) / total_detect
            total_result = [
                total_label, total_detect, total_corr, avg_recall, avg_iou,
                avg_acc, avg_precision
            ]
            cdl.ExportAnaRes(objtype, cmp_result, total_result, image_path,
                             result_path)
            print("total_label: %d   total_detect: %d   total_corr: %d   recall: %f   average iou: %f   accuracy: %f   precision: %f \n" % \
                (total_result[0],total_result[1],total_result[2],total_result[3],total_result[4],total_result[5],total_result[6]))

            result.append([weights_name] + [objtype] + total_result)
        # dn.free_net(net)
        cdl.ExportAnaResAll(result, result_dir)
        time.sleep(0.001)
コード例 #3
0
def batch_analysis(weights_list_file, image_list_file, thresh, iou_thresh,
                   result_dir):
    image_list = LoadFileList(image_list_file)
    image_num = len(image_list)
    weights_list = LoadFileList(weights_list_file)
    result = []
    for weights in weights_list:
        weights_name = getFileName(weights)

        # print('weights_name: ',weights)

        meta_file, cfg_file = getMetaCfgName(weights)
        # meta = dn.load_meta(meta_file)
        # net = dn.load_net(cfg_file,bytes(weights,'utf-8'),0)

        # 选择对应的dn
        meta = dn.load_meta(meta_file)
        net = dn.load_net(cfg_file, bytes(weights, 'utf-8'), 0)

        object_type = [
            meta.names[i].decode('utf-8').strip() for i in range(meta.classes)
        ]

        result_path = os.path.join(result_dir, weights_name)
        if not os.path.exists(result_path):
            os.mkdir(result_path)

        # detect result and save to text
        timeall = 0
        for j, image_path in enumerate(image_list):
            print('detect: ' + str(j + 1) + '/' + str(len(image_list)))
            label_path = imagePath2labelPath(image_path)
            image_name = getFileName(image_path)
            det_save_path = os.path.join(result_path, image_name + '.txt')
            # det = dn.detect_ext(net, meta, bytes(image_path,'utf-8'),thresh)

            # 选择对应的dn
            det, time1 = dn.detect_ext(net, meta, bytes(image_path, 'utf-8'),
                                       thresh)
            timeall = timeall + time1

            # save detection result to text
            saveDetRes(det, det_save_path, object_type)
            time.sleep(0.001)
        print('xxxxxxxxxxx', 'FPS, ', len(image_list) / timeall)
        # dn.free_net(net)

        # campare label and detection result
        for i, objtype in enumerate(object_type):

            # if objtype != 'fr':
            #     continue
            total_label = 0
            total_detect = 0
            total_corr = 0
            total_iou = 0
            cmp_result = []
            det_ = []
            annopath = []

            detall = [['name', 'obj_type', 'score', 0, 0, 0,
                       0]]  # 此处为xywh(中心),应该变为xmin,ymin,xmax,ymax

            imagesetfile = []
            for j, image_path in enumerate(image_list):
                label_path = imagePath2labelPath(image_path)
                image_name = getFileName(image_path)
                imagesetfile.append(image_name)
                img_save_path = os.path.join(result_path, image_name + '.jpg')
                det_save_path = os.path.join(result_path, image_name + '.txt')

                # detpath.append(det_save_path)
                annopath.append(label_path)
                # print(img_save_path)
                label = []
                if os.path.exists(label_path):
                    label = LoadLabel(label_path, object_type)

                # save detection result to text
                det = readDetRes(det_save_path)
                for d in det:
                    if d[0] > len(object_type) - 1:
                        d[0] = ' '
                        continue
                    d[0] = object_type[d[0]]

                for d in det:
                    xmin = float(copy.deepcopy(
                        d[2])) - float(copy.deepcopy(d[4])) / 2.0
                    ymin = float(copy.deepcopy(
                        d[3])) - float(copy.deepcopy(d[5])) / 2.0
                    xmax = float(copy.deepcopy(
                        d[2])) + float(copy.deepcopy(d[4])) / 2.0
                    ymax = float(copy.deepcopy(
                        d[3])) + float(copy.deepcopy(d[5])) / 2.0
                    # 该文件格式:imagename1 type confidence xmin ymin xmax ymax
                    d_ = [image_name, d[0], d[1], xmin, ymin, xmax, ymax]
                    det_.append(d_)

                if len(det_) != 0:
                    detall = numpy.vstack((detall, det_))
                det_ = []

                if i > 0:
                    image_path = img_save_path
                # print(j,image_path)
                img = cv2.imread(image_path)
                if img is None:
                    print(
                        "load image error&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&"
                    )
                    continue

                cmp_res = cdl.cmp_data(objtype, det, label, thresh, iou_thresh,
                                       img)

                cmp_res.update_tracking({'image_name': image_name})
                total_corr += cmp_res['correct']
                total_iou += cmp_res['avg_iou'] * cmp_res['label_num']

                cmp_result.append(cmp_res)
                print("%s: %d/%d  label: %d   detect: %d   correct: %d   recall: %f   avg_iou: %f   accuracy: %f   precision: %f\n" % \
                    (str(objtype),j+1,image_num,cmp_res['label_num'],cmp_res['detect_num'],\
                        cmp_res['correct'],cmp_res['recall'],cmp_res['avg_iou'],\
                            cmp_res['accuracy'],cmp_res['precision']))
                total_label += cmp_res['label_num']
                total_detect += cmp_res['detect_num']
                cv2.imwrite(img_save_path, img)
                img = []
                time.sleep(0.001)

            # 求出AP值
            ap = 0
            # detall = numpy.delete(detall, 0, axis = 0)
            # det_objtype = [obj for obj in detall if obj[1] == objtype]
            # if len(det_objtype) == 0:
            #     ap = 0
            # else:
            #     ap = voc_eval(det_objtype, annopath, imagesetfile, objtype, iou_thresh)
            # detall=[]

            #数据集分析结果
            avg_recall = 0
            if total_label > 0:
                avg_recall = total_corr / float(total_label)
            avg_iou = 0
            if total_iou > 0:
                avg_iou = total_iou / total_label
            avg_acc = 0
            if total_label + total_detect - total_corr > 0:
                avg_acc = float(total_corr) / (total_label + total_detect -
                                               total_corr)
            avg_precision = 0
            if total_detect > 0:
                avg_precision = float(total_corr) / total_detect
            total_result = [
                total_label, total_detect, total_corr, avg_recall, avg_iou,
                avg_acc, avg_precision
            ]
            cdl.ExportAnaRes(objtype, cmp_result, total_result, image_path,
                             result_path)
            print("total_label: %d   total_detect: %d   total_corr: %d   recall: %f   average iou: %f   accuracy: %f   precision: %f ap: %f\n" % \
                (total_result[0],total_result[1],total_result[2],total_result[3],total_result[4],total_result[5],total_result[6],ap))

            result.append([weights_name] + [objtype] + total_result +
                          [float(ap)])
        cdl.ExportAnaResAll(result, result_dir)
        time.sleep(0.001)
コード例 #4
0
def batch_analysis(meta_file, cfg_file, wgt_file, thresh, nms, src_path,
                   dst_path):

    image_list = listdir(src_path)
    image_list.sort()
    image_num = len(image_list)
    meta = dn.load_meta(meta_file)
    object_type = [
        meta.names[i].decode('utf-8').strip() for i in range(meta.classes)
    ]
    net = dn.load_net(cfg_file, wgt_file, 0)
    move_count = 0
    boxes_last = []

    for j, image_path in enumerate(image_list):

        print(str(j) + '/' + str(image_num) + "  moved: " + str(move_count))
        # print(image_path)

        try:
            img = cv2.imread(image_path)
        except:
            print(
                'can not read image******************************************')
            continue
        h, w = img.shape[:2]
        image_name = getFileName(image_path)
        print("image_name", image_name)
        image_name = image_name.replace('(', '1_')
        image_name = image_name.replace(')', '_1')
        img_save_path = os.path.join(dst_path, image_name + '.jpg')
        # print(img_save_path)
        det = dn.detect_ext(net, meta, bytes(image_path, 'utf-8'), thresh)
        boxes = []
        is_move_file = False

        if j % 20 == 0:  #20数值越大 比对iou的间隔越大
            is_move_file = True

        for d in det:
            # try:
            #     img = cv2.imread(image_path)
            # except:
            #     print('can not read image******************************************')
            #     continue
            # h,w = img.shape[:2]
            print("d", d)
            boxes.append(d[2:])
            bw = d[4] * w
            bh = d[5] * h


#            if bw < 20 or bh < 20:
#                print("bw or bh is less than 20")
#                continue
#            obj_type = d[0]
#            if obj_type == 'tricycle':
#                print("tricycle ************************************************")
#                is_move_file = True
#                break
#            elif obj_type == 'car':
#                if bw*bh/(w*h) > 0.25:
#                    print("big car ....................................................")
#                    is_move_file = True
#                    break
        if boxes_last != [] and boxes != []:
            iou = batch_iou(boxes_last, boxes, w, h)
            # print('iou: '+str(iou))
            if iou > 0.6:
                print('batch iou: ' + str(iou))
                is_move_file = False
                print("iou^^^^^^^^^^^^^^^^^^^^^^^^^")
                # continue
        if is_move_file:
            move_count += 1
            if not os.path.exists(img_save_path):
                mymovefile(image_path, img_save_path)
            boxes_last = boxes
    dn.free_net(net)