Beispiel #1
0
    def run(self):
        #cap = cv2.VideoCapture(0)
        detector = Detector()
        fever = Fever()
        image = Image.new('P', (WIDTH, HEIGHT))
        image.putpalette(get_thermal_image_color_palette())
        image = image.resize((WIDTH, HEIGHT))
        ti = BrickletThermalImaging(UID, ipconIMG)

        while True:
            try:
                ti.set_image_transfer_config(
                    ti.IMAGE_TRANSFER_MANUAL_HIGH_CONTRAST_IMAGE)
                constrast_image = ti.get_high_contrast_image()
                image.resize((80, 60))
                image.putdata(constrast_image)
                img = np.asarray(image.convert())
                img, boxes = detector.detect_face(img)

                ti.set_image_transfer_config(
                    ti.IMAGE_TRANSFER_MANUAL_TEMPERATURE_IMAGE)
                temperatures = ti.get_temperature_image()

                img = fever.draw_temperatures(temperatures, boxes, img)

                #cv2.imshow("thermal", img)
                #cv2.waitKey(1)

                h, w, ch = img.shape
                bytesPerLine = ch * w
                convertToQtFormat = QImage(img, w, h, bytesPerLine,
                                           QImage.Format_BGR888)
                p = convertToQtFormat.scaled(640, 480, Qt.IgnoreAspectRatio)
                self.changePixmap.emit(p)
                time.sleep(.25)
            except Exception as ex:
                print(ex)
Beispiel #2
0
def setup_devices():
    ipcon = IPConnection()  # Create IP connection
    stepper = SilentStepper(STEPPER_UID, ipcon)  # Create device object

    # ir
    ti = BrickletThermalImaging(IR_UID, ipcon)  # Create device object

    ipcon.connect(HOST, PORT)  # Connect to brickd

    # setup ir
    ti.set_image_transfer_config(ti.IMAGE_TRANSFER_MANUAL_TEMPERATURE_IMAGE)
    ti.set_resolution(BrickletThermalImaging.RESOLUTION_0_TO_655_KELVIN)
    # Don't use device before ipcon is connected

    stepper.set_motor_current(800)  # 800 mA
    stepper.set_step_configuration(stepper.STEP_RESOLUTION_1,
                                   True)  # 1/8 steps (interpolated)
    stepper.set_max_velocity(2000)  # Velocity 2000 steps/s

    # Slow acceleration (500 steps/s^2),
    # Fast deacceleration (5000 steps/s^2)
    stepper.set_speed_ramping(500, 5000)
    stepper.enable()
    return stepper, ti
Beispiel #3
0
def on_closing(window, exit_queue):
    exit_queue.put(True)


