Exemplo n.º 1
0
def get_sensor_last_image(id):
    processor = ImageProcessor()

    print(f'requesting last image with id={id}')
    scaled_up = request.args.get('scale_up')
    scaled_up = 1 if scaled_up is None else int(scaled_up)

    interpolate = request.args.get('interpolate')
    interpolate = True if interpolate == "1" else False

    simulated = request.args.get('simulate')
    simulated = True if simulated == "1" else False

    if simulated:
        last_result = Measurement_test.query.filter(
            Measurement_test.sensor_id == id).order_by(
                Measurement_test.timestamp.desc()).first()
    else:
        last_result = Measurement.query.filter(
            Measurement.sensor_id == int(id)).order_by(
                Measurement.timestamp.desc()).first()
    #get processed frame
    processor.process(last_result.data)
    cv2_data = processor.plot_frame()
    img = fast_thermal_image(cv2_data, scale=scaled_up)

    return Response(img)
Exemplo n.º 2
0
def save_video_frames(sensor_id, measurements, FPS=28.84):
    start_time = measurements[0].timestamp
    end_time = measurements[-1].timestamp
    cur_time = start_time

    pros = ImageProcessor()

    margin = 10

    for index, meas in enumerate(measurements):
        comp = Image.new('RGB', (320 * 5 + margin * 4, 280),
                         color=(255, 255, 255))
        pros.set_thermal_data(meas.data)

        local_time = get_time_str(meas.timestamp,
                                  microseconds=True,
                                  seconds=True)

        pros_imgs = pros.get_imgs()

        for img_index, img in enumerate(pros_imgs):
            comp.paste(img, (img_index * (320 + margin), 20))

        d = ImageDraw.Draw(comp)
        d.text((0, 0), local_time, fill=(0, 0, 0))

        img_name = f'{frame_folder}{sensor_id}_' + ('000000' +
                                                    str(index))[-6:] + '.png'
        print(img_name)
        comp.save(img_name)
Exemplo n.º 3
0
    def __init__(self, db_bridge, app):
        self.db_bridge = db_bridge
        self.app = app

        self.layout = None
        self.label = None
        self.checkbox = None

        self.checkbox_callback = None
        self.img_processor = ImageProcessor()

        self.start_time = None
        self.stop_time = None

        self.meas_list = None
Exemplo n.º 4
0
 def __init__(self, sensor_id):
     self.sensor_id = sensor_id
     self.matrix = None
     self.calibration_points = []  # key=px_index, val=world_coord
     self.tracker = None
     self.processcor = ImageProcessor()
     self.com_module = None
     # if this flag is true, all centroids are converted to world coords before
     # sending to the tracker
     self.WORLD_CORDS_FLAG = True
     self.calibrated = True
     self.timestamp = None
Exemplo n.º 5
0
def stream_gen(id, simulated, show_webcam=True):
    print(f'requested stream for {id}')
    processor = ImageProcessor()
    while True:
        time.sleep(.1)
        if simulated:
            last_result = Measurement_test.query.filter(
                Measurement_test.sensor_id == id).order_by(
                    Measurement_test.timestamp.desc()).first()
        else:
            last_result = Measurement.query.filter(
                Measurement.sensor_id == id).order_by(
                    Measurement.timestamp.desc()).first()
        # get processed frame

        yield (b'--frame\r\nContent-Type: image/png\r\n\r\n' + img_bytes +
               b'\r\n')
from localization.processing import ImageProcessor

proc = ImageProcessor()

print(sum(proc.weight_values))
print(proc.weight_step)
print(proc.weight_min)
print(proc.weight_values)
"""
This files convert a database (from pgadmin) to a csv that contains 3 columns: the centroids, epoch time and local time
"""

import csv
from localization.processing import ImageProcessor
from help_module.time_helper import convert_to_datetime, get_time_str

pros = ImageProcessor()
data_list = []

folder_location = '../../data/'
file_name = '19042019.csv'

with open(folder_location + file_name) as csvfile:
    reader = csv.reader(csvfile, delimiter=',')
    for index, row in enumerate(reader):
        # print(row)
        # print(index)
        thermal_data = eval(row[1])
        centroids = pros.process(thermal_data)
        meas_datetime = convert_to_datetime(row[3])
        epoch_time = meas_datetime.timestamp()
        local_time = get_time_str(meas_datetime, microseconds=True)
        data_list.append([centroids, epoch_time, local_time])

