Пример #1
0
class WsWarning(object):
    def __init__(self,con,loop=None):
        ''':param web.rest.base.Connection con: the base http connection
        '''
        self.conn = con
        conf = Config()
        self.ws = None
        self.cal = Calibration()
        self.url = conf.get_rest_base()+ "/warning"
        self.ws_url = conf.get_ws_base()+ "/warn"
        self.configs = []
        if loop is not None:
            self.loop = loop
        else:
            self.loop = asyncio.new_event_loop()

        self.__current = {}

    def get_configs(self):
        if len(self.configs) == 0:
            try:
                pkt = self.conn.get(self.url)
                if pkt['status'] == 'OK':
                    for c in pkt['configs']:
                        u = self.url + "/"+ str(c['id'])
                        pkt_c = self.conn.get(u)
                        if pkt_c['status'] == 'OK':
                            c = {'id':c['id'],'name':pkt_c['name'],'config':[]}
                            for w in pkt_c['data']:
                                pol = w['pol']
                                hk  = w['warn']
                                c['config'].append({
                                    'pol'   : pol,
                                    'hk'    : hk,
                                    'min_a' : self.cal.calibrate(pol,hk,w['min_a']),
                                    'min_w' : self.cal.calibrate(pol,hk,w['min_w']),
                                    'max_w' : self.cal.calibrate(pol,hk,w['max_w']),
                                    'max_a' : self.cal.calibrate(pol,hk,w['max_a'])
                                })
                            self.configs.append(c)
                        else:
                            warnings.warn('error while loading configuration',RuntimeWarning)
                else:
                    warnings.warn('error while loading configurations',RuntimeWarning)
            except Exception as e:
                warnings.warn(str(e),RuntimeWarning)
        return self.configs


    def set_config(self,conf):
        if type(conf) == int: #load conf from id
            self.get_configs()
            for i in self.configs:
                if conf == i['id']:
                    self.__set_config(i)
                    break
            else:
                raise RuntimeError('config not found')
        elif type(conf) == str:
            self.get_configs()
            for i in self.configs:
                if conf == i['name']:
                    self.__set_config(i)
                    break
            else:
                raise RuntimeError('config not found')
        elif type(conf) == dict: #set new config
            self.__set_config(conf)
        else:
            raise RuntimeError('Bad Configuration')

    def add_warning(self,pol,hk,min_a,min_w,max_w,max_a):
        warn = {
            'pol'   : pol,
            'warn'  : hk,
            'min_a' : self.cal.reverse(pol,hk,min_a),
            'min_w' : self.cal.reverse(pol,hk,min_w),
            'max_w' : self.cal.reverse(pol,hk,max_w),
            'max_a' : self.cal.reverse(pol,hk,max_a)
        }
        if self.__current.get('config') is None:
            self.__current['config'] = []
        self.__current['config'].append(warn)

        if self.ws is not None:
            self.__add_warning(warn)

    async def connect(self):
        if self.ws is None:
            self.ws = WsBase(self.conn)
            await self.ws.connect(self.ws_url)


    def clear_config(self):
        if self.ws is not None and self.__current.get('config') is not None:
            for c in self.__current['config']:
                pkt = {'pol':c['pol'],'remove':c['warn']}
                self.loop.call_soon_threadsafe(asyncio.async,self.ws.send(pkt))
        self.__current = {}


    def save_config(self,name=None,force_new=False):
        if force_new and self.__current.get('id') is not None:
            del self.__current['id']

        if name is not None:
            self.__current['name'] = name
        pkt = {'save':self.__current.get('name','unnamed_config'),
               'config' : self.__current.get('config',[])
        }
        res = {'status':'ERROR'}
        if self.__current.get('id') is None: #save new config
            res = self.conn.post(self.url,message)
        else: #update existing conf
            url = self.url + "/" + str(self.__current['id'])
            res = self.conn.put(self.url,pkt)
        if res['status'] == 'OK':
            self.__current['id'] = res['id']


    async def recv(self):
        ''' waits for warning packet and decodes it from json string
           :return: dictionary of the decoded json.
        '''
        pkt = await self.ws.recv()
        return pkt


    def __set_config(self,conf):
        self.clear_config()
        self.__current = {'config':[]}
        self.__current['id']   = conf.get('id')
        self.__current['name'] = conf.get('name')
        for c in conf['config']:
            pol = c['pol']
            hk  = c['hk']
            warn = {
                'pol'   : pol,
                'warn'  : hk,
                'min_a' : self.cal.reverse(pol,hk,c['min_a']),
                'min_w' : self.cal.reverse(pol,hk,c['min_w']),
                'max_w' : self.cal.reverse(pol,hk,c['max_w']),
                'max_a' : self.cal.reverse(pol,hk,c['max_a'])
            }
            self.__current['config'].append(warn)
            if self.ws is not None:
                self.__add_warning(warn)


    def __add_warning(self,warn):
        self.loop.call_soon_threadsafe(asyncio.async,self.ws.send(warn))
def main():
    parser = argparse.ArgumentParser('Calibrate camera from image files')
    parser.add_argument('--config', '-c', type=str, required=True)
    parser.add_argument('--priors',
                        '-p',
                        type=str,
                        default='assets/vehicle_keypoint_priors.yaml')
    parser.add_argument('--input_dir',
                        '-i',
                        type=str,
                        required=True,
                        help='path to image directory')
    parser.add_argument(
        '--object_checkpoint',
        type=str,
        default=
        '/home/yuthon/Workspace/Yet-Another-EfficientDet-Pytorch/weights',
        help='checkpoint directory for object detector')
    parser.add_argument(
        '--keypoint_checkpoint',
        type=str,
        default=
        '/home/yuthon/Workspace/Vehicle_Key_Point_Orientation_Estimation/'
        'checkpoints/stage2/best_fine_kp_checkpoint.pth.tar',
        help='checkpoint path for keypoint detector')
    parser.add_argument(
        '--keypoint_mean_std',
        type=str,
        default=
        '/home/yuthon/Workspace/Vehicle_Key_Point_Orientation_Estimation/'
        'data/VeRi/mean.pth.tar',
        help='mean-std path for keypoint datasets')
    parser.add_argument('--visualize', action='store_true')
    opt = parser.parse_args()

    with open(opt.config) as f:
        config = yaml.load(f, Loader=yaml.FullLoader)
    with open(opt.priors) as f:
        priors = yaml.load(f, Loader=yaml.FullLoader)['vehicle_keypoints']

    print('Initializing')
    object_detector = ObjectDetector(checkpoint_dir=opt.object_checkpoint)
    keypoint_detector = KeypointDetector(
        checkpoint_path=opt.keypoint_checkpoint,
        mean_std_path=opt.keypoint_mean_std)
    calibrator = Calibration(camera_matrix=np.array(
        config['camera']['intrinsic']),
                             dist_coeff=config['camera']['distortion'],
                             priors=priors)

    print('Detecting')
    image_files = glob.glob(os.path.join(opt.input_dir, '*.bmp'))
    calib_candidates = []
    for image_idx, image_file in enumerate(tqdm(image_files)):
        # if image_idx >= 10:
        #     break
        # detection
        image = cv2.imread(image_file)
        detections = object_detector.detect(frame=image, visualize=False)
        image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        keypoints, orientations = keypoint_detector.detect(image_rgb,
                                                           detections[0],
                                                           visualize=False)
        # visualization
        if opt.visualize:
            result = image.copy()
            for box_idx in range(keypoints.shape[0]):
                result = visualize_bbox(
                    image=result,
                    roi=detections[0]['rois'][box_idx],
                    class_id=detections[0]['class_ids'][box_idx],
                    score=detections[0]['scores'][box_idx],
                    obj_list=object_detector.obj_list)
                for kp_idx in range(keypoints.shape[1]):
                    result = visualize_keypoint(
                        image=result,
                        coord=keypoints[box_idx][kp_idx],
                        kp_idx=kp_idx,
                        orientation=orientations[box_idx])
            cv2.imshow('detection result', image_resize(result, width=1280))
            cv2.waitKey(0)
        # calibration
        calibs = calibrator.calibrate(keypoints.cpu().numpy(),
                                      orientations.cpu().numpy())
        if calibs.shape[0] != 0:
            calib_candidates.append(calibs)

    print('Averaging')
    calib_candidates = np.concatenate(calib_candidates, axis=0)
    calib_est = calibrator.filter_and_average(calib_candidates)
    print(calib_est)

    with open('calib.pkl', 'wb') as f:
        pickle.dump({
            'candidates': calib_candidates,
            'estimation': calib_est
        }, f)
Пример #3
0
import os
import glob
import matplotlib.pyplot as plt
import matplotlib.image as mpimg

from moviepy.editor import VideoFileClip

from find_lanes import LineFitter
from calibration import Calibration
from perspective import Transformer
from pipeline import Pipeline

calibration = Calibration()
calibration.calibrate('camera_cal')

transform = Transformer()
line_fitter = LineFitter()

pipeline = Pipeline(calibration,transform,line_fitter)

# test_images = 'test_images/test6.jpg'
# for img_name in glob.glob(test_images):
#     img = mpimg.imread(img_name)
#
#     result = pipeline.process_image(img)
#
#     plt.imshow(result)
#     plt.show()

output_videos_folder = 'output_videos'
if not os.path.exists(output_videos_folder):
Пример #4
0
class WsWarning(object):
    def __init__(self, con, loop=None):
        """:param web.rest.base.Connection con: the base http connection"""
        self.conn = con
        conf = Config()
        self.ws = None
        self.cal = Calibration()
        self.url = conf.get_rest_base() + "/warning"
        self.ws_url = conf.get_ws_base() + "/warn"
        self.configs = []
        if loop is not None:
            self.loop = loop
        else:
            self.loop = asyncio.new_event_loop()

        self.__current = {}

    def get_configs(self):
        if len(self.configs) == 0:
            try:
                pkt = self.conn.get(self.url)
                if pkt["status"] == "OK":
                    for c in pkt["configs"]:
                        u = self.url + "/" + str(c["id"])
                        pkt_c = self.conn.get(u)
                        if pkt_c["status"] == "OK":
                            c = {
                                "id": c["id"],
                                "name": pkt_c["name"],
                                "config": []
                            }
                            for w in pkt_c["data"]:
                                pol = w["pol"]
                                hk = w["warn"]
                                c["config"].append({
                                    "pol":
                                    pol,
                                    "hk":
                                    hk,
                                    "min_a":
                                    self.cal.calibrate(pol, hk, w["min_a"]),
                                    "min_w":
                                    self.cal.calibrate(pol, hk, w["min_w"]),
                                    "max_w":
                                    self.cal.calibrate(pol, hk, w["max_w"]),
                                    "max_a":
                                    self.cal.calibrate(pol, hk, w["max_a"]),
                                })
                            self.configs.append(c)
                        else:
                            warnings.warn("error while loading configuration",
                                          RuntimeWarning)
                else:
                    warnings.warn("error while loading configurations",
                                  RuntimeWarning)
            except Exception as e:
                warnings.warn(str(e), RuntimeWarning)
        return self.configs

    def set_config(self, conf):
        if type(conf) == int:  # load conf from id
            self.get_configs()
            for i in self.configs:
                if conf == i["id"]:
                    self.__set_config(i)
                    break
            else:
                raise RuntimeError("config not found")
        elif type(conf) == str:
            self.get_configs()
            for i in self.configs:
                if conf == i["name"]:
                    self.__set_config(i)
                    break
            else:
                raise RuntimeError("config not found")
        elif type(conf) == dict:  # set new config
            self.__set_config(conf)
        else:
            raise RuntimeError("Bad Configuration")

    def add_warning(self, pol, hk, min_a, min_w, max_w, max_a):
        warn = {
            "pol": pol,
            "warn": hk,
            "min_a": self.cal.reverse(pol, hk, min_a),
            "min_w": self.cal.reverse(pol, hk, min_w),
            "max_w": self.cal.reverse(pol, hk, max_w),
            "max_a": self.cal.reverse(pol, hk, max_a),
        }
        if self.__current.get("config") is None:
            self.__current["config"] = []
        self.__current["config"].append(warn)

        if self.ws is not None:
            self.__add_warning(warn)

    async def connect(self):
        if self.ws is None:
            self.ws = WsBase(self.conn)
            await self.ws.connect(self.ws_url)

    def clear_config(self):
        if self.ws is not None and self.__current.get("config") is not None:
            for c in self.__current["config"]:
                pkt = {"pol": c["pol"], "remove": c["warn"]}
                self.loop.call_soon_threadsafe(getattr(asyncio, "async"),
                                               self.ws.send(pkt))
        self.__current = {}

    def save_config(self, name=None, force_new=False):
        if force_new and self.__current.get("id") is not None:
            del self.__current["id"]

        if name is not None:
            self.__current["name"] = name
        pkt = {
            "save": self.__current.get("name", "unnamed_config"),
            "config": self.__current.get("config", []),
        }
        res = {"status": "ERROR"}
        if self.__current.get("id") is None:  # save new config
            res = self.conn.post(self.url, pkt)
        else:  # update existing conf
            url = self.url + "/" + str(self.__current["id"])
            res = self.conn.put(url, pkt)
        if res["status"] == "OK":
            self.__current["id"] = res["id"]

    async def recv(self):
        """waits for warning packet and decodes it from json string
        :return: dictionary of the decoded json.
        """
        pkt = await self.ws.recv()
        return pkt

    def __set_config(self, conf):
        self.clear_config()
        self.__current = {"config": []}
        self.__current["id"] = conf.get("id")
        self.__current["name"] = conf.get("name")
        for c in conf["config"]:
            pol = c["pol"]
            hk = c["hk"]
            warn = {
                "pol": pol,
                "warn": hk,
                "min_a": self.cal.reverse(pol, hk, c["min_a"]),
                "min_w": self.cal.reverse(pol, hk, c["min_w"]),
                "max_w": self.cal.reverse(pol, hk, c["max_w"]),
                "max_a": self.cal.reverse(pol, hk, c["max_a"]),
            }
            self.__current["config"].append(warn)
            if self.ws is not None:
                self.__add_warning(warn)

    def __add_warning(self, warn):
        self.loop.call_soon_threadsafe(getattr(asyncio, "async"),
                                       self.ws.send(warn))
Пример #5
0
                    addContact = True
            if event.type == pygame.KEYUP:
                if event.key == K_q:
                    qPress = False

        if qPress:
            buttonHoldTime = time.time() - buttonTimerStart

        if buttonHoldTime > 4:
            buttonHoldTime = 0
            stateString = "CALIBRATE"
            ca.initCalibration()
            calibrationStep = 0

        if stateString == "CALIBRATE":
            xy = ca.calibrate(calibrationStep)

            if calibrationStep == 0:
                cg.initBackground()
                cg.update(xy, ca)
            if calibrationStep == 500:
                stateString = "TRACK"
            else:
                cg.update(xy, ca)
                calibrationStep = calibrationStep + 1

        else:

            #process tracker graphics
            args = tg.update(wave_size, addContact, ca)
            if args != 999: