Пример #1
0
def map_time_step(img_path,
                  rpm=19.6,
                  field=14,
                  diameter=70,
                  suffix=".bmp",
                  save_path=None):

    img_list = sorted(gb.glob(img_path + r"/*" + suffix), key=os.path.getmtime)
    pvoc = PascalVocXmlParser()

    # Get the linear velocity of the rotating cloth
    perimeter = np.pi * diameter
    min_velo = rpm * perimeter
    speed = min_velo / 60.0

    for i in range(1, len(img_list), 1):
        img_file = img_list[i]
        print(img_file)
        ann_file = get_ann_file(img_file)
        if os.path.isfile(ann_file):
            boxes = pvoc.get_boxes(ann_file)
        else:
            boxes = []

        pre_img_file = img_list[i - 1]
        pre_ann_file = get_ann_file(pre_img_file)
        if os.path.isfile(pre_ann_file):
            pre_boxes = pvoc.get_boxes(pre_ann_file)
        else:
            pre_boxes = []

        img = cv2.imread(img_file, cv2.IMREAD_COLOR)
        img_h, img_w = img.shape[:2]
        intv = get_time_interval(img_file, pre_img_file)
        nboxes = map_next_boxes(pre_boxes, speed, intv, field, (img_h, img_w))

        #print(boxes)
        #print(nboxes)
        img = draw_boxes(img, boxes, color=(0, 0, 255))
        img = draw_boxes(img, nboxes, color=(255, 0, 0))

        if save_path is not None:
            _, filename = os.path.split(img_file)
            cv2.imwrite(os.path.join(save_path, filename), img)
        else:
            cv2.imshow("image", img)
            cv2.waitKey(0)
Пример #2
0
import os, sys
import numpy as np
import glob as gb
import xml.etree.ElementTree as ET
from sklearn.cluster import KMeans
from matplotlib import pyplot as plt
from PascalVocParser import PascalVocXmlParser

pvoc = PascalVocXmlParser()


def show_histogram(path):
    dataset = encode_dataset(path)
    widths = dataset[:, 0]
    heights = dataset[:, 1]
    plt.subplot(1, 2, 1), plt.hist(widths), plt.title("Width Distribution")
    plt.subplot(1, 2, 2), plt.hist(heights), plt.title("Heights Distribution")
    plt.show()


def cluster_anchors(path, k=6, label=None):
    dataset = encode_dataset(path, label=label)
    km = KMeans(n_clusters=k, random_state=9).fit(dataset)
    ac = np.array(km.cluster_centers_, dtype=np.int32)

    return ac


def encode_dataset(path, label=None):
    dataset = []  # dataset that x_length larger than x_thres
    for xml_file in gb.glob(path + r"/*.xml"):
Пример #3
0
 def __init__(self, params):
     self.pvoc = PascalVocXmlParser()
     self.updateParams(params)
Пример #4
0
class CheckLabels(object):
    def __init__(self, params):
        self.pvoc = PascalVocXmlParser()
        self.updateParams(params)
        
    def updateParams(self, params):
        self.format = params['format']
        self.is_alert = params['is_alert']
        self.collection = params['collection']
        self.params = params
        self.matrix = {}
        self.abnormal_matrix = {}
        
    def __call__(self, input):
        if os.path.isfile(input):
            _, suffix = os.path.splitext(input)
            if suffix[1:] == 'json':
                self.checkJson(input)
            elif suffix[1:] == 'xml':
                self.checkXml(input)
            else:
                raise ValueError('Unsupported input format.')
        elif os.path.exists(input):
            self.checkDir(input)
        else:
            raise ValueError('Invalid input.')
        
    def checkDir(self, dir):
        if self.format.lower() in ['labelme', 'json']:
            file_list = gb.glob(dir + r'/*.json')
            for json_file in file_list: 
                self.checkJson(json_file, disp=False)
                
        elif self.format.lower() in ['labelimg', 'xml', 'pascalvoc', 'voc']:
            file_list = gb.glob(dir + r'/*.xml')
            for xml_file in file_list:
                self.checkXml(xml_file, disp=False)
        
        else:
            raise ValueError('Invalid format.')
        
        self._display()
               
    def checkJson(self, json_file, disp=True):
        print('Processing file', json_file, '...')
        with open(json_file, "r", encoding="utf-8") as f:
            js_obj = json.load(f)
            for elem in js_obj['shapes']:
                self._register(elem['label'])
                if self.is_alert: 
                    self._register_abnormal(elem['label'], json_file)
            f.close()
        
        if disp: self._display()
     
    def checkXml(self, xml_file, disp=True):  
        print('Processing file', xml_file, '...')
        labels = self.pvoc.get_labels(xml_file)
        for label in labels:
            self._register(label)
            if self.is_alert: 
                self._register_abnormal(label, xml_file)
            
        if disp: self._display()
        
    def _register(self, label):
        if label not in self.matrix:
            self.matrix[label] = 1
        else:
            self.matrix[label] += 1
            
    def _register_abnormal(self, label, ann_file):
        if label in self.collection: return
        
        _, filename = os.path.split(ann_file)
        if label not in self.abnormal_matrix:
            self.abnormal_matrix[label] = [filename]
        else:
            self.abnormal_matrix[label].append(filename)
        
    def _display(self):
        print('\nDisplaying the results:\n')
        for label in self.matrix:
            print(label + ': ' + str(self.matrix[label]))
        
        if not self.is_alert: return
        for ab_label in self.abnormal_matrix:
            print('Abnormal label: ' + ab_label + '\n')
            for filename in self.abnormal_matrix[ab_label]:
                print('  ' + filename + '\n')