with open(folder_location + 'centroid_' + file_name, 'w+',
          newline='') as csvfile:
    writer = csv.writer(csvfile)
    for row in data_list:
        writer.writerow(row)
Exemplo n.º 8
0
from datetime import datetime
from help_module.csv_helper import load_csv, write_csv_list_frames
from help_module.time_helper import convert_to_datetime
from localization.processing import ImageProcessor
import numpy as np
from PIL import Image

csv_file = "../../data/progression_test.csv"

pros = ImageProcessor()

#
# pcb_versions = ['v2_met_batterij', 'v2_zonder_batterij', 'pcb_Gilles', 'breadboard', 'v1', 'volledig_batterij']
#
# for file_name in pcb_versions:
#     meas = load_csv(file_name + '.csv', to_numpy=True, split=False, csv_tag=False)[0]
#     pros.set_thermal_data(meas.data)
#

meas = load_csv(csv_file, to_numpy=True, split=False, csv_tag=False)[0]
pros.set_thermal_data(meas.data)

imgs = pros.plot_contour_progression()
comp_img = Image.new('RGB', (1280 + 3 * 20, 240), color=(255, 255, 255))
for index, img in enumerate(imgs):
    comp_img.paste(img, (index * (320 + 20), 0))
comp_img.save('test_progression.png')
import csv
from localization.processing import ImageProcessor
from help_module.time_helper import convert_to_datetime, get_time_str
from PIL import Image, ImageDraw
import os

pros = ImageProcessor()
data_list = []

folder_location = '../../data/'
file_name = 'bad_frames.csv'

timestamp_top_margin = 20

file_without_ext = file_name.split('.')[0]
video_folder_name = 'video_' + file_without_ext + '/'

if not os.path.exists(folder_location + video_folder_name):
    os.mkdir(folder_location + video_folder_name)

with open(folder_location + file_name) as csvfile:
    reader = csv.reader(csvfile, delimiter=',')
    for index, row in enumerate(reader):
        print(row)
        thermal_data = eval(row[0])
        pros.set_thermal_data(thermal_data)
        meas_datetime = convert_to_datetime(row[1])
        local_time = get_time_str(meas_datetime, microseconds=True, seconds=True)

        centroid_frame = Image.fromarray(pros.plot_centroids(rgb=True), 'RGB')
# data=read_data(r'C:\Users\Thomas\Documents\School\VOP\VOP\server\data_vis\files\sensor_data_episode_20190221-143435_0.csv',0,502)
# #manually selected empty frames of this episode
# img_processor=ImageProcessor()
# print(data[num][0])
# plt.subplot(2,1,1).pcolor(data[num][0].reshape((24,32)))
# img_processor.process(data[num][0])
# res=img_processor.plot_frame()
# plt.subplot(2,1,2).imshow(res)
#
# plt.show()

from localization.processing import ImageProcessor
from help_module.img_helper import fast_thermal_image
import cv2

