def main(): shutil.rmtree(res_folder, ignore_errors=True) os.makedirs(res_folder, exist_ok=True) tot_cnt, ok_cnt = 0, 0 for fname_path in glob.glob(f'{inp_folder}/{inp_mask}'): Debug.set_log_image_names(fname_path, res_folder) image0 = cv.imread(fname_path, cv.IMREAD_GRAYSCALE) qr_anchor_list = QrAnchorList( image0) # qr_anchor - 3 big squares keypoints for qr_anchor in qr_anchor_list: qr_area = QrArea( image0, qr_anchor ) # qr_are is a part of image0 with qr_code and all related methods # qr_area_img = qr_area.draw() # Debug.log_image('qr_area_img') qr_list = qr_read.QrMarkList(qr_area.image_finished) qr_list.draw_boxes() qr_list.draw_polygons() found = len(qr_list.qr_marks) ok_cnt += 1 if found else 0 tot_cnt += 1 print( f'{fname_path}: Total: {ok_cnt}/{tot_cnt} ({ok_cnt/tot_cnt*100:.2f}%)' )
def main_find_qr(): shutil.rmtree(Cfg.log_folder, ignore_errors=True) os.makedirs(Cfg.log_folder, exist_ok=True) Debug.set_params(log_folder=Cfg.log_folder, log_image=Cfg.verbose) for folder in sorted(glob.glob(f'{Cfg.inp_folders}')): if not os.path.isdir(folder): continue files_cnt = 0 files_found = 0 for fname_path in glob.glob(f'{folder}/{Cfg.inp_fname_mask}'): Debug.set_log_image_names(fname_path) ok_cnt = process_file(fname_path) # ok_cnt=0 # find_offset(fname_path) if ok_cnt: files_found += 1 files_cnt += 1 print( f'\t\t{fname_path}: {ok_cnt} marks. Files: ' f'{files_found}/{files_cnt} rate={100*files_found/files_cnt:.2f}%') print(f'Folder {folder}: {files_found}/{files_cnt} rate={100*files_found/files_cnt:.2f}%')
def take_readings(fname_path_flir): # file --> db + files in images/date Debug.set_log_image_names(fname_path_flir) try: fi = FlirImage(fname_path_flir) except (ValueError, KeyError): logger.exception( f'error while flir-processing file {fname_path_flir}. Skipped.') return 0, None qr_mark_list = QrDecode.get_all_qrs(fi.visual_img) visual_img_copy = fi.visual_img.copy() thermal_img_copy = fi.thermal_img.copy() reading_cnt = 0 meter_ids = [] for qr_mark in qr_mark_list: meter_records = Db.get_meters_from_db(qr_mark.code) if meter_records is None: logger.info(f'qrs_to_readings: no equips for qr_mark {qr_mark}') continue for meter_id, offset_x, offset_y in meter_records: meter_ids.append(meter_id) if offset_x == 9999.: continue logger.debug(f'take readings: meter_id={meter_id}: ') reading = Reading(meter_id, (offset_x, offset_y), qr_mark, fi) if reading.temperature is None: logger.error( f'cannot create Reading ' f'for meter_id={meter_id} offset=({offset_x},{offset_y}) ' f'due to illegal coordinates after offset_to_xy()') continue if _GroupEquip.ready_to_analyze(event='readings_taken', meter_ids=meter_ids): Analyzer.run() reading.save_to_db() # reading.save_to_csv() # logger.debug(reading) reading.draw_visual(visual_img_copy) reading.draw_thermal(thermal_img_copy) Debug.log_image('visual_read', visual_img_copy) Debug.log_image('thermal_read', thermal_img_copy) reading_cnt += 1 return reading_cnt, meter_ids
def main_find_offset(): MeterXy = collections.namedtuple('MeterXy', ['file_name', 'meter_id', 'code', 'x', 'y']) MeterOff = collections.namedtuple('MeterXy', ['file_name', 'meter_id', 'code', 'x', 'y', 'off_x', 'off_y']) folder = '../tmp/out/images/2019-08-08' # read meter_xy.csv with open(Cfg.xy_info_file, newline='') as csvfile: reader = csv.reader(csvfile, delimiter=',', quotechar='"') next(reader, None) # skip headers meter_xy_list = [MeterXy(row[0], row[1], row[2], row[3], row[4]) for row in reader] shutil.rmtree(Cfg.log_folder, ignore_errors=True) os.makedirs(Cfg.log_folder, exist_ok=True) Debug.set_params(log_folder=Cfg.log_folder, log_image=Cfg.verbose) offsets = [] for m in meter_xy_list: # if not str(m.file_name).endswith('447_v'): # 554 # continue fname_path = f'{folder}/{m.file_name}.jpg' Debug.set_log_image_names(fname_path) off = find_offset(fname_path, (m.x, m.y), m.meter_id, m.code) offsets.append(MeterOff(m.file_name, m.meter_id, m.code, m.x, m.y, off[0], off[1])) offsets = sorted(offsets, key=lambda x: x.meter_id) print('offsets:\n\t', '\n\t'.join([f'{m.meter_id} : {m.file_name} : ({m.off_x:.5f},{m.off_y:.5f}) ' f'xy:({m.x},{m.y})' for m in offsets if m.off_x != 9999.]), sep='') for meter_id in sorted(list(set([off.meter_id for off in offsets])), key=lambda x: int(x)): lst = [(m.off_x, m.off_y) for m in offsets if m.meter_id == meter_id and m.off_x != 9999] xy = [[l[i] for l in lst] for i in [0, 1]] # avg = (statistics.mean([l[0] for l in lst]), statistics.mean([l[0] for l in lst])) avg = (statistics.mean(xy[0]), statistics.mean(xy[1])) min_v = (min(xy[0]), min(xy[1])) max_v = (max(xy[0]), max(xy[1])) diff = (max_v[0] - min_v[0], max_v[1] - min_v[1]) print(f'meter_id={meter_id} ' f'avg=({avg[0]: .5f},{avg[1]: .5f}) ' f'min=({min_v[0]: .5f},{min_v[1]: .5f}) ' f'max=({max_v[0]: .5f},{max_v[1]: .5f})' f'diff=({diff[0]: .5f},{diff[1]: .5f})' )