def generate_print_poster(_print): last_pic = last_pic_of_print(_print, 'raw') if not last_pic: # This print does not have any raw pics return rotated_jpg_url = save_print_snapshot( _print.printer, last_pic, f'private/{_print.id}_poster.jpg', to_container=settings.TIMELAPSE_CONTAINER, rotated=True, to_long_term_storage=True) if rotated_jpg_url: _print.poster_url = rotated_jpg_url _print.save() # Save the unrotated snapshot so that it is still viewable even after the print is done. unrotated_jpg_url = save_print_snapshot( _print.printer, last_pic, f'snapshots/{_print.printer.id}/latest_unrotated.jpg', rotated=False, to_long_term_storage=False) cache.printer_pic_set(_print.printer.id, {'img_url': unrotated_jpg_url}, ex=IMG_URL_TTL_SECONDS)
def post(self, request): printer = request.auth printer.refresh_from_db() # Connection is keep-alive, which means printer object can be stale. pic = request.FILES['pic'] pic = cap_image_size(pic) pic_id = str(timezone.now().timestamp()) if not printer.current_print: # Some times pics come in when current_print is not set - maybe because printer status is out of sync between plugin and server? pic_path = f'{printer.id}/0/{pic_id}.jpg' else: pic_path = f'{printer.id}/{printer.current_print.id}/{pic_id}.jpg' internal_url, external_url = save_file_obj(f'raw/{pic_path}', pic, settings.PICS_CONTAINER, long_term_storage=False) if not printer.should_watch() or not printer.actively_printing(): cache.printer_pic_set(printer.id, {'img_url': external_url}, ex=IMG_URL_TTL_SECONDS) send_status_to_web(printer.id) return Response({'result': 'ok'}) req = requests.get(settings.ML_API_HOST + '/p/', params={'img': internal_url}, headers=ml_api_auth_headers(), verify=False) req.raise_for_status() resp = req.json() cache.print_num_predictions_incr(printer.current_print.id) detections = resp['detections'] prediction, _ = PrinterPrediction.objects.get_or_create(printer=printer) update_prediction_with_detections(prediction, detections) prediction.save() if prediction.current_p > settings.THRESHOLD_LOW * 0.2: # Select predictions high enough for focused feedback cache.print_high_prediction_add(printer.current_print.id, prediction.current_p, pic_id) pic.file.seek(0) # Reset file object pointer so that we can load it again tagged_img = io.BytesIO() detections_to_visualize = [d for d in detections if d[1] > VISUALIZATION_THRESH] overlay_detections(Image.open(pic), detections_to_visualize).save(tagged_img, "JPEG") tagged_img.seek(0) _, external_url = save_file_obj(f'tagged/{pic_path}', tagged_img, settings.PICS_CONTAINER, long_term_storage=False) cache.printer_pic_set(printer.id, {'img_url': external_url}, ex=IMG_URL_TTL_SECONDS) prediction_json = serializers.serialize("json", [prediction, ]) p_out = io.BytesIO() p_out.write(prediction_json.encode('UTF-8')) p_out.seek(0) save_file_obj(f'p/{printer.id}/{printer.current_print.id}/{pic_id}.json', p_out, settings.PICS_CONTAINER, long_term_storage=False) if is_failing(prediction, printer.detective_sensitivity, escalating_factor=settings.ESCALATING_FACTOR): pause_if_needed(printer) elif is_failing(prediction, printer.detective_sensitivity, escalating_factor=1): alert_if_needed(printer) send_status_to_web(printer.id) return Response({'result': 'ok'})
def generate_print_poster(_print): (unrotated_jpg_url, rotated_jpg_url) = save_print_snapshot( _print, last_pic_of_print(_print, 'raw'), unrotated_jpg_path= f'snapshots/{_print.printer.id}/{_print.id}/{str(timezone.now().timestamp())}_unrotated.jpg', rotated_jpg_path=f'private/{_print.id}_poster.jpg') if unrotated_jpg_url: cache.printer_pic_set(_print.printer.id, {'img_url': unrotated_jpg_url}, ex=IMG_URL_TTL_SECONDS) if rotated_jpg_url: _print.poster_url = rotated_jpg_url _print.save()
def generate_print_poster(_print): unrotated_jpg_url = save_print_snapshot( _print, last_pic_of_print(_print, 'raw'), f'snapshots/{_print.printer.id}/{_print.id}/{str(timezone.now().timestamp())}_unrotated.jpg', rotated=False, to_long_term_storage=False) if unrotated_jpg_url: cache.printer_pic_set(_print.printer.id, {'img_url': unrotated_jpg_url}, ex=IMG_URL_TTL_SECONDS) rotated_jpg_url = save_print_snapshot( _print, last_pic_of_print(_print, 'raw'), f'private/{_print.id}_poster.jpg', to_container=settings.TIMELAPSE_CONTAINER, rotated=True, to_long_term_storage=True) if rotated_jpg_url: _print.poster_url = rotated_jpg_url _print.save()
def will_record_timelapse(_print): last_pic = last_pic_of_print(_print, 'raw') if not last_pic: # This print does not have any raw pics return False # Save the unrotated snapshot so that it is still viewable even after the print is done. unrotated_jpg_url = save_print_snapshot( _print.printer, last_pic, f'snapshots/{_print.printer.id}/latest_unrotated.jpg', rotated=False, to_long_term_storage=False) cache.printer_pic_set(_print.printer.id, {'img_url': unrotated_jpg_url}, ex=IMG_URL_TTL_SECONDS) min_timelapse_secs = _print.printer.min_timelapse_secs_on_cancel if _print.is_canceled( ) else _print.printer.min_timelapse_secs_on_finish if min_timelapse_secs < 0 or (_print.ended_at() - _print.started_at ).total_seconds() < min_timelapse_secs: _print.delete() clean_up_print_pics(_print) return False rotated_jpg_url = save_print_snapshot( _print.printer, last_pic, f'private/{_print.id}_poster.jpg', to_container=settings.TIMELAPSE_CONTAINER, rotated=True, to_long_term_storage=True) if rotated_jpg_url: _print.poster_url = rotated_jpg_url _print.save(keep_deleted=True) return True