pros = ImageProcessor()
pros.enable_logging()
thermal_data = [
    103, 104, 105, 107, 106, 106, 106, 107, 107, 107, 107, 104, 105, 106, 106,
    107, 105, 106, 107, 107, 106, 107, 105, 106, 106, 105, 105, 105, 105, 102,
    101, 102, 105, 105, 104, 105, 106, 106, 106, 106, 107, 106, 105, 105, 107,
    106, 104, 106, 105, 105, 105, 106, 106, 105, 105, 105, 104, 105, 105, 104,
    104, 101, 100, 101, 108, 107, 105, 107, 103, 105, 105, 107, 105, 106, 106,
    106, 106, 106, 105, 106, 106, 105, 104, 106, 104, 106, 104, 103, 104, 105,
    103, 105, 103, 102, 101, 102, 107, 105, 106, 102, 106, 105, 105, 105, 106,
    105, 105, 105, 107, 105, 106, 105, 105, 106, 105, 103, 104, 105, 104, 104,
    104, 105, 104, 103, 104, 100, 103, 101, 107, 106, 104, 105, 106, 105, 106,
    108, 105, 106, 105, 105, 105, 105, 106, 106, 105, 105, 105, 105, 105, 106,
    106, 104, 102, 105, 103, 104, 98, 103, 103, 100, 105, 106, 105, 104, 106,
    106, 105, 106, 106, 106, 106, 106, 106, 106, 104, 106, 105, 105, 105, 105,
    106, 105, 105, 105, 104, 104, 103, 102, 100, 101, 101, 101, 106, 106, 106,
Exemplo n.º 11
0
class Sensor:
    def __init__(self, db_bridge, app):
        self.db_bridge = db_bridge
        self.app = app

        self.layout = None
        self.label = None
        self.checkbox = None

        self.checkbox_callback = None
        self.img_processor = ImageProcessor()

        self.start_time = None
        self.stop_time = None

        self.meas_list = None

    def set_sensor_values(self,
                          sensor_type,
                          sensor_id=None,
                          file_name=None,
                          data=None):
        self.sensor_type = sensor_type
        self.meas_list = data
        self.file_name = file_name
        self.sensor_id = sensor_id

    def checkbox_activate(self):
        self.checkbox_callback(self)

    def create_ui(self, callback):
        self.layout = QHBoxLayout()
        self.label = QLabel(
            f'{self.sensor_type}: {self.sensor_id if self.sensor_id is not None else self.file_name}'
        )
        self.checkbox = QCheckBox()
        self.layout.addWidget(self.label)
        self.layout.addWidget(self.checkbox)

        self.checkbox_callback = callback
        self.checkbox.stateChanged.connect(self.checkbox_activate)

        return self.layout

    def delete_ui(self):
        self.layout.deleteLater()
        self.label.deleteLater()
        self.checkbox.deleteLater()

    def data_loaded(self):
        return self.meas_list is not None

    def reload(self):
        if self.meas_list is not None:
            self.load_data()

    def is_active(self):
        return self.checkbox.isChecked()

    def get_data(self):
        if self.meas_list is None:
            print("WARNING attempting to load data that is not loaded")
        return self.meas_list

    def load_data(self):
        """
        This function takes a source and then looks at its type to find the function to get that data

        :param source: A dict containing all the information to load the data
        :return: list of measurements
        """
        if self.sensor_type == 'csv':
            self.meas_list = load_csv(self.file_name)
        elif self.sensor_type == 'sensor':
            self.meas_list = self.load_sensor()
        else:
            raise Exception('Source type was not recognized')

        self.start_time = self.meas_list[0].timestamp
        self.stop_time = self.meas_list[-1].timestamp

        for index, meas in enumerate(self.meas_list):
            meas.set_or_index(index)
            meas.set_sensor(self)
            meas.convert_to_numpy()

    def load_sensor(self):
        """
        Uses a database query to get measurements from the sensor with id == sensor_id
        Explanation for converting to different class is written in the data_model_helper.py

        :param sensor_id:
        :return:
        """
        param = self.app.get_query_param()
        db_values = self.db_bridge.get_values(self.sensor_id, param)
        sens_values = [Measurement(meas) for meas in db_values]

        return sens_values

    def get_default_vis(self, index):
        thermal_data = self.meas_list[index].data
        self.img_processor.set_thermal_data(thermal_data)
        imgs_batch_1 = self.img_processor.get_imgs()

        print(
            f'diff in thermal_data: {np.max(thermal_data) - np.min(thermal_data)}'
        )

        return imgs_batch_1

    def get_multi_processing(self, index):
        hist_amount = self.img_processor.get_hist_length()
        start_index = max(0, index - hist_amount)
        prev_frames = [meas.data for meas in self.meas_list[start_index:index]]
        cur_frame = self.meas_list[index].data

        self.img_processor.set_current_frame(cur_frame)
        self.img_processor.set_history(prev_frames)

        return self.img_processor.subtract_history()

    def get_closest_meas(self, time):
        cur_time = timedelta(seconds=time)

        min_diff = float('inf')
        min_index = 0

        for meas, index in enumerate(self.meas_list):
            if abs_diff(meas.timestamp, cur_time) < min_diff:
                min_diff = abs_diff(meas.timestamp, cur_time)
                min_index = index

        return min_index