if __name__ == "__main__":
    ipcon = IPConnection()  # Create IP connection
    ti = BrickletThermalImaging(UID, ipcon)  # Create device object

    ipcon.connect(HOST, PORT)  # Connect to brickd
    # Don't use device before ipcon is connected

    # Register illuminance callback to function cb_high_contrast_image
    ti.register_callback(ti.CALLBACK_HIGH_CONTRAST_IMAGE, cb_high_contrast_image)

    # Enable high contrast image transfer for callback
    ti.set_image_transfer_config(ti.IMAGE_TRANSFER_CALLBACK_HIGH_CONTRAST_IMAGE)

    # Create Tk window and label
    window = Tk()

    # Run maximized
    if SCALE == -1:
        window.geometry("%dx%d+0+0" % (window.winfo_screenwidth(), window.winfo_screenheight()))
        window.update()  # Update to resize the window

        w, h = window.winfo_width(), window.winfo_height()
        SCALE = min(w // WIDTH, h // HEIGHT)

    label = Label(window)
    label.pack()
class MainWindow(QMainWindow, Ui_MainWindow):
    qtcb_ipcon_enumerate = pyqtSignal(str, str, 'char', type((0, )), type(
        (0, )), int, int)
    qtcb_ipcon_connected = pyqtSignal(int)

    qtcb_high_contrast_image = pyqtSignal(object)

    image_pixel_width = 1
    crosshair_width = 1

    def __init__(self, parent=None):
        QMainWindow.__init__(self, parent)

        self.setupUi(self)
        self.setWindowTitle(
            'Tinkerforge CES Demo: Thermal Imaging Bricklet by FLIR')

        self.qtcb_ipcon_enumerate.connect(self.cb_ipcon_enumerate)
        self.qtcb_ipcon_connected.connect(self.cb_ipcon_connected)
        self.qtcb_high_contrast_image.connect(self.cb_high_contrast_image)

        self.image = QImage(QSize(80, 60), QImage.Format_RGB32)

        # Add Tinkerforge logo
        # Adapt this path
        self.label_logo.setPixmap(
            QPixmap('/home/pi/Desktop/demo/logo_klein.png'))

        # create and setup ipcon
        self.ipcon = IPConnection()
        self.ipcon.connect(HOST, PORT)
        self.ipcon.register_callback(IPConnection.CALLBACK_ENUMERATE,
                                     self.qtcb_ipcon_enumerate.emit)
        self.ipcon.register_callback(IPConnection.CALLBACK_CONNECTED,
                                     self.qtcb_ipcon_connected.emit)

        self.ti = BrickletThermalImaging(UID_TI, self.ipcon)
        self.ti.register_callback(self.ti.CALLBACK_HIGH_CONTRAST_IMAGE,
                                  self.qtcb_high_contrast_image.emit)
        self.ti.set_image_transfer_config(
            self.ti.IMAGE_TRANSFER_CALLBACK_HIGH_CONTRAST_IMAGE)

        self.ti.set_spotmeter_config([35, 25, 45, 35])
        #print(ti.get_spotmeter_config())

        al = BrickletAmbientLightV2(UID_AL, self.ipcon)
        al.register_callback(al.CALLBACK_ILLUMINANCE, self.cb_illuminance)
        al.set_illuminance_callback_period(500)

        hu = BrickletHumidityV2(UID_HU, self.ipcon)
        hu.register_callback(hu.CALLBACK_HUMIDITY, self.cb_humidity)
        hu.set_humidity_callback_configuration(500, False, "x", 0, 0)

        bm = BrickletBarometer(UID_BM, self.ipcon)
        bm.register_callback(bm.CALLBACK_AIR_PRESSURE, self.cb_air_pressure)
        bm.set_air_pressure_callback_period(500)

        self.rgb_lookup = []
        for x in range(256):
            x /= 255.0
            r = int(round(255 * math.sqrt(x)))
            g = int(round(255 * pow(x, 3)))
            if math.sin(2 * math.pi * x) >= 0:
                b = int(round(255 * math.sin(2 * math.pi * x)))
            else:
                b = 0
            self.rgb_lookup.append((r, g, b))

        def paintEvent(event):
            painter = QPainter(self.label_image)

            painter.setRenderHint(QPainter.SmoothPixmapTransform, True)

            w = self.label_image.size().width()
            h = self.label_image.size().height()

            painter.scale(w / 80.0, h / 60.0)
            painter.drawImage(0, 0, self.image)

            if 35 != None and 45 != None:
                pen = QPen()
                pen.setColor(Qt.white)
                pen.setWidth(0.2)
                painter.setPen(pen)

                from_x, from_y, to_x, to_y = [35, 25, 45, 35]

                from_x = from_x * self.image_pixel_width + 1
                from_y = from_y * self.image_pixel_width + 1
                to_x = to_x * self.image_pixel_width + 1
                to_y = to_y * self.image_pixel_width + 1

                cross_x = from_x + (to_x - from_x) / 2.0
                cross_y = from_y + (to_y - from_y) / 2.0

                if to_x - from_x > 5 or to_y - from_y > 5:
                    lines = [
                        QLine(from_x, from_y, from_x + self.crosshair_width,
                              from_y),
                        QLine(from_x, from_y, from_x,
                              from_y + self.crosshair_width),
                        QLine(to_x, to_y, to_x, to_y - self.crosshair_width),
                        QLine(to_x, to_y, to_x - self.crosshair_width, to_y),
                        QLine(from_x, to_y, from_x,
                              to_y - self.crosshair_width),
                        QLine(from_x, to_y, from_x + self.crosshair_width,
                              to_y),
                        QLine(to_x, from_y, to_x,
                              from_y + self.crosshair_width),
                        QLine(to_x, from_y, to_x - self.crosshair_width,
                              from_y)
                    ]
                    painter.drawLines(lines)

                lines = [
                    QLine(cross_x - self.crosshair_width, cross_y,
                          cross_x + self.crosshair_width, cross_y),
                    QLine(cross_x, cross_y - self.crosshair_width, cross_x,
                          cross_y + self.crosshair_width)
                ]
                painter.drawLines(lines)

            self.update_spotmeter_roi_label()

        self.label_image.paintEvent = paintEvent

    def update_spotmeter_roi_label(self):
        from_x, from_y, to_x, to_y = self.ti.get_spotmeter_config()
        self.spotmeter_roi_label.setText(
            'from <b>[{0}, {1}]</b> to <b>[{2}, {3}]</b>'.format(
                from_x, from_y, to_x, to_y))

    def cb_ipcon_enumerate(self, uid, connected_uid, position,
                           hardware_version, firmware_version,
                           device_identifier, enumeration_type):
        if self.ipcon.get_connection_state(
        ) != IPConnection.CONNECTION_STATE_CONNECTED:
            # ignore enumerate callbacks that arrived after the connection got closed
            return

    def cb_ipcon_connected(self, connect_reason):
        try:
            self.ipcon.enumerate()
        except:
            pass

    def cb_high_contrast_image(self, image):
        if image != None:
            self.new_image(image)

        print("New image")
        self.statistics(self.ti.get_statistics())

    def statistics(self, data):
        self.valid_resolution = data.resolution
        spot_mean = self.kelvin_to_degstr(data.spotmeter_statistics[0])
        spot_max = self.kelvin_to_degstr(data.spotmeter_statistics[1])
        spot_min = self.kelvin_to_degstr(data.spotmeter_statistics[2])
        spot_pix = str(data.spotmeter_statistics[3])
        self.spotmeter_mean_label.setText(spot_mean)
        self.spotmeter_minimum_label.setText(spot_min)
        self.spotmeter_maximum_label.setText(spot_max)
        self.spotmeter_pixel_count_label.setText(spot_pix)

        #temp_fpa = self.kelvin_to_degstr(data.temperatures[0], 1)
        #temp_fpa_ffc = self.kelvin_to_degstr(data.temperatures[1], 1)
        #temp_housing = self.kelvin_to_degstr(data.temperatures[2], 1)
        #temp_housing_ffc = self.kelvin_to_degstr(data.temperatures[3], 1)
        #self.temp_fpa_label.setText(temp_fpa)
        #self.temp_fpa_ffc_label.setText(temp_fpa_ffc)
        #self.temp_housing_label.setText(temp_housing)
        #self.temp_housing_ffc_label.setText(temp_housing_ffc)

        #sheet_green  = "QLabel { color: green; }"
        #sheet_orange = "QLabel { color: orange; }"
        #sheet_red    = "QLabel { color: red; }"

        #if data.ffc_status == 0b00:
        #    self.ffc_state_label.setStyleSheet(sheet_orange)
        #    self.ffc_state_label.setText('Never Commanded')
        #elif data.ffc_status == 0b01:
        #    self.ffc_state_label.setStyleSheet(sheet_orange)
        #    self.ffc_state_label.setText('Imminent')
        #elif data.ffc_status == 0b10:
        #    self.ffc_state_label.setStyleSheet(sheet_orange)
        #    self.ffc_state_label.setText('In Progress')
        #elif data.ffc_status == 0b11:
        #    self.ffc_state_label.setStyleSheet(sheet_green)
        #    self.ffc_state_label.setText('Complete')

        #if data.temperature_warning[0]:
        #    self.shutter_lockout_label.setStyleSheet(sheet_red)
        #    self.shutter_lockout_label.setText('Yes')
        #else:
        #    self.shutter_lockout_label.setStyleSheet(sheet_green)
        #    self.shutter_lockout_label.setText('No')

        #if data.temperature_warning[1]:
        #    self.overtemp_label.setStyleSheet(sheet_red)
        #    self.overtemp_label.setText('Yes')
        #else:
        #    self.overtemp_label.setStyleSheet(sheet_green)
        #    self.overtemp_label.setText('No')

    def cb_humidity(self, humidity):
        #print("Humidity: " + str(humidity/100.0) + " %RH")
        self.label_humidity.setText(str(humidity / 100) + " %RH")

    def cb_illuminance(self, illuminance):
        #print("Illuminance: " + str(illuminance/100.0) + " Lux")
        self.label_brightness.setText(str(illuminance / 100) + " Lux")

    def cb_air_pressure(self, air_pressure):
        #print("Air Pressure: " + str(air_pressure/1000.0) + " mbar")
        self.label_airpressure.setText(str(air_pressure / 1000) + " mbar")

    def new_image(self, image):

        for i, value in enumerate(image):
            r, g, b = self.rgb_lookup[value]
            self.image.setPixel(QPoint(i % 80, i // 80),
                                (r << 16) | (g << 8) | b)
        self.label_image.update()
        #self.label_image.setPixmap(QPixmap(self.image))

    def kelvin_to_degstr(self, value, res=None):
        if res == None:
            res = self.valid_resolution
        if res == 0:
            return "{0:.2f}".format(value / 10.0 - 273.15)
        else:
            return "{0:.2f}".format(value / 100.0 - 273.15)
    def super_loop(self):
        global ultimaFecha, images_queues, temperatures_queues, final_images, logging

        images_queues = LifoQueue()
        temperatures_queues = LifoQueue()

        ipconn = IPConnection()
        ipconn.connect(HOST, PORT)

        ti = BrickletThermalImaging(UID, ipconn)
        ti.set_resolution(ti.RESOLUTION_0_TO_655_KELVIN)
        ti.register_callback(ti.CALLBACK_HIGH_CONTRAST_IMAGE,
                             callback_image_constrant)
        ti.register_callback(ti.CALLBACK_TEMPERATURE_IMAGE,
                             callback_image_temperature)

        indexImage = 0

        while True:
            try:
                ultimaFecha = datetime.datetime.now()
                ti.set_image_transfer_config(
                    ti.IMAGE_TRANSFER_CALLBACK_HIGH_CONTRAST_IMAGE)
                image_queue = images_queues.get(True)
                #logging.debug(image_queue)
                ti.set_image_transfer_config(
                    ti.IMAGE_TRANSFER_CALLBACK_TEMPERATURE_IMAGE)
                temperatures = temperatures_queues.get(True)
                #logging.debug(temperatures)
                ti.set_image_transfer_config(
                    ti.IMAGE_TRANSFER_MANUAL_HIGH_CONTRAST_IMAGE)

                with images_queues.mutex as iq, temperatures_queues.mutex as tq:
                    images_queues.queue.clear()
                    temperatures_queues.queue.clear()

                if image_queue is not None:
                    image.putdata(image_queue)
                    img = np.asarray(image.convert())
                    img = cv2.rotate(img, cv2.ROTATE_180)
                    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
                    #cv2.imwrite("data/"+str(indexImage)+"_a.png", img)
                    img = cv2.flip(img, 1)
                    #cv2.imwrite("data/" + str(indexImage) + "_b.png", img)
                    #indexImage += 1

                    positions = detector.detect_face(img)
                    if len(positions) > 0:
                        img = fever.draw_temperatures(temperatures, positions,
                                                      img)
                    else:
                        img = cv2.resize(img, (800, 600))

                    img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)

                    final_images.put(img)

            except Exception as ex:
                print(ex)
            finally:
                # logging.debug("Main: Ventana actualizada")
                pass
Beispiel #6
0
    for x in range(256):
        x /= 255.0
        palette.append(int(round(255 * math.sqrt(x))))  # RED
        palette.append(int(round(255 * pow(x, 3))))  # GREEN
        if math.sin(2 * math.pi * x) >= 0:
            palette.append(int(round(255 * math.sin(2 * math.pi * x))))  # BLUE
        else:
            palette.append(0)

    return palette


fps = 0
count = 0

ti.set_image_transfer_config(ti.IMAGE_TRANSFER_MANUAL_HIGH_CONTRAST_IMAGE)
time.sleep(0.5)

start = time.time()
while True:

    #request termalimage
    image_data = ti.get_high_contrast_image()

    # Display Thermal Image
    image = Image.new('P', (80, 60))
    image.putdata(image_data)
    image.putpalette(get_thermal_image_color_palette())
    #print(numpy.array(image))
    image = image.resize((80 * 8, 60 * 8), Image.ANTIALIAS)
    image = ImageOps.flip(image)