def process_files(ftp_client, ftp_files, args): results = [] for ftp_file in ftp_files: if not args.delete and processed is not None and ftp_file in processed: continue logging.info(ftp_file) with tempfile.NamedTemporaryFile(suffix='_' + ftp_file, mode='rb+') as image: ftp_client.retrbinary('RETR ' + ftp_file, image.write) api_res = recognition_api(image, args.regions, args.api_key, args.sdk_url, camera_id=args.camera_id, timestamp=args.timestamp) results.append(api_res) if args.delete: ftp_client.delete(ftp_file) elif processed is not None: processed.append(ftp_file) if args.output_file: save_results(results, args) else: print(json.dumps(results, indent=2))
def ftp_process(args, skip=None): ftp = FTP() ftp.connect(args.ftp_host) ftp.login(args.ftp_user, args.ftp_password) ftp.cwd(args.folder) ftp_files = ftp.nlst() logging.info('Connected. Found %s file(s) in %s.', len(ftp_files), args.folder) results = [] for ftp_file in ftp_files: if skip and ftp_file in skip: continue logging.info(ftp_file) with tempfile.NamedTemporaryFile(suffix='_' + ftp_file, mode='rb+') as image: ftp.retrbinary('RETR ' + ftp_file, image.write) api_res = recognition_api(image, args.regions, args.api_key, args.sdk_url, camera_id=args.camera_id, timestamp=args.timestamp) results.append(api_res) if args.delete: ftp.delete(ftp_file) if args.output_file: save_results(results, args) else: print(json.dumps(results, indent=2)) return ftp_files
def process_image(path, args, i): config = dict(threshold_d=args.detection_threshold, threshold_o=args.ocr_threshold, mode='redaction') # Predictions source_im = Image.open(path) if source_im.mode != 'RGB': source_im = source_im.convert('RGB') images = [((0, 0), source_im)] # Entire image # Top left and top right crops if args.split_image: y = 0 win_size = .55 width, height = source_im.width * win_size, source_im.height * win_size for x in [0, int((1 - win_size) * source_im.width)]: images.append(((x, y), source_im.crop( (x, y, x + width, y + height)))) # Inference results = [] for (x, y), im in images: im_bytes = io.BytesIO() im.save(im_bytes, 'JPEG', quality=95) im_bytes.seek(0) im_results = recognition_api(im_bytes, args.regions, args.api_key, args.sdk_url, config=config) results.append(dict(prediction=im_results, x=x, y=y)) results = post_processing(merge_results(results)) results['filename'] = Path(path).name # Set bounding box padding for item in results['results']: # Decrease padding size for large bounding boxes b = item['box'] width, height = b['xmax'] - b['xmin'], b['ymax'] - b['ymin'] padding_x = int( max(0, width * (.3 * math.exp(-10 * width / source_im.width)))) padding_y = int( max(0, height * (.3 * math.exp(-10 * height / source_im.height)))) b['xmin'] = b['xmin'] - padding_x b['ymin'] = b['ymin'] - padding_y b['xmax'] = b['xmax'] + padding_x b['ymax'] = b['ymax'] + padding_y if args.show_boxes or args.save_blurred: im = blur(source_im, 5, results) if args.show_boxes: im.show() if args.save_blurred: filename = Path(path) im.save(filename.parent / ('%s_blurred%s' % (filename.stem, filename.suffix))) if 0: draw_bb(source_im, results['results']).show() return results
def recognition(my_file, regions, sdk_url, mmc, config): with st.spinner('Processing'): res = recognition_api(my_file, regions=regions, sdk_url=sdk_url, mmc=mmc, config=config) return res
def process_files(ftp_client, ftp_files, args): """ Process a list of file paths by: 1. Deletes old files in ftp_files from ftp_client 2. For new files, retrieving the full file from ftp_client 3. Calling Snapshot API and Tracking Successfully Processed file paths :param ftp_client: :param ftp_files: List of files in the format [path, modified datetime], usually from a single folder. :param args: :return: """ results = [] for file_last_modified in ftp_files: ftp_file = file_last_modified[0] last_modified = file_last_modified[1] if track_processed(args) and ftp_file in processed: if args.delete is not None: rm_older_than_date = datetime.now() - timedelta( seconds=args.delete) if rm_older_than_date > last_modified: ftp_client.delete(ftp_file) processed.remove(ftp_file) continue logging.info(ftp_file) with tempfile.NamedTemporaryFile(suffix='_' + ftp_file, mode='rb+') as image: ftp_client.retrbinary('RETR ' + ftp_file, image.write) api_res = recognition_api(image, args.regions, args.api_key, args.sdk_url, camera_id=args.camera_id, timestamp=args.timestamp, mmc=args.mmc, exit_on_error=False) results.append(api_res) if track_processed(args): processed.append(ftp_file) if args.output_file: save_results(results, args) else: print(json.dumps(results, indent=2))
def process_image(path, args): options = CAMERA_OPTIONS[args.camera] config = dict(threshold_d=.2, threshold_o=.3, mode='redaction') # Pre process image source_im = Image.open(path) rotated = source_im.rotate(options['rotation'], expand=True) top = rotated.height * options['crop_top'] bottom = rotated.height * options['crop_bottom'] api_im = rotated.crop((0, top, rotated.width, bottom)) # Predictions images = [((0, 0), api_im)] # Entire image # Top left and top right crops y = 0 win_size = .55 width, height = api_im.width * win_size, api_im.height * win_size for x in [0, (1 - win_size) * api_im.width]: images.append(((x, y), api_im.crop((x, y, x + width, y + height)))) # Inference results = [] for (x, y), im in images: im_bytes = io.BytesIO() im.save(im_bytes, 'JPEG', quality=95) im_bytes.seek(0) im_results = recognition_api(im_bytes, args.regions, args.api_key, args.sdk_url, config=config) results.append(dict(prediction=im_results, x=x, y=y)) results = merge_results(results) # Update bounding boxes for lp in results['results']: box = lp['box'] box['ymin'] += top box['ymax'] += top # Rotate lp['box'] = rotate_bb(box, options['rotation'], rotated.size) if 0: draw_bb(source_im, results['results']).show() return results
def process_image(path, args, i): config = dict(threshold_d=args.detection_threshold, threshold_o=args.ocr_threshold, mode='redaction') # Predictions source_im = Image.open(path) images = [((0, 0), source_im)] # Entire image # Top left and top right crops if args.split_image: y = 0 win_size = .55 width, height = source_im.width * win_size, source_im.height * win_size for x in [0, int((1 - win_size) * source_im.width)]: images.append(((x, y), source_im.crop( (x, y, x + width, y + height)))) # Inference results = [] for (x, y), im in images: im_bytes = io.BytesIO() if im.mode == 'RGBA': im = im.convert('RGB') im.save(im_bytes, 'JPEG', quality=95) im_bytes.seek(0) im_results = recognition_api(im_bytes, args.regions, args.api_key, args.sdk_url, config=config) results.append(dict(prediction=im_results, x=x, y=y)) results = merge_results(results) if args.show_boxes: blur(source_im, 5, results).show() if 0: draw_bb(source_im, results['results']).show() return results
def main(): args = parse_arguments() scores = [] for path in args.files: blur_amount = 0 init_value = '' while True: # Blur image image = Image.open(path) if image.height > 1080: image = image.resize( (int(image.width * 1080 / image.height), 1080)) elif image.width > 1980: image = image.resize( (1980, int(image.height * 1980 / image.width))) if blur_amount > 0: image = image.filter( ImageFilter.GaussianBlur(radius=blur_amount)) buffer = io.BytesIO() image.save(buffer, 'jpeg') buffer.seek(0) # Do prediction api_res = recognition_api(buffer, args.regions, args.api_key, args.sdk_url, camera_id=args.camera_id) if not init_value: init_value = api_res['results'][0]['plate'] elif not api_res[ 'results'] or init_value != api_res['results'][0]['plate']: break blur_amount += .5 scores.append(blur_amount) print(path, blur_amount) print('Blur score', np.mean(scores))
def process_files(ftp_client, ftp_files, args): results = [] rm_older_than_date = datetime.now() - timedelta(seconds=args.delete) for file_last_modified in ftp_files: ftp_file = file_last_modified[0] last_modified = file_last_modified[1] if ftp_file in processed: if args.delete: if rm_older_than_date > last_modified: ftp_client.delete(ftp_file) processed.remove(ftp_file) continue logging.info(ftp_file) with tempfile.NamedTemporaryFile(suffix='_' + ftp_file, mode='rb+') as image: ftp_client.retrbinary('RETR ' + ftp_file, image.write) api_res = recognition_api(image, args.regions, args.api_key, args.sdk_url, camera_id=args.camera_id, timestamp=args.timestamp, exit_on_error=False) results.append(api_res) processed.append(ftp_file) if args.output_file: save_results(results, args) else: print(json.dumps(results, indent=2))
def call_duration(path, sdk_url, config): now = default_timer() with open(path, 'rb') as fp: recognition_api(fp, sdk_url=sdk_url, config=config) return (default_timer() - now) * 1000