Ejemplo n.º 1
0
def main():
    # we need a QApplication, that runs our QT Gui Framework    
    app = PyuEyeQtApp()
 
    # a basic qt window
    view = PyuEyeQtView()
    view.show()
    view.user_callback = process_image

    # camera class to simplify uEye API access
    cam = Camera()
    cam.init()
    cam.set_colormode(ueye.IS_CM_MONO8)
    print('Color Mode:', cam.get_colormode())

#     print(ueye.IS_CM_BGR8_PACKED)
    cam.set_aoi(0, 0, 1280, 4)
    aoi = cam.get_aoi()
    print('AOI:', aoi.x, aoi.y, aoi.width, aoi.height)
    
    print('Framerate Range:', cam.get_FrameTimeRange()[0], cam.get_FrameTimeRange()[1],cam.get_FrameTimeRange()[2])
    
#     cam.set_fps(10)
    cam.set_fps(1/cam.get_FrameTimeRange()[0])
    
    cam.set_exposure(0.1)
    print('Exposure Time:', cam.get_exposure())
    
#     print(cam.get_colordepth()[0], cam.get_colordepth()[1])
    cam.alloc()
    cam.capture_video()
    
    #a thread that waits for new images and processes all connected views
    thread = FrameThread(cam, view)
#     thread.setDaemon(True)
    thread.start()
    
    
    # cleanup
    app.exit_connect(thread.stop)
    app.exec_()
 
    print('Frame Rate:', cam.get_fps())
    thread.stop()
    thread.join()

    cam.stop_video()
    cam.exit()
Ejemplo n.º 2
0
class uEyePprzlink:
    def __init__(self, verbose=False, output_dir='images'):
        self.new_msg = False
        self.idx = 0
        self.last_msg = None
        self.verbose = verbose
        self.calib = None
        self.output_dir_orig = path.join(output_dir, 'orig')
        self.output_dir_undist = path.join(output_dir, 'undist')
        if not path.exists(self.output_dir_orig):
            makedirs(self.output_dir_orig)
        if not path.exists(self.output_dir_undist):
            makedirs(self.output_dir_undist)

        # camera class to simplify uEye API access
        self.verbose_print("Start uEye interface")

        self.cam = Camera()
        try:
            self.cam.init()
        except uEyeException:
            self.verbose_print("Camera init failed")
            self.cam = None
            return

        check(
            ueye.is_SetExternalTrigger(self.cam.handle(),
                                       ueye.IS_SET_TRIGGER_SOFTWARE))
        self.cam.set_colormode(ueye.IS_CM_BGR8_PACKED)
        self.cam.set_aoi(0, 0, 2048, 2048)

        # pixel clock
        self.verbose_print("Pixel clock")
        self.cam.get_pixel_clock_range(self.verbose)
        self.cam.set_pixel_clock(20)
        self.cam.get_pixel_clock(self.verbose)

        # set expo
        self.verbose_print("Expo")
        self.cam.get_exposure_range(self.verbose)
        self.cam.set_exposure(1.)
        self.cam.get_exposure(self.verbose)

        self.verbose_print("Init done")
        self.buff = self.cam.alloc_single()
        check(ueye.is_SetDisplayMode(self.cam.handle(), ueye.IS_SET_DM_DIB))
        self.verbose_print("Alloc done")

    def stop(self):
        if self.cam is not None:
            self.cam.free_single(self.buff)
            self.verbose_print("Free mem done")
            self.cam.exit()
            self.verbose_print("leaving")

    def set_calib(self, conf_file):
        with open(conf_file, 'r') as f:
            conf = json.load(f)
            self.verbose_print(json.dumps(conf))
            K = np.array(conf['K'])
            D = np.array(conf['D'])
            dim = tuple(conf['dim'])
            map1, map2 = cv2.fisheye.initUndistortRectifyMap(
                K, D, np.eye(3), K, dim, cv2.CV_16SC2)
            self.calib = (map1, map2)
            self.verbose_print("Calib set")

    def verbose_print(self, text):
        if self.verbose:
            print(text)

    def process_msg(self, ac_id, msg):
        self.new_msg = True
        self.idx = int(msg['photo_nr'])
        self.last_msg = msg

    def set_gps_exif(self, file_name, lat, lon, alt):
        """Adds GPS position as EXIF metadata
        file_name -- image file
        lat -- latitude (1e7 deg, as int)
        lon -- longitude (1e7 deg, as int)
        alt -- altitude MSL (in mm, as int)
        """
        def get_loc(value, loc):
            if value < 0:
                return loc[0]
            elif value > 0:
                return loc[1]
            else:
                return ""

        try:
            exiv_lat = (make_fraction(abs(lat), 10000000), make_fraction(0, 1),
                        make_fraction(0, 1))
            exiv_lng = (make_fraction(abs(lon), 10000000), make_fraction(0, 1),
                        make_fraction(0, 1))
            if alt > 0.:
                exiv_alt = make_fraction(alt, 1000)
            else:
                exiv_alt = make_fraction(0, 1)

            exiv_image = pyexiv2.ImageMetadata(file_name)
            exiv_image.read()
            exiv_image["Exif.GPSInfo.GPSLatitude"] = exiv_lat
            exiv_image["Exif.GPSInfo.GPSLatitudeRef"] = get_loc(
                lat, ["S", "N"])
            exiv_image["Exif.GPSInfo.GPSLongitude"] = exiv_lng
            exiv_image["Exif.GPSInfo.GPSLongitudeRef"] = get_loc(
                lon, ["W", "E"])
            exiv_image["Exif.GPSInfo.GPSAltitude"] = exiv_alt
            exiv_image["Exif.GPSInfo.GPSAltitudeRef"] = '0'
            exiv_image["Exif.Image.GPSTag"] = 654
            exiv_image["Exif.GPSInfo.GPSMapDatum"] = "WGS-84"
            exiv_image["Exif.GPSInfo.GPSVersionID"] = '2 0 0 0'
            exiv_image.write()
            self.verbose_print("writing exif done")
        except:
            self.verbose_print("writing exif failed")
            pass

    def process_image(self, image_data, file_type='jpg'):
        # reshape the image data as 1dimensional array
        image = image_data.as_1d_image()
        lat = int(self.last_msg['lat'])
        lon = int(self.last_msg['lon'])
        alt = int(self.last_msg['hmsl'])
        phi = int(self.last_msg['phi'])
        theta = int(self.last_msg['theta'])
        psi = int(self.last_msg['psi'])
        time = int(self.last_msg['itow'])
        image_name = "img_{:04d}_{:d}_{:d}_{:d}_{:d}_{:d}_{:d}_{:d}.jpg".format(
            self.idx, lat, lon, alt, phi, theta, psi, time)
        image_name_full = path.join(self.output_dir_orig, image_name)
        cv2.imwrite(image_name_full, image)
        # also set GPS pos in exif metadata
        self.set_gps_exif(image_name_full, lat, lon, alt)
        self.verbose_print("save image: {}".format(image_name_full))
        if self.calib is not None:
            map1, map2 = self.calib
            undist_img = cv2.remap(image,
                                   map1,
                                   map2,
                                   interpolation=cv2.INTER_LINEAR,
                                   borderMode=cv2.BORDER_CONSTANT)
            undist_name_full = path.join(self.output_dir_undist,
                                         "undist_{}".format(image_name))
            cv2.imwrite(undist_name_full, undist_img)
            self.set_gps_exif(undist_name_full, lat, lon, alt)
            self.verbose_print("save undist: {}".format(undist_name